@timurproko/a1 0.1.1-dev.1 → 0.1.1-dev.10

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 (176) hide show
  1. package/README.md +8 -43
  2. package/bin/guardian.js +22 -0
  3. package/bin/{a1-ui.js → ui.js} +4 -8
  4. package/dist/native/darwin-arm64/manifest.json +20 -0
  5. package/dist/native/darwin-arm64/process-guardian +0 -0
  6. package/dist/native/linux-x64/manifest.json +20 -0
  7. package/dist/native/linux-x64/process-guardian +0 -0
  8. package/dist/native/win32-x64/manifest.json +20 -0
  9. package/dist/native/win32-x64/process-guardian.exe +0 -0
  10. package/dist/src/composition/index.d.ts +26 -0
  11. package/dist/src/composition/index.js +94 -2
  12. package/dist/src/features/launch/initialize-profile.js +23 -2
  13. package/dist/src/features/launch/profiles.d.ts +1 -1
  14. package/dist/src/features/launch/profiles.js +2 -2
  15. package/dist/src/features/launch/runtime-selection.d.ts +13 -8
  16. package/dist/src/features/launch/runtime-selection.js +6 -6
  17. package/dist/src/features/owned-ui/index.d.ts +1 -0
  18. package/dist/src/features/owned-ui/index.js +1 -0
  19. package/dist/src/features/owned-ui/run.d.ts +6 -0
  20. package/dist/src/features/owned-ui/run.js +3 -1
  21. package/dist/src/features/owned-ui/settings-app.d.ts +22 -0
  22. package/dist/src/features/owned-ui/settings-app.js +633 -0
  23. package/dist/src/foundation/agent-engine-contracts/domain.d.ts +26 -0
  24. package/dist/src/foundation/launch-guardian/index.d.ts +1 -0
  25. package/dist/src/foundation/launch-guardian/index.js +1 -0
  26. package/dist/src/foundation/launch-guardian/main.d.ts +31 -0
  27. package/dist/src/foundation/launch-guardian/main.js +165 -0
  28. package/dist/src/foundation/lifecycle/commands.d.ts +40 -0
  29. package/dist/src/foundation/lifecycle/commands.js +1 -0
  30. package/dist/src/foundation/lifecycle/index.d.ts +2 -0
  31. package/dist/src/foundation/lifecycle/index.js +2 -0
  32. package/dist/src/foundation/lifecycle/launch-instance.d.ts +46 -0
  33. package/dist/src/foundation/lifecycle/launch-instance.js +134 -0
  34. package/dist/src/foundation/lifecycle/model.d.ts +6 -42
  35. package/dist/src/foundation/owned-ui-settings/declarations.d.ts +13 -0
  36. package/dist/src/foundation/owned-ui-settings/declarations.js +39 -0
  37. package/dist/src/foundation/owned-ui-settings/index.d.ts +6 -0
  38. package/dist/src/foundation/owned-ui-settings/index.js +6 -0
  39. package/dist/src/foundation/owned-ui-settings/migrations.d.ts +13 -0
  40. package/dist/src/foundation/owned-ui-settings/migrations.js +28 -0
  41. package/dist/src/foundation/owned-ui-settings/resolution.d.ts +45 -0
  42. package/dist/src/foundation/owned-ui-settings/resolution.js +147 -0
  43. package/dist/src/foundation/owned-ui-settings/sections.d.ts +70 -0
  44. package/dist/src/foundation/owned-ui-settings/sections.js +93 -0
  45. package/dist/src/foundation/owned-ui-settings/session.d.ts +44 -0
  46. package/dist/src/foundation/owned-ui-settings/session.js +145 -0
  47. package/dist/src/foundation/owned-ui-settings/store.d.ts +28 -0
  48. package/dist/src/foundation/owned-ui-settings/store.js +90 -0
  49. package/dist/src/foundation/pi-component-adapter/index.d.ts +1 -0
  50. package/dist/src/foundation/pi-component-adapter/index.js +1 -0
  51. package/dist/src/foundation/pi-component-adapter/shell-selectors-dialogs.d.ts +9 -1
  52. package/dist/src/foundation/pi-component-adapter/shell-selectors-dialogs.js +10 -8
  53. package/dist/src/foundation/pi-component-adapter/upstream/theme/theme.d.ts +8 -0
  54. package/dist/src/foundation/pi-component-adapter/upstream/theme/theme.js +12 -0
  55. package/dist/src/foundation/pi-engine-adapter/adapter.d.ts +17 -3
  56. package/dist/src/foundation/pi-engine-adapter/adapter.js +80 -7
  57. package/dist/src/foundation/pi-engine-adapter/pi-settings-metadata.json +374 -0
  58. package/dist/src/foundation/pi-engine-adapter/public-main-entry.js +10 -1
  59. package/dist/src/foundation/pi-engine-adapter/settings-integration.d.ts +40 -2
  60. package/dist/src/foundation/pi-engine-adapter/settings-integration.js +172 -24
  61. package/dist/src/foundation/pi-engine-adapter/workflows.d.ts +9 -0
  62. package/dist/src/foundation/pi-owned-ui-integration/index.d.ts +1 -0
  63. package/dist/src/foundation/pi-owned-ui-integration/index.js +1 -0
  64. package/dist/src/foundation/pi-owned-ui-integration/route-host.d.ts +29 -0
  65. package/dist/src/foundation/pi-owned-ui-integration/route-host.js +1 -0
  66. package/dist/src/foundation/pi-owned-ui-integration/session-shell.d.ts +6 -0
  67. package/dist/src/foundation/pi-owned-ui-integration/session-shell.js +71 -1
  68. package/dist/src/foundation/pi-tui-runtime-adapter/adapter.d.ts +6 -0
  69. package/dist/src/foundation/pi-tui-runtime-adapter/adapter.js +9 -0
  70. package/dist/src/foundation/process-containment/artifact.d.ts +1 -0
  71. package/dist/src/foundation/process-containment/artifact.js +34 -0
  72. package/dist/src/foundation/process-containment/contracts.d.ts +31 -0
  73. package/dist/src/foundation/process-containment/contracts.js +35 -0
  74. package/dist/src/foundation/process-containment/index.d.ts +5 -0
  75. package/dist/src/foundation/process-containment/index.js +5 -0
  76. package/dist/src/foundation/process-containment/linux-process-inspector.d.ts +6 -0
  77. package/dist/src/foundation/process-containment/linux-process-inspector.js +26 -0
  78. package/dist/src/foundation/process-containment/native-guardian-containment.d.ts +16 -0
  79. package/dist/src/foundation/process-containment/native-guardian-containment.js +139 -0
  80. package/dist/src/foundation/process-containment/windows-process-inspector.d.ts +17 -0
  81. package/dist/src/foundation/process-containment/windows-process-inspector.js +53 -0
  82. package/dist/src/foundation/protocol/client.d.ts +2 -1
  83. package/dist/src/foundation/protocol/client.js +3 -0
  84. package/dist/src/foundation/protocol/messages.d.ts +5 -5
  85. package/dist/src/foundation/protocol/messages.js +1 -1
  86. package/dist/src/foundation/release/bootstrap.d.ts +2 -2
  87. package/dist/src/foundation/release/bootstrap.js +69 -18
  88. package/dist/src/foundation/release/cohort-selection.js +3 -3
  89. package/dist/src/foundation/release/cohort-state.d.ts +3 -2
  90. package/dist/src/foundation/release/concurrency.d.ts +1 -0
  91. package/dist/src/foundation/release/concurrency.js +17 -0
  92. package/dist/src/foundation/release/process-cleanup.d.ts +1 -1
  93. package/dist/src/foundation/release/process-cleanup.js +2 -2
  94. package/dist/src/foundation/release/release-store.d.ts +35 -2
  95. package/dist/src/foundation/release/release-store.js +90 -31
  96. package/dist/src/foundation/release/release.d.ts +15 -0
  97. package/dist/src/foundation/release/release.js +65 -36
  98. package/dist/src/foundation/release/update.d.ts +19 -2
  99. package/dist/src/foundation/release/update.js +132 -16
  100. package/dist/src/foundation/storage/control-store.d.ts +8 -7
  101. package/dist/src/foundation/storage/control-store.js +105 -55
  102. package/dist/src/foundation/supervision/main.js +6 -5
  103. package/dist/src/foundation/supervision/server.d.ts +4 -2
  104. package/dist/src/foundation/supervision/server.js +254 -87
  105. package/dist/src/foundation/ui-apps/contracts.d.ts +42 -0
  106. package/dist/src/foundation/ui-apps/contracts.js +12 -0
  107. package/dist/src/foundation/ui-apps/host.d.ts +42 -0
  108. package/dist/src/foundation/ui-apps/host.js +152 -0
  109. package/dist/src/foundation/ui-apps/index.d.ts +3 -0
  110. package/dist/src/foundation/ui-apps/index.js +3 -0
  111. package/dist/src/foundation/ui-apps/registry.d.ts +11 -0
  112. package/dist/src/foundation/ui-apps/registry.js +32 -0
  113. package/dist/src/foundation/ui-components/dialog-panel.d.ts +36 -0
  114. package/dist/src/foundation/ui-components/dialog-panel.js +47 -0
  115. package/dist/src/foundation/ui-components/frame.d.ts +21 -0
  116. package/dist/src/foundation/ui-components/frame.js +54 -0
  117. package/dist/src/foundation/ui-components/index.d.ts +18 -0
  118. package/dist/src/foundation/ui-components/index.js +18 -0
  119. package/dist/src/foundation/ui-components/label.d.ts +8 -0
  120. package/dist/src/foundation/ui-components/label.js +36 -0
  121. package/dist/src/foundation/ui-components/line-input.d.ts +74 -0
  122. package/dist/src/foundation/ui-components/line-input.js +245 -0
  123. package/dist/src/foundation/ui-components/list-block.d.ts +66 -0
  124. package/dist/src/foundation/ui-components/list-block.js +176 -0
  125. package/dist/src/foundation/ui-components/list-view.d.ts +66 -0
  126. package/dist/src/foundation/ui-components/list-view.js +76 -0
  127. package/dist/src/foundation/ui-components/mouse.d.ts +15 -0
  128. package/dist/src/foundation/ui-components/mouse.js +46 -0
  129. package/dist/src/foundation/ui-components/pane.d.ts +33 -0
  130. package/dist/src/foundation/ui-components/pane.js +7 -0
  131. package/dist/src/foundation/ui-components/revision.d.ts +35 -0
  132. package/dist/src/foundation/ui-components/revision.js +79 -0
  133. package/dist/src/foundation/ui-components/scrollbar.d.ts +62 -0
  134. package/dist/src/foundation/ui-components/scrollbar.js +102 -0
  135. package/dist/src/foundation/ui-components/shortcuts.d.ts +57 -0
  136. package/dist/src/foundation/ui-components/shortcuts.js +81 -0
  137. package/dist/src/foundation/ui-components/spans.d.ts +7 -0
  138. package/dist/src/foundation/ui-components/spans.js +59 -0
  139. package/dist/src/foundation/ui-components/status-line.d.ts +16 -0
  140. package/dist/src/foundation/ui-components/status-line.js +14 -0
  141. package/dist/src/foundation/ui-components/stepper.d.ts +24 -0
  142. package/dist/src/foundation/ui-components/stepper.js +33 -0
  143. package/dist/src/foundation/ui-components/surface.d.ts +20 -0
  144. package/dist/src/foundation/ui-components/surface.js +38 -0
  145. package/dist/src/foundation/ui-components/text.d.ts +24 -0
  146. package/dist/src/foundation/ui-components/text.js +123 -0
  147. package/dist/src/foundation/ui-components/theme.d.ts +23 -0
  148. package/dist/src/foundation/ui-components/theme.js +13 -0
  149. package/dist/src/foundation/ui-components/value-menu.d.ts +41 -0
  150. package/dist/src/foundation/ui-components/value-menu.js +33 -0
  151. package/dist/src/product-identity.d.ts +2 -2
  152. package/dist/src/product-identity.js +2 -1
  153. package/dist/src/product-identity.json +8 -5
  154. package/docs/architecture/boundaries.md +31 -13
  155. package/docs/architecture/process-guardian-provenance.md +26 -0
  156. package/docs/architecture/toolchain.md +9 -3
  157. package/docs/architecture/ui-reference-provenance.md +50 -0
  158. package/docs/ci-release-runbook.md +67 -0
  159. package/docs/features/launch-profiles.md +7 -1
  160. package/docs/manual-launch-instance-acceptance.md +66 -0
  161. package/docs/manual-terminal-colour-check.md +55 -0
  162. package/package.json +20 -14
  163. package/dist/src/composition/transparent-runtime.d.ts +0 -6
  164. package/dist/src/composition/transparent-runtime.js +0 -19
  165. package/dist/src/foundation/transparent-terminal/command-resolution.d.ts +0 -13
  166. package/dist/src/foundation/transparent-terminal/command-resolution.js +0 -69
  167. package/dist/src/foundation/transparent-terminal/foreground-broker.d.ts +0 -32
  168. package/dist/src/foundation/transparent-terminal/foreground-broker.js +0 -87
  169. package/dist/src/foundation/transparent-terminal/index.d.ts +0 -4
  170. package/dist/src/foundation/transparent-terminal/index.js +0 -4
  171. package/dist/src/foundation/transparent-terminal/main.d.ts +0 -9
  172. package/dist/src/foundation/transparent-terminal/main.js +0 -94
  173. package/dist/src/foundation/transparent-terminal/native-launcher.d.ts +0 -25
  174. package/dist/src/foundation/transparent-terminal/native-launcher.js +0 -146
  175. /package/bin/{a1.js → cli.js} +0 -0
  176. /package/bin/{a1-supervisor.js → supervisor.js} +0 -0
@@ -3,8 +3,41 @@ export declare const RELEASE_MANIFEST_FILENAME: string;
3
3
  export interface MaterializedRelease extends ReleaseIdentity {
4
4
  readonly releaseRoot: string;
5
5
  }
6
- export declare function materializeRelease(packageRoot: string, dataDir: string): Promise<MaterializedRelease>;
6
+ export type ReleaseContentOperation = "source-read" | "candidate-write" | "verification-read";
7
+ export interface ReleaseContentOperationEvent {
8
+ readonly operation: ReleaseContentOperation;
9
+ readonly path: string;
10
+ readonly bytes: number;
11
+ }
12
+ export interface MaterializeReleaseOptions {
13
+ readonly onProgress?: (progress: {
14
+ readonly phase: "copying";
15
+ readonly fileCount: number;
16
+ }) => void;
17
+ readonly onOperation?: (event: ReleaseContentOperationEvent) => void;
18
+ /** Test seam for deterministic write-failure coverage. */
19
+ readonly writeCandidateFile?: (path: string, bytes: Uint8Array, mode: number) => Promise<void>;
20
+ }
21
+ export interface VerifyMaterializedReleaseOptions {
22
+ readonly onOperation?: (event: ReleaseContentOperationEvent) => void;
23
+ }
24
+ export interface CertifiedReleaseRecord {
25
+ readonly releaseId: string;
26
+ readonly releaseRoot: string;
27
+ readonly packageVersion?: string;
28
+ readonly contentDigest: string;
29
+ }
30
+ export declare function materializeRelease(packageRoot: string, dataDir: string, options?: MaterializeReleaseOptions): Promise<MaterializedRelease>;
31
+ /** Consume proof that this exact object was freshly materialized or fully verified in this process. */
32
+ export declare function consumeMaterializationProof(release: MaterializedRelease): boolean;
7
33
  export declare function readMaterializedRelease(releaseRoot: string): Promise<MaterializedRelease>;
8
- export declare function verifyMaterializedRelease(releaseRoot: string, expected?: ReleaseIdentity, selectedStoreRoot?: string): Promise<MaterializedRelease>;
34
+ /**
35
+ * Load metadata for a release whose bytes were already certified by the
36
+ * current parent process or an authenticated live supervisor. Callers must
37
+ * establish one of those preconditions; untrusted releases require full
38
+ * verification.
39
+ */
40
+ export declare function readCertifiedReleaseManifest(record: CertifiedReleaseRecord, selectedStoreRoot: string): Promise<MaterializedRelease>;
41
+ export declare function verifyMaterializedRelease(releaseRoot: string, expected?: ReleaseIdentity, selectedStoreRoot?: string, options?: VerifyMaterializedReleaseOptions): Promise<MaterializedRelease>;
9
42
  export declare function assertImmutableExecutionRoot(release: MaterializedRelease, dataDir: string): Promise<void>;
10
43
  export declare function resolveReleaseEntryPoint(release: MaterializedRelease, entryPoint: string): Promise<string>;
@@ -1,43 +1,101 @@
1
1
  import { createHash, randomUUID } from "node:crypto";
2
- import { chmod, copyFile, lstat, mkdir, readFile, realpath, rename, rm, writeFile } from "node:fs/promises";
2
+ import { chmod, lstat, mkdir, readFile, realpath, rename, rm, writeFile } from "node:fs/promises";
3
3
  import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
4
- import { PRODUCT_PACKAGE_NAME, deriveReleaseIdentity, resolveWithin } from "./release.js";
4
+ import { PRODUCT_PACKAGE_NAME, createReleaseIdentity, digestManifestFiles, discoverReleasePayload, releaseFileIdentity, resolveWithin, } from "./release.js";
5
5
  import { PRODUCT_IDENTITY, PRODUCT_TEXT } from "../../product-identity.js";
6
+ import { mapWithConcurrency } from "./concurrency.js";
7
+ const RELEASE_FILE_IO_CONCURRENCY = 32;
8
+ const certificationReady = new WeakSet();
6
9
  export const RELEASE_MANIFEST_FILENAME = PRODUCT_IDENTITY.manifest.releaseFilename;
7
- export async function materializeRelease(packageRoot, dataDir) {
8
- const identity = await deriveReleaseIdentity(packageRoot);
10
+ export async function materializeRelease(packageRoot, dataDir, options = {}) {
11
+ const payload = await discoverReleasePayload(packageRoot, {
12
+ onSourceRead: (path, bytes) => options.onOperation?.({ operation: "source-read", path, bytes }),
13
+ });
9
14
  const storeRoot = resolve(dataDir, "releases");
10
15
  await mkdir(storeRoot, { recursive: true, mode: 0o700 });
11
- const releaseRoot = resolveWithin(storeRoot, identity.releaseId);
12
- const existing = await lstat(releaseRoot).catch(() => null);
13
- if (existing)
14
- return await verifyMaterializedRelease(releaseRoot, identity);
15
- const candidate = resolveWithin(storeRoot, `.candidate-${identity.releaseId}-${randomUUID()}`);
16
+ options.onProgress?.({ phase: "copying", fileCount: payload.paths.length });
17
+ const candidate = resolveWithin(storeRoot, `.candidate-${randomUUID()}`);
16
18
  await mkdir(candidate, { recursive: false, mode: 0o700 });
17
19
  try {
18
- for (const file of identity.files) {
19
- const source = resolveWithin(identity.packageRoot, file.path);
20
- const destination = resolveWithin(candidate, file.path);
21
- await mkdir(dirname(destination), { recursive: true, mode: 0o700 });
22
- await copyFile(source, destination);
23
- await chmod(destination, file.executable ? 0o500 : 0o400);
20
+ const directories = [...new Set(payload.paths.map(path => dirname(resolveWithin(candidate, path))))];
21
+ await mapWithConcurrency(directories, RELEASE_FILE_IO_CONCURRENCY, async (directory) => {
22
+ await mkdir(directory, { recursive: true, mode: 0o700 });
23
+ });
24
+ const files = await mapWithConcurrency(payload.paths, RELEASE_FILE_IO_CONCURRENCY, async (path) => {
25
+ const source = resolveWithin(payload.packageRoot, path);
26
+ const destination = resolveWithin(candidate, path);
27
+ const metadata = await lstat(source);
28
+ if (!metadata.isFile() || metadata.isSymbolicLink())
29
+ throw new Error(`release payload is not a regular file: ${path}`);
30
+ const cached = payload.cachedFiles.get(path);
31
+ const bytes = cached ?? await readFile(source);
32
+ if (!cached)
33
+ options.onOperation?.({ operation: "source-read", path, bytes: bytes.length });
34
+ const mode = (metadata.mode & 0o111) !== 0 ? 0o500 : 0o400;
35
+ if (options.writeCandidateFile)
36
+ await options.writeCandidateFile(destination, bytes, mode);
37
+ else {
38
+ await writeFile(destination, bytes, { flag: "wx", mode });
39
+ await chmod(destination, mode);
40
+ }
41
+ options.onOperation?.({ operation: "candidate-write", path, bytes: bytes.length });
42
+ return releaseFileIdentity(path, bytes, (metadata.mode & 0o111) !== 0);
43
+ });
44
+ const identity = createReleaseIdentity(payload.packageRoot, payload.packageVersion, files);
45
+ const releaseRoot = resolveWithin(storeRoot, identity.releaseId);
46
+ if (await lstat(releaseRoot).catch(() => null)) {
47
+ await rm(candidate, { recursive: true, force: true });
48
+ return certificationReadyRelease(await verifyMaterializedRelease(releaseRoot, identity));
24
49
  }
25
50
  await writeFile(resolve(candidate, RELEASE_MANIFEST_FILENAME), JSON.stringify(identity, null, 2), { mode: 0o400, flag: "wx" });
26
- await verifyMaterializedRelease(candidate, identity);
27
- await rename(candidate, releaseRoot);
28
- return await verifyMaterializedRelease(releaseRoot, identity);
51
+ try {
52
+ await rename(candidate, releaseRoot);
53
+ }
54
+ catch (error) {
55
+ if (!await lstat(releaseRoot).catch(() => null))
56
+ throw error;
57
+ await rm(candidate, { recursive: true, force: true });
58
+ return certificationReadyRelease(await verifyMaterializedRelease(releaseRoot, identity));
59
+ }
60
+ return certificationReadyRelease({ ...identity, releaseRoot: await realpath(releaseRoot) });
29
61
  }
30
62
  catch (error) {
31
63
  await rm(candidate, { recursive: true, force: true });
32
64
  throw error;
33
65
  }
34
66
  }
67
+ /** Consume proof that this exact object was freshly materialized or fully verified in this process. */
68
+ export function consumeMaterializationProof(release) {
69
+ if (!certificationReady.has(release))
70
+ return false;
71
+ certificationReady.delete(release);
72
+ return true;
73
+ }
35
74
  export async function readMaterializedRelease(releaseRoot) {
36
75
  const canonical = await realpath(releaseRoot);
37
76
  const manifest = JSON.parse(await readFile(resolve(canonical, RELEASE_MANIFEST_FILENAME), "utf8"));
38
77
  return await verifyMaterializedRelease(canonical, manifest);
39
78
  }
40
- export async function verifyMaterializedRelease(releaseRoot, expected, selectedStoreRoot) {
79
+ /**
80
+ * Load metadata for a release whose bytes were already certified by the
81
+ * current parent process or an authenticated live supervisor. Callers must
82
+ * establish one of those preconditions; untrusted releases require full
83
+ * verification.
84
+ */
85
+ export async function readCertifiedReleaseManifest(record, selectedStoreRoot) {
86
+ const canonical = await realpath(record.releaseRoot);
87
+ assertContained(await realpath(selectedStoreRoot), canonical, "release root is outside the selected release store");
88
+ const manifest = JSON.parse(await readFile(resolveWithin(canonical, RELEASE_MANIFEST_FILENAME), "utf8"));
89
+ validateManifest(manifest);
90
+ if (manifest.releaseId !== record.releaseId || manifest.contentDigest !== record.contentDigest
91
+ || (record.packageVersion !== undefined && manifest.packageVersion !== record.packageVersion)) {
92
+ throw new Error(`certified release record differs from manifest for ${canonical}`);
93
+ }
94
+ if (canonical.split(sep).at(-1) !== manifest.releaseId)
95
+ throw new Error(`release directory does not match identity ${manifest.releaseId}`);
96
+ return { ...manifest, releaseRoot: canonical };
97
+ }
98
+ export async function verifyMaterializedRelease(releaseRoot, expected, selectedStoreRoot, options = {}) {
41
99
  const canonical = await realpath(releaseRoot);
42
100
  if (selectedStoreRoot)
43
101
  assertContained(await realpath(selectedStoreRoot), canonical, "release root is outside the selected release store");
@@ -50,15 +108,17 @@ export async function verifyMaterializedRelease(releaseRoot, expected, selectedS
50
108
  if (canonical.split(sep).at(-1) !== manifest.releaseId && !canonical.split(sep).at(-1)?.startsWith(`.candidate-${manifest.releaseId}-`)) {
51
109
  throw new Error(`release directory does not match identity ${manifest.releaseId}`);
52
110
  }
53
- for (const file of manifest.files)
54
- await verifyFile(canonical, file);
111
+ await mapWithConcurrency(manifest.files, RELEASE_FILE_IO_CONCURRENCY, async (file) => {
112
+ await verifyFile(canonical, file, options);
113
+ });
55
114
  const recomputed = digestManifestFiles(manifest.files);
56
115
  if (recomputed !== manifest.contentDigest)
57
116
  throw new Error(`release content digest mismatch for ${manifest.releaseId}`);
58
117
  return { ...manifest, releaseRoot: canonical };
59
118
  }
60
119
  export async function assertImmutableExecutionRoot(release, dataDir) {
61
- await verifyMaterializedRelease(release.releaseRoot, release, resolve(dataDir, "releases"));
120
+ const storeRoot = await realpath(resolve(dataDir, "releases"));
121
+ assertContained(storeRoot, release.releaseRoot, "release root is outside the selected release store");
62
122
  const selectedRoot = process.env[PRODUCT_IDENTITY.environment.releaseRoot];
63
123
  if (!selectedRoot)
64
124
  throw new Error(PRODUCT_TEXT.diagnostic("persistent process has no immutable release root"));
@@ -75,17 +135,23 @@ export async function resolveReleaseEntryPoint(release, entryPoint) {
75
135
  assertContained(release.releaseRoot, canonical, "entry point resolves outside the selected release root");
76
136
  return canonical;
77
137
  }
78
- async function verifyFile(root, file) {
138
+ async function verifyFile(root, file, options) {
79
139
  const path = resolveWithin(root, file.path);
80
140
  const metadata = await lstat(path).catch(() => null);
81
141
  if (!metadata?.isFile() || metadata.isSymbolicLink())
82
142
  throw new Error(`release candidate is incomplete: ${file.path}`);
83
143
  if (metadata.size !== file.bytes)
84
144
  throw new Error(`release file size mismatch: ${file.path}`);
85
- const digest = createHash("sha256").update(await readFile(path)).digest("hex");
145
+ const bytes = await readFile(path);
146
+ options.onOperation?.({ operation: "verification-read", path: file.path, bytes: bytes.length });
147
+ const digest = createHash("sha256").update(bytes).digest("hex");
86
148
  if (digest !== file.sha256)
87
149
  throw new Error(`release file digest mismatch: ${file.path}`);
88
150
  }
151
+ function certificationReadyRelease(release) {
152
+ certificationReady.add(release);
153
+ return release;
154
+ }
89
155
  function validateManifest(value) {
90
156
  if (value.packageName !== PRODUCT_PACKAGE_NAME || typeof value.packageVersion !== "string")
91
157
  throw new Error(PRODUCT_TEXT.diagnostic("release manifest metadata is invalid"));
@@ -101,13 +167,6 @@ function validateManifest(value) {
101
167
  throw new Error(`invalid release manifest file identity: ${file.path}`);
102
168
  }
103
169
  }
104
- function digestManifestFiles(files) {
105
- const digest = createHash("sha256");
106
- for (const file of [...files].sort((left, right) => left.path < right.path ? -1 : left.path > right.path ? 1 : 0)) {
107
- digest.update(`${file.path}\0${file.bytes}\0${file.sha256}\0${file.executable ? 1 : 0}\n`);
108
- }
109
- return digest.digest("hex");
110
- }
111
170
  function assertContained(parent, child, message) {
112
171
  const fromParent = relative(parent, child);
113
172
  if (fromParent === "" || (!fromParent.startsWith(`..${sep}`) && fromParent !== ".." && !isAbsolute(fromParent)))
@@ -13,11 +13,26 @@ export interface ReleaseIdentity {
13
13
  readonly packageRoot: string;
14
14
  readonly files: readonly ReleaseFileIdentity[];
15
15
  }
16
+ export interface DiscoveredReleasePayload {
17
+ readonly packageRoot: string;
18
+ readonly packageVersion: string;
19
+ readonly paths: readonly string[];
20
+ /** Package manifests already read to discover the dependency closure. */
21
+ readonly cachedFiles: ReadonlyMap<string, Buffer>;
22
+ }
23
+ export interface DiscoverReleasePayloadOptions {
24
+ readonly onSourceRead?: (path: string, bytes: number) => void;
25
+ }
26
+ /** Discover the complete payload without reading ordinary file contents. */
27
+ export declare function discoverReleasePayload(packageRoot: string, options?: DiscoverReleasePayloadOptions): Promise<DiscoveredReleasePayload>;
16
28
  /**
17
29
  * Derive release execution identity only from installed distribution metadata and
18
30
  * bytes. The version remains display metadata; the digest selects executable
19
31
  * content.
20
32
  */
21
33
  export declare function deriveReleaseIdentity(packageRoot: string): Promise<ReleaseIdentity>;
34
+ export declare function createReleaseIdentity(packageRoot: string, packageVersion: string, files: readonly ReleaseFileIdentity[]): ReleaseIdentity;
35
+ export declare function releaseFileIdentity(path: string, bytes: Uint8Array, executable: boolean): ReleaseFileIdentity;
36
+ export declare function digestManifestFiles(files: readonly ReleaseFileIdentity[]): string;
22
37
  export declare function resolveWithin(root: string, candidate: string): string;
23
38
  export declare function packageRootFromModule(moduleUrl: string): string;
@@ -2,54 +2,81 @@ import { createHash } from "node:crypto";
2
2
  import { lstat, readFile, readdir, realpath } from "node:fs/promises";
3
3
  import { basename, dirname, isAbsolute, relative, resolve, sep } from "node:path";
4
4
  import { PRODUCT_IDENTITY, PRODUCT_TEXT } from "../../product-identity.js";
5
+ import { mapWithConcurrency } from "./concurrency.js";
6
+ const RELEASE_FILE_IO_CONCURRENCY = 32;
5
7
  export const PRODUCT_PACKAGE_NAME = PRODUCT_IDENTITY.packageName;
6
- /**
7
- * Derive release execution identity only from installed distribution metadata and
8
- * bytes. The version remains display metadata; the digest selects executable
9
- * content.
10
- */
11
- export async function deriveReleaseIdentity(packageRoot) {
8
+ /** Discover the complete payload without reading ordinary file contents. */
9
+ export async function discoverReleasePayload(packageRoot, options = {}) {
12
10
  const canonicalRoot = await realpath(packageRoot);
13
- const manifestPath = resolve(canonicalRoot, "package.json");
14
- const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
11
+ const cachedFiles = new Map();
12
+ const readManifest = async (manifestRoot) => {
13
+ const manifestPath = resolve(manifestRoot, "package.json");
14
+ const bytes = await readFile(manifestPath);
15
+ const path = normalizeRelative(relative(canonicalRoot, manifestPath));
16
+ cachedFiles.set(path, bytes);
17
+ options.onSourceRead?.(path, bytes.length);
18
+ return JSON.parse(bytes.toString("utf8"));
19
+ };
20
+ const manifest = await readManifest(canonicalRoot);
15
21
  if (manifest.name !== PRODUCT_PACKAGE_NAME)
16
22
  throw new Error(`unexpected ${PRODUCT_TEXT.displayName} package name: ${String(manifest.name)}`);
17
23
  if (typeof manifest.version !== "string" || manifest.version.length === 0)
18
24
  throw new Error(PRODUCT_TEXT.diagnostic("package metadata has no version"));
19
25
  const roots = distributionRoots(manifest);
20
26
  const paths = new Set(["package.json"]);
21
- for (const root of roots) {
22
- const absolute = resolveWithin(canonicalRoot, root);
23
- await collectFiles(canonicalRoot, absolute, paths);
24
- }
25
- await collectDependencyClosure(canonicalRoot, canonicalRoot, manifest, paths, new Set());
26
- const files = [];
27
- for (const path of [...paths].sort()) {
28
- const absolute = resolveWithin(canonicalRoot, path);
27
+ for (const root of roots)
28
+ await collectFiles(canonicalRoot, resolveWithin(canonicalRoot, root), paths);
29
+ await collectDependencyClosure(canonicalRoot, canonicalRoot, manifest, paths, new Set(), readManifest);
30
+ return {
31
+ packageRoot: canonicalRoot,
32
+ packageVersion: manifest.version,
33
+ paths: [...paths].sort(),
34
+ cachedFiles,
35
+ };
36
+ }
37
+ /**
38
+ * Derive release execution identity only from installed distribution metadata and
39
+ * bytes. The version remains display metadata; the digest selects executable
40
+ * content.
41
+ */
42
+ export async function deriveReleaseIdentity(packageRoot) {
43
+ const payload = await discoverReleasePayload(packageRoot);
44
+ const files = await mapWithConcurrency(payload.paths, RELEASE_FILE_IO_CONCURRENCY, async (path) => {
45
+ const absolute = resolveWithin(payload.packageRoot, path);
29
46
  const metadata = await lstat(absolute);
30
- if (!metadata.isFile())
47
+ if (!metadata.isFile() || metadata.isSymbolicLink())
31
48
  throw new Error(`release payload is not a regular file: ${path}`);
32
- const bytes = await readFile(absolute);
33
- files.push({
34
- path: normalizeRelative(path),
35
- bytes: bytes.length,
36
- sha256: createHash("sha256").update(bytes).digest("hex"),
37
- executable: (metadata.mode & 0o111) !== 0,
38
- });
39
- }
40
- const digest = createHash("sha256");
41
- for (const file of files)
42
- digest.update(`${file.path}\0${file.bytes}\0${file.sha256}\0${file.executable ? 1 : 0}\n`);
43
- const contentDigest = digest.digest("hex");
49
+ const bytes = payload.cachedFiles.get(path) ?? await readFile(absolute);
50
+ return releaseFileIdentity(path, bytes, (metadata.mode & 0o111) !== 0);
51
+ });
52
+ return createReleaseIdentity(payload.packageRoot, payload.packageVersion, files);
53
+ }
54
+ export function createReleaseIdentity(packageRoot, packageVersion, files) {
55
+ const contentDigest = digestManifestFiles(files);
44
56
  return {
45
57
  packageName: PRODUCT_PACKAGE_NAME,
46
- packageVersion: manifest.version,
58
+ packageVersion,
47
59
  contentDigest,
48
- releaseId: `${manifest.version}-${contentDigest.slice(0, 20)}`,
49
- packageRoot: canonicalRoot,
50
- files,
60
+ releaseId: `${packageVersion}-${contentDigest.slice(0, 20)}`,
61
+ packageRoot,
62
+ files: [...files].sort(compareReleaseFiles),
51
63
  };
52
64
  }
65
+ export function releaseFileIdentity(path, bytes, executable) {
66
+ return {
67
+ path: normalizeRelative(path),
68
+ bytes: bytes.length,
69
+ sha256: createHash("sha256").update(bytes).digest("hex"),
70
+ executable,
71
+ };
72
+ }
73
+ export function digestManifestFiles(files) {
74
+ const digest = createHash("sha256");
75
+ for (const file of [...files].sort(compareReleaseFiles)) {
76
+ digest.update(`${file.path}\0${file.bytes}\0${file.sha256}\0${file.executable ? 1 : 0}\n`);
77
+ }
78
+ return digest.digest("hex");
79
+ }
53
80
  export function resolveWithin(root, candidate) {
54
81
  if (candidate.includes("\0"))
55
82
  throw new Error("release path contains a null byte");
@@ -91,7 +118,7 @@ async function collectFiles(root, path, output, skipNodeModules = false) {
91
118
  await collectFiles(root, resolve(path, entry), output, skipNodeModules);
92
119
  }
93
120
  }
94
- async function collectDependencyClosure(root, requesterRoot, manifest, output, visited) {
121
+ async function collectDependencyClosure(root, requesterRoot, manifest, output, visited, readManifest) {
95
122
  const required = dependencyNames(manifest.dependencies);
96
123
  const optional = new Set(dependencyNames(manifest.optionalDependencies));
97
124
  for (const name of [...new Set([...required, ...optional])].sort()) {
@@ -105,8 +132,7 @@ async function collectDependencyClosure(root, requesterRoot, manifest, output, v
105
132
  continue;
106
133
  visited.add(packagePath);
107
134
  await collectFiles(root, packagePath, output, true);
108
- const dependencyManifest = JSON.parse(await readFile(resolve(packagePath, "package.json"), "utf8"));
109
- await collectDependencyClosure(root, packagePath, dependencyManifest, output, visited);
135
+ await collectDependencyClosure(root, packagePath, await readManifest(packagePath), output, visited, readManifest);
110
136
  }
111
137
  }
112
138
  async function findInstalledDependency(root, requesterRoot, name) {
@@ -134,6 +160,9 @@ function dependencyNames(value) {
134
160
  throw new Error(PRODUCT_TEXT.diagnostic("dependency metadata is invalid"));
135
161
  return Object.keys(value);
136
162
  }
163
+ function compareReleaseFiles(left, right) {
164
+ return left.path < right.path ? -1 : left.path > right.path ? 1 : 0;
165
+ }
137
166
  function normalizeRelative(path) {
138
167
  return path.split(sep).join("/").replace(/^\.\//, "");
139
168
  }
@@ -18,6 +18,18 @@ export interface UpdateOutput {
18
18
  stdout(message: string): void;
19
19
  stderr(message: string): void;
20
20
  }
21
+ export type UpdateMeasuredPhase = "package-version" | "target-resolution" | "global-root" | "ownership-release" | "npm-install" | "materialized" | "certified" | "active-reference-committed" | "supervisor-verified" | "transaction-complete";
22
+ export interface UpdatePhaseTimingEvent {
23
+ readonly phase: UpdateMeasuredPhase;
24
+ readonly durationMs: number;
25
+ }
26
+ export interface UpdatePerformanceEvidence {
27
+ readonly fileCount: number;
28
+ readonly sourceReads: number;
29
+ readonly candidateWrites: number;
30
+ readonly verificationReads: number;
31
+ readonly postNpmDurationMs: number;
32
+ }
21
33
  export interface SelfUpdateOptions {
22
34
  packageRoot: string;
23
35
  channel?: UpdateChannel;
@@ -27,14 +39,18 @@ export interface SelfUpdateOptions {
27
39
  runner?: UpdateProcessRunner;
28
40
  lifecycle?: UpdateLifecycleCoordinator;
29
41
  transactionStore?: UpdateTransactionJournal;
42
+ progress?: boolean;
43
+ onPhaseTiming?: (event: UpdatePhaseTimingEvent) => void;
44
+ now?: () => number;
30
45
  }
46
+ export type UpdateActivationPhase = Extract<UpdateTransactionPhase, "materialized" | "certified" | "active-reference-committed">;
31
47
  export interface UpdateLifecycleCoordinator {
32
48
  targetIsActive(targetVersion: string): Promise<boolean>;
33
49
  shutdownVerifiedOwners(targetVersion: string): Promise<{
34
50
  priorActiveVersion: string | null;
35
51
  }>;
36
52
  verifyPackageUnlocked(packageRoot: string): Promise<void>;
37
- activateInstalled(packageRoot: string, targetVersion: string, phase: (phase: UpdateTransactionPhase) => Promise<void>): Promise<void>;
53
+ activateInstalled(packageRoot: string, targetVersion: string, phase: (phase: UpdateActivationPhase) => Promise<void>): Promise<void>;
38
54
  }
39
55
  export interface UpdateTransactionJournal {
40
56
  readonly path: string;
@@ -50,5 +66,6 @@ export interface UpdateTransactionJournal {
50
66
  clearCompleted(): Promise<void>;
51
67
  }
52
68
  export declare function createNpmProcessRunner(platform?: NodeJS.Platform): UpdateProcessRunner;
53
- export declare function createUpdateLifecycleCoordinator(environment?: NodeJS.ProcessEnv, fileSystem?: UpdateFileSystem): UpdateLifecycleCoordinator;
69
+ export declare function createUpdateLifecycleCoordinator(environment?: NodeJS.ProcessEnv, fileSystem?: UpdateFileSystem, output?: UpdateOutput): UpdateLifecycleCoordinator;
54
70
  export declare function runSelfUpdate(options: SelfUpdateOptions): Promise<number>;
71
+ export declare function assertUpdatePerformanceBudget(evidence: UpdatePerformanceEvidence, maximumPostNpmDurationMs?: number): void;