@ipab/cli 0.1.0-rc.11 → 0.1.0-rc.12

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 (95) hide show
  1. package/README.md +15 -10
  2. package/dist/authoring.d.ts +6 -2
  3. package/dist/authoring.d.ts.map +1 -1
  4. package/dist/authoring.js +65 -18
  5. package/dist/authoring.js.map +1 -1
  6. package/dist/builtins/skill.ipab.car +0 -0
  7. package/dist/cli.d.ts +2 -1
  8. package/dist/cli.d.ts.map +1 -1
  9. package/dist/cli.js +854 -179
  10. package/dist/cli.js.map +1 -1
  11. package/dist/config-secret.js +1 -1
  12. package/dist/config-secret.js.map +1 -1
  13. package/dist/daemon-ui.d.ts +1 -0
  14. package/dist/daemon-ui.d.ts.map +1 -1
  15. package/dist/daemon-ui.js +2 -0
  16. package/dist/daemon-ui.js.map +1 -1
  17. package/dist/history.d.ts +3 -1
  18. package/dist/history.d.ts.map +1 -1
  19. package/dist/history.js +16 -1
  20. package/dist/history.js.map +1 -1
  21. package/dist/kubo-topology.d.ts +2 -0
  22. package/dist/kubo-topology.d.ts.map +1 -1
  23. package/dist/kubo-topology.js +17 -20
  24. package/dist/kubo-topology.js.map +1 -1
  25. package/dist/kubo.d.ts +5 -3
  26. package/dist/kubo.d.ts.map +1 -1
  27. package/dist/kubo.js +83 -13
  28. package/dist/kubo.js.map +1 -1
  29. package/dist/name-reference.d.ts +8 -0
  30. package/dist/name-reference.d.ts.map +1 -0
  31. package/dist/name-reference.js +63 -0
  32. package/dist/name-reference.js.map +1 -0
  33. package/dist/pivot-rpc.d.ts +37 -2
  34. package/dist/pivot-rpc.d.ts.map +1 -1
  35. package/dist/pivot-rpc.js +188 -8
  36. package/dist/pivot-rpc.js.map +1 -1
  37. package/dist/search-index.d.ts.map +1 -1
  38. package/dist/search-index.js +1 -0
  39. package/dist/search-index.js.map +1 -1
  40. package/dist/skill-definition.d.ts +9 -0
  41. package/dist/skill-definition.d.ts.map +1 -1
  42. package/dist/skill-definition.js +26 -2
  43. package/dist/skill-definition.js.map +1 -1
  44. package/dist/skill-source.d.ts +1 -0
  45. package/dist/skill-source.d.ts.map +1 -1
  46. package/dist/skill-source.js +10 -4
  47. package/dist/skill-source.js.map +1 -1
  48. package/dist/skills.d.ts +8 -2
  49. package/dist/skills.d.ts.map +1 -1
  50. package/dist/skills.js +31 -2
  51. package/dist/skills.js.map +1 -1
  52. package/dist/swarm-fs-core.d.ts +217 -0
  53. package/dist/swarm-fs-core.d.ts.map +1 -0
  54. package/dist/swarm-fs-core.js +885 -0
  55. package/dist/swarm-fs-core.js.map +1 -0
  56. package/dist/swarm-fs-path.d.ts +17 -0
  57. package/dist/swarm-fs-path.d.ts.map +1 -0
  58. package/dist/swarm-fs-path.js +46 -0
  59. package/dist/swarm-fs-path.js.map +1 -0
  60. package/dist/swarm-fs.d.ts +9 -0
  61. package/dist/swarm-fs.d.ts.map +1 -0
  62. package/dist/swarm-fs.js +21 -0
  63. package/dist/swarm-fs.js.map +1 -0
  64. package/dist/swarm-observations.d.ts +6 -0
  65. package/dist/swarm-observations.d.ts.map +1 -1
  66. package/dist/swarm-observations.js +28 -8
  67. package/dist/swarm-observations.js.map +1 -1
  68. package/dist/swarm.d.ts +124 -17
  69. package/dist/swarm.d.ts.map +1 -1
  70. package/dist/swarm.js +1085 -450
  71. package/dist/swarm.js.map +1 -1
  72. package/dist/unixfs-tree.d.ts +55 -0
  73. package/dist/unixfs-tree.d.ts.map +1 -1
  74. package/dist/unixfs-tree.js +361 -2
  75. package/dist/unixfs-tree.js.map +1 -1
  76. package/package.json +5 -5
  77. package/src/authoring.ts +88 -28
  78. package/src/cli.ts +1142 -267
  79. package/src/config-secret.ts +1 -1
  80. package/src/daemon-ui.ts +3 -0
  81. package/src/history.ts +26 -1
  82. package/src/kubo-topology.ts +25 -22
  83. package/src/kubo.ts +127 -9
  84. package/src/name-reference.ts +91 -0
  85. package/src/pivot-rpc.ts +272 -14
  86. package/src/search-index.ts +1 -0
  87. package/src/skill-definition.ts +45 -3
  88. package/src/skill-source.ts +10 -4
  89. package/src/skills.ts +43 -2
  90. package/src/swarm-fs-core.ts +1461 -0
  91. package/src/swarm-fs-path.ts +68 -0
  92. package/src/swarm-fs.ts +104 -0
  93. package/src/swarm-observations.ts +40 -14
  94. package/src/swarm.ts +1538 -656
  95. package/src/unixfs-tree.ts +447 -2
@@ -119,15 +119,314 @@ export async function listUnixfsDirectory(
119
119
  store: IpfsStore,
120
120
  dirCid: CID,
121
121
  ): Promise<Map<string, CID>> {
122
+ const links = await listUnixfsDirectoryLinks(store, dirCid);
123
+ const result = new Map<string, CID>();
124
+ for (const [name, entry] of links) result.set(name, entry.cid);
125
+ return result;
126
+ }
127
+
128
+ /**
129
+ * Reads direct entries of a single UnixFS directory block (including Tsize) without fetching children.
130
+ */
131
+ export async function listUnixfsDirectoryLinks(
132
+ store: IpfsStore,
133
+ dirCid: CID,
134
+ ): Promise<Map<string, { cid: CID; tsize: number | undefined }>> {
122
135
  if (dirCid.code !== dagPb.code)
123
136
  throw new HostError("Expected a UnixFS directory CID.");
124
137
  const bytes = await store.block.get(dirCid.toV1().toString());
125
138
  const dir = decodeUnixfsDirectory(bytes);
126
- const result = new Map<string, CID>();
127
- for (const [name, link] of dir.links) result.set(name, link.Hash.toV1());
139
+ const result = new Map<string, { cid: CID; tsize: number | undefined }>();
140
+ for (const [name, link] of dir.links) {
141
+ result.set(name, {
142
+ cid: link.Hash.toV1(),
143
+ tsize: link.Tsize,
144
+ });
145
+ }
128
146
  return result;
129
147
  }
130
148
 
149
+ export type UnixfsMutation =
150
+ { op: "set"; path: string; targetCid: CID } | { op: "delete"; path: string };
151
+
152
+ export interface ApplyUnixfsMutationsResult {
153
+ newRootCid: CID;
154
+ changed: boolean;
155
+ createdDirCids: string[];
156
+ removedCids: Map<string, CID>;
157
+ }
158
+
159
+ function compareSegments(a: readonly string[], b: readonly string[]): number {
160
+ const len = Math.min(a.length, b.length);
161
+ for (let i = 0; i < len; i++) {
162
+ if (a[i]! < b[i]!) return -1;
163
+ if (a[i]! > b[i]!) return 1;
164
+ }
165
+ return a.length - b.length;
166
+ }
167
+
168
+ interface MutableDirNode {
169
+ originalCid: CID | undefined;
170
+ links: Map<string, dagPb.PBLink>;
171
+ children: Map<string, MutableDirNode>;
172
+ dirty: boolean;
173
+ encodedCid?: CID;
174
+ newlyWritten?: boolean;
175
+ }
176
+
177
+ /**
178
+ * Batch O(affected depth) atomic UnixFS tree mutation: applies all set/delete
179
+ * operations and empty parent pruning on an in-memory overlay tree, then encodes
180
+ * and writes each modified directory block at most once.
181
+ */
182
+ export async function applyUnixfsMutations(
183
+ store: IpfsStore,
184
+ rootCid: CID,
185
+ mutations: readonly UnixfsMutation[],
186
+ ): Promise<ApplyUnixfsMutationsResult> {
187
+ const canonicalRoot = rootCid.toV1();
188
+ if (mutations.length === 0) {
189
+ return {
190
+ newRootCid: canonicalRoot,
191
+ changed: false,
192
+ createdDirCids: [],
193
+ removedCids: new Map(),
194
+ };
195
+ }
196
+
197
+ // 1. Validate all paths and check for duplicates or ancestor/descendant conflicts.
198
+ const parsed = mutations.map((mutation) => ({
199
+ mutation,
200
+ segments: splitPath(mutation.path),
201
+ }));
202
+ const sorted = [...parsed].sort((a, b) =>
203
+ compareSegments(a.segments, b.segments),
204
+ );
205
+ for (let i = 0; i < sorted.length - 1; i++) {
206
+ const a = sorted[i]!;
207
+ const b = sorted[i + 1]!;
208
+ if (
209
+ a.segments.length === b.segments.length &&
210
+ a.segments.every((seg, idx) => seg === b.segments[idx])
211
+ ) {
212
+ throw new HostError(`Duplicate UnixFS mutation path: ${a.mutation.path}`);
213
+ }
214
+ if (
215
+ a.segments.length < b.segments.length &&
216
+ a.segments.every((seg, idx) => seg === b.segments[idx])
217
+ ) {
218
+ throw new HostError(
219
+ `Conflicting ancestor/descendant UnixFS mutation paths: ${a.mutation.path} and ${b.mutation.path}`,
220
+ );
221
+ }
222
+ }
223
+
224
+ // 2. Pre-resolve all set target blocks before modifying any directory blocks.
225
+ const resolvedTargets = new Map<string, { cid: CID; tsize: number }>();
226
+ for (const { mutation } of parsed) {
227
+ if (mutation.op !== "set") continue;
228
+ const canonicalTarget = mutation.targetCid.toV1();
229
+ const tsize = await nodeTsize(store, canonicalTarget);
230
+ resolvedTargets.set(mutation.path, { cid: canonicalTarget, tsize });
231
+ }
232
+
233
+ // 3. Load root directory block on demand.
234
+ let rootDir: DecodedUnixfsDirectory;
235
+ if (canonicalRoot.code !== dagPb.code) {
236
+ const first = parsed[0]!.mutation;
237
+ throw new HostError(
238
+ first.op === "delete"
239
+ ? `Path does not exist: ${first.path}`
240
+ : `Cannot write path '${first.path}': ancestor '' is not a directory.`,
241
+ );
242
+ }
243
+ const rootBytes = await store.block.get(canonicalRoot.toString());
244
+ try {
245
+ rootDir = decodeUnixfsDirectory(rootBytes);
246
+ } catch {
247
+ const first = parsed[0]!.mutation;
248
+ throw new HostError(
249
+ first.op === "delete"
250
+ ? `Path does not exist: ${first.path}`
251
+ : `Cannot write path '${first.path}': ancestor '' is not a directory.`,
252
+ );
253
+ }
254
+
255
+ const rootNode: MutableDirNode = {
256
+ originalCid: canonicalRoot,
257
+ links: rootDir.links,
258
+ children: new Map(),
259
+ dirty: false,
260
+ };
261
+
262
+ // 4. Apply all mutations onto the in-memory overlay tree.
263
+ const removedCids = new Map<string, CID>();
264
+ for (const { mutation, segments } of parsed) {
265
+ const trail: MutableDirNode[] = [rootNode];
266
+ let current = rootNode;
267
+
268
+ if (mutation.op === "set") {
269
+ for (let i = 0; i < segments.length - 1; i++) {
270
+ const seg = segments[i]!;
271
+ let child = current.children.get(seg);
272
+ if (!child) {
273
+ const existingLink = current.links.get(seg);
274
+ if (existingLink) {
275
+ const childCid = existingLink.Hash.toV1();
276
+ if (childCid.code !== dagPb.code) {
277
+ throw new HostError(
278
+ `Cannot write path '${mutation.path}': ancestor '${segments.slice(0, i + 1).join("/")}' is not a directory.`,
279
+ );
280
+ }
281
+ const childBytes = await store.block.get(childCid.toString());
282
+ let decoded: DecodedUnixfsDirectory;
283
+ try {
284
+ decoded = decodeUnixfsDirectory(childBytes);
285
+ } catch {
286
+ throw new HostError(
287
+ `Cannot write path '${mutation.path}': ancestor '${segments.slice(0, i + 1).join("/")}' is not a directory.`,
288
+ );
289
+ }
290
+ child = {
291
+ originalCid: childCid,
292
+ links: decoded.links,
293
+ children: new Map(),
294
+ dirty: false,
295
+ };
296
+ } else {
297
+ child = {
298
+ originalCid: undefined,
299
+ links: new Map(),
300
+ children: new Map(),
301
+ dirty: true,
302
+ };
303
+ }
304
+ current.children.set(seg, child);
305
+ }
306
+ current = child;
307
+ trail.push(current);
308
+ }
309
+
310
+ const leaf = segments[segments.length - 1]!;
311
+ const target = resolvedTargets.get(mutation.path)!;
312
+ current.links.set(leaf, {
313
+ Name: leaf,
314
+ Hash: target.cid,
315
+ Tsize: target.tsize,
316
+ });
317
+ for (const node of trail) node.dirty = true;
318
+ } else {
319
+ for (let i = 0; i < segments.length - 1; i++) {
320
+ const seg = segments[i]!;
321
+ let child = current.children.get(seg);
322
+ if (!child) {
323
+ const existingLink = current.links.get(seg);
324
+ if (!existingLink)
325
+ throw new HostError(`Path does not exist: ${mutation.path}`);
326
+ const childCid = existingLink.Hash.toV1();
327
+ if (childCid.code !== dagPb.code)
328
+ throw new HostError(`Path does not exist: ${mutation.path}`);
329
+ const childBytes = await store.block.get(childCid.toString());
330
+ let decoded: DecodedUnixfsDirectory;
331
+ try {
332
+ decoded = decodeUnixfsDirectory(childBytes);
333
+ } catch {
334
+ throw new HostError(`Path does not exist: ${mutation.path}`);
335
+ }
336
+ child = {
337
+ originalCid: childCid,
338
+ links: decoded.links,
339
+ children: new Map(),
340
+ dirty: false,
341
+ };
342
+ current.children.set(seg, child);
343
+ }
344
+ current = child;
345
+ trail.push(current);
346
+ }
347
+
348
+ const leaf = segments[segments.length - 1]!;
349
+ const existing = current.links.get(leaf);
350
+ if (!existing)
351
+ throw new HostError(`Path does not exist: ${mutation.path}`);
352
+ removedCids.set(mutation.path, existing.Hash.toV1());
353
+ current.links.delete(leaf);
354
+ for (const node of trail) node.dirty = true;
355
+ }
356
+ }
357
+
358
+ // 5. Single bottom-up pass: prune empty non-root directories and encode dirty blocks once.
359
+ const finalize = async (
360
+ node: MutableDirNode,
361
+ isRoot: boolean,
362
+ ): Promise<{ pruned: true } | { pruned: false; cid: CID; tsize: number }> => {
363
+ const childNames = [...node.children.keys()].sort((a, b) =>
364
+ a < b ? -1 : a > b ? 1 : 0,
365
+ );
366
+ for (const childName of childNames) {
367
+ const child = node.children.get(childName)!;
368
+ if (!child.dirty) continue;
369
+ const result = await finalize(child, false);
370
+ if (result.pruned) {
371
+ node.links.delete(childName);
372
+ node.children.delete(childName);
373
+ } else {
374
+ node.links.set(childName, {
375
+ Name: childName,
376
+ Hash: result.cid,
377
+ Tsize: result.tsize,
378
+ });
379
+ }
380
+ }
381
+
382
+ if (!isRoot && node.links.size === 0) {
383
+ return { pruned: true };
384
+ }
385
+
386
+ const encoded = await encodeUnixfsDirectory(node.links.values());
387
+ if (!node.originalCid || !encoded.cid.equals(node.originalCid)) {
388
+ await store.put(encoded.cid, encoded.bytes);
389
+ node.newlyWritten = true;
390
+ node.encodedCid = encoded.cid;
391
+ } else {
392
+ node.newlyWritten = false;
393
+ node.encodedCid = node.originalCid;
394
+ }
395
+ return { pruned: false, cid: node.encodedCid, tsize: encoded.tsize };
396
+ };
397
+
398
+ const rootResult = await finalize(rootNode, true);
399
+ const finalRootCid = rootResult.pruned ? canonicalRoot : rootResult.cid;
400
+ const changed = !finalRootCid.equals(canonicalRoot);
401
+
402
+ // 6. Collect newly written directory CIDs in deterministic pre-order (root first, deduplicated).
403
+ const createdDirCids: string[] = [];
404
+ const seen = new Set<string>();
405
+ const collect = (node: MutableDirNode): void => {
406
+ if (node.newlyWritten && node.encodedCid) {
407
+ const key = node.encodedCid.toString();
408
+ if (!seen.has(key)) {
409
+ seen.add(key);
410
+ createdDirCids.push(key);
411
+ }
412
+ }
413
+ const childNames = [...node.children.keys()].sort((a, b) =>
414
+ a < b ? -1 : a > b ? 1 : 0,
415
+ );
416
+ for (const name of childNames) {
417
+ collect(node.children.get(name)!);
418
+ }
419
+ };
420
+ if (changed) collect(rootNode);
421
+
422
+ return {
423
+ newRootCid: finalRootCid,
424
+ changed,
425
+ createdDirCids,
426
+ removedCids,
427
+ };
428
+ }
429
+
131
430
  /**
132
431
  * Shallow O(depth) insert/replace of a target CID at `path` under `rootCid`.
133
432
  * Preserves all sibling links and their Tsize values without fetching sibling blocks.
@@ -399,3 +698,149 @@ export async function walkUnixfsNames(
399
698
  entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
400
699
  return { entries, truncated, skippedSubtrees };
401
700
  }
701
+
702
+ export interface WalkUnixfsTreeOptions {
703
+ /** Root-level metadata files to skip (e.g. .bundle.yaml, .bundle-type.yaml, entry.json). */
704
+ skipRootFiles?: ReadonlySet<string>;
705
+ /** Prefix prepended to discovered paths when walking a subtree. */
706
+ prefix?: string;
707
+ /** Maximum directory traversal depth (default: 16). */
708
+ maxDepth?: number;
709
+ /** Maximum directory blocks visited (default: 2048). */
710
+ maxNodes?: number;
711
+ }
712
+
713
+ export interface WalkUnixfsTreeEntry {
714
+ path: string;
715
+ name: string;
716
+ cid: string;
717
+ size: number | undefined;
718
+ kind: "directory" | "file" | "raw" | "other";
719
+ }
720
+
721
+ export interface WalkUnixfsTreeResult {
722
+ entries: WalkUnixfsTreeEntry[];
723
+ truncated: boolean;
724
+ skippedSubtrees: number;
725
+ }
726
+
727
+ function classifyDagPbBlock(bytes: Uint8Array): "directory" | "file" | "other" {
728
+ let node: dagPb.PBNode;
729
+ try {
730
+ node = dagPb.decode(bytes);
731
+ } catch {
732
+ return "other";
733
+ }
734
+ if (!node.Data) return "other";
735
+ try {
736
+ const unixfs = UnixFS.unmarshal(node.Data);
737
+ if (unixfs.type === "directory") return "directory";
738
+ if (unixfs.type === "file" || unixfs.type === "raw") return "file";
739
+ return "other";
740
+ } catch {
741
+ return "other";
742
+ }
743
+ }
744
+
745
+ /**
746
+ * Pure UnixFS bounded recursive traversal: does not fold or skip files inside
747
+ * directories containing `.bundle.yaml`. Unreachable children are counted in
748
+ * `skippedSubtrees` rather than failing or fabricating a node kind.
749
+ */
750
+ export async function walkUnixfsTree(
751
+ store: IpfsStore,
752
+ rootCid: CID,
753
+ options: WalkUnixfsTreeOptions = {},
754
+ ): Promise<WalkUnixfsTreeResult> {
755
+ const skipRoot = options.skipRootFiles ?? new Set<string>();
756
+ const prefix = options.prefix ?? "";
757
+ const maxDepth = options.maxDepth ?? 16;
758
+ const maxNodes = options.maxNodes ?? 2048;
759
+
760
+ const entries: WalkUnixfsTreeEntry[] = [];
761
+ let visitedNodes = 0;
762
+ let truncated = false;
763
+ let skippedSubtrees = 0;
764
+
765
+ const visit = async (
766
+ dirCid: CID,
767
+ currentPath: string,
768
+ depth: number,
769
+ ): Promise<void> => {
770
+ if (depth > maxDepth || visitedNodes >= maxNodes) {
771
+ truncated = true;
772
+ skippedSubtrees++;
773
+ return;
774
+ }
775
+ visitedNodes++;
776
+ let bytes: Uint8Array;
777
+ try {
778
+ bytes = await store.block.get(dirCid.toV1().toString());
779
+ } catch {
780
+ skippedSubtrees++;
781
+ return;
782
+ }
783
+ let dir: DecodedUnixfsDirectory;
784
+ try {
785
+ dir = decodeUnixfsDirectory(bytes);
786
+ } catch {
787
+ return;
788
+ }
789
+
790
+ const isRoot = currentPath === "";
791
+ const sortedNames = [...dir.links.keys()].sort((a, b) =>
792
+ a < b ? -1 : a > b ? 1 : 0,
793
+ );
794
+ for (const name of sortedNames) {
795
+ if (isRoot && skipRoot.has(name)) continue;
796
+ const link = dir.links.get(name)!;
797
+ const childCid = link.Hash.toV1();
798
+ const childPath = currentPath ? `${currentPath}/${name}` : name;
799
+ const fullPath = prefix ? `${prefix}/${childPath}` : childPath;
800
+
801
+ if (childCid.code === 0x55) {
802
+ entries.push({
803
+ path: fullPath,
804
+ name,
805
+ cid: childCid.toString(),
806
+ size: link.Tsize,
807
+ kind: "raw",
808
+ });
809
+ continue;
810
+ }
811
+ if (childCid.code !== dagPb.code) {
812
+ entries.push({
813
+ path: fullPath,
814
+ name,
815
+ cid: childCid.toString(),
816
+ size: link.Tsize,
817
+ kind: "other",
818
+ });
819
+ continue;
820
+ }
821
+
822
+ let childBytes: Uint8Array;
823
+ try {
824
+ childBytes = await store.block.get(childCid.toString());
825
+ } catch {
826
+ skippedSubtrees++;
827
+ continue;
828
+ }
829
+ const kind = classifyDagPbBlock(childBytes);
830
+ entries.push({
831
+ path: fullPath,
832
+ name,
833
+ cid: childCid.toString(),
834
+ size: link.Tsize,
835
+ kind,
836
+ });
837
+ if (kind === "directory") {
838
+ await visit(childCid, childPath, depth + 1);
839
+ }
840
+ }
841
+ };
842
+
843
+ await visit(rootCid.toV1(), "", 0);
844
+ entries.sort((a, b) => (a.path < b.path ? -1 : a.path > b.path ? 1 : 0));
845
+ return { entries, truncated, skippedSubtrees };
846
+ }