@solidactions/cli 3.21.0 → 3.23.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.
Files changed (53) hide show
  1. package/README.md +7 -3
  2. package/dist/command-manifest.json +1 -1
  3. package/dist/commands/connection-list.js +1 -1
  4. package/dist/commands/crew-env-delete.js +2 -2
  5. package/dist/commands/crew-env-list.js +2 -2
  6. package/dist/commands/crew-env-map-database.js +4 -1
  7. package/dist/commands/crew-env-push.js +5 -2
  8. package/dist/commands/crew-env-set.js +2 -2
  9. package/dist/commands/database-push.js +2 -1
  10. package/dist/commands/deploy.js +44 -28
  11. package/dist/commands/device-login.js +3 -2
  12. package/dist/commands/doc-pull.js +628 -90
  13. package/dist/commands/doc-push.js +54 -3
  14. package/dist/commands/doc-upload.js +5 -2
  15. package/dist/commands/env-delete.js +12 -5
  16. package/dist/commands/env-list.js +12 -7
  17. package/dist/commands/env-map.js +7 -5
  18. package/dist/commands/env-pull.js +5 -6
  19. package/dist/commands/env-push.js +10 -6
  20. package/dist/commands/env-reset.js +12 -8
  21. package/dist/commands/env-set.js +15 -11
  22. package/dist/commands/login.js +84 -20
  23. package/dist/commands/oauth-action-list.js +2 -2
  24. package/dist/commands/oauth-action-platforms.js +2 -2
  25. package/dist/commands/oauth-action-search.js +2 -2
  26. package/dist/commands/oauth-action-view.js +2 -2
  27. package/dist/commands/project-create.js +1 -1
  28. package/dist/commands/project-list.js +2 -2
  29. package/dist/commands/project-logs.js +7 -3
  30. package/dist/commands/project-view.js +26 -9
  31. package/dist/commands/pull.js +4 -2
  32. package/dist/commands/run-list.js +24 -3
  33. package/dist/commands/run-start.js +12 -9
  34. package/dist/commands/run-view.js +2 -2
  35. package/dist/commands/schedule-delete.js +5 -10
  36. package/dist/commands/schedule-list.js +5 -10
  37. package/dist/commands/schedule-set.js +39 -44
  38. package/dist/commands/schedule-state.js +12 -16
  39. package/dist/commands/skill-exec.js +4 -1
  40. package/dist/commands/skill-run.js +4 -1
  41. package/dist/commands/state.js +3 -3
  42. package/dist/commands/webhook-list.js +12 -4
  43. package/dist/commands/webhook-secret.js +12 -4
  44. package/dist/commands/workflow-view.js +20 -4
  45. package/dist/commands/workspaces.js +53 -2
  46. package/dist/index.js +2 -1
  47. package/dist/utils/api.js +18 -4
  48. package/dist/utils/config.js +5 -4
  49. package/dist/utils/crew.js +6 -1
  50. package/dist/utils/host-display.js +47 -0
  51. package/dist/utils/project-ref.js +91 -0
  52. package/dist/utils/workspace-lookup.js +11 -1
  53. package/package.json +2 -2
@@ -2,10 +2,11 @@
2
2
  /**
3
3
  * solidactions doc pull <folder> [dest]
4
4
  *
5
- * Downloads a Docs folder tree from SA-Docs into a local markdown tree (the
5
+ * Downloads a Docs folder tree from SA-Docs into a local file tree (the
6
6
  * inverse of `doc push`): BFS-walks the folder via `docs_read` `list`,
7
7
  * bulk-fetches doc bodies via `docs_read` `bulk_read`, and writes each doc as
8
- * <dest>/<relative-folder>/<sanitized-title>.md plus a revision manifest
8
+ * <dest>/<relative-folder>/<sanitized-title>.md — visual docs as `.html` and
9
+ * canvases as `.canvas.json` — plus a revision manifest
9
10
  * (<dest>/.solidactions-docs.json) recording id/title/current_revision_id
10
11
  * per file for later diffing.
11
12
  *
@@ -166,7 +167,13 @@ async function listTree(config, folderPath) {
166
167
  queue.push({ folder_path: folder.folder_path, relative: childRelative });
167
168
  }
168
169
  for (const doc of result.data?.docs ?? []) {
169
- rows.push({ id: doc.id, title: doc.title, relative });
170
+ rows.push({
171
+ id: doc.id,
172
+ title: doc.title,
173
+ relative,
174
+ docType: doc.doc_type == null ? null : (doc.doc_type.slug ?? null),
175
+ docTypeKnown: Object.prototype.hasOwnProperty.call(doc, 'doc_type'),
176
+ });
170
177
  }
171
178
  }
172
179
  return { ok: true, rows };
@@ -224,6 +231,39 @@ async function fetchBodies(config, rows) {
224
231
  }
225
232
  return { fetched, warnings };
226
233
  }
234
+ /**
235
+ * Fill in the doc type for fetched docs whose list row did not carry a
236
+ * `doc_type` key (cli#157): one `read_doc {id}` per unknown doc. A doc whose
237
+ * type still cannot be read keeps `null` (written as .md) with a warning.
238
+ */
239
+ async function backfillDocTypes(config, fetched) {
240
+ const warnings = [];
241
+ for (const doc of fetched) {
242
+ if (doc.docTypeKnown)
243
+ continue;
244
+ let slug = null;
245
+ let readOk = false;
246
+ try {
247
+ const result = await (0, mcp_1.callDocsTool)(config, { action: 'read_doc', id: doc.id });
248
+ if (result.ok) {
249
+ readOk = true;
250
+ slug = result.data?.doc_type?.slug ?? null;
251
+ }
252
+ }
253
+ catch {
254
+ readOk = false;
255
+ }
256
+ // A successful read that simply reports no type (doc_type: null) is a
257
+ // valid untyped doc, not a failure — warn only when the type could
258
+ // not be read at all.
259
+ if (!readOk) {
260
+ warnings.push(`warn: could not read the type of doc ${doc.id} (${doc.title}); writing it as .md`);
261
+ }
262
+ doc.docType = slug;
263
+ doc.docTypeKnown = true;
264
+ }
265
+ return warnings;
266
+ }
227
267
  /**
228
268
  * Allocate the first free `<base>[ -N]<ext>` name not present in `used`,
229
269
  * recording the winner in `used`.
@@ -231,7 +271,8 @@ async function fetchBodies(config, rows) {
231
271
  function allocateName(used, base, ext) {
232
272
  let candidate = base;
233
273
  let suffix = 2;
234
- while (used.has(`${candidate}${ext}`)) {
274
+ const usedFolded = new Set([...used].map((name) => name.toLowerCase()));
275
+ while (usedFolded.has(`${candidate}${ext}`.toLowerCase())) {
235
276
  candidate = `${base}-${suffix}`;
236
277
  suffix++;
237
278
  }
@@ -265,9 +306,11 @@ function isCollisionVariant(file, base, ext) {
265
306
  * name still forces a suffix. When the tracked title is unchanged and the
266
307
  * tracked path is a collision variant of that title in the same folder,
267
308
  * the doc keeps that exact path (R2-I2); a renamed/stale path still
268
- * reallocates. Folder pulls pass nothing and behave exactly as before.
309
+ * reallocates. Folder pulls reserve only paths whose title, directory and
310
+ * extension stay unchanged, keeping their exact spelling regardless of list
311
+ * order without reserving the old names of docs that move away.
269
312
  */
270
- async function planDocs(docs, config, reserved) {
313
+ async function planDocs(docs, config, reserved, previousManifest) {
271
314
  const planned = [];
272
315
  const warnings = [];
273
316
  const usedNamesByDir = new Map();
@@ -276,24 +319,18 @@ async function planDocs(docs, config, reserved) {
276
319
  usedNamesByDir.set(dir, new Set(names));
277
320
  }
278
321
  }
279
- for (const doc of docs) {
280
- const dirRel = doc.relative;
281
- let used = usedNamesByDir.get(dirRel);
282
- if (!used) {
283
- used = new Set();
284
- usedNamesByDir.set(dirRel, used);
285
- }
286
- // The pulled doc keeps its own tracked name: free it in its own
287
- // directory so allocation below can hand it back. Names other ids
288
- // own stay reserved.
289
- const ownRelPath = reserved?.pathById.get(doc.id);
290
- if (ownRelPath !== undefined) {
291
- const slash = ownRelPath.lastIndexOf('/');
292
- const ownDir = slash === -1 ? '' : ownRelPath.slice(0, slash);
293
- if (ownDir === dirRel) {
294
- used.delete(slash === -1 ? ownRelPath : ownRelPath.slice(slash + 1));
295
- }
322
+ const pathById = reserved?.pathById ?? new Map();
323
+ const titleById = reserved?.titleById ?? new Map();
324
+ if (!reserved && previousManifest) {
325
+ for (const [rel, entry] of Object.entries(previousManifest.docs)) {
326
+ if (pathById.has(entry.id))
327
+ continue;
328
+ pathById.set(entry.id, rel);
329
+ titleById.set(entry.id, entry.title);
296
330
  }
331
+ }
332
+ const prepared = [];
333
+ for (const doc of docs) {
297
334
  const media = isMediaCandidate(doc) ? await resolveMedia(config, doc) : { isMedia: false };
298
335
  if (media.warning)
299
336
  warnings.push(media.warning);
@@ -311,32 +348,11 @@ async function planDocs(docs, config, reserved) {
311
348
  ext = (media.mime && MIME_EXTENSIONS[media.mime]) ?? '';
312
349
  }
313
350
  }
314
- // Unchanged title keeps its existing collision-assigned path (R2-I2):
315
- // the tracked path must be in this same folder and be the base name
316
- // or a `-N` collision variant of it. A stale/renamed path (e.g. the
317
- // `renamed.md` merge fixture) falls through to reallocation below.
318
- const ownTitle = reserved?.titleById.get(doc.id);
319
- let fileName;
320
- let relPath;
321
- if (ownRelPath !== undefined && ownTitle !== undefined && ownTitle === doc.title) {
322
- const slash = ownRelPath.lastIndexOf('/');
323
- const ownDir = slash === -1 ? '' : ownRelPath.slice(0, slash);
324
- const ownFile = slash === -1 ? ownRelPath : ownRelPath.slice(slash + 1);
325
- if (ownDir === dirRel && isCollisionVariant(ownFile, base, ext)) {
326
- fileName = ownFile;
327
- relPath = ownRelPath;
328
- used.add(ownFile);
329
- }
330
- else {
331
- const allocated = allocateName(used, base, ext);
332
- fileName = allocated;
333
- relPath = dirRel ? `${dirRel}/${fileName}` : fileName;
334
- }
351
+ else if (doc.docType === 'visual') {
352
+ ext = '.html';
335
353
  }
336
- else {
337
- const allocated = allocateName(used, base, ext);
338
- fileName = allocated;
339
- relPath = dirRel ? `${dirRel}/${fileName}` : fileName;
354
+ else if (doc.docType === 'canvas') {
355
+ ext = '.canvas.json';
340
356
  }
341
357
  let bodySha256;
342
358
  let mediaBytes = null;
@@ -355,6 +371,41 @@ async function planDocs(docs, config, reserved) {
355
371
  else {
356
372
  bodySha256 = (0, docs_manifest_1.sha256Hex)(doc.body);
357
373
  }
374
+ prepared.push({ doc, media, base, ext, bodySha256, mediaBytes });
375
+ }
376
+ const stablePath = (doc, base, ext) => {
377
+ const ownRelPath = pathById.get(doc.id);
378
+ if (ownRelPath === undefined || titleById.get(doc.id) !== doc.title)
379
+ return undefined;
380
+ const slash = ownRelPath.lastIndexOf('/');
381
+ const ownDir = slash === -1 ? '' : ownRelPath.slice(0, slash);
382
+ const ownFile = slash === -1 ? ownRelPath : ownRelPath.slice(slash + 1);
383
+ return ownDir === doc.relative && isCollisionVariant(ownFile, base, ext) ? ownRelPath : undefined;
384
+ };
385
+ if (!reserved) {
386
+ for (const { doc, base, ext } of prepared) {
387
+ const rel = stablePath(doc, base, ext);
388
+ if (rel === undefined)
389
+ continue;
390
+ const used = usedNamesByDir.get(doc.relative) ?? new Set();
391
+ used.add(path_1.default.posix.basename(rel));
392
+ usedNamesByDir.set(doc.relative, used);
393
+ }
394
+ }
395
+ for (const { doc, media, base, ext, bodySha256, mediaBytes } of prepared) {
396
+ const dirRel = doc.relative;
397
+ const used = usedNamesByDir.get(dirRel) ?? new Set();
398
+ usedNamesByDir.set(dirRel, used);
399
+ const ownRelPath = pathById.get(doc.id);
400
+ if (reserved && ownRelPath !== undefined && path_1.default.posix.dirname(ownRelPath) === (dirRel || '.')) {
401
+ // Exact ownership matters: freeing Page.md must not free page.md
402
+ // when another tracked doc owns that differently cased name.
403
+ used.delete(path_1.default.posix.basename(ownRelPath));
404
+ }
405
+ const keptPath = stablePath(doc, base, ext);
406
+ const fileName = keptPath === undefined ? allocateName(used, base, ext) : path_1.default.posix.basename(keptPath);
407
+ const relPath = keptPath ?? (dirRel ? `${dirRel}/${fileName}` : fileName);
408
+ used.add(fileName);
358
409
  planned.push({ doc, relPath, dirRel, fileName, isMedia: media.isMedia, mediaBytes, bodySha256 });
359
410
  }
360
411
  return { planned, warnings };
@@ -362,9 +413,10 @@ async function planDocs(docs, config, reserved) {
362
413
  /**
363
414
  * Write every planned doc to disk under `destination` and build the
364
415
  * manifest docs map. The only step in the whole pull that creates
365
- * directories or writes/overwrites files — called only after any
366
- * unpushed-local-changes conflict has already refused, so it never clobbers
367
- * a file the caller decided to keep.
416
+ * directories or writes/overwrites files — called only after every refusal
417
+ * (unpushed local changes, rename conflicts, a target that is not a regular
418
+ * file) has had its chance, so it never stops half-way or clobbers a file
419
+ * the caller decided to keep.
368
420
  */
369
421
  function commitDocs(destination, planned) {
370
422
  const manifestDocs = {};
@@ -372,14 +424,7 @@ function commitDocs(destination, planned) {
372
424
  for (const p of planned) {
373
425
  const dirAbs = p.dirRel ? path_1.default.join(destination, ...p.dirRel.split('/')) : destination;
374
426
  fs_1.default.mkdirSync(dirAbs, { recursive: true });
375
- // A destination path that already exists as a directory would make writeFileSync
376
- // throw EISDIR mid-walk, after earlier docs were written and before the manifest
377
- // is saved — a half-pulled tree with a stale sidecar. Fail cleanly instead.
378
427
  const targetAbs = path_1.default.join(dirAbs, p.fileName);
379
- if (fs_1.default.existsSync(targetAbs) && !fs_1.default.statSync(targetAbs).isFile()) {
380
- process.stderr.write(chalk_1.default.red(`error: "${p.relPath}" exists and is not a regular file — cannot write doc ${p.doc.id} (${p.doc.title}).\n`));
381
- process.exit(1);
382
- }
383
428
  if (p.isMedia) {
384
429
  if (p.mediaBytes) {
385
430
  fs_1.default.writeFileSync(targetAbs, p.mediaBytes);
@@ -441,7 +486,7 @@ async function docPullWithConfig(folderPath, dest, options, config) {
441
486
  const entries = fs_1.default.readdirSync(destination);
442
487
  if (entries.length > 0 && !options.yes && !options.overwrite) {
443
488
  console.log(chalk_1.default.yellow(`Destination "${destination}" is not empty (${entries.length} items).`));
444
- console.log(chalk_1.default.yellow('Pulling will overwrite existing files.'));
489
+ console.log(chalk_1.default.yellow("Pulling overwrites tracked files; local files the folder doesn't track are refused unless --overwrite."));
445
490
  const response = await (0, prompts_1.default)({
446
491
  type: 'confirm',
447
492
  name: 'proceed',
@@ -483,6 +528,8 @@ async function docPullWithConfig(folderPath, dest, options, config) {
483
528
  id: data.id,
484
529
  title: data.title,
485
530
  relative: '',
531
+ docType: data.doc_type?.slug ?? null,
532
+ docTypeKnown: true,
486
533
  body: data.body ?? '',
487
534
  current_revision_id: data.current_revision_id ?? null,
488
535
  properties: data.properties ?? {},
@@ -500,7 +547,204 @@ async function docPullWithConfig(folderPath, dest, options, config) {
500
547
  return;
501
548
  }
502
549
  const { fetched, warnings: fetchWarnings } = await fetchBodies(config, rows);
503
- await report(destination, folderPath, fetched, options, config, fetchWarnings, previousManifest, usedSingleDocFallback);
550
+ const typeWarnings = await backfillDocTypes(config, fetched);
551
+ await report(destination, folderPath, fetched, options, config, [...fetchWarnings, ...typeWarnings], previousManifest, usedSingleDocFallback);
552
+ }
553
+ /**
554
+ * Safety guards shared by deletion propagation and rename cleanup (cli#157):
555
+ * lexical containment in the destination, a regular file, physical
556
+ * containment of the real parent directory (no symlink escape), and never a
557
+ * file this pull just wrote. The only code in the CLI that deletes files —
558
+ * never act on a path it did not create.
559
+ */
560
+ function isSafeToRemoveTrackedFile(absPath, destPrefix, realDest, writtenIdentities) {
561
+ // Containment: a hand-edited or corrupt manifest key such as "../../etc/passwd"
562
+ // would otherwise resolve outside the pull destination.
563
+ if (!absPath.startsWith(destPrefix))
564
+ return false;
565
+ // Only ever remove regular files. A directory here means a corrupt manifest;
566
+ // rmSync would throw mid-pull, leaving a half-deleted tree.
567
+ let stat;
568
+ try {
569
+ stat = fs_1.default.statSync(absPath);
570
+ }
571
+ catch {
572
+ return false;
573
+ }
574
+ if (!stat.isFile())
575
+ return false;
576
+ // Physical containment: the lexical check above only guards the string. If
577
+ // an intermediate path segment is a symlink to a directory OUTSIDE the
578
+ // destination, statSync/rmSync all follow it transparently.
579
+ let realParent;
580
+ try {
581
+ realParent = fs_1.default.realpathSync(path_1.default.dirname(absPath));
582
+ }
583
+ catch {
584
+ return false;
585
+ }
586
+ if (realParent !== realDest && !realParent.startsWith(realDest + path_1.default.sep))
587
+ return false;
588
+ // Identity: never delete a file this pull just wrote.
589
+ if (writtenIdentities.has(`${stat.dev}:${stat.ino}`))
590
+ return false;
591
+ return true;
592
+ }
593
+ /** Resolve directory aliases even when a planned file or its parent does not exist yet. */
594
+ function physicalTargetPath(absPath) {
595
+ const missing = [];
596
+ let existing = absPath;
597
+ while (true) {
598
+ try {
599
+ return path_1.default.join(fs_1.default.realpathSync(existing), ...missing.reverse());
600
+ }
601
+ catch (error) {
602
+ if (error.code !== 'ENOENT')
603
+ throw error;
604
+ try {
605
+ if (fs_1.default.lstatSync(existing).isSymbolicLink()) {
606
+ existing = path_1.default.resolve(path_1.default.dirname(existing), fs_1.default.readlinkSync(existing));
607
+ continue;
608
+ }
609
+ }
610
+ catch (linkError) {
611
+ if (linkError.code !== 'ENOENT')
612
+ throw linkError;
613
+ }
614
+ const parent = path_1.default.dirname(existing);
615
+ if (parent === existing)
616
+ throw error;
617
+ missing.push(path_1.default.basename(existing));
618
+ existing = parent;
619
+ }
620
+ }
621
+ }
622
+ /** The first symbolic link on `rel`'s way (inside `destination`, the destination itself excluded), as a relative path; null when there is none. */
623
+ function linkOnTheWay(destination, rel) {
624
+ const parts = rel.split('/');
625
+ for (let i = 1; i <= parts.length; i++) {
626
+ const abs = path_1.default.join(destination, ...parts.slice(0, i));
627
+ try {
628
+ if (fs_1.default.lstatSync(abs).isSymbolicLink())
629
+ return parts.slice(0, i).join('/');
630
+ }
631
+ catch {
632
+ return null; // the rest does not exist yet
633
+ }
634
+ }
635
+ return null;
636
+ }
637
+ function refuseLink(rel, link, doc) {
638
+ const where = link === rel ? '' : ` (or sits under one: ${link})`;
639
+ process.stderr.write(chalk_1.default.red(`error: ${rel} is a symbolic link${where}; this pull would write doc ${doc.id} ("${doc.title}") through it.\n`));
640
+ process.stderr.write(chalk_1.default.red('Replace it with a regular file or folder and pull again.\n'));
641
+ process.exit(1);
642
+ }
643
+ /** One line for a path that cannot be resolved (spec §2.3): the ELOOP wording, or the error code and message. Never returns. */
644
+ function explainUnresolvable(label, error) {
645
+ const code = error.code ?? 'ERROR';
646
+ const reason = code === 'ELOOP' ? 'too many symbolic links (ELOOP). Fix or remove the link and pull again.' : `${code} ${error.message}.`;
647
+ process.stderr.write(chalk_1.default.red(`error: cannot resolve ${label}: ${reason}\n`));
648
+ process.exit(1);
649
+ }
650
+ /** physicalTargetPath, but a resolution error becomes one line (spec §2.3). A planned target with a link on the way gets the symbolic-link refusal. */
651
+ function resolveOrExplain(destination, rel, target) {
652
+ const abs = path_1.default.resolve(destination, ...rel.split('/'));
653
+ try {
654
+ return physicalTargetPath(abs);
655
+ }
656
+ catch (error) {
657
+ if (target) {
658
+ const link = linkOnTheWay(destination, rel);
659
+ if (link !== null)
660
+ refuseLink(rel, link, target);
661
+ }
662
+ explainUnresolvable(rel, error);
663
+ }
664
+ }
665
+ /**
666
+ * Refuse, before any write, a pull that would write a planned doc through a
667
+ * symbolic link or to a place that resolves outside the destination. Covers
668
+ * every planned doc, including a failed media download: commitDocs still
669
+ * creates its directory. Also refuse, unless `--overwrite`, a planned write
670
+ * over a regular file the pull does not own (cli#167): one the previous
671
+ * manifest does not track with a hash and whose bytes differ from the
672
+ * pulled ones. A file already holding the pulled bytes is adopted, and a
673
+ * rename's own source file (a case-only retitle on a case-insensitive
674
+ * filesystem) is never "untracked".
675
+ */
676
+ function checkPlannedWrites(destination, planned, previousManifest, options, renameMoves) {
677
+ // First, so a destination that is itself a looping link gets one line, not a raw ELOOP from the sidecar check below.
678
+ let realDest;
679
+ try {
680
+ realDest = physicalTargetPath(path_1.default.resolve(destination));
681
+ }
682
+ catch (error) {
683
+ explainUnresolvable(destination, error);
684
+ }
685
+ // The manifest sidecar is written at the end of every pull: never through a link.
686
+ try {
687
+ if (fs_1.default.lstatSync(path_1.default.join(destination, docs_manifest_1.DOCS_MANIFEST)).isSymbolicLink()) {
688
+ process.stderr.write(chalk_1.default.red(`error: ${docs_manifest_1.DOCS_MANIFEST} is a symbolic link; this pull would write the docs manifest through it.\n`));
689
+ process.stderr.write(chalk_1.default.red('Replace it with a regular file or folder and pull again.\n'));
690
+ process.exit(1);
691
+ }
692
+ }
693
+ catch (error) {
694
+ if (error.code !== 'ENOENT')
695
+ explainUnresolvable(docs_manifest_1.DOCS_MANIFEST, error);
696
+ }
697
+ for (const p of planned) {
698
+ const link = linkOnTheWay(destination, p.relPath);
699
+ if (link !== null)
700
+ refuseLink(p.relPath, link, p.doc);
701
+ const physical = resolveOrExplain(destination, p.relPath, p.doc);
702
+ // Belt and braces behind linkOnTheWay, which refuses every link first; kept in case the physical path ever diverges without one.
703
+ if (physical !== realDest && !physical.startsWith(realDest + path_1.default.sep)) {
704
+ process.stderr.write(chalk_1.default.red(`error: ${p.relPath} resolves outside the destination (${physical}); this pull would write doc ${p.doc.id} ("${p.doc.title}") there.\n`));
705
+ process.exit(1);
706
+ }
707
+ }
708
+ if (options.overwrite)
709
+ return;
710
+ const untracked = [];
711
+ for (const p of planned) {
712
+ if (p.isMedia && p.mediaBytes === null)
713
+ continue; // a failed download writes nothing
714
+ const tracked = previousManifest?.docs[p.relPath];
715
+ if (tracked !== undefined && tracked.body_sha256 != null)
716
+ continue; // checked as unpushed local changes
717
+ const targetAbs = path_1.default.join(destination, ...p.relPath.split('/'));
718
+ let targetStat;
719
+ let current;
720
+ try {
721
+ targetStat = fs_1.default.statSync(targetAbs);
722
+ if (!targetStat.isFile())
723
+ continue;
724
+ current = fs_1.default.readFileSync(targetAbs);
725
+ }
726
+ catch (error) {
727
+ const code = error.code ?? 'ERROR';
728
+ if (code === 'ENOENT')
729
+ continue; // absent: nothing to overwrite here
730
+ untracked.push(`${p.relPath} (cannot be read: ${code})`); // present but unverifiable: not owned
731
+ continue;
732
+ }
733
+ if ((0, docs_manifest_1.sha256Hex)(current) === p.bodySha256)
734
+ continue;
735
+ const ownSource = renameMoves.find((m) => m.id === p.doc.id)?.sourceIdentity;
736
+ if (ownSource === `${targetStat.dev}:${targetStat.ino}`)
737
+ continue;
738
+ untracked.push(p.relPath);
739
+ }
740
+ if (untracked.length > 0) {
741
+ const one = untracked.length === 1;
742
+ process.stderr.write(chalk_1.default.red(`${untracked.length} ${one ? 'file exists' : 'files exist'} locally but ${one ? 'is' : 'are'} not tracked:\n`));
743
+ for (const rel of untracked)
744
+ process.stderr.write(chalk_1.default.red(` ${rel}\n`));
745
+ process.stderr.write(chalk_1.default.red('Move them aside and pull again, or pass --overwrite to replace them.\n'));
746
+ process.exit(1);
747
+ }
504
748
  }
505
749
  /** Write the docs + manifest to disk and print the result (chalk lines or --json). */
506
750
  async function report(destination, folderPath, fetched, options, config, extraWarnings, previousManifest, usedSingleDocFallback) {
@@ -528,7 +772,227 @@ async function report(destination, folderPath, fetched, options, config, extraWa
528
772
  }
529
773
  reserved = { usedNames, pathById, titleById };
530
774
  }
531
- const { planned, warnings } = await planDocs(fetched, config, reserved);
775
+ const { planned, warnings } = await planDocs(fetched, config, reserved, previousManifest);
776
+ // Same doc id under a different path is a rename (PM ruling 2, cli#157):
777
+ // the previous manifest tracks a planned doc under a DIFFERENT path `old`,
778
+ // whatever is on disk at `old` (cli#157 FR4-C1). What is at `old` decides
779
+ // only what happens to `old`; the target checks below apply to every
780
+ // rename. Every rename ends one of three ways:
781
+ // (a) `old` is removed only after its replacement is written (nothing to
782
+ // remove when `old` holds no regular file);
783
+ // (b) `old`'s tracking (and file, if any) is kept, the replacement is
784
+ // not written;
785
+ // (c) the pull refuses here, before any write.
786
+ // Never: bytes lost, an untracked file overwritten without --overwrite,
787
+ // or one doc's tracking attached to another doc's bytes.
788
+ const renameMoves = [];
789
+ if (previousManifest) {
790
+ const pathById = new Map();
791
+ for (const [relPath, entry] of Object.entries(previousManifest.docs)) {
792
+ if (!pathById.has(entry.id))
793
+ pathById.set(entry.id, relPath);
794
+ }
795
+ for (const p of planned) {
796
+ const old = pathById.get(p.doc.id);
797
+ if (old === undefined || old === p.relPath)
798
+ continue;
799
+ const absOld = path_1.default.join(destination, ...old.split('/'));
800
+ let sourceStat = null;
801
+ try {
802
+ const stat = fs_1.default.statSync(absOld);
803
+ if (stat.isFile())
804
+ sourceStat = stat;
805
+ }
806
+ catch {
807
+ sourceStat = null;
808
+ }
809
+ let modified = false;
810
+ let targetIsSource = false;
811
+ if (sourceStat !== null) {
812
+ const entry = previousManifest.docs[old];
813
+ let currentHash = null;
814
+ try {
815
+ currentHash = (0, docs_manifest_1.sha256Hex)(fs_1.default.readFileSync(absOld));
816
+ }
817
+ catch {
818
+ currentHash = null;
819
+ }
820
+ modified = currentHash === null || currentHash !== entry.body_sha256;
821
+ try {
822
+ const targetStat = fs_1.default.statSync(path_1.default.join(destination, ...p.relPath.split('/')));
823
+ targetIsSource = targetStat.dev === sourceStat.dev && targetStat.ino === sourceStat.ino;
824
+ }
825
+ catch {
826
+ targetIsSource = false;
827
+ }
828
+ }
829
+ renameMoves.push({
830
+ oldRel: old,
831
+ newRel: p.relPath,
832
+ id: p.doc.id,
833
+ title: p.doc.title,
834
+ sourcePresent: sourceStat !== null,
835
+ sourceIdentity: sourceStat === null ? null : `${sourceStat.dev}:${sourceStat.ino}`,
836
+ modified,
837
+ // A failed media download plans a new path but writes nothing there.
838
+ replacementWritten: !p.isMedia || p.mediaBytes !== null,
839
+ targetIsSource,
840
+ });
841
+ }
842
+ const plannedPaths = new Map(planned.map((p) => [p.relPath, p]));
843
+ const plannedByIdentity = new Map();
844
+ const plannedByPhysicalPath = new Map();
845
+ for (const p of planned) {
846
+ const physicalPath = resolveOrExplain(destination, p.relPath, p.doc);
847
+ const physicalTargets = plannedByPhysicalPath.get(physicalPath) ?? [];
848
+ physicalTargets.push(p);
849
+ plannedByPhysicalPath.set(physicalPath, physicalTargets);
850
+ try {
851
+ const stat = fs_1.default.statSync(path_1.default.join(destination, ...p.relPath.split('/')));
852
+ if (!stat.isFile())
853
+ continue;
854
+ const identity = `${stat.dev}:${stat.ino}`;
855
+ const targets = plannedByIdentity.get(identity) ?? [];
856
+ targets.push(p);
857
+ plannedByIdentity.set(identity, targets);
858
+ }
859
+ catch {
860
+ // An absent target cannot alias a present source.
861
+ continue;
862
+ }
863
+ }
864
+ // Path claims matter even without a source (a failed download restores
865
+ // its old tracking). Present sources also need protection from writes
866
+ // through another name, hard link, symlink or directory component alias.
867
+ const plannedTargetForSource = (m) => plannedPaths.get(m.oldRel)
868
+ ?? plannedByPhysicalPath.get(resolveOrExplain(destination, m.oldRel))?.find((p) => p.doc.id !== m.id)
869
+ ?? (m.sourceIdentity === null
870
+ ? undefined
871
+ : plannedByIdentity.get(m.sourceIdentity)?.find((p) => p.doc.id !== m.id));
872
+ // Edited sources cannot be discarded by --overwrite during a rename.
873
+ // Report a cross-doc collision first, including an alias's actual path.
874
+ for (const m of renameMoves) {
875
+ if (!m.modified)
876
+ continue;
877
+ const taker = plannedTargetForSource(m);
878
+ if (!taker)
879
+ continue;
880
+ const oldTitle = previousManifest.docs[m.oldRel]?.title ?? `#${m.id}`;
881
+ process.stderr.write(chalk_1.default.red(`error: ${m.oldRel} holds unpublished edits for doc ${m.id} ("${oldTitle}", now written as ${m.newRel}), but this pull would write doc ${taker.doc.id} ("${taker.doc.title}") to ${taker.relPath}, the same file.\n`));
882
+ process.stderr.write(chalk_1.default.red(`Move or rename the edited ${m.oldRel} (or push it first) and pull again.\n`));
883
+ process.exit(1);
884
+ }
885
+ // A rename whose replacement will not be written keeps its old file and
886
+ // tracking (b) — which is only possible while no other doc in this pull
887
+ // claims the old path. Otherwise refuse (cli#157 issuecomment-5962867475):
888
+ // no relocation or re-allocation of names.
889
+ for (const m of renameMoves) {
890
+ if (m.replacementWritten)
891
+ continue;
892
+ const taker = plannedTargetForSource(m);
893
+ if (!taker)
894
+ continue;
895
+ process.stderr.write(chalk_1.default.red(`error: doc ${m.id} ("${m.title}") failed to download to ${m.newRel}, so ${m.oldRel} must keep its previous file and tracking, but this pull would put doc ${taker.doc.id} ("${taker.doc.title}") at ${taker.relPath}, the same file.\n`));
896
+ process.stderr.write(chalk_1.default.red('Nothing was written. Pull again once the download succeeds.\n'));
897
+ process.exit(1);
898
+ }
899
+ // An edited source whose replacement will not be written cannot move
900
+ // and must not lose its tracking: refuse, with or without --overwrite.
901
+ for (const m of renameMoves) {
902
+ if (m.replacementWritten || !m.modified)
903
+ continue;
904
+ process.stderr.write(chalk_1.default.red(`error: ${m.oldRel} holds unpublished edits for doc ${m.id}, which is now ${m.newRel}, but its download failed.\n`));
905
+ process.stderr.write(chalk_1.default.red(`Nothing was written. Push ${m.oldRel} first (or move it aside), then pull again once the download succeeds.\n`));
906
+ process.exit(1);
907
+ }
908
+ const blocked = renameMoves.filter((m) => m.modified);
909
+ if (blocked.length > 0) {
910
+ for (const m of blocked) {
911
+ process.stderr.write(chalk_1.default.red(`error: ${m.oldRel} holds unpublished edits for doc ${m.id} ("${m.title}"), which is now written as ${m.newRel}.\n`));
912
+ process.stderr.write(chalk_1.default.red(`Nothing was written. Push ${m.oldRel} first (or move it aside) and pull again.\n`));
913
+ }
914
+ process.exit(1);
915
+ }
916
+ // Two writes through different names of one file cannot both retain
917
+ // their own doc's bytes and hash. Refuse compositions involving a
918
+ // rename or legacy case-folded names that already alias one file.
919
+ // Other non-rename-only target handling is outside this rule.
920
+ // Physical paths cover absent targets under aliased directories, where
921
+ // there is no inode yet to compare.
922
+ const renamedIds = new Set(renameMoves.map((m) => m.id));
923
+ for (const targets of [...plannedByIdentity.values(), ...plannedByPhysicalPath.values()]) {
924
+ const writes = targets.filter((p) => !p.isMedia || p.mediaBytes !== null);
925
+ if (writes.length < 2)
926
+ continue;
927
+ const foldedPaths = new Set(writes.map((p) => p.relPath.toLowerCase()));
928
+ if (!writes.some((p) => renamedIds.has(p.doc.id)) && foldedPaths.size === writes.length)
929
+ continue;
930
+ const [first, second] = writes;
931
+ process.stderr.write(chalk_1.default.red(`error: ${first.relPath} (doc ${first.doc.id}) and ${second.relPath} (doc ${second.doc.id}) are the same file on this filesystem; this pull would write different docs through those names.\n`));
932
+ process.stderr.write(chalk_1.default.red('Nothing was written. Move the aliased files apart and pull again.\n'));
933
+ process.exit(1);
934
+ }
935
+ // A rename target that is a symbolic link would be written through, to
936
+ // a file this pull does not own (possibly outside the destination):
937
+ // refuse, with or without --overwrite.
938
+ for (const m of renameMoves) {
939
+ if (!m.replacementWritten)
940
+ continue;
941
+ let isLink = false;
942
+ try {
943
+ isLink = fs_1.default.lstatSync(path_1.default.join(destination, ...m.newRel.split('/'))).isSymbolicLink();
944
+ }
945
+ catch {
946
+ isLink = false;
947
+ }
948
+ if (!isLink)
949
+ continue;
950
+ process.stderr.write(chalk_1.default.red(`error: ${m.newRel} is a symbolic link; this pull would write doc ${m.id} ("${m.title}", renamed from ${m.oldRel}) through it.\n`));
951
+ process.stderr.write(chalk_1.default.red(`Replace ${m.newRel} with a regular file (or remove it) and pull again.\n`));
952
+ process.exit(1);
953
+ }
954
+ // A rename target holding an untracked local file (not in the previous
955
+ // manifest) would be overwritten: refuse unless --overwrite, whatever
956
+ // the source's state. A file already holding exactly the bytes this
957
+ // pull writes loses nothing and is adopted. A target tracked by another
958
+ // doc with a recorded hash is protected by the unpushed-local-changes
959
+ // check below; one tracked without a hash cannot be verified there, so
960
+ // it is checked here like an untracked file. A target that is the
961
+ // source itself is governed by the source rules above.
962
+ if (!options.overwrite) {
963
+ for (const m of renameMoves) {
964
+ if (!m.replacementWritten || m.targetIsSource)
965
+ continue;
966
+ const targetEntry = previousManifest.docs[m.newRel];
967
+ if (targetEntry !== undefined && targetEntry.body_sha256 != null)
968
+ continue;
969
+ const absNew = path_1.default.join(destination, ...m.newRel.split('/'));
970
+ let existing = null;
971
+ let unreadable = '';
972
+ try {
973
+ if (!fs_1.default.statSync(absNew).isFile())
974
+ continue;
975
+ existing = fs_1.default.readFileSync(absNew);
976
+ }
977
+ catch (error) {
978
+ const code = error.code ?? 'ERROR';
979
+ if (code === 'ENOENT')
980
+ continue;
981
+ unreadable = ` (cannot be read: ${code})`; // present but unverifiable: not owned
982
+ }
983
+ if (existing !== null && (0, docs_manifest_1.sha256Hex)(existing) === plannedPaths.get(m.newRel)?.bodySha256)
984
+ continue;
985
+ const state = (targetEntry === undefined ? 'exists locally but is not tracked' : `holds local bytes with no recorded hash (tracked for doc ${targetEntry.id})`) + unreadable;
986
+ process.stderr.write(chalk_1.default.red(`error: ${m.newRel} ${state}; this pull would overwrite it with doc ${m.id} ("${m.title}", renamed from ${m.oldRel}).\n`));
987
+ process.stderr.write(chalk_1.default.red(`Move ${m.newRel} aside and pull again, or pass --overwrite to replace it.\n`));
988
+ process.exit(1);
989
+ }
990
+ }
991
+ }
992
+ const handledOldPaths = new Set(renameMoves.map((m) => m.oldRel));
993
+ // After the rename block (which keeps its own messages for rename cases) and before anything
994
+ // else that reads or writes the plan: no planned write may go through a link or outside.
995
+ checkPlannedWrites(destination, planned, previousManifest, options, renameMoves);
532
996
  // Unpushed-local-changes protection: now that the server walk is known, refuse only
533
997
  // for a file whose doc still exists remotely — i.e. its relative path is part of this
534
998
  // pull's plan and would be overwritten by commitDocs below. A modified file whose
@@ -550,17 +1014,114 @@ async function report(destination, folderPath, fetched, options, config, extraWa
550
1014
  process.exit(1);
551
1015
  }
552
1016
  }
1017
+ // A destination path that already exists as a directory would make writeFileSync
1018
+ // throw EISDIR mid-walk, after earlier docs were written and before the manifest
1019
+ // is saved — a half-pulled tree with a stale sidecar. Refuse before any write.
1020
+ for (const p of planned) {
1021
+ const targetAbs = path_1.default.join(destination, ...p.relPath.split('/'));
1022
+ if (fs_1.default.existsSync(targetAbs) && !fs_1.default.statSync(targetAbs).isFile()) {
1023
+ process.stderr.write(chalk_1.default.red(`error: "${p.relPath}" exists and is not a regular file — cannot write doc ${p.doc.id} (${p.doc.title}).\n`));
1024
+ process.exit(1);
1025
+ }
1026
+ }
553
1027
  fs_1.default.mkdirSync(destination, { recursive: true });
1028
+ const destPrefix = path_1.default.resolve(destination) + path_1.default.sep;
1029
+ const realDest = fs_1.default.realpathSync(destination);
554
1030
  const { manifestDocs, files } = commitDocs(destination, planned);
1031
+ // cli#167: a media doc whose download failed must not be tracked over a
1032
+ // local file the previous manifest does not track, with a hash, for that
1033
+ // same doc: nothing was written, so the manifest would claim bytes the
1034
+ // pull never gave it. A rename keeps its restore-old-entry handling below.
1035
+ const renamedIds = new Set(renameMoves.map((m) => m.id));
1036
+ for (const p of planned) {
1037
+ if (!p.isMedia || p.mediaBytes !== null || renamedIds.has(p.doc.id))
1038
+ continue;
1039
+ const tracked = previousManifest?.docs[p.relPath];
1040
+ if (tracked !== undefined && tracked.id === p.doc.id && tracked.body_sha256 != null)
1041
+ continue;
1042
+ let holdsLocalFile = false;
1043
+ try {
1044
+ holdsLocalFile = fs_1.default.statSync(path_1.default.join(destination, ...p.relPath.split('/'))).isFile();
1045
+ }
1046
+ catch {
1047
+ holdsLocalFile = false;
1048
+ }
1049
+ if (!holdsLocalFile)
1050
+ continue;
1051
+ delete manifestDocs[p.relPath];
1052
+ process.stderr.write(chalk_1.default.yellow(`! doc ${p.doc.id} ("${p.doc.title}") failed to download and ${p.relPath} holds a local file; not tracking it — pull again later\n`));
1053
+ }
555
1054
  // Identity of every file this pull actually wrote, keyed by dev:ino. Used by the
556
1055
  // deletion-propagation loop below to recognize an "orphan" that is really just the
557
1056
  // same file this pull wrote under a different manifest key (e.g. a case-only title
558
1057
  // rename on a case-insensitive filesystem) — that file must never be deleted.
1058
+ // writtenPathByIdentity names the path this pull wrote for each identity, so
1059
+ // rename cleanup can warn instead of silently keeping an old path that
1060
+ // aliases a written file (cli#167 M2).
559
1061
  const writtenIdentities = new Set();
1062
+ const writtenPathByIdentity = new Map();
560
1063
  for (const file of files) {
561
1064
  try {
562
1065
  const stat = fs_1.default.statSync(path_1.default.join(destination, ...file.path.split('/')));
563
- writtenIdentities.add(`${stat.dev}:${stat.ino}`);
1066
+ const identity = `${stat.dev}:${stat.ino}`;
1067
+ writtenIdentities.add(identity);
1068
+ writtenPathByIdentity.set(identity, file.path);
1069
+ }
1070
+ catch {
1071
+ continue;
1072
+ }
1073
+ }
1074
+ // Rename cleanup (PM ruling 2, cli#157): each unmodified old twin is gone
1075
+ // now that the new file is written — but never one this pull just wrote
1076
+ // for another doc (e.g. a new markdown doc that now takes `page.md`).
1077
+ // Edited sources were all refused before any write, even with --overwrite.
1078
+ // A twin whose replacement was NOT written (a failed media download plans
1079
+ // a new path but writes nothing) keeps its old file and its old manifest
1080
+ // entry — deleting it would destroy the only good copy. Without an old
1081
+ // file the old entry is still kept, so the doc is not tracked (with no
1082
+ // hash) at a new path that may hold an untracked file. The refusals above
1083
+ // guarantee no other doc in this pull claims that old path.
1084
+ for (const m of renameMoves) {
1085
+ if (!m.replacementWritten) {
1086
+ delete manifestDocs[m.newRel];
1087
+ const oldEntry = previousManifest?.docs[m.oldRel];
1088
+ if (oldEntry !== undefined) {
1089
+ manifestDocs[m.oldRel] = oldEntry;
1090
+ }
1091
+ if (m.sourcePresent) {
1092
+ process.stderr.write(chalk_1.default.yellow(`! kept ${m.oldRel} — doc ${m.id} download failed; still tracked as ${m.oldRel}\n`));
1093
+ }
1094
+ else {
1095
+ process.stderr.write(chalk_1.default.yellow(`! doc ${m.id} download failed; still tracked as ${m.oldRel}, which is not present locally\n`));
1096
+ }
1097
+ continue;
1098
+ }
1099
+ if (!m.sourcePresent)
1100
+ continue;
1101
+ const absOld = path_1.default.resolve(destination, ...m.oldRel.split('/'));
1102
+ // cli#167 M2: the old path is the same file as one this pull just
1103
+ // wrote under another name (a link) — it is kept, loudly, so the
1104
+ // extra name is not mistaken for an orphan. The same-file case
1105
+ // (targetIsSource) stays silent: there is no extra name.
1106
+ if (!m.targetIsSource) {
1107
+ let oldIdentity = null;
1108
+ try {
1109
+ const oldStat = fs_1.default.statSync(absOld);
1110
+ if (oldStat.isFile())
1111
+ oldIdentity = `${oldStat.dev}:${oldStat.ino}`;
1112
+ }
1113
+ catch {
1114
+ oldIdentity = null;
1115
+ }
1116
+ const writtenPath = oldIdentity === null ? undefined : writtenPathByIdentity.get(oldIdentity);
1117
+ if (writtenPath !== undefined) {
1118
+ process.stderr.write(chalk_1.default.yellow(`! kept ${m.oldRel}: it is the same file as ${writtenPath} (a link); the extra name is not tracked — remove it yourself if you don't need it\n`));
1119
+ }
1120
+ }
1121
+ if (!isSafeToRemoveTrackedFile(absOld, destPrefix, realDest, writtenIdentities))
1122
+ continue;
1123
+ try {
1124
+ fs_1.default.rmSync(absOld);
564
1125
  }
565
1126
  catch {
566
1127
  continue;
@@ -598,43 +1159,20 @@ async function report(destination, folderPath, fetched, options, config, extraWa
598
1159
  }
599
1160
  const keptModifiedMedia = new Set();
600
1161
  if (canPropagateDeletions) {
601
- const destPrefix = path_1.default.resolve(destination) + path_1.default.sep;
602
- const realDest = fs_1.default.realpathSync(destination);
603
1162
  for (const [relPath, entry] of Object.entries(previousManifest.docs)) {
604
1163
  if (manifestDocs[relPath])
605
1164
  continue;
606
- const absPath = path_1.default.resolve(destination, ...relPath.split('/'));
607
- // Containment: a hand-edited or corrupt manifest key such as "../../etc/passwd"
608
- // would otherwise resolve outside the pull destination. `doc pull` never writes
609
- // such a key (titles are sanitized), but this loop is the only code in the CLI
610
- // that deletes files — never let it act on a path it did not create.
611
- if (!absPath.startsWith(destPrefix))
1165
+ // Rename cleanup (cli#157) already handled this path: never a
1166
+ // "deleted remotely" orphan warning for it.
1167
+ if (handledOldPaths.has(relPath))
612
1168
  continue;
613
- // Only ever remove regular files. A directory here means a corrupt manifest;
614
- // rmSync would throw mid-pull, leaving a half-deleted tree.
615
- //
1169
+ const absPath = path_1.default.resolve(destination, ...relPath.split('/'));
616
1170
  // The whole entry is guarded: the new manifest is already on disk by now, so an
617
1171
  // uncaught throw would abort the report, silently skip every later orphan, and
618
1172
  // print a raw stack trace instead of this command's normal error format. One bad
619
1173
  // entry must cost only that entry.
620
1174
  try {
621
- const stat = fs_1.default.statSync(absPath);
622
- if (!stat.isFile())
623
- continue;
624
- // Physical containment: the lexical check above only guards the string. If
625
- // an intermediate path segment (e.g. "sub" in "sub/x.png") is a symlink to a
626
- // directory OUTSIDE the destination, statSync/readFileSync/rmSync all follow
627
- // it transparently. Resolve the real parent directory and require it to
628
- // physically live under the real destination before ever touching the file.
629
- const realParent = fs_1.default.realpathSync(path_1.default.dirname(absPath));
630
- if (realParent !== realDest && !realParent.startsWith(realDest + path_1.default.sep))
631
- continue;
632
- // Identity: never delete a file this pull just wrote. A case-only title
633
- // rename on a case-insensitive filesystem (or a hand-edited manifest with a
634
- // hardlinked entry) can make an "orphan" key resolve to the very file the
635
- // new manifest just wrote under a different key — bytes trivially match.
636
- const identity = `${stat.dev}:${stat.ino}`;
637
- if (writtenIdentities.has(identity))
1175
+ if (!isSafeToRemoveTrackedFile(absPath, destPrefix, realDest, writtenIdentities))
638
1176
  continue;
639
1177
  const currentHash = (0, docs_manifest_1.sha256Hex)(fs_1.default.readFileSync(absPath));
640
1178
  if (entry.body_sha256 != null && entry.body_sha256 === currentHash) {