@mearl/setup 3.0.0-beta.8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,248 +1 @@
1
- import { createHash, randomUUID } from 'node:crypto';
2
- import { constants, closeSync, fstatSync, lstatSync, linkSync, mkdirSync, openSync, readSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
3
- import { homedir } from 'node:os';
4
- import { join, resolve } from 'node:path';
5
- import { withRecordLock } from '@mearl/daemon-core';
6
- import { mergePresetDefaults, prepareNodeBundleSnapshot, prepareCapabilitySnapshot, verifyCapabilityActivation, resolveCapabilityConfig, MAX_CAPABILITY_SELECTION_BYTES } from '@mearl/preset';
7
- import { BundleError, MAX_BROWSER_STORAGE_BYTES, presetRequiresBrowser, validateBundleManifest, verifyPreset, verifyBrowserPreset, verifyPreparedPreset } from '@mearl/preset/bundles';
8
- const digest = (bytes) => createHash('sha256').update(bytes).digest('hex');
9
- const hashPattern = /^[a-f0-9]{64}$/;
10
- const MAX_RECEIPT_BYTES = 16 * 1024 * 1024;
11
- const MAX_OVERRIDES_BYTES = 262_144;
12
- const POINTER_BYTES = MAX_CAPABILITY_SELECTION_BYTES + 128;
13
- function readBounded(file, limit) {
14
- const before = lstatSync(file);
15
- if (!before.isFile() || before.isSymbolicLink() || before.size > limit)
16
- throw new Error('Invalid preset resource file or size');
17
- const descriptor = openSync(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
18
- try {
19
- const stat = fstatSync(descriptor);
20
- if (!stat.isFile() || stat.size > limit)
21
- throw new Error('Invalid preset resource file or size');
22
- const bytes = Buffer.alloc(Math.min(stat.size, limit) + 1);
23
- let length = 0;
24
- while (length < bytes.byteLength) {
25
- const count = readSync(descriptor, bytes, length, bytes.byteLength - length, null);
26
- if (count === 0)
27
- break;
28
- length += count;
29
- }
30
- if (length > stat.size)
31
- throw new Error('Preset resource changed while reading');
32
- return bytes.subarray(0, length);
33
- }
34
- finally {
35
- closeSync(descriptor);
36
- }
37
- }
38
- function optionalFile(file, limit) {
39
- try {
40
- return readBounded(file, limit);
41
- }
42
- catch (error) {
43
- if (error.code === 'ENOENT')
44
- return undefined;
45
- throw error;
46
- }
47
- }
48
- function atomicWrite(file, bytes) {
49
- const temporary = `${file}.${process.pid}.${randomUUID()}.tmp`;
50
- try {
51
- writeFileSync(temporary, bytes, { flag: 'wx', mode: 0o600 });
52
- renameSync(temporary, file);
53
- }
54
- finally {
55
- rmSync(temporary, { force: true });
56
- }
57
- }
58
- function revision(state) {
59
- return state.activation?.installationRevision ?? (state.activation ? `node:${state.activation.revision}` : null);
60
- }
61
- function overrides(value) {
62
- const accepted = mergePresetDefaults({}, value);
63
- if (Buffer.byteLength(JSON.stringify(accepted)) > MAX_OVERRIDES_BYTES)
64
- throw new BundleError('BUNDLE_QUOTA', 'User overrides exceed 256 KiB');
65
- return accepted;
66
- }
67
- /** One pointer binds the desired preset, user overrides and exact Node selection. */
68
- export class PresetInstallationStore {
69
- directory;
70
- mearlApiVersion;
71
- pointer;
72
- constructor(options = {}) {
73
- this.directory = resolve(options.directory ?? (process.env.MEARL_CAPABILITY_DIR || join(homedir(), '.mearl/capabilities')));
74
- this.mearlApiVersion = options.mearlApiVersion ?? '1.0.0';
75
- this.pointer = join(this.directory, 'active-node.json');
76
- }
77
- assertCurrent(expected) {
78
- const bytes = optionalFile(this.pointer, POINTER_BYTES);
79
- if ((bytes ? digest(bytes) : null) !== expected)
80
- throw new BundleError('BUNDLE_CONFLICT', 'The installed preset changed during preparation');
81
- }
82
- async current(options = {}) {
83
- const raw = optionalFile(this.pointer, POINTER_BYTES);
84
- if (!raw)
85
- return { rawHash: null };
86
- const rawHash = digest(raw);
87
- const activation = await verifyCapabilityActivation(JSON.parse(raw.toString('utf8')));
88
- if (!activation.installationRevision)
89
- return { rawHash, activation };
90
- const captured = withRecordLock(this.pointer, () => {
91
- this.assertCurrent(rawHash);
92
- const receiptBytes = readBounded(join(this.directory, 'installations', `${activation.installationRevision}.json`), MAX_RECEIPT_BYTES);
93
- const receipt = JSON.parse(receiptBytes.toString('utf8'));
94
- const { revision: receiptRevision, ...data } = receipt;
95
- if (receiptRevision !== activation.installationRevision || digest(JSON.stringify(data)) !== receiptRevision || data.schemaVersion !== 1 ||
96
- Object.keys(data).some(key => !['schemaVersion', 'snapshot', 'bundles', 'browserRevision', 'nodeRevision', 'managedModules', 'nodeSyncRequired', 'browserSyncRequired', 'overrides'].includes(key)) || typeof data.nodeSyncRequired !== 'boolean' || (data.browserSyncRequired !== undefined && typeof data.browserSyncRequired !== 'boolean') ||
97
- data.nodeRevision !== activation.revision || !Array.isArray(data.bundles) || data.bundles.length > 64 ||
98
- !Array.isArray(data.managedModules) || new Set(data.managedModules).size !== data.managedModules.length || data.managedModules.some(id => typeof id !== 'string' || !Object.hasOwn(activation.modules, id))) {
99
- throw new BundleError('BUNDLE_INTEGRITY', 'Installed preset receipt does not match its selection');
100
- }
101
- const acceptedOverrides = overrides(data.overrides);
102
- let total = 0;
103
- const metadata = data.bundles.map(bundle => {
104
- if (Object.keys(bundle).some(key => !['source', 'manifest', 'revision'].includes(key)))
105
- throw new BundleError('BUNDLE_INVALID', 'Invalid installed package metadata');
106
- const manifest = validateBundleManifest(bundle.manifest, this.mearlApiVersion);
107
- total += Object.values(manifest.resources).reduce((sum, entry) => sum + entry.byteLength, 0);
108
- return { ...bundle, manifest };
109
- });
110
- if (total > MAX_BROWSER_STORAGE_BYTES)
111
- throw new BundleError('BUNDLE_QUOTA', 'Installed preset resources exceed 64 MiB');
112
- const bundles = options.verifyResources === false ? [] : metadata.map(bundle => ({ ...bundle, resources: Object.fromEntries(Object.entries(bundle.manifest.resources).map(([name, entry]) => [name, readBounded(join(this.directory, 'blobs', `${entry.sha256}.bin`), entry.byteLength)])) }));
113
- for (const entry of options.verifyResources === false ? [] : Object.values(activation.modules)) {
114
- const bytes = readBounded(join(this.directory, 'objects', `${entry.sha256}.cjs`), entry.byteLength);
115
- if (bytes.byteLength !== entry.byteLength || digest(bytes) !== entry.sha256)
116
- throw new BundleError('BUNDLE_INTEGRITY', 'Installed Node object is missing or corrupt');
117
- }
118
- return { receipt: { ...receipt, overrides: acceptedOverrides }, bundles };
119
- });
120
- if (options.verifyResources === false) {
121
- const graph = await verifyPreset(captured.receipt.snapshot, this.mearlApiVersion);
122
- await verifyBrowserPreset({ source: graph.snapshot.source, manifests: graph.snapshot.manifests, packageTargets: graph.snapshot.packageTargets,
123
- bundleRevisions: Object.fromEntries(Object.entries(graph.snapshot.bundleRevisions).filter(([id]) => graph.preset.packages[id].targets?.includes('browser'))), revision: captured.receipt.browserRevision }, this.mearlApiVersion);
124
- return { rawHash, activation, receipt: captured.receipt, preset: graph.preset };
125
- }
126
- const prepared = await verifyPreparedPreset({ snapshot: captured.receipt.snapshot, bundles: captured.bundles }, this.mearlApiVersion);
127
- if (prepared.browser.snapshot.revision !== captured.receipt.browserRevision)
128
- throw new BundleError('BUNDLE_INTEGRITY', 'Installed browser projection does not match');
129
- const selected = await prepareNodeBundleSnapshot(prepared.bundles.filter(bundle => prepared.preset.packages[bundle.manifest.id].targets?.includes('node')), 'native-host', this.mearlApiVersion, resolveCapabilityConfig(mergePresetDefaults(prepared.preset.defaults, captured.receipt.overrides)));
130
- if (Object.keys(selected.snapshot.modules).length !== captured.receipt.managedModules.length || captured.receipt.managedModules.some(id => !Object.hasOwn(selected.snapshot.modules, id)) ||
131
- Object.entries(selected.snapshot.modules).some(([id, entry]) => JSON.stringify(entry) !== JSON.stringify(activation.modules[id])) ||
132
- Object.entries(selected.snapshot.bindings).some(([hook, entry]) => JSON.stringify(entry) !== JSON.stringify(activation.bindings[hook])) ||
133
- Object.values(activation.bindings).filter(entry => captured.receipt.managedModules.includes(entry.module)).length !== Object.keys(selected.snapshot.bindings).length) {
134
- throw new BundleError('BUNDLE_INTEGRITY', 'Installed managed Node selection does not match its packages');
135
- }
136
- return { rawHash, activation, prepared, receipt: captured.receipt, preset: prepared.preset };
137
- }
138
- async apply(input, options) {
139
- options.signal?.throwIfAborted();
140
- const acceptedOverrides = options.overrides === undefined ? undefined : overrides(options.overrides);
141
- const [current, prepared] = await Promise.all([this.current({ verifyResources: false }), verifyPreparedPreset(input, this.mearlApiVersion)]);
142
- if (revision(current) !== options.expectedRevision)
143
- throw new BundleError('BUNDLE_CONFLICT', 'The installed preset changed');
144
- const selected = await prepareNodeBundleSnapshot(prepared.bundles.filter(bundle => prepared.preset.packages[bundle.manifest.id].targets?.includes('node')), 'native-host', this.mearlApiVersion, resolveCapabilityConfig(mergePresetDefaults(prepared.preset.defaults, acceptedOverrides ?? current.receipt?.overrides ?? {})));
145
- const previousManaged = new Set(current.receipt?.managedModules ?? []);
146
- const unmanaged = Object.fromEntries(Object.entries(current.activation?.modules ?? {}).filter(([id]) => !previousManaged.has(id)));
147
- for (const entry of Object.values(unmanaged)) {
148
- const bytes = readBounded(join(this.directory, 'objects', `${entry.sha256}.cjs`), entry.byteLength);
149
- if (bytes.byteLength !== entry.byteLength || digest(bytes) !== entry.sha256)
150
- throw new BundleError('BUNDLE_INTEGRITY', 'An independent Node module is corrupt');
151
- }
152
- if (Object.keys(selected.snapshot.modules).some(id => Object.hasOwn(unmanaged, id)))
153
- throw new BundleError('BUNDLE_CONFLICT', 'A preset module conflicts with an independently installed Node module');
154
- const bindings = Object.fromEntries(Object.entries(current.activation?.bindings ?? {}).filter(([, entry]) => Object.hasOwn(unmanaged, entry.module)));
155
- if (Object.keys(selected.snapshot.bindings).some(hook => Object.hasOwn(bindings, hook)))
156
- throw new BundleError('BUNDLE_CONFLICT', 'A preset hook conflicts with an independently installed Node module');
157
- const snapshot = await prepareCapabilitySnapshot({ schemaVersion: 1, modules: { ...unmanaged, ...selected.snapshot.modules }, bindings: { ...bindings, ...selected.snapshot.bindings } });
158
- const data = {
159
- schemaVersion: 1, snapshot: prepared.snapshot,
160
- bundles: prepared.bundles.map(({ source, revision, manifest }) => ({ source, revision, manifest })),
161
- browserRevision: prepared.browser.snapshot.revision, nodeRevision: snapshot.revision,
162
- managedModules: Object.keys(selected.snapshot.modules), overrides: acceptedOverrides ?? current.receipt?.overrides ?? {},
163
- nodeSyncRequired: Boolean(current.receipt?.nodeSyncRequired || current.receipt?.managedModules.length || Object.values(prepared.preset.packages).some(entry => entry.enabled !== false && entry.targets?.includes('node'))),
164
- browserSyncRequired: Boolean(current.receipt?.browserSyncRequired || presetRequiresBrowser(prepared.preset)),
165
- };
166
- const receipt = { ...data, revision: digest(JSON.stringify(data)) };
167
- const receiptBytes = JSON.stringify(receipt);
168
- if (Buffer.byteLength(receiptBytes) > MAX_RECEIPT_BYTES)
169
- throw new BundleError('BUNDLE_QUOTA', 'Preset installation receipt exceeds 16 MiB');
170
- const activation = { ...snapshot, installationRevision: receipt.revision };
171
- await options.confirm?.();
172
- withRecordLock(this.pointer, () => {
173
- this.assertCurrent(current.rawHash);
174
- options.signal?.throwIfAborted();
175
- for (const child of ['blobs', 'objects', 'installations'])
176
- mkdirSync(join(this.directory, child), { recursive: true, mode: 0o700 });
177
- for (const bundle of prepared.bundles)
178
- for (const [name, entry] of Object.entries(bundle.manifest.resources)) {
179
- atomicWrite(join(this.directory, 'blobs', `${entry.sha256}.bin`), bundle.resources[name]);
180
- }
181
- for (const entry of Object.values(selected.snapshot.modules)) {
182
- const destination = join(this.directory, 'objects', `${entry.sha256}.cjs`);
183
- const temporary = `${destination}.${randomUUID()}.tmp`;
184
- try {
185
- linkSync(join(this.directory, 'blobs', `${entry.sha256}.bin`), temporary);
186
- renameSync(temporary, destination);
187
- }
188
- finally {
189
- rmSync(temporary, { force: true });
190
- }
191
- }
192
- atomicWrite(join(this.directory, 'installations', `${receipt.revision}.json`), receiptBytes);
193
- if (current.receipt?.revision !== receipt.revision)
194
- atomicWrite(join(this.directory, 'rollback.json'), JSON.stringify({ candidate: receipt.revision, previous: current.activation ?? null }));
195
- atomicWrite(this.pointer, JSON.stringify(activation));
196
- });
197
- return receipt;
198
- }
199
- async rollback(expectedRevision) {
200
- const current = await this.current({ verifyResources: false });
201
- if (current.activation?.installationRevision !== expectedRevision)
202
- throw new BundleError('BUNDLE_CONFLICT', 'A newer preset installation cannot be rolled back');
203
- const record = JSON.parse(readBounded(join(this.directory, 'rollback.json'), POINTER_BYTES + 256).toString('utf8'));
204
- if (record.candidate !== expectedRevision)
205
- throw new BundleError('BUNDLE_CONFLICT', 'The preset rollback record has changed');
206
- const previous = record.previous === null ? undefined : await verifyCapabilityActivation(record.previous);
207
- withRecordLock(this.pointer, () => {
208
- this.assertCurrent(current.rawHash);
209
- if (previous)
210
- atomicWrite(this.pointer, JSON.stringify(previous));
211
- else
212
- rmSync(this.pointer, { force: true });
213
- rmSync(join(this.directory, 'rollback.json'), { force: true });
214
- this.collectLocked(previous);
215
- });
216
- }
217
- async collectGarbage() {
218
- const current = await this.current({ verifyResources: false });
219
- const raw = optionalFile(join(this.directory, 'rollback.json'), POINTER_BYTES + 256);
220
- const record = raw && JSON.parse(raw.toString('utf8'));
221
- const previous = record?.candidate === current.activation?.installationRevision && record.previous !== null ? await verifyCapabilityActivation(record.previous) : undefined;
222
- withRecordLock(this.pointer, () => {
223
- this.assertCurrent(current.rawHash);
224
- for (const child of ['blobs', 'objects', 'installations'])
225
- mkdirSync(join(this.directory, child), { recursive: true, mode: 0o700 });
226
- this.collectLocked(current.activation, previous);
227
- });
228
- }
229
- collectLocked(active, previous) {
230
- const receipts = new Set([active?.installationRevision, previous?.installationRevision].filter((value) => Boolean(value)));
231
- const blobs = new Set();
232
- const modules = new Set([active, previous].flatMap(selection => Object.values(selection?.modules ?? {}).map(entry => entry.sha256)));
233
- for (const id of receipts) {
234
- const record = JSON.parse(readBounded(join(this.directory, 'installations', `${id}.json`), MAX_RECEIPT_BYTES).toString('utf8'));
235
- for (const bundle of record.bundles)
236
- for (const entry of Object.values(bundle.manifest.resources))
237
- blobs.add(entry.sha256);
238
- }
239
- for (const [directory, extension, retained] of [['blobs', '.bin', blobs], ['objects', '.cjs', modules], ['installations', '.json', receipts]]) {
240
- for (const name of readdirSync(join(this.directory, directory))) {
241
- const id = name.slice(0, -extension.length);
242
- if (name.endsWith(extension) && hashPattern.test(id) && !retained.has(id))
243
- rmSync(join(this.directory, directory, name), { force: true });
244
- }
245
- }
246
- }
247
- }
248
- export function presetSelectionRevision(state) { return revision(state); }
1
+ export { PresetInstallationStore, presetSelectionRevision } from '@mearl/daemon-core/installation';
@@ -1,7 +1,7 @@
1
1
  import { PresetInstaller } from './preset.js';
2
2
  /** Preserve the accepted installation and resume its exact resources before Skill operations. */
3
3
  export async function prepareSetupPreset(options, dependencies = {}) {
4
- const installer = dependencies.installer ?? new PresetInstaller({ directory: (options.env ?? process.env).MEARL_CAPABILITY_DIR });
4
+ const installer = dependencies.installer ?? new PresetInstaller({ directory: (options.env ?? process.env).MEARL_CAPABILITY_DIR, connectedExtensionsOnly: true });
5
5
  if (options.source)
6
6
  return installer.stage(options.source);
7
7
  const current = await installer.store.current();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mearl/setup",
3
- "version": "3.0.0-beta.8",
3
+ "version": "3.0.0",
4
4
  "description": "One-shot environment setup and configuration for Mearl",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -55,6 +55,7 @@
55
55
  "mearl-setup": "./dist/bin.js"
56
56
  },
57
57
  "files": [
58
+ "README.en.md",
58
59
  "dist",
59
60
  "!dist/**/*.map",
60
61
  "README.md"
@@ -78,11 +79,11 @@
78
79
  "ws": "^8.18.3",
79
80
  "semver": "^7.8.5",
80
81
  "adm-zip": "^0.6.1",
81
- "@mearl/client": "3.0.0-beta.8",
82
- "@mearl/provider": "3.0.0-beta.8",
83
- "@mearl/daemon-core": "3.0.0-beta.8",
84
- "@mearl/browser-core": "3.0.0-beta.8",
85
- "@mearl/preset": "3.0.0-beta.8"
82
+ "@mearl/client": "3.0.0",
83
+ "@mearl/provider": "3.0.0",
84
+ "@mearl/daemon-core": "3.0.0",
85
+ "@mearl/preset": "3.0.0",
86
+ "@mearl/browser-core": "3.0.0"
86
87
  },
87
88
  "devDependencies": {
88
89
  "@types/node": "^24.9.1",