@altopelago/aeon-cli 0.12.0 → 0.13.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.
package/dist/main.js CHANGED
@@ -8,6 +8,11 @@
8
8
  * - aeon doctor Check environment and contract wiring
9
9
  * - aeon fmt [file] Format AEON document (stdout by default)
10
10
  * - aeon inspect <file> Inspect AEON document (human-readable)
11
+ * - aeon telex decode <file> Decode and validate Telex
12
+ * - aeon telex canonicalize <file> Canonicalize Telex
13
+ * - aeon telex materialize <file> Materialize complete Telex as JSON
14
+ * - aeon film decode <file> Decode and validate Film v1
15
+ * - aeon film materialize <file> Materialize complete Film v1 as JSON
11
16
  * - aeon finalize <file> Finalize AEON document to JSON
12
17
  * - aeon bind <file> Run typed runtime binding with schema JSON
13
18
  * - aeon integrity validate <file> Validate integrity envelope
@@ -16,6 +21,9 @@
16
21
  *
17
22
  * Flags:
18
23
  * - --json Output as JSON (inspect/finalize/integrity)
24
+ * - --portable-aes Emit the portable flat AES projection (inspect JSON only)
25
+ * - --source-provenance Bind portable origin/span to the exact inspected UTF-8 source
26
+ * - --telex Export the portable AES stream as Telex (inspect only)
19
27
  * - --contract-registry Trusted contract registry JSON path (doctor/bind)
20
28
  * - --write Write formatted output back to file (fmt only)
21
29
  * - --annotations Include annotation stream records in inspect/bind output
@@ -28,9 +36,13 @@
28
36
  * - --recovery Enable recovery mode (partial results with errors)
29
37
  * - --max-input-bytes Maximum UTF-8 input size in bytes
30
38
  * - --max-attribute-depth Maximum attribute selector depth
31
- * - --max-separator-depth Maximum separator-spec depth
39
+ * - --max-clarifier-values Maximum clarifier values per datatype descriptor
40
+ * - --max-separator-depth Deprecated alias for --max-clarifier-values
32
41
  * - --max-generic-depth Maximum nested generic type depth
33
- * - --max-nesting-depth Maximum container nesting depth
42
+ * - --max-generic-arguments Maximum generic arguments per datatype descriptor
43
+ * - --max-datatype-components Maximum components in one recursive datatype
44
+ * - --max-value-nesting-depth Maximum logical container nesting depth
45
+ * - --max-nesting-depth Deprecated alias for --max-value-nesting-depth
34
46
  * - --max-materialized-weight Maximum cumulative clone materialization weight
35
47
  * - --max-reference-depth Maximum clone resolution depth
36
48
  * - --schema Schema JSON path (bind only)
@@ -54,8 +66,9 @@ import * as path from 'node:path';
54
66
  import { fileURLToPath } from 'node:url';
55
67
  import { createHash } from 'node:crypto';
56
68
  import { canonicalize } from '@altopelago/aeon-canonical';
57
- import { compile, VERSION, formatPath } from '@altopelago/aeon-core';
58
- import { finalizeJson, finalizeMap } from '@altopelago/aeon-finalize';
69
+ import { adaptTypeScriptAssignmentEventsToPortableAes, aeonCompileLimits, aesStreamLimits, compile, exportTelex, finalizationLimits, loadAeonicLimits, telexLimits, VERSION, formatPath } from '@altopelago/aeon-core';
70
+ import { canonicalizeTelex, decodeFilm, DEFAULT_FILM_LIMITS, parseTelex, validateTelex } from '@altopelago/aeon-aes';
71
+ import { finalizeJson, finalizeMap, finalizePortableJson } from '@altopelago/aeon-finalize';
59
72
  import { buildCanonicalReceipt, computeCanonicalHash, computeByteHash, signStringPayload, validateEnvelopeEvents, verifyStringPayloadSignature, } from '@altopelago/aeon-integrity';
60
73
  import { tokenize } from '@altopelago/aeon-lexer';
61
74
  import { parse } from '@altopelago/aeon-parser';
@@ -96,6 +109,12 @@ switch (command) {
96
109
  case 'inspect':
97
110
  inspect(args.slice(1));
98
111
  break;
112
+ case 'telex':
113
+ telex(args.slice(1));
114
+ break;
115
+ case 'film':
116
+ film(args.slice(1));
117
+ break;
99
118
  case 'finalize':
100
119
  finalize(args.slice(1));
101
120
  break;
@@ -134,6 +153,11 @@ Commands:
134
153
  doctor Check environment and contract wiring
135
154
  fmt [file] Format AEON document (stdout by default)
136
155
  inspect <file> Inspect AEON document
156
+ telex decode <file> Decode and validate Telex as JSON
157
+ telex canonicalize <file> Canonicalize a Telex stream
158
+ telex materialize <file> Materialize complete Telex as JSON
159
+ film decode <file> Decode and validate Film v1 as JSON
160
+ film materialize <file> Materialize complete Film v1 as JSON
137
161
  finalize <file> Finalize AEON document to JSON
138
162
  bind <file> Run typed runtime binding with schema JSON
139
163
  integrity validate <file> Validate integrity envelope
@@ -144,6 +168,10 @@ Options:
144
168
  --write Write formatted output back to file (fmt only)
145
169
  --contract-registry Trusted contract registry JSON path (doctor/bind)
146
170
  --json Output as JSON (inspect/finalize)
171
+ --portable-aes Emit portable flat AES node projection (inspect JSON only)
172
+ --source-provenance Include exact-source origin/span (requires --portable-aes or --telex)
173
+ --telex Emit Telex instead of the inspect report
174
+ --include-headers Include AEON headers in the explicit document projection
147
175
  --annotations Include annotation stream records in inspect/bind output
148
176
  --annotations-only Output only annotation stream records in inspect output
149
177
  --sort-annotations Sort annotation records deterministically before output (inspect/bind)
@@ -154,9 +182,13 @@ Options:
154
182
  --recovery Enable recovery mode (partial results)
155
183
  --max-input-bytes Maximum UTF-8 input size in bytes
156
184
  --max-attribute-depth Maximum attribute selector depth
157
- --max-separator-depth Maximum separator-spec depth
185
+ --max-clarifier-values Maximum clarifier values per datatype descriptor
186
+ --max-separator-depth Deprecated alias for --max-clarifier-values
158
187
  --max-generic-depth Maximum nested generic type depth
159
- --max-nesting-depth Maximum container nesting depth
188
+ --max-generic-arguments Maximum generic arguments per datatype descriptor
189
+ --max-datatype-components Maximum components in one recursive datatype
190
+ --max-value-nesting-depth Maximum logical container nesting depth
191
+ --max-nesting-depth Deprecated alias for --max-value-nesting-depth
160
192
  --schema Schema JSON path (bind only)
161
193
  --profile Profile id (bind only)
162
194
  --contract-registry Trusted contract registry JSON path (bind only)
@@ -183,6 +215,12 @@ Examples:
183
215
  aeon fmt config.aeon --write
184
216
  aeon inspect config.aeon
185
217
  aeon inspect config.aeon --json
218
+ aeon inspect config.aeon --telex
219
+ aeon telex decode stream.telex.aes
220
+ aeon telex canonicalize stream.telex.aes
221
+ aeon telex materialize stream.telex.aes --scope full
222
+ aeon film decode stream.film.aes
223
+ aeon film materialize stream.film.aes --scope full
186
224
  aeon inspect config.aeon --json --annotations
187
225
  aeon inspect config.aeon --json --annotations-only
188
226
  aeon inspect config.aeon --json --annotations-only --sort-annotations
@@ -331,14 +369,167 @@ function fmt(args) {
331
369
  }
332
370
  process.stdout.write(formatted);
333
371
  }
372
+ /** Decode/validate or canonicalize an existing Telex stream. */
373
+ function telex(args) {
374
+ const action = args[0];
375
+ const file = args[1];
376
+ const usage = 'Usage: aeon telex <decode|canonicalize|materialize> <file> [--limits-file <path>] [--scope <payload|header|full>] [--strict|--transport] [--max-materialized-weight <n>] [--max-reference-depth <n>]';
377
+ if ((action !== 'decode' && action !== 'canonicalize' && action !== 'materialize') || !file || file.startsWith('--')) {
378
+ console.error(usage);
379
+ process.exit(2);
380
+ }
381
+ const mode = resolveFinalizeMode(args);
382
+ const scope = resolveFinalizeScope(args);
383
+ const maxMaterializedWeight = resolveDepthOption(args, '--max-materialized-weight');
384
+ const maxReferenceDepth = resolveDepthOption(args, '--max-reference-depth');
385
+ const limitsFile = getFlagValue(args, '--limits-file');
386
+ if (mode === null || scope === null || maxMaterializedWeight === null || maxReferenceDepth === null) {
387
+ console.error(usage);
388
+ process.exit(2);
389
+ }
390
+ if (args.includes('--limits-file') && !limitsFile) {
391
+ console.error('Error: --limits-file requires a path');
392
+ process.exit(2);
393
+ }
394
+ let selectedTelexLimits;
395
+ let selectedFinalizationLimits;
396
+ if (limitsFile) {
397
+ const loaded = loadAeonicLimits(fs.readFileSync(limitsFile, 'utf8'));
398
+ if (!loaded.limits) {
399
+ for (const error of loaded.errors)
400
+ console.error(`[${error.code}] ${error.path}: ${error.message}`);
401
+ process.exit(2);
402
+ }
403
+ try {
404
+ selectedTelexLimits = telexLimits(loaded.limits);
405
+ selectedFinalizationLimits = finalizationLimits(loaded.limits);
406
+ }
407
+ catch (error) {
408
+ console.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
409
+ process.exit(2);
410
+ }
411
+ }
412
+ const input = selectedTelexLimits
413
+ ? readFileWithLimit(file, selectedTelexLimits.maxInputBytes)
414
+ : readFile(file);
415
+ try {
416
+ if (action === 'canonicalize') {
417
+ process.stdout.write(canonicalizeTelex(input, selectedTelexLimits));
418
+ return;
419
+ }
420
+ const parsed = parseTelex(input, selectedTelexLimits);
421
+ const validation = validateTelex(parsed, {
422
+ ...selectedTelexLimits,
423
+ profile: parsed.profile,
424
+ projection: parsed.projection,
425
+ });
426
+ if (action === 'materialize') {
427
+ const finalized = finalizePortableJson(parsed.records, {
428
+ profile: parsed.profile,
429
+ projection: parsed.projection,
430
+ mode,
431
+ scope,
432
+ ...selectedTelexLimits,
433
+ ...selectedFinalizationLimits,
434
+ ...(maxMaterializedWeight !== undefined ? { maxMaterializedWeight } : {}),
435
+ ...(maxReferenceDepth !== undefined ? { maxReferenceDepth } : {}),
436
+ });
437
+ console.log(JSON.stringify(finalized, null, 2));
438
+ if (!validation.valid || (finalized.meta?.errors?.length ?? 0) > 0)
439
+ process.exit(1);
440
+ return;
441
+ }
442
+ console.log(JSON.stringify({ ...parsed, validation }, null, 2));
443
+ if (!validation.valid)
444
+ process.exit(1);
445
+ }
446
+ catch (error) {
447
+ const failure = error;
448
+ console.error(`[${failure.code ?? 'TELEX_SYNTAX_ERROR'}] ${failure.message ?? String(error)}`);
449
+ process.exit(1);
450
+ }
451
+ }
452
+ /** Decode/validate or materialize an existing Film v1 stream. */
453
+ function film(args) {
454
+ const action = args[0];
455
+ const file = args[1];
456
+ const usage = 'Usage: aeon film <decode|materialize> <file> [--limits-file <path>] [--scope <payload|header|full>] [--strict|--transport] [--max-materialized-weight <n>] [--max-reference-depth <n>]';
457
+ if ((action !== 'decode' && action !== 'materialize') || !file || file.startsWith('--')) {
458
+ console.error(usage);
459
+ process.exit(2);
460
+ }
461
+ const mode = resolveFinalizeMode(args);
462
+ const scope = resolveFinalizeScope(args);
463
+ const maxMaterializedWeight = resolveDepthOption(args, '--max-materialized-weight');
464
+ const maxReferenceDepth = resolveDepthOption(args, '--max-reference-depth');
465
+ const limitsFile = getFlagValue(args, '--limits-file');
466
+ if (mode === null || scope === null || maxMaterializedWeight === null || maxReferenceDepth === null) {
467
+ console.error(usage);
468
+ process.exit(2);
469
+ }
470
+ if (args.includes('--limits-file') && !limitsFile) {
471
+ console.error('Error: --limits-file requires a path');
472
+ process.exit(2);
473
+ }
474
+ let selectedAesLimits;
475
+ let selectedFinalizationLimits;
476
+ if (limitsFile) {
477
+ const loaded = loadAeonicLimits(fs.readFileSync(limitsFile, 'utf8'));
478
+ if (!loaded.limits) {
479
+ for (const error of loaded.errors)
480
+ console.error(`[${error.code}] ${error.path}: ${error.message}`);
481
+ process.exit(2);
482
+ }
483
+ try {
484
+ selectedAesLimits = aesStreamLimits(loaded.limits);
485
+ selectedFinalizationLimits = finalizationLimits(loaded.limits);
486
+ }
487
+ catch (error) {
488
+ console.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
489
+ process.exit(2);
490
+ }
491
+ }
492
+ const input = readBinaryFileWithLimit(file, DEFAULT_FILM_LIMITS.maxInputBytes);
493
+ try {
494
+ const decoded = decodeFilm(input, {
495
+ ...(selectedAesLimits !== undefined ? { aesLimits: selectedAesLimits } : {}),
496
+ });
497
+ if (action === 'materialize') {
498
+ const finalized = finalizePortableJson(decoded.records, {
499
+ profile: decoded.profile,
500
+ projection: decoded.projection,
501
+ mode,
502
+ scope,
503
+ ...selectedAesLimits,
504
+ ...selectedFinalizationLimits,
505
+ ...(maxMaterializedWeight !== undefined ? { maxMaterializedWeight } : {}),
506
+ ...(maxReferenceDepth !== undefined ? { maxReferenceDepth } : {}),
507
+ });
508
+ console.log(JSON.stringify(finalized, null, 2));
509
+ if ((finalized.meta?.errors?.length ?? 0) > 0)
510
+ process.exit(1);
511
+ return;
512
+ }
513
+ console.log(JSON.stringify(decoded, null, 2));
514
+ }
515
+ catch (error) {
516
+ const failure = error;
517
+ console.error(`[${failure.code ?? 'FILM_DECODE_ERROR'}] ${failure.message ?? String(error)}`);
518
+ process.exit(1);
519
+ }
520
+ }
334
521
  /**
335
522
  * aeon inspect <file> [--json] [--recovery] [--annotations] [--annotations-only] [--sort-annotations]
336
523
  * Purpose: human inspection (default) or JSON output
337
524
  */
338
525
  function inspect(args) {
339
- const inspectUsage = 'Usage: aeon inspect <file> [--json] [--recovery] [--strict|--transport] [--annotations] [--annotations-only] [--sort-annotations] [--datatype-policy <reserved_only|allow_custom>] [--max-input-bytes <n>] [--max-events <n>] [--max-attribute-depth <n>] [--max-separator-depth <n>] [--max-generic-depth <n>] [--max-nesting-depth <n>]';
340
- const file = findFileWithValueFlags(args, ['--datatype-policy', '--max-input-bytes', '--max-events', '--max-attribute-depth', '--max-separator-depth', '--max-generic-depth', '--max-nesting-depth']);
526
+ const inspectUsage = 'Usage: aeon inspect <file> [--json|--telex] [--portable-aes] [--source-provenance] [--include-headers] [--recovery] [--strict|--transport] [--annotations] [--annotations-only] [--sort-annotations] [--datatype-policy <reserved_only|allow_custom>] [--limits-file <path>] [--max-input-bytes <n>] [--max-events <n>] [--max-attribute-depth <n>] [--max-clarifier-values <n>] [--max-generic-depth <n>] [--max-generic-arguments <n>] [--max-datatype-components <n>] [--max-value-nesting-depth <n>]';
527
+ const file = findFileWithValueFlags(args, ['--datatype-policy', '--limits-file', '--max-input-bytes', '--max-events', '--max-attribute-depth', '--max-clarifier-values', '--max-separator-depth', '--max-generic-depth', '--max-generic-arguments', '--max-datatype-components', '--max-value-nesting-depth', '--max-nesting-depth']);
341
528
  const jsonOutput = args.includes('--json');
529
+ const telexOutput = args.includes('--telex');
530
+ const includeHeaders = args.includes('--include-headers');
531
+ const portableAes = args.includes('--portable-aes');
532
+ const sourceProvenance = args.includes('--source-provenance');
342
533
  const recovery = args.includes('--recovery');
343
534
  const annotationsOnly = args.includes('--annotations-only');
344
535
  const includeAnnotations = args.includes('--annotations');
@@ -348,14 +539,39 @@ function inspect(args) {
348
539
  const maxInputBytes = resolveMaxInputBytes(args);
349
540
  const maxEvents = resolveDepthOption(args, '--max-events');
350
541
  const maxAttributeDepth = resolveDepthOption(args, '--max-attribute-depth');
542
+ const maxClarifierValues = resolveDepthOption(args, '--max-clarifier-values');
351
543
  const maxSeparatorDepth = resolveDepthOption(args, '--max-separator-depth');
352
544
  const maxGenericDepth = resolveDepthOption(args, '--max-generic-depth');
545
+ const maxGenericArguments = resolveDepthOption(args, '--max-generic-arguments');
546
+ const maxDatatypeComponents = resolveDepthOption(args, '--max-datatype-components');
547
+ const maxValueNestingDepth = resolveDepthOption(args, '--max-value-nesting-depth');
353
548
  const maxNestingDepth = resolveDepthOption(args, '--max-nesting-depth');
549
+ const limitsFile = getFlagValue(args, '--limits-file');
354
550
  if (!file) {
355
551
  console.error('Error: No file specified');
356
552
  console.error(inspectUsage);
357
553
  process.exit(2);
358
554
  }
555
+ if (portableAes && !jsonOutput) {
556
+ console.error('Error: --portable-aes requires --json');
557
+ console.error(inspectUsage);
558
+ process.exit(2);
559
+ }
560
+ if (sourceProvenance && !portableAes && !telexOutput) {
561
+ console.error('Error: --source-provenance requires --portable-aes or --telex');
562
+ console.error(inspectUsage);
563
+ process.exit(2);
564
+ }
565
+ if (telexOutput && (jsonOutput || portableAes || annotationsOnly || includeAnnotations)) {
566
+ console.error('Error: --telex cannot be combined with JSON or annotation output flags');
567
+ console.error(inspectUsage);
568
+ process.exit(2);
569
+ }
570
+ if (includeHeaders && !telexOutput && !(jsonOutput && portableAes)) {
571
+ console.error('Error: --include-headers requires --telex or --json --portable-aes');
572
+ console.error(inspectUsage);
573
+ process.exit(2);
574
+ }
359
575
  if (args.includes('--datatype-policy') && !datatypePolicy) {
360
576
  console.error('Error: Invalid value for --datatype-policy (expected reserved_only or allow_custom)');
361
577
  console.error(inspectUsage);
@@ -378,6 +594,10 @@ function inspect(args) {
378
594
  console.error('Error: Invalid value for --max-attribute-depth (expected a non-negative integer)');
379
595
  process.exit(2);
380
596
  }
597
+ if (maxClarifierValues === null) {
598
+ console.error('Error: Invalid value for --max-clarifier-values (expected a non-negative integer)');
599
+ process.exit(2);
600
+ }
381
601
  if (maxSeparatorDepth === null) {
382
602
  console.error('Error: Invalid value for --max-separator-depth (expected a non-negative integer)');
383
603
  process.exit(2);
@@ -386,12 +606,46 @@ function inspect(args) {
386
606
  console.error('Error: Invalid value for --max-generic-depth (expected a non-negative integer)');
387
607
  process.exit(2);
388
608
  }
609
+ if (maxGenericArguments === null) {
610
+ console.error('Error: Invalid value for --max-generic-arguments (expected a non-negative integer)');
611
+ process.exit(2);
612
+ }
613
+ if (maxDatatypeComponents === null) {
614
+ console.error('Error: Invalid value for --max-datatype-components (expected a non-negative integer)');
615
+ process.exit(2);
616
+ }
617
+ if (maxValueNestingDepth === null) {
618
+ console.error('Error: Invalid value for --max-value-nesting-depth (expected a non-negative integer)');
619
+ process.exit(2);
620
+ }
389
621
  if (maxNestingDepth === null) {
390
622
  console.error('Error: Invalid value for --max-nesting-depth (expected a non-negative integer)');
391
623
  process.exit(2);
392
624
  }
393
- const input = readFileWithLimit(file, maxInputBytes);
625
+ let limitsOptions;
626
+ if (args.includes('--limits-file')) {
627
+ if (!limitsFile) {
628
+ console.error('Error: --limits-file requires a path');
629
+ process.exit(2);
630
+ }
631
+ const loaded = loadAeonicLimits(fs.readFileSync(limitsFile, 'utf8'));
632
+ if (!loaded.limits) {
633
+ for (const error of loaded.errors)
634
+ console.error(`[${error.code}] ${error.path}: ${error.message}`);
635
+ process.exit(2);
636
+ }
637
+ try {
638
+ limitsOptions = aeonCompileLimits(loaded.limits);
639
+ }
640
+ catch (error) {
641
+ console.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
642
+ process.exit(2);
643
+ }
644
+ }
645
+ const effectiveInputLimit = maxInputBytes ?? limitsOptions?.maxInputBytes;
646
+ const input = readFileWithLimit(file, effectiveInputLimit);
394
647
  const result = compile(input, {
648
+ ...limitsOptions,
395
649
  recovery,
396
650
  emitAnnotations: includeAnnotations || annotationsOnly,
397
651
  ...(effectiveMode ? { mode: effectiveMode } : {}),
@@ -399,14 +653,34 @@ function inspect(args) {
399
653
  ...(maxInputBytes !== undefined ? { maxInputBytes } : {}),
400
654
  ...(maxEvents !== undefined ? { maxEvents } : {}),
401
655
  ...(maxAttributeDepth !== undefined ? { maxAttributeDepth } : {}),
402
- ...(maxSeparatorDepth !== undefined ? { maxSeparatorDepth } : {}),
656
+ ...(maxClarifierValues !== undefined
657
+ ? { maxClarifierValues }
658
+ : maxSeparatorDepth !== undefined ? { maxSeparatorDepth } : {}),
403
659
  ...(maxGenericDepth !== undefined ? { maxGenericDepth } : {}),
404
- ...(maxNestingDepth !== undefined ? { maxNestingDepth } : {}),
660
+ ...(maxGenericArguments !== undefined ? { maxGenericArguments } : {}),
661
+ ...(maxDatatypeComponents !== undefined ? { maxDatatypeComponents } : {}),
662
+ ...(maxValueNestingDepth !== undefined
663
+ ? { maxValueNestingDepth }
664
+ : maxNestingDepth !== undefined ? { maxNestingDepth } : {}),
405
665
  });
406
666
  const headerInfo = extractHeaderInfo(input);
407
667
  const mode = headerInfo.mode;
408
- if (jsonOutput) {
409
- outputJSON(result, { includeAnnotations, annotationsOnly, sortAnnotations }, headerInfo);
668
+ if (telexOutput && result.errors.length === 0) {
669
+ process.stdout.write(exportTelex(result.events, {
670
+ includeHeaders,
671
+ headerFieldNames: result.header ? [...result.header.fields.keys()] : [],
672
+ ...(sourceProvenance ? { sourceBytes: Buffer.from(input, 'utf8') } : {}),
673
+ }));
674
+ }
675
+ else if (jsonOutput) {
676
+ outputJSON(result, {
677
+ includeAnnotations,
678
+ annotationsOnly,
679
+ sortAnnotations,
680
+ portableAes,
681
+ includeHeaders,
682
+ ...(sourceProvenance ? { sourceBytes: Buffer.from(input, 'utf8') } : {}),
683
+ }, headerInfo);
410
684
  }
411
685
  else {
412
686
  outputMarkdown(file, result, {
@@ -431,8 +705,8 @@ function inspect(args) {
431
705
  * Purpose: finalize AES into JSON output
432
706
  */
433
707
  function finalize(args) {
434
- const finalizeUsage = 'Usage: aeon finalize <file> [--json|--map] [--recovery] [--strict|--transport] [--projected] [--include-path <$.path>] [--scope <payload|header|full>] [--datatype-policy <reserved_only|allow_custom>] [--max-input-bytes <n>] [--max-materialized-weight <n>] [--max-reference-depth <n>]';
435
- const file = findFileWithValueFlags(args, ['--datatype-policy', '--include-path', '--scope', '--max-input-bytes', '--max-materialized-weight', '--max-reference-depth']);
708
+ const finalizeUsage = 'Usage: aeon finalize <file> [--json|--map] [--recovery] [--strict|--transport] [--projected] [--include-path <$.path>] [--scope <payload|header|full>] [--datatype-policy <reserved_only|allow_custom>] [--limits-file <path>] [--max-input-bytes <n>] [--max-materialized-weight <n>] [--max-reference-depth <n>]';
709
+ const file = findFileWithValueFlags(args, ['--datatype-policy', '--include-path', '--scope', '--limits-file', '--max-input-bytes', '--max-materialized-weight', '--max-reference-depth']);
436
710
  const recovery = args.includes('--recovery');
437
711
  const mode = resolveFinalizeMode(args);
438
712
  const effectiveMode = resolveCoreMode(args);
@@ -442,6 +716,7 @@ function finalize(args) {
442
716
  const maxInputBytes = resolveMaxInputBytes(args);
443
717
  const maxMaterializedWeight = resolveDepthOption(args, '--max-materialized-weight');
444
718
  const maxReferenceDepth = resolveDepthOption(args, '--max-reference-depth');
719
+ const limitsFile = getFlagValue(args, '--limits-file');
445
720
  const includePaths = getFlagValues(args, '--include-path');
446
721
  const projected = args.includes('--projected') || includePaths.length > 0;
447
722
  if (!file) {
@@ -475,6 +750,10 @@ function finalize(args) {
475
750
  console.error('Error: Invalid value for --max-reference-depth (expected a non-negative integer)');
476
751
  process.exit(2);
477
752
  }
753
+ if (args.includes('--limits-file') && !limitsFile) {
754
+ console.error('Error: --limits-file requires a path');
755
+ process.exit(2);
756
+ }
478
757
  if (args.includes('--include-path') && includePaths.length === 0) {
479
758
  console.error('Error: Missing value for --include-path <$.path>');
480
759
  console.error(finalizeUsage);
@@ -485,14 +764,35 @@ function finalize(args) {
485
764
  console.error(finalizeUsage);
486
765
  process.exit(2);
487
766
  }
488
- const input = readFileWithLimit(file, maxInputBytes);
767
+ let limitsOptions;
768
+ let finalizeLimitOptions;
769
+ if (limitsFile) {
770
+ const loaded = loadAeonicLimits(fs.readFileSync(limitsFile, 'utf8'));
771
+ if (!loaded.limits) {
772
+ for (const error of loaded.errors)
773
+ console.error(`[${error.code}] ${error.path}: ${error.message}`);
774
+ process.exit(2);
775
+ }
776
+ try {
777
+ limitsOptions = aeonCompileLimits(loaded.limits);
778
+ finalizeLimitOptions = finalizationLimits(loaded.limits);
779
+ }
780
+ catch (error) {
781
+ console.error(`Error: ${error instanceof Error ? error.message : String(error)}`);
782
+ process.exit(2);
783
+ }
784
+ }
785
+ const effectiveInputLimit = maxInputBytes ?? limitsOptions?.maxInputBytes;
786
+ const input = readFileWithLimit(file, effectiveInputLimit);
489
787
  const result = compile(input, {
788
+ ...limitsOptions,
490
789
  recovery,
491
790
  ...(effectiveMode ? { mode: effectiveMode } : {}),
492
791
  ...(datatypePolicy ? { datatypePolicy } : {}),
493
792
  ...(maxInputBytes !== undefined ? { maxInputBytes } : {}),
494
793
  });
495
794
  const finalizeOptions = {
795
+ ...finalizeLimitOptions,
496
796
  mode,
497
797
  scope,
498
798
  ...(result.header ? { header: result.header } : {}),
@@ -876,7 +1176,7 @@ function integrityVerify(args) {
876
1176
  diagnostics.errors.push({
877
1177
  level: 'error',
878
1178
  code: 'ENVELOPE_HASH_MISMATCH',
879
- message: 'canonical_hash does not match computed AES hash',
1179
+ message: 'canonical_hash does not match computed legacy AEON canonical hash',
880
1180
  });
881
1181
  }
882
1182
  }
@@ -1206,8 +1506,14 @@ function integritySign(args) {
1206
1506
  /**
1207
1507
  * JSON output (--json flag)
1208
1508
  */
1509
+ function isLegacyVisibleEvent(event) {
1510
+ if (event.sourcePlane !== undefined) {
1511
+ return event.sourcePlane !== 'header' || event.path.segments.length > 2;
1512
+ }
1513
+ return !event.key.startsWith('aeon:');
1514
+ }
1209
1515
  function outputJSON(result, options, headerInfo) {
1210
- const visibleEvents = result.events.filter(e => !e.key.startsWith('aeon:'));
1516
+ const visibleEvents = result.events.filter(isLegacyVisibleEvent);
1211
1517
  const annotations = options.sortAnnotations
1212
1518
  ? sortAnnotationRecords(result.annotations ?? [])
1213
1519
  : (result.annotations ?? []);
@@ -1216,14 +1522,22 @@ function outputJSON(result, options, headerInfo) {
1216
1522
  return;
1217
1523
  }
1218
1524
  const output = {
1219
- events: visibleEvents.map(event => ({
1220
- path: formatPath(event.path),
1221
- key: event.key,
1222
- datatype: event.datatype ?? null,
1223
- span: event.span,
1224
- // Preserve AST-like shape (no coercion/inference)
1225
- value: jsonSafe(event.value),
1226
- })),
1525
+ events: options.portableAes
1526
+ ? [...adaptTypeScriptAssignmentEventsToPortableAes(options.includeHeaders ? result.events : visibleEvents, {
1527
+ includeHeaders: options.includeHeaders,
1528
+ headerFieldNames: result.header ? [...result.header.fields.keys()] : [],
1529
+ ...(options.sourceBytes === undefined ? {} : { sourceBytes: options.sourceBytes }),
1530
+ }).events]
1531
+ : visibleEvents.map(event => ({
1532
+ path: formatPath(event.path),
1533
+ key: event.key,
1534
+ ...(event.structuralId !== undefined ? { structuralId: event.structuralId } : {}),
1535
+ datatype: event.datatype ?? null,
1536
+ span: event.span,
1537
+ // Preserve AST-like shape (no coercion/inference)
1538
+ value: jsonSafe(event.value),
1539
+ ...(event.annotations ? { annotations: jsonSafe(event.annotations) } : {}),
1540
+ })),
1227
1541
  errors: result.errors.map(error => ({
1228
1542
  code: error.code,
1229
1543
  path: getErrorPath(error) ?? '$',
@@ -1234,6 +1548,9 @@ function outputJSON(result, options, headerInfo) {
1234
1548
  message: error.message,
1235
1549
  })),
1236
1550
  };
1551
+ if (options.portableAes && options.includeHeaders) {
1552
+ output.projection = 'aeon.document.v1';
1553
+ }
1237
1554
  if (options.includeAnnotations) {
1238
1555
  output.annotations = annotations;
1239
1556
  }
@@ -1357,7 +1674,8 @@ function finalizeMapOutput(result, options) {
1357
1674
  function entryToJson(entry) {
1358
1675
  return {
1359
1676
  path: entry.path,
1360
- value: entry.value,
1677
+ ...(entry.structuralId !== undefined ? { structuralId: entry.structuralId } : {}),
1678
+ value: jsonSafe(entry.value),
1361
1679
  span: entry.span,
1362
1680
  ...(entry.datatype ? { datatype: entry.datatype } : {}),
1363
1681
  ...(entry.annotations ? { annotations: mapAnnotations(entry.annotations) } : {}),
@@ -1367,8 +1685,10 @@ function mapAnnotations(annotations) {
1367
1685
  const entries = {};
1368
1686
  for (const [key, value] of annotations.entries()) {
1369
1687
  entries[key] = {
1370
- value: value.value,
1688
+ ...(value.structuralId !== undefined ? { structuralId: value.structuralId } : {}),
1689
+ value: jsonSafe(value.value),
1371
1690
  ...(value.datatype ? { datatype: value.datatype } : {}),
1691
+ ...(value.annotations ? { annotations: mapAnnotations(value.annotations) } : {}),
1372
1692
  };
1373
1693
  }
1374
1694
  return entries;
@@ -1653,6 +1973,23 @@ function readFileWithLimit(file, maxInputBytes) {
1653
1973
  enforceInputByteLimitOrExit(input, maxInputBytes);
1654
1974
  return input;
1655
1975
  }
1976
+ function readBinaryFileWithLimit(file, maxInputBytes) {
1977
+ try {
1978
+ const stats = fs.statSync(file);
1979
+ if (maxInputBytes !== undefined && stats.isFile() && stats.size > maxInputBytes) {
1980
+ failInputByteLimit(stats.size, maxInputBytes);
1981
+ }
1982
+ const input = fs.readFileSync(file);
1983
+ if (maxInputBytes !== undefined && input.byteLength > maxInputBytes) {
1984
+ failInputByteLimit(input.byteLength, maxInputBytes);
1985
+ }
1986
+ return input;
1987
+ }
1988
+ catch (err) {
1989
+ console.error(`Error: Cannot read file: ${file}`);
1990
+ process.exit(2);
1991
+ }
1992
+ }
1656
1993
  function readStdin(maxInputBytes) {
1657
1994
  try {
1658
1995
  if (maxInputBytes === undefined) {
@@ -1786,7 +2123,10 @@ function readSchemaContractAeonFile(file, expectedSchemaId) {
1786
2123
  }
1787
2124
  process.exit(2);
1788
2125
  }
1789
- const finalized = finalizeJson(compiled.events, { mode: 'strict' });
2126
+ const finalized = finalizeJson(compiled.events, {
2127
+ mode: 'strict',
2128
+ ...(compiled.header ? { header: compiled.header } : {}),
2129
+ });
1790
2130
  if ((finalized.meta?.errors?.length ?? 0) > 0) {
1791
2131
  console.error(`Error: Schema contract AEON file failed to finalize: ${file}`);
1792
2132
  for (const error of finalized.meta?.errors ?? []) {
@@ -2512,7 +2852,7 @@ function outputEnvelopeJson(errors, warnings, ok, verification, receipt) {
2512
2852
  console.log(JSON.stringify(payload, null, 2));
2513
2853
  }
2514
2854
  function outputMarkdown(file, result, info) {
2515
- const visibleEvents = result.events.filter(e => !e.key.startsWith('aeon:'));
2855
+ const visibleEvents = result.events.filter(isLegacyVisibleEvent);
2516
2856
  const annotations = info.sortAnnotations
2517
2857
  ? sortAnnotationRecords(result.annotations ?? [])
2518
2858
  : (result.annotations ?? []);
@@ -2671,18 +3011,23 @@ function inferPhaseLabelFromCode(code) {
2671
3011
  case 'UNTERMINATED_BLOCK_COMMENT':
2672
3012
  case 'UNTERMINATED_STRING':
2673
3013
  case 'UNTERMINATED_TRIMTICK':
3014
+ case 'INVALID_STRUCTURAL_IDENTITY':
2674
3015
  return 'Lexical Analysis';
2675
3016
  case 'SYNTAX_ERROR':
2676
3017
  case 'INVALID_DATE':
2677
3018
  case 'INVALID_TIME':
2678
3019
  case 'INVALID_DATETIME':
2679
3020
  case 'INVALID_SEPARATOR_CHAR':
3021
+ case 'CLARIFIER_VALUES_EXCEEDED':
3022
+ case 'GENERIC_ARGUMENTS_EXCEEDED':
3023
+ case 'DATATYPE_COMPONENTS_EXCEEDED':
2680
3024
  case 'SEPARATOR_DEPTH_EXCEEDED':
2681
3025
  case 'GENERIC_DEPTH_EXCEEDED':
2682
3026
  return 'Parsing';
2683
3027
  case 'HEADER_CONFLICT':
2684
3028
  case 'DUPLICATE_KEY':
2685
3029
  case 'DUPLICATE_CANONICAL_PATH':
3030
+ case 'DUPLICATE_STRUCTURAL_IDENTITY':
2686
3031
  case 'DATATYPE_LITERAL_MISMATCH':
2687
3032
  return 'Core Validation';
2688
3033
  case 'MISSING_REFERENCE_TARGET':