@arcadia-eternity/content-sdk 4.5.3 → 4.7.1

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 (72) hide show
  1. package/README.md +2 -2
  2. package/dist/author-api.d.ts +12 -2
  3. package/dist/author-api.js +11 -2
  4. package/dist/author-git.d.ts +63 -0
  5. package/dist/author-git.js +324 -0
  6. package/dist/author-reference.d.ts +15 -1
  7. package/dist/author-reference.js +20 -1
  8. package/dist/cli.js +5 -4
  9. package/dist/content-query.d.ts +15 -1
  10. package/dist/content-query.js +53 -11
  11. package/dist/content-workspace.d.ts +435 -6
  12. package/dist/content-workspace.js +238 -77
  13. package/dist/debug-environments.d.ts +393 -33
  14. package/dist/debug-environments.js +10 -50
  15. package/dist/demo.js +1 -1
  16. package/dist/effect-declarations.d.ts +14 -0
  17. package/dist/effect-declarations.js +103 -0
  18. package/dist/environment-host-base.d.ts +175 -0
  19. package/dist/environment-host-base.js +86 -0
  20. package/dist/environment-member-transfer.d.ts +5 -0
  21. package/dist/environment-member-transfer.js +95 -0
  22. package/dist/environment-rules.d.ts +728 -3
  23. package/dist/environment-rules.js +158 -74
  24. package/dist/environment-sharing.d.ts +51 -0
  25. package/dist/environment-sharing.js +321 -0
  26. package/dist/environment.d.ts +2 -0
  27. package/dist/environment.js +32 -9
  28. package/dist/jobs.d.ts +5 -0
  29. package/dist/jobs.js +59 -10
  30. package/dist/learnsets.d.ts +47 -0
  31. package/dist/learnsets.js +124 -0
  32. package/dist/lib/content.d.ts +1 -1
  33. package/dist/lib/content.js +26 -2
  34. package/dist/lib/presentation.js +5 -11
  35. package/dist/managed-projects.d.ts +6 -0
  36. package/dist/managed-projects.js +137 -45
  37. package/dist/managed-sources.d.ts +17 -0
  38. package/dist/managed-sources.js +28 -10
  39. package/dist/mcp-contracts.d.ts +405 -12
  40. package/dist/mcp-contracts.js +3 -1
  41. package/dist/mcp.js +23 -1
  42. package/dist/pack-project.d.ts +2 -1
  43. package/dist/pack-project.js +40 -5
  44. package/dist/prepared-document.d.ts +476 -0
  45. package/dist/prepared-document.js +47 -0
  46. package/dist/preview.js +5 -5
  47. package/dist/project.d.ts +3 -1
  48. package/dist/project.js +47 -4
  49. package/dist/rule-composition.d.ts +290 -0
  50. package/dist/rule-composition.js +267 -0
  51. package/dist/template/.github/scripts/release-payload.mjs +91 -0
  52. package/dist/template/.github/scripts/release-test-npm.mjs +27 -0
  53. package/dist/template/.github/scripts/verify-release.mjs +138 -0
  54. package/dist/template/.github/workflows/release-pack.yml +77 -0
  55. package/dist/template/README.md +15 -2
  56. package/dist/template/gitignore-template +7 -0
  57. package/dist/template/npm-lock-template.json +2319 -0
  58. package/dist/template/package.json +4 -2
  59. package/dist/template/skills/arcadia-content-authoring/references/tool-workflows.md +1 -1
  60. package/dist/template/skills/arcadia-content-git/SKILL.md +14 -0
  61. package/dist/template/skills/arcadia-content-release/SKILL.md +29 -0
  62. package/dist/template/test/pack.test.mjs +24 -5
  63. package/dist/testing.d.ts +1 -0
  64. package/dist/testing.js +26 -7
  65. package/dist/workspace-cli.js +2 -2
  66. package/dist/workspace-materials.d.ts +4 -1
  67. package/dist/workspace-materials.js +8 -2
  68. package/dist/workspace.d.ts +11 -0
  69. package/dist/workspace.js +191 -31
  70. package/package.json +16 -4
  71. package/release-payload.mjs +91 -0
  72. package/release-test-npm.mjs +27 -0
@@ -1,8 +1,8 @@
1
+ import { sha256Hex } from '@arcadia-eternity/const';
1
2
  import { execFile } from 'node:child_process';
2
- import { createHash } from 'node:crypto';
3
- import { mkdir, mkdtemp, readFile, realpath, rename, rm, writeFile } from 'node:fs/promises';
3
+ import { mkdir, mkdtemp, readFile, readdir, realpath, rename, rm, writeFile } from 'node:fs/promises';
4
4
  import { join, resolve } from 'node:path';
5
- import { promisify } from 'node:util';
5
+ import { isDeepStrictEqual, promisify } from 'node:util';
6
6
  import { valid, validRange } from 'semver';
7
7
  import { inspectNpmProject, resolveNpmInvocation, withShippedEnvironmentProject, withShippedCatalogArchive, } from '@arcadia-eternity/pack-loader/node';
8
8
  import { normalizeEnvironmentDocument } from './debug-environments.js';
@@ -140,6 +140,88 @@ export class ManagedProjects {
140
140
  throw contentError('NPM_INSTALL_FAILED');
141
141
  }
142
142
  }
143
+ /** Resolve an author lock in isolation from the source tree and its linked development SDK. */
144
+ async authorLock(manifest, previous) {
145
+ const allowed = [
146
+ 'name',
147
+ 'version',
148
+ 'license',
149
+ 'engines',
150
+ 'dependencies',
151
+ 'devDependencies',
152
+ 'optionalDependencies',
153
+ 'peerDependencies',
154
+ 'peerDependenciesMeta',
155
+ ];
156
+ const metadata = Object.fromEntries(allowed.filter(key => Object.hasOwn(manifest, key)).map(key => [key, manifest[key]]));
157
+ checkDependencySpecs(metadata);
158
+ checkDependencySpecs({ dependencies: metadata.devDependencies });
159
+ for (const field of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) {
160
+ const specs = metadata[field];
161
+ if (isRecord(specs) && Object.values(specs).some(value => typeof value !== 'string' || !validRange(value)))
162
+ throw contentError('INSTALL_DEPENDENCY_SOURCE_FORBIDDEN');
163
+ }
164
+ if (previous)
165
+ checkLock(JSON.parse(previous));
166
+ const project = join(this.root, 'project');
167
+ await mkdir(project);
168
+ await writeFile(join(project, 'package.json'), JSON.stringify(metadata));
169
+ const path = join(project, 'package-lock.json');
170
+ if (previous)
171
+ await writeFile(path, previous);
172
+ await this.#npm(['install', '--package-lock-only', '--ignore-scripts', '--registry', this.#options.registry], project, this.#controller.signal);
173
+ const result = await readFile(path, 'utf8');
174
+ checkLock(JSON.parse(result));
175
+ return result;
176
+ }
177
+ async #packSource(source, destination, signal, expectedName) {
178
+ const captured = await acquireSource(source, {
179
+ destination,
180
+ preserveNpmArchive: true,
181
+ signal,
182
+ maxBytes: this.#options.maxBytes,
183
+ maxFiles: this.#options.maxFiles,
184
+ timeoutMs: this.#options.timeoutMs,
185
+ });
186
+ const manifest = JSON.parse(await readFile(join(captured.directory, 'package.json'), 'utf8'));
187
+ if (typeof manifest.name !== 'string' ||
188
+ !packageName.test(manifest.name) ||
189
+ (expectedName && manifest.name !== expectedName))
190
+ throw contentError('INSTALL_SOURCE_IDENTITY');
191
+ if (typeof manifest.version !== 'string' || !valid(manifest.version))
192
+ throw contentError('INSTALL_SOURCE_VERSION');
193
+ checkDependencySpecs(manifest);
194
+ try {
195
+ checkLock(JSON.parse(await readFile(join(captured.directory, 'npm-shrinkwrap.json'), 'utf8')));
196
+ }
197
+ catch (error) {
198
+ if (errorCode(error) !== 'ENOENT')
199
+ throw error;
200
+ }
201
+ if (captured.archive)
202
+ return { file: captured.archive, provenance: captured.provenance };
203
+ const output = join(destination, 'packed');
204
+ await mkdir(output);
205
+ await this.#npm(['pack', '--ignore-scripts', '--json', '--pack-destination', output, '--registry', this.#options.registry], captured.directory, signal);
206
+ const files = await readdir(output);
207
+ if (files.length !== 1 || !files[0].endsWith('.tgz'))
208
+ throw contentError('INSTALL_PACK_INVALID');
209
+ return { file: join(output, files[0]), provenance: captured.provenance };
210
+ }
211
+ async withLocalArchive(file, signal, consume) {
212
+ return this.withSourceArchive({ kind: 'archive', file }, signal, consume);
213
+ }
214
+ async withSourceArchive(source, signal, consume) {
215
+ await mkdir(this.root, { recursive: true, mode: 0o700 });
216
+ const staging = await mkdtemp(join(this.root, 'archive-'));
217
+ try {
218
+ const captured = await this.#packSource(source, join(staging, 'source'), signal);
219
+ return await consume(captured.file, captured.provenance);
220
+ }
221
+ finally {
222
+ await rm(staging, { recursive: true, force: true });
223
+ }
224
+ }
143
225
  prepareEnvironment(workspace, input, validate) {
144
226
  if (typeof validate !== 'function')
145
227
  return Promise.reject(contentError('STATIC_PREPARER_REQUIRED'));
@@ -152,7 +234,10 @@ export class ManagedProjects {
152
234
  if ((draft?.revision ?? null) !== input.expectedRevision)
153
235
  throw contentError('REVISION_CONFLICT');
154
236
  return input.document ?? draft?.document;
155
- }, { ...input, projectDirectory: join(workspace.root, 'environments', input.environmentId) }, async (directory, signal, document) => {
237
+ }, {
238
+ ...input,
239
+ projectDirectory: input.seedDirectory ?? join(workspace.root, 'environments', input.environmentId),
240
+ }, async (directory, signal, document) => {
156
241
  return workspace.prepareEnvironment({ ...input, document, projectDirectory: directory, signal }, validate);
157
242
  }));
158
243
  }
@@ -324,11 +409,23 @@ export class ManagedProjects {
324
409
  await mkdir(join(this.root, 'staging'), { recursive: true, mode: 0o700 });
325
410
  directory = await mkdtemp(join(this.root, 'staging', 'prepare-'));
326
411
  await mkdir(join(directory, 'candidates'));
327
- for (const [name, spec] of Object.entries(dependencies)) {
328
- if (name === baseName)
329
- continue;
330
- if (!spec.startsWith('file:'))
331
- continue;
412
+ const localSpecs = new Set(Object.entries(dependencies)
413
+ .filter(([name, spec]) => name !== baseName && spec.startsWith('file:'))
414
+ .map(([, spec]) => spec));
415
+ if (input.projectDirectory && localSpecs.size) {
416
+ const previous = await readFile(join(input.projectDirectory, 'package-lock.json'), 'utf8').catch(error => {
417
+ if (errorCode(error) !== 'ENOENT')
418
+ throw error;
419
+ });
420
+ if (previous)
421
+ for (const entry of Object.values(JSON.parse(previous).packages ?? {})) {
422
+ if (isRecord(entry) &&
423
+ typeof entry.resolved === 'string' &&
424
+ /^file:candidates\/[a-zA-Z0-9_.-]+\.tgz$/.test(entry.resolved))
425
+ localSpecs.add(entry.resolved);
426
+ }
427
+ }
428
+ for (const spec of localSpecs) {
332
429
  if (!input.projectDirectory)
333
430
  throw contentError('ENVIRONMENT_SOURCE_MISSING');
334
431
  const relativeFile = spec.slice(5);
@@ -340,7 +437,7 @@ export class ManagedProjects {
340
437
  if (includesBase) {
341
438
  await withShippedCatalogArchive(baseRoot, undefined, async (file) => {
342
439
  const bytes = await readFile(file);
343
- const filename = `candidates/base-${createHash('sha256').update(bytes).digest('hex')}.tgz`;
440
+ const filename = `candidates/base-${sha256Hex(bytes)}.tgz`;
344
441
  await writeFile(join(directory, filename), bytes);
345
442
  dependencies[baseName] = 'file:' + filename;
346
443
  });
@@ -357,42 +454,14 @@ export class ManagedProjects {
357
454
  if (this.#options.offline && (source.kind === 'url' || source.kind === 'git'))
358
455
  throw contentError('INSTALL_SOURCE_OFFLINE');
359
456
  await input.onProgress?.({ stage: 'acquiring', completed: index, total: document.sources.length });
360
- const captured = await acquireSource(source, {
361
- destination: join(directory, 'source-' + index),
362
- signal,
363
- maxBytes: this.#options.maxBytes,
364
- maxFiles: this.#options.maxFiles,
365
- timeoutMs: this.#options.timeoutMs,
366
- });
367
- const manifest = JSON.parse(await readFile(join(captured.directory, 'package.json'), 'utf8'));
368
- if (manifest.name !== name)
369
- throw contentError('INSTALL_SOURCE_IDENTITY');
370
- if (typeof manifest.version !== 'string' || !valid(manifest.version))
371
- throw contentError('INSTALL_SOURCE_VERSION');
372
- checkDependencySpecs(manifest);
373
- try {
374
- checkLock(JSON.parse(await readFile(join(captured.directory, 'npm-shrinkwrap.json'), 'utf8')));
375
- }
376
- catch (error) {
377
- if (errorCode(error) !== 'ENOENT')
378
- throw error;
379
- }
380
- const output = join(directory, 'packed-' + index);
381
- await mkdir(output);
382
- await this.#npm(['pack', '--ignore-scripts', '--json', '--pack-destination', output, '--registry', this.#options.registry], captured.directory, signal);
383
- const { readdir } = await import('node:fs/promises');
384
- const files = await readdir(output);
385
- if (files.length !== 1 || !files[0].endsWith('.tgz'))
386
- throw contentError('INSTALL_PACK_INVALID');
387
- const archiveDigest = createHash('sha256')
388
- .update(await readFile(join(output, files[0])))
389
- .digest('hex');
457
+ const staged = join(directory, 'source-' + index);
458
+ const captured = await this.#packSource(source, staged, signal, name);
459
+ const archiveDigest = sha256Hex(await readFile(captured.file));
390
460
  const filename = `candidates/source-${index}-${archiveDigest}.tgz`;
391
- await rename(join(output, files[0]), join(directory, filename));
461
+ await rename(captured.file, join(directory, filename));
392
462
  dependencies[name] = 'file:' + filename;
393
463
  provenance[name] = captured.provenance;
394
- await rm(captured.directory, { recursive: true, force: true });
395
- await rm(output, { recursive: true, force: true });
464
+ await rm(staged, { recursive: true, force: true });
396
465
  }
397
466
  const savedManifest = input.projectDirectory
398
467
  ? JSON.parse(await readFile(join(input.projectDirectory, 'package.json'), 'utf8').catch(error => {
@@ -407,8 +476,26 @@ export class ManagedProjects {
407
476
  throw error;
408
477
  return undefined;
409
478
  });
410
- if (lock)
479
+ if (lock) {
411
480
  await writeFile(join(directory, 'package-lock.json'), lock);
481
+ // A captured installation is sufficient for rule edits and reuse after the source/cache is gone.
482
+ if (localSpecs.size &&
483
+ !document.sources.length &&
484
+ isDeepStrictEqual(savedManifest.dependencies, dependencies)) {
485
+ const locked = JSON.parse(lock.toString());
486
+ checkLock(locked, true);
487
+ if (isDeepStrictEqual(locked.packages['']?.dependencies, dependencies)) {
488
+ const inspection = await inspectNpmProject(input.projectDirectory).catch(() => undefined);
489
+ signal.throwIfAborted();
490
+ if (inspection) {
491
+ const captured = await mkdtemp(join(directory, 'captured-'));
492
+ const limits = { signal, maxBytes: this.#options.maxBytes, maxFiles: this.#options.maxFiles };
493
+ await writeMaterials(captured, await materialFiles(input.projectDirectory, limits), limits);
494
+ return await consume(captured, signal, document);
495
+ }
496
+ }
497
+ }
498
+ }
412
499
  }
413
500
  await writeFile(join(directory, 'package.json'), JSON.stringify({
414
501
  ...savedManifest,
@@ -430,7 +517,12 @@ export class ManagedProjects {
430
517
  this.#options.registry,
431
518
  ...(this.#options.offline ? ['--offline'] : []),
432
519
  ], directory, signal);
433
- checkLock(JSON.parse(await readFile(join(directory, 'package-lock.json'), 'utf8')), true);
520
+ const nextLock = JSON.parse(await readFile(join(directory, 'package-lock.json'), 'utf8'));
521
+ checkLock(nextLock, true);
522
+ const retained = new Set(Object.values(nextLock.packages).flatMap(entry => (isRecord(entry) ? [entry.resolved] : [])));
523
+ for (const file of await readdir(join(directory, 'candidates')))
524
+ if (!retained.has('file:candidates/' + file))
525
+ await rm(join(directory, 'candidates', file));
434
526
  await input.onProgress?.({ stage: 'installing' });
435
527
  await this.#npm([
436
528
  'ci',
@@ -1,3 +1,4 @@
1
+ import type { Readable } from 'node:stream';
1
2
  export type ManagedSource = {
2
3
  kind: 'archive';
3
4
  file: string;
@@ -16,13 +17,29 @@ export type ManagedSource = {
16
17
  };
17
18
  export interface SourceAcquisitionOptions {
18
19
  destination: string;
20
+ preserveNpmArchive?: boolean;
19
21
  signal?: AbortSignal;
20
22
  maxBytes?: number;
21
23
  maxFiles?: number;
22
24
  timeoutMs?: number;
23
25
  }
26
+ export declare function matchesArchiveIntegrity(bytes: Uint8Array, integrity: string): boolean;
27
+ interface BoundedOptions {
28
+ rejectPrivateFiles?: boolean;
29
+ signal: AbortSignal;
30
+ maxBytes: number;
31
+ maxFiles: number;
32
+ timeoutMs: number;
33
+ }
34
+ interface SourceFile {
35
+ name: string;
36
+ bytes: Buffer;
37
+ }
38
+ export declare function tarFiles(file: string | Readable, options: BoundedOptions): Promise<SourceFile[]>;
24
39
  /** Only trusted host configuration supplies descriptors. HTTP callers select registered source IDs. */
25
40
  export declare function acquireSource(source: ManagedSource, input: SourceAcquisitionOptions): Promise<{
26
41
  directory: string;
42
+ archive?: string;
27
43
  provenance: Record<string, string>;
28
44
  }>;
45
+ export {};
@@ -1,13 +1,20 @@
1
1
  import { execFile } from 'node:child_process';
2
- import { createHash } from 'node:crypto';
2
+ import { sha256, sha384, sha512 } from '@noble/hashes/sha2.js';
3
3
  import { constants, createReadStream } from 'node:fs';
4
- import { mkdir, open, readdir, lstat, realpath, rm, writeFile } from 'node:fs/promises';
4
+ import { mkdir, open, readdir, lstat, realpath, rename, rm, writeFile } from 'node:fs/promises';
5
5
  import { dirname, isAbsolute, join, relative } from 'node:path';
6
6
  import { promisify } from 'node:util';
7
7
  import { Parser } from 'tar';
8
8
  import yauzl from 'yauzl';
9
9
  import { errorCode, errorMessage, isRecord } from './types.js';
10
10
  const exec = promisify(execFile);
11
+ export function matchesArchiveIntegrity(bytes, integrity) {
12
+ const match = /^(sha256|sha384|sha512)-([A-Za-z0-9+/]+={0,2})$/.exec(integrity);
13
+ if (!match)
14
+ return false;
15
+ const hash = match[1] === 'sha256' ? sha256 : match[1] === 'sha384' ? sha384 : sha512;
16
+ return Buffer.from(hash(bytes)).toString('base64') === match[2];
17
+ }
11
18
  function fail(code) {
12
19
  throw Object.assign(new Error(code), { code });
13
20
  }
@@ -52,6 +59,8 @@ function boundedFiles(options) {
52
59
  function track(name) {
53
60
  options.signal.throwIfAborted();
54
61
  name = safePath(name);
62
+ if (options.rejectPrivateFiles && name.split('/').some(forbidden))
63
+ throw fail('SOURCE_PRIVATE_FILE');
55
64
  if (++entries > options.maxFiles)
56
65
  throw fail('SOURCE_LIMIT');
57
66
  const key = name.normalize('NFC').toLowerCase();
@@ -114,10 +123,10 @@ async function readBounded(stream, expectedSize, options) {
114
123
  abort();
115
124
  });
116
125
  }
117
- async function tarFiles(file, options) {
126
+ export async function tarFiles(file, options) {
118
127
  const output = boundedFiles(options);
119
128
  await new Promise((resolve, reject) => {
120
- const input = createReadStream(file);
129
+ const input = typeof file === 'string' ? createReadStream(file) : file;
121
130
  const parser = new Parser({ strict: true, maxMetaEntrySize: 64 * 1024, maxDecompressionRatio: 1000 });
122
131
  let failed = false;
123
132
  const stop = (error) => {
@@ -320,10 +329,7 @@ async function download(source, file, options) {
320
329
  const tokens = source.integrity.trim().split(/\s+/);
321
330
  if (!tokens.length || tokens.some((token) => !/^sha(?:256|384|512)-[A-Za-z0-9+/]+={0,2}$/.test(token)))
322
331
  throw fail('SOURCE_INTEGRITY_INVALID');
323
- if (!tokens.some((token) => {
324
- const [algorithm, expected] = token.split('-');
325
- return createHash(algorithm).update(bytes).digest('base64') === expected;
326
- }))
332
+ if (!tokens.some((token) => matchesArchiveIntegrity(bytes, token)))
327
333
  throw fail('SOURCE_INTEGRITY_MISMATCH');
328
334
  }
329
335
  await writeFile(file, bytes, { flag: 'wx', mode: 0o600 });
@@ -359,6 +365,7 @@ async function gitArchive(source, root, options) {
359
365
  SystemRoot: process.env.SystemRoot,
360
366
  HOME: root,
361
367
  GIT_CONFIG_NOSYSTEM: '1',
368
+ ...(process.env.GIT_SSL_CAINFO ? { GIT_SSL_CAINFO: process.env.GIT_SSL_CAINFO } : {}),
362
369
  GIT_CONFIG_GLOBAL: join(root, 'no-config'),
363
370
  GIT_TERMINAL_PROMPT: '0',
364
371
  };
@@ -466,6 +473,7 @@ export async function acquireSource(source, input) {
466
473
  try {
467
474
  const scratch = join(input.destination, '.source');
468
475
  await mkdir(scratch);
476
+ let original;
469
477
  let files, provenance;
470
478
  if (source.kind === 'directory') {
471
479
  if (typeof source.directory !== 'string' || !isAbsolute(source.directory))
@@ -502,7 +510,14 @@ export async function acquireSource(source, input) {
502
510
  finally {
503
511
  await handle.close();
504
512
  }
505
- files = magic.readUInt32LE(0) === 0x04034b50 ? await zipFiles(file, options) : await tarFiles(file, options);
513
+ if (magic.readUInt32LE(0) === 0x04034b50)
514
+ files = await zipFiles(file, options);
515
+ else {
516
+ const preserve = source.kind === 'url' && !!input.preserveNpmArchive && magic.readUInt16LE(0) === 0x8b1f;
517
+ files = await tarFiles(file, { ...options, rejectPrivateFiles: preserve });
518
+ if (preserve && files.every(entry => entry.name.startsWith('package/')))
519
+ original = file;
520
+ }
506
521
  }
507
522
  const packageFiles = files.filter(file => file.name === 'package.json' || file.name.endsWith('/package.json'));
508
523
  const rootManifest = packageFiles.find(file => file.name === 'package.json');
@@ -526,8 +541,11 @@ export async function acquireSource(source, input) {
526
541
  await mkdir(dirname(output), { recursive: true });
527
542
  await writeFile(output, file.bytes, { flag: 'wx', mode: 0o600 });
528
543
  }
544
+ const archive = original && prefix === 'package/' ? join(input.destination, 'verified.tgz') : undefined;
545
+ if (original && archive)
546
+ await rename(original, archive);
529
547
  await rm(scratch, { recursive: true, force: true });
530
- return { directory, provenance: { ...provenance, name: manifest.name, version: manifest.version } };
548
+ return { directory, archive, provenance: { ...provenance, name: manifest.name, version: manifest.version } };
531
549
  }
532
550
  catch (error) {
533
551
  await rm(input.destination, { recursive: true, force: true });