@ipab/cli 0.1.0-rc.5 → 0.1.0-rc.7

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 (157) hide show
  1. package/README.md +128 -22
  2. package/dist/app-authoring.d.ts +5 -0
  3. package/dist/app-authoring.d.ts.map +1 -1
  4. package/dist/app-authoring.js +4 -1
  5. package/dist/app-authoring.js.map +1 -1
  6. package/dist/app-templates.d.ts +0 -1
  7. package/dist/app-templates.d.ts.map +1 -1
  8. package/dist/app-templates.js +6 -45
  9. package/dist/app-templates.js.map +1 -1
  10. package/dist/builtins/skill.ipab.car +0 -0
  11. package/dist/builtins/template.bundle-viewer.car +0 -0
  12. package/dist/builtins/template.check.car +0 -0
  13. package/dist/builtins/template.node.car +0 -0
  14. package/dist/builtins/template.web-app.car +0 -0
  15. package/dist/builtins.d.ts.map +1 -1
  16. package/dist/builtins.js +1 -39
  17. package/dist/builtins.js.map +1 -1
  18. package/dist/cli.d.ts +4 -1
  19. package/dist/cli.d.ts.map +1 -1
  20. package/dist/cli.js +934 -336
  21. package/dist/cli.js.map +1 -1
  22. package/dist/content-provider.d.ts +1 -1
  23. package/dist/content-provider.d.ts.map +1 -1
  24. package/dist/content-provider.js +4 -3
  25. package/dist/content-provider.js.map +1 -1
  26. package/dist/context.d.ts +16 -1
  27. package/dist/context.d.ts.map +1 -1
  28. package/dist/context.js +20 -10
  29. package/dist/context.js.map +1 -1
  30. package/dist/doctor.d.ts +29 -0
  31. package/dist/doctor.d.ts.map +1 -0
  32. package/dist/doctor.js +576 -0
  33. package/dist/doctor.js.map +1 -0
  34. package/dist/identity.d.ts +1 -1
  35. package/dist/identity.d.ts.map +1 -1
  36. package/dist/identity.js +9 -9
  37. package/dist/identity.js.map +1 -1
  38. package/dist/ipc.d.ts +2 -2
  39. package/dist/ipc.d.ts.map +1 -1
  40. package/dist/ipc.js +44 -12
  41. package/dist/ipc.js.map +1 -1
  42. package/dist/kubo-install.d.ts +18 -1
  43. package/dist/kubo-install.d.ts.map +1 -1
  44. package/dist/kubo-install.js +424 -61
  45. package/dist/kubo-install.js.map +1 -1
  46. package/dist/kubo-release.d.ts +19 -0
  47. package/dist/kubo-release.d.ts.map +1 -1
  48. package/dist/kubo-release.js +164 -0
  49. package/dist/kubo-release.js.map +1 -1
  50. package/dist/kubo.d.ts +1 -0
  51. package/dist/kubo.d.ts.map +1 -1
  52. package/dist/kubo.js +102 -96
  53. package/dist/kubo.js.map +1 -1
  54. package/dist/pins.d.ts +15 -17
  55. package/dist/pins.d.ts.map +1 -1
  56. package/dist/pins.js +144 -106
  57. package/dist/pins.js.map +1 -1
  58. package/dist/publish-source.d.ts +3 -0
  59. package/dist/publish-source.d.ts.map +1 -0
  60. package/dist/publish-source.js +85 -0
  61. package/dist/publish-source.js.map +1 -0
  62. package/dist/runtime.d.ts.map +1 -1
  63. package/dist/runtime.js +6 -5
  64. package/dist/runtime.js.map +1 -1
  65. package/dist/skill-definition.d.ts +6 -0
  66. package/dist/skill-definition.d.ts.map +1 -0
  67. package/dist/skill-definition.js +38 -0
  68. package/dist/skill-definition.js.map +1 -0
  69. package/dist/skill-source.d.ts +5 -0
  70. package/dist/skill-source.d.ts.map +1 -0
  71. package/dist/skill-source.js +12 -0
  72. package/dist/skill-source.js.map +1 -0
  73. package/dist/skills.d.ts +84 -0
  74. package/dist/skills.d.ts.map +1 -0
  75. package/dist/skills.js +736 -0
  76. package/dist/skills.js.map +1 -0
  77. package/dist/space.d.ts +15 -2
  78. package/dist/space.d.ts.map +1 -1
  79. package/dist/space.js +153 -3
  80. package/dist/space.js.map +1 -1
  81. package/dist/state-storage-windows.js +1 -1
  82. package/dist/state-storage.d.ts +3 -1
  83. package/dist/state-storage.d.ts.map +1 -1
  84. package/dist/state-storage.js +38 -7
  85. package/dist/state-storage.js.map +1 -1
  86. package/dist/state.d.ts +40 -6
  87. package/dist/state.d.ts.map +1 -1
  88. package/dist/state.js +208 -82
  89. package/dist/state.js.map +1 -1
  90. package/dist/swarm-envelope.d.ts +1 -0
  91. package/dist/swarm-envelope.d.ts.map +1 -1
  92. package/dist/swarm-envelope.js +12 -0
  93. package/dist/swarm-envelope.js.map +1 -1
  94. package/dist/swarm-files.d.ts +1 -1
  95. package/dist/swarm-files.d.ts.map +1 -1
  96. package/dist/swarm-files.js +2 -2
  97. package/dist/swarm-files.js.map +1 -1
  98. package/dist/swarm-meta.d.ts +17 -0
  99. package/dist/swarm-meta.d.ts.map +1 -0
  100. package/dist/swarm-meta.js +96 -0
  101. package/dist/swarm-meta.js.map +1 -0
  102. package/dist/swarm.d.ts +117 -8
  103. package/dist/swarm.d.ts.map +1 -1
  104. package/dist/swarm.js +1275 -131
  105. package/dist/swarm.js.map +1 -1
  106. package/dist/template-paths.d.ts +7 -0
  107. package/dist/template-paths.d.ts.map +1 -0
  108. package/dist/template-paths.js +23 -0
  109. package/dist/template-paths.js.map +1 -0
  110. package/dist/template-presentation.d.ts +20 -0
  111. package/dist/template-presentation.d.ts.map +1 -0
  112. package/dist/template-presentation.js +133 -0
  113. package/dist/template-presentation.js.map +1 -0
  114. package/dist/template-source.d.ts +2 -1
  115. package/dist/template-source.d.ts.map +1 -1
  116. package/dist/template-source.js +7 -1
  117. package/dist/template-source.js.map +1 -1
  118. package/dist/templates.d.ts +11 -0
  119. package/dist/templates.d.ts.map +1 -0
  120. package/dist/templates.js +53 -0
  121. package/dist/templates.js.map +1 -0
  122. package/package.json +7 -6
  123. package/src/app-authoring.ts +18 -2
  124. package/src/app-templates.ts +8 -72
  125. package/src/builtins.ts +2 -48
  126. package/src/cli.ts +1154 -387
  127. package/src/content-provider.ts +4 -2
  128. package/src/context.ts +45 -10
  129. package/src/doctor.ts +902 -0
  130. package/src/identity.ts +10 -10
  131. package/src/ipc.ts +63 -10
  132. package/src/kubo-install.ts +562 -78
  133. package/src/kubo-release.ts +213 -0
  134. package/src/kubo.ts +134 -106
  135. package/src/pins.ts +188 -126
  136. package/src/publish-source.ts +103 -0
  137. package/src/runtime.ts +6 -5
  138. package/src/skill-definition.ts +53 -0
  139. package/src/skill-source.ts +15 -0
  140. package/src/skills.ts +1005 -0
  141. package/src/space.ts +214 -5
  142. package/src/state-storage-windows.ts +1 -1
  143. package/src/state-storage.ts +53 -7
  144. package/src/state.ts +357 -98
  145. package/src/swarm-envelope.ts +14 -0
  146. package/src/swarm-files.ts +2 -1
  147. package/src/swarm-meta.ts +109 -0
  148. package/src/swarm.ts +1877 -175
  149. package/src/template-paths.ts +43 -0
  150. package/src/template-presentation.ts +151 -0
  151. package/src/template-source.ts +9 -2
  152. package/src/templates.ts +77 -0
  153. package/dist/setup.d.ts +0 -8
  154. package/dist/setup.d.ts.map +0 -1
  155. package/dist/setup.js +0 -27
  156. package/dist/setup.js.map +0 -1
  157. package/src/setup.ts +0 -34
package/src/space.ts CHANGED
@@ -5,8 +5,10 @@ import { CID } from "multiformats/cid";
5
5
  import { sha256 } from "multiformats/hashes/sha2";
6
6
  import type { IpfsStore } from "@ipab/ipfs-store";
7
7
  import { openBundle, type ContentBundle } from "@ipab/bundle";
8
- import { HostError, message } from "./errors.js";
9
- import type { Ownership } from "./history.js";
8
+ import { HostError, message, object } from "./errors.js";
9
+ import { Ownership } from "./history.js";
10
+ import { randomUUID } from "node:crypto";
11
+ import { ReadableStream as NodeReadableStream } from "node:stream/web";
10
12
  import { appDescription, appSchemaCid } from "./app-definition.js";
11
13
  import { readSupportedSchemas } from "./supported-schemas.js";
12
14
  export { appSchema, appSchemaCid } from "./app-definition.js";
@@ -42,7 +44,7 @@ export function relativePath(path: string): string {
42
44
  }
43
45
 
44
46
  export async function bytes(
45
- stream: ReadableStream<Uint8Array>,
47
+ stream: ReadableStream<Uint8Array> | NodeReadableStream<Uint8Array>,
46
48
  limit: number,
47
49
  ): Promise<Uint8Array> {
48
50
  const reader = stream.getReader();
@@ -157,6 +159,75 @@ export async function loadApp(
157
159
  };
158
160
  }
159
161
 
162
+ /** Checkpoints are owned by the execution, not pins; only the final result is handed to history. */
163
+ export async function deriveBundle(
164
+ inputCid: string,
165
+ replacements: unknown,
166
+ store: IpfsStore,
167
+ owner: Ownership,
168
+ signal?: AbortSignal,
169
+ ): Promise<string> {
170
+ signal?.throwIfAborted();
171
+ const changes = Object.entries(object(replacements, "Bundle replacements"));
172
+ if (changes.length > MAX_PATHS)
173
+ throw new HostError("Bundle replacement path limit exceeded.");
174
+ let total = 0;
175
+ for (const [path, value] of changes) {
176
+ relativePath(path);
177
+ if (!(value instanceof Uint8Array))
178
+ throw new HostError("Bundle replacements must contain byte arrays.");
179
+ total += value.byteLength;
180
+ if (total > OUTPUT_BYTES)
181
+ throw new HostError("Bundle replacement byte limit exceeded.");
182
+ }
183
+ const input = CID.parse(inputCid).toV1().toString();
184
+ const original = await validatedBundle(input, store, owner, signal);
185
+ const id = randomUUID();
186
+ const session = `ipab-derive:${owner.state.identity}:${id}`;
187
+ const temporary = await Ownership.create(
188
+ owner.state,
189
+ store,
190
+ `derive:${id}`,
191
+ session,
192
+ );
193
+ try {
194
+ const output = new OutputSpace(store, temporary, session, signal);
195
+ const replaced = new Set(changes.map(([path]) => path));
196
+ for (const path of original.files) {
197
+ signal?.throwIfAborted();
198
+ if (replaced.has(path)) continue;
199
+ const file = await store.read(
200
+ `${input}/${path}`,
201
+ signal ? { signal } : {},
202
+ );
203
+ await output.link(path, file.cid.toString(), signal);
204
+ }
205
+ for (const [path, value] of changes) {
206
+ const content = value as Uint8Array;
207
+ const chunks = async function* () {
208
+ for (let offset = 0; offset < content.length; offset += CHUNK_BYTES) {
209
+ signal?.throwIfAborted();
210
+ yield await Promise.resolve(
211
+ content.subarray(offset, offset + CHUNK_BYTES),
212
+ );
213
+ }
214
+ };
215
+ await output.write(path, chunks(), signal);
216
+ }
217
+ const result = await output.finalize();
218
+ const derived = await validatedBundle(result, store, temporary, signal);
219
+ if (derived.schemaKey !== original.schemaKey)
220
+ throw new HostError(
221
+ "Derived Bundle must preserve the input's exact type CID.",
222
+ );
223
+ await owner.retain([result], signal);
224
+ signal?.throwIfAborted();
225
+ return result;
226
+ } finally {
227
+ await temporary.release();
228
+ }
229
+ }
230
+
160
231
  interface Entry {
161
232
  cid: CID;
162
233
  size: bigint;
@@ -175,6 +246,11 @@ export class OutputSpace {
175
246
  private readonly pending = new Set<string>();
176
247
  private totalBytes = 0;
177
248
  private fault: Error | undefined;
249
+ private bundleMode = false;
250
+ private bundleSchema: string | undefined;
251
+ private submittedBundle: string | undefined;
252
+ private selectingBundle = false;
253
+ private sessionStarted = false;
178
254
  constructor(
179
255
  readonly store: IpfsStore,
180
256
  readonly owner: Ownership,
@@ -184,6 +260,10 @@ export class OutputSpace {
184
260
  this.session = session;
185
261
  }
186
262
  private claim(path: string): void {
263
+ if (this.bundleMode)
264
+ throw new HostError(
265
+ "Explicit Bundle results cannot be mixed with output Space writes.",
266
+ );
187
267
  relativePath(path);
188
268
  if (this.claimed.size >= MAX_PATHS)
189
269
  throw new HostError("Output path limit exceeded.");
@@ -199,7 +279,125 @@ export class OutputSpace {
199
279
  this.pending.add(path);
200
280
  }
201
281
  fail(error: unknown): void {
202
- this.fault ??= new HostError(message(error));
282
+ this.fault ??=
283
+ error instanceof HostError ? error : new HostError(message(error));
284
+ }
285
+ private selectBundleMode(): void {
286
+ if (this.fault) throw this.fault;
287
+ if (this.claimed.size)
288
+ throw new HostError(
289
+ "Explicit Bundle results cannot be mixed with output Space writes.",
290
+ );
291
+ if (this.selectingBundle)
292
+ throw new HostError(
293
+ "Concurrent Bundle result selection is not supported.",
294
+ );
295
+ this.bundleMode = true;
296
+ }
297
+ async beginOpenSession(
298
+ inputCid: string,
299
+ signal?: AbortSignal,
300
+ supportedTypes?: ReadonlySet<string>,
301
+ expectedTypes?: unknown,
302
+ ): Promise<void> {
303
+ try {
304
+ this.selectBundleMode();
305
+ if (this.sessionStarted || this.submittedBundle)
306
+ throw new HostError(
307
+ "Open session has already started or submitted a result.",
308
+ );
309
+ this.sessionStarted = true;
310
+ this.selectingBundle = true;
311
+ if (expectedTypes !== undefined) {
312
+ if (
313
+ !Array.isArray(expectedTypes) ||
314
+ expectedTypes.length > MAX_PATHS ||
315
+ !expectedTypes.every(
316
+ (cid: unknown) =>
317
+ typeof cid === "string" &&
318
+ CID.parse(cid).toV1().toString() === cid,
319
+ )
320
+ )
321
+ throw new HostError(
322
+ "Open supportedTypes must be exact canonical CID strings.",
323
+ );
324
+ const expected = new Set<string>(expectedTypes as string[]);
325
+ if (expected.size !== expectedTypes.length)
326
+ throw new HostError(
327
+ "Open supportedTypes must not contain duplicate CIDs.",
328
+ );
329
+ if (
330
+ !supportedTypes ||
331
+ expected.size !== supportedTypes.size ||
332
+ [...expected].some((cid) => !supportedTypes.has(cid))
333
+ )
334
+ throw new HostError(
335
+ "Open supportedTypes must exactly match the App supportedSchemas declaration.",
336
+ );
337
+ }
338
+ const bundle = await validatedBundle(
339
+ inputCid,
340
+ this.store,
341
+ this.owner,
342
+ signal,
343
+ );
344
+ signal?.throwIfAborted();
345
+ if (supportedTypes && !supportedTypes.has(bundle.schemaKey))
346
+ throw new HostError(
347
+ "App does not declare support for the Open input's exact type CID.",
348
+ );
349
+ this.bundleSchema = bundle.schemaKey;
350
+ } catch (error) {
351
+ this.fail(error);
352
+ throw error;
353
+ } finally {
354
+ this.selectingBundle = false;
355
+ }
356
+ }
357
+ cancelOpenSession(): void {
358
+ this.bundleMode = true;
359
+ this.fail(
360
+ new HostError(
361
+ "Open session cancelled without a committed Bundle result.",
362
+ 130,
363
+ "CANCELLED",
364
+ "execution",
365
+ ),
366
+ );
367
+ }
368
+ discardBundleResult(): void {
369
+ this.submittedBundle = undefined;
370
+ }
371
+ async submitBundleResult(root: string, signal?: AbortSignal): Promise<void> {
372
+ try {
373
+ this.selectBundleMode();
374
+ } catch (error) {
375
+ this.fail(error);
376
+ throw error;
377
+ }
378
+ this.selectingBundle = true;
379
+ try {
380
+ const cid = CID.parse(root).toV1().toString();
381
+ if (this.submittedBundle && this.submittedBundle !== cid) {
382
+ const error = new HostError(
383
+ "A different Bundle result has already been submitted.",
384
+ );
385
+ this.fail(error);
386
+ throw error;
387
+ }
388
+ signal?.throwIfAborted();
389
+ if (this.submittedBundle === cid) return;
390
+ const bundle = await validatedBundle(cid, this.store, this.owner, signal);
391
+ signal?.throwIfAborted();
392
+ if (this.bundleSchema && bundle.schemaKey !== this.bundleSchema)
393
+ throw new HostError(
394
+ "Bundle result must preserve the Open session's exact type CID.",
395
+ );
396
+ this.submittedBundle = cid;
397
+ } finally {
398
+ // Validation/storage failures leave the explicit session open for retry, never a Space result.
399
+ this.selectingBundle = false;
400
+ }
203
401
  }
204
402
  async write(
205
403
  path: string,
@@ -235,7 +433,7 @@ export class OutputSpace {
235
433
  block instanceof Uint8Array
236
434
  ? block
237
435
  : await bytes(
238
- ReadableStream.from(block),
436
+ NodeReadableStream.from(block),
239
437
  this.store.limits.blockBytes,
240
438
  );
241
439
  await this.store.stageBlock(
@@ -312,7 +510,18 @@ export class OutputSpace {
312
510
  }
313
511
  }
314
512
  async finalize(): Promise<string> {
513
+ this.signal?.throwIfAborted();
315
514
  if (this.fault) throw this.fault;
515
+ if (this.bundleMode) {
516
+ if (this.selectingBundle || !this.submittedBundle)
517
+ throw new HostError(
518
+ "Open session ended without submitting a Bundle result.",
519
+ 130,
520
+ "CANCELLED",
521
+ "commit",
522
+ );
523
+ return this.submittedBundle;
524
+ }
316
525
  if (this.pending.size)
317
526
  throw new HostError(
318
527
  "Output has unfinished stream writes.",
@@ -37,7 +37,7 @@ function Check-Path($path) {
37
37
  $attrs = [IO.File]::GetAttributes($path)
38
38
  if ($attrs -band [IO.FileAttributes]::ReparsePoint) { throw 'Invalid state path' }
39
39
  $dir = [bool]($attrs -band [IO.FileAttributes]::Directory)
40
- $isDir = $path -eq $root -or [IO.Path]::GetFileName($path) -in 'runtime','logs','kubo-bin','swarm-nodes','swarm-credentials'
40
+ $isDir = $path -eq $root -or [IO.Path]::GetFileName($path) -in 'runtime','logs','kubo-bin','kubo-cache','swarm-nodes','swarm-credentials'
41
41
  if ($dir -ne $isDir) { throw 'Invalid state type' }
42
42
  if ($dir) { $acl = [IO.Directory]::GetAccessControl($path) }
43
43
  else { $acl = [IO.File]::GetAccessControl($path) }
@@ -31,13 +31,18 @@ const directories = [
31
31
  "runtime",
32
32
  "logs",
33
33
  "kubo-bin",
34
+ "kubo-cache",
34
35
  "swarm-nodes",
35
36
  "swarm-credentials",
36
37
  ];
37
38
  const windows = () => process.platform === "win32";
38
- function unsafe(): never {
39
+ function unsafe(reason?: string): never {
39
40
  throw new HostError(
40
- "State storage requires a dedicated, account-only regular directory and files. Stop IPAB processes, back up your state, and migrate only known IPAB files to a new private --state-dir; do not change permissions on a shared directory.",
41
+ "State storage requires a dedicated, account-only regular directory and files. " +
42
+ (reason ??
43
+ "Stop IPAB processes, back up your state, and migrate only known IPAB files to a new private --state-dir; do not change permissions on a shared directory."),
44
+ 1,
45
+ "UNSAFE_STATE_STORAGE",
41
46
  );
42
47
  }
43
48
 
@@ -222,7 +227,10 @@ function access(directory: string, initialize: boolean, sealed = false): void {
222
227
  const entries: Entry[] = [];
223
228
  for (const name of root ? readdirSync(directory) : []) {
224
229
  const dir = directories.includes(name);
225
- if (!dir && !files.includes(name)) unsafe();
230
+ if (!dir && !files.includes(name))
231
+ unsafe(
232
+ `Unexpected entry ${JSON.stringify(name)} in the state directory. Move backups or unrelated entries outside this directory; keep state.sqlite and the IPAB runtime directories in place. No files were moved or deleted.`,
233
+ );
226
234
  const info = lstatSync(join(directory, name), { throwIfNoEntry: false });
227
235
  // SQLite may unlink a transient sidecar as another connection closes.
228
236
  if (!info) {
@@ -314,16 +322,54 @@ export function prepareStateStorage(
314
322
  const path = join(directory, database);
315
323
  createFile(path);
316
324
  return path;
317
- } catch {
325
+ } catch (cause) {
326
+ if (cause instanceof HostError && cause.code === "UNSAFE_STATE_STORAGE")
327
+ throw cause;
318
328
  unsafe();
319
329
  }
320
330
  }
321
331
 
322
- export function verifyStateStorage(directory: string): void {
332
+ export function openStateStorage(
333
+ directory: string,
334
+ env: NodeJS.ProcessEnv = process.env,
335
+ ): string {
336
+ directory = resolve(directory);
337
+ const markerInfo = lstatSync(join(directory, marker), {
338
+ throwIfNoEntry: false,
339
+ });
340
+ if (!markerInfo) return prepareStateStorage(directory, env);
341
+ const root = lstatSync(directory);
342
+ if (
343
+ !root.isDirectory() ||
344
+ !markerInfo.isFile() ||
345
+ markerInfo.nlink !== 1 ||
346
+ markerInfo.size !== 0
347
+ )
348
+ unsafe();
349
+ const path = join(directory, database);
350
+ const info = lstatSync(path, { throwIfNoEntry: false });
351
+ if (!info) return prepareStateStorage(directory, env);
352
+ if (!info.isFile() || info.nlink !== 1) unsafe();
353
+ return path;
354
+ }
355
+
356
+ export function verifyStateStorage(
357
+ directory: string,
358
+ env: NodeJS.ProcessEnv = process.env,
359
+ ): void {
323
360
  try {
324
- checkLocation(resolve(directory), process.env);
361
+ checkLocation(resolve(directory), env);
325
362
  access(directory, false);
326
- } catch {
363
+ } catch (cause) {
364
+ if (cause instanceof HostError && cause.code === "UNSAFE_STATE_STORAGE")
365
+ throw cause;
327
366
  unsafe();
328
367
  }
329
368
  }
369
+
370
+ export function verifyStateLocation(
371
+ directory: string,
372
+ env: NodeJS.ProcessEnv,
373
+ ): void {
374
+ checkLocation(resolve(directory), env);
375
+ }