@mulmoclaude/core 7.6.0 → 7.7.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.
@@ -17,3 +17,9 @@ export declare function isCacheValid(cachePath: string): boolean;
17
17
  * marker is written ONLY after tar exits 0 — readers should test
18
18
  * `isCacheValid()`, not `existsSync(cachePath)`. */
19
19
  export declare function extractTgz(tgzAbs: string, destDir: string): void;
20
+ export declare function publishStaged(staging: string, destDir: string): void;
21
+ /** Unpack `bytes` (a gzipped npm tarball) to `destDir` so that no reader ever sees a partial tree, even
22
+ * with two processes (MulmoClaude and MulmoTerminal on one workspace) unpacking the same folder at once:
23
+ * into a unique sibling folder, completed with the marker, then renamed into place. The bytes are the
24
+ * ones the caller already read and keyed the folder by, so what is unpacked is what the key names. */
25
+ export declare function extractBytesAtomically(bytes: Buffer, destDir: string): void;
@@ -142,6 +142,53 @@ function extractTgz(tgzAbs, destDir) {
142
142
  throw err;
143
143
  }
144
144
  }
145
+ var removeQuietly = (dir) => {
146
+ try {
147
+ (0, node_fs.rmSync)(dir, {
148
+ recursive: true,
149
+ force: true
150
+ });
151
+ } catch {}
152
+ };
153
+ function publishStaged(staging, destDir) {
154
+ try {
155
+ (0, node_fs.renameSync)(staging, destDir);
156
+ } catch (err) {
157
+ removeQuietly(staging);
158
+ if (!isCacheValid(destDir)) throw err;
159
+ }
160
+ }
161
+ /** Unpack `bytes` (a gzipped npm tarball) to `destDir` so that no reader ever sees a partial tree, even
162
+ * with two processes (MulmoClaude and MulmoTerminal on one workspace) unpacking the same folder at once:
163
+ * into a unique sibling folder, completed with the marker, then renamed into place. The bytes are the
164
+ * ones the caller already read and keyed the folder by, so what is unpacked is what the key names. */
165
+ function extractBytesAtomically(bytes, destDir) {
166
+ if (isCacheValid(destDir)) return;
167
+ const parent = node_path.default.dirname(destDir);
168
+ (0, node_fs.mkdirSync)(parent, { recursive: true });
169
+ const staging = (0, node_fs.mkdtempSync)(node_path.default.join(parent, `.${node_path.default.basename(destDir)}-`));
170
+ try {
171
+ (0, node_child_process.execFileSync)("tar", [
172
+ "-xzf",
173
+ "-",
174
+ "-C",
175
+ staging,
176
+ "--strip-components=1"
177
+ ], {
178
+ input: bytes,
179
+ stdio: [
180
+ "pipe",
181
+ "pipe",
182
+ "pipe"
183
+ ]
184
+ });
185
+ writeMarker(node_path.default.join(staging, EXTRACT_MARKER));
186
+ publishStaged(staging, destDir);
187
+ } catch (err) {
188
+ removeQuietly(staging);
189
+ throw err;
190
+ }
191
+ }
145
192
  // src/runtime-plugins/containment.ts
146
193
  /** True when `file` exists and, with every link followed, is inside `root`. */
147
194
  function resolvesInside(file, root) {
@@ -313,9 +360,50 @@ function ensureInsideBase(candidate, base) {
313
360
  /** Make a ledger entry's cache directory ready to load: both of its paths anchored inside their
314
361
  * roots, its tgz present, and the tgz extracted unless a complete extract is already there.
315
362
  * Returns the cache directory, or null (logged) when the entry cannot be loaded. */
363
+ var MAX_PACKAGE_FILE_BYTES = 52428800;
364
+ var READ_CHUNK_BYTES$1 = 65536;
365
+ var READ_FLAGS$1 = node_fs.constants.O_RDONLY | (node_fs.constants.O_NONBLOCK ?? 0);
366
+ function readChunks(fileHandle, maxBytes) {
367
+ const chunks = [];
368
+ let received = 0;
369
+ for (;;) {
370
+ const chunk = Buffer.alloc(READ_CHUNK_BYTES$1);
371
+ const bytesRead = (0, node_fs.readSync)(fileHandle, chunk, 0, READ_CHUNK_BYTES$1, null);
372
+ if (bytesRead === 0) return Buffer.concat(chunks, received);
373
+ received += bytesRead;
374
+ if (received > maxBytes) return null;
375
+ chunks.push(chunk.subarray(0, bytesRead));
376
+ }
377
+ }
378
+ /** The package file's bytes, or null when it is not a regular file or grows past the limit while read. */
379
+ function readPackageFile$1(tgzAbs) {
380
+ const fileHandle = (0, node_fs.openSync)(tgzAbs, READ_FLAGS$1);
381
+ try {
382
+ return (0, node_fs.fstatSync)(fileHandle).isFile() ? readChunks(fileHandle, MAX_PACKAGE_FILE_BYTES) : null;
383
+ } finally {
384
+ (0, node_fs.closeSync)(fileHandle);
385
+ }
386
+ }
387
+ function loggedPackageBytes(tgzAbs, entry, log) {
388
+ try {
389
+ const bytes = readPackageFile$1(tgzAbs);
390
+ if (bytes === null) log.warn("tgz is not a package file within the size limit — skipping", {
391
+ name: entry.name,
392
+ tgz: entry.tgz
393
+ });
394
+ return bytes;
395
+ } catch (err) {
396
+ log.error("tgz unreadable — skipping", {
397
+ name: entry.name,
398
+ tgz: entry.tgz,
399
+ error: String(err)
400
+ });
401
+ return null;
402
+ }
403
+ }
316
404
  function prepareLedgerEntry(entry, roots, log) {
317
405
  const tgzAbs = node_path.default.join(roots.pluginsDir, entry.tgz);
318
- const cachePath = node_path.default.join(roots.cacheDir, entry.name, entry.version);
406
+ const versionDir = node_path.default.join(roots.cacheDir, entry.name, entry.version);
319
407
  if (!ensureInsideBase(tgzAbs, roots.pluginsDir)) {
320
408
  log.warn("ledger entry tgz escapes plugins root — skipping", {
321
409
  name: entry.name,
@@ -323,7 +411,7 @@ function prepareLedgerEntry(entry, roots, log) {
323
411
  });
324
412
  return null;
325
413
  }
326
- if (!ensureInsideBase(cachePath, roots.cacheDir)) {
414
+ if (!ensureInsideBase(versionDir, roots.cacheDir)) {
327
415
  log.warn("ledger entry escapes plugin cache root — skipping", {
328
416
  name: entry.name,
329
417
  version: entry.version
@@ -337,8 +425,11 @@ function prepareLedgerEntry(entry, roots, log) {
337
425
  });
338
426
  return null;
339
427
  }
428
+ const bytes = loggedPackageBytes(tgzAbs, entry, log);
429
+ if (bytes === null) return null;
430
+ const cachePath = node_path.default.join(versionDir, (0, node_crypto.createHash)("sha256").update(bytes).digest("hex"));
340
431
  if (!isCacheValid(cachePath)) try {
341
- extractTgz(tgzAbs, cachePath);
432
+ extractBytesAtomically(bytes, cachePath);
342
433
  log.info("extracted", {
343
434
  name: entry.name,
344
435
  version: entry.version
@@ -1001,16 +1092,6 @@ async function preparePlan(source, deps) {
1001
1092
  raw: ledger.raw
1002
1093
  };
1003
1094
  }
1004
- async function dropStaleCache(plan, raw, paths) {
1005
- const sameVersion = parseLedger(raw).filter((entry) => entry.name === plan.name && entry.version === plan.version);
1006
- if (sameVersion.length > 0 && sameVersion.every((entry) => entry.tgz === plan.tgz)) return;
1007
- const cacheDir = paths.cacheDir ?? node_path.default.join(paths.pluginsDir, ".cache");
1008
- const stale = node_path.default.join(cacheDir, plan.name, plan.version);
1009
- if (ensureInsideBase(stale, cacheDir)) await (0, node_fs_promises.rm)(stale, {
1010
- recursive: true,
1011
- force: true
1012
- });
1013
- }
1014
1095
  async function writePackageFile(plan, bytes, pluginsDir) {
1015
1096
  const tgzPath = node_path.default.join(pluginsDir, plan.tgz);
1016
1097
  const fileWasThere = (0, node_fs.existsSync)(tgzPath);
@@ -1032,7 +1113,6 @@ async function commit(plan, bytes, raw, deps) {
1032
1113
  source: plan.source
1033
1114
  };
1034
1115
  try {
1035
- await dropStaleCache(plan, raw, deps.paths);
1036
1116
  await writeLedger(deps.paths.ledgerPath, withLedgerEntry(raw, entry));
1037
1117
  } catch (err) {
1038
1118
  if (!fileWasThere) await (0, node_fs_promises.rm)(tgzPath, { force: true });