mnemonica 1.2.0 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/.ai/ONBOARDING.md +1 -1
  2. package/.ai/PROTOTYPE-CHAIN.md +15 -3
  3. package/.ai/rules-async-constructors.md +81 -1
  4. package/.ai/rules-define-patterns.md +39 -0
  5. package/.ai/rules-error-system.md +10 -0
  6. package/.ai/rules-instance-methods.md +1 -1
  7. package/AGENTS.md +14 -1
  8. package/CONTRIBUTING.md +4 -18
  9. package/FOR_HUMANS.md +128 -23
  10. package/README.md +44 -9
  11. package/SKILL.md +7 -2
  12. package/build/api/errors/exceptionConstructor.js +20 -47
  13. package/build/api/errors/index.js +7 -5
  14. package/build/api/errors/throwModificationError.js +26 -61
  15. package/build/api/index.d.ts +1 -0
  16. package/build/api/index.js +2 -1
  17. package/build/api/types/InstanceCreator.js +20 -5
  18. package/build/api/types/Mnemosyne.d.ts +3 -14
  19. package/build/api/types/Mnemosyne.js +21 -8
  20. package/build/api/types/Props.js +24 -11
  21. package/build/api/types/TypeProxy.d.ts +1 -1
  22. package/build/api/types/TypeProxy.js +9 -9
  23. package/build/api/types/compileNewModificatorFunctionBody.js +3 -5
  24. package/build/api/types/index.d.ts +5 -0
  25. package/build/api/types/index.js +70 -31
  26. package/build/api/utils/index.d.ts +1 -0
  27. package/build/api/utils/index.js +13 -3
  28. package/build/descriptors/types/index.js +46 -2
  29. package/build/index.d.ts +6 -2
  30. package/build/index.js +18 -3
  31. package/build/types/index.d.ts +31 -8
  32. package/build/types/index.js +1 -1
  33. package/build/utils/parent.js +35 -5
  34. package/docs/async-constructors.md +3 -1
  35. package/docs/empathy-in-ai.md +2 -3
  36. package/docs/prototype-chain.md +4 -0
  37. package/docs/typed-lookup.md +19 -2
  38. package/package.json +8 -8
  39. package/src/api/errors/exceptionConstructor.ts +40 -83
  40. package/src/api/errors/index.ts +10 -4
  41. package/src/api/errors/throwModificationError.ts +64 -104
  42. package/src/api/index.ts +2 -8
  43. package/src/api/types/InstanceCreator.ts +42 -6
  44. package/src/api/types/Mnemosyne.ts +34 -15
  45. package/src/api/types/Props.ts +57 -22
  46. package/src/api/types/TypeProxy.ts +10 -9
  47. package/src/api/types/compileNewModificatorFunctionBody.ts +12 -4
  48. package/src/api/types/index.ts +122 -65
  49. package/src/api/utils/index.ts +27 -9
  50. package/src/descriptors/types/index.ts +81 -3
  51. package/src/index.ts +66 -0
  52. package/src/types/index.ts +120 -15
  53. package/src/utils/parent.ts +48 -4
@@ -13,6 +13,10 @@ export type PropsType = Record<string, unknown>;
13
13
  // supply Args to get typed constructor params: IDEF<MyType, [string, number]>
14
14
  export type IDEF<T, Args extends unknown[] = unknown[]> = { new(): T } | { (this: T, ...args: Args): void };
15
15
 
16
+ // Lazy getter type - a zero-arg factory that returns a constructor.
17
+ // Used by the explicit .lazy() API.
18
+ export type LazyDef<T, Args extends unknown[] = unknown[]> = () => IDEF<T, Args>;
19
+
16
20
  // Error message types - all error messages are strings
17
21
  export type ErrorMessageKey =
18
22
  | 'BASE_ERROR_MESSAGE'
@@ -26,8 +30,8 @@ export type ErrorMessageKey =
26
30
  | 'MISSING_HOOK_CALLBACK'
27
31
  | 'MISSING_CALLBACK_ARGUMENT'
28
32
  | 'OPTIONS_ERROR'
29
- | 'WRONG_STACK_CLEANER';
30
- // | 'TYPENAME_MUST_BE_A_STRING'
33
+ | 'WRONG_STACK_CLEANER'
34
+ | 'TYPENAME_MUST_BE_A_STRING';
31
35
  // | 'FLOW_CHECKER_REDEFINITION'
32
36
 
33
37
  // Error messages object type
@@ -50,11 +54,21 @@ export interface MnemonicaErrorConstructor {
50
54
  // Errors types map - indexable record of error constructors
51
55
  export type ErrorsTypesMap = Record<string, MnemonicaErrorConstructor>;
52
56
 
53
- // Mnemonica error interface for extended Error objects
54
- export interface MnemonicaError extends Error {
55
- exceptionReason?: Error;
56
- reasons?: Error[];
57
- surplus?: Error[];
57
+ // Mnemonica-processed error. It carries no custom properties of its own:
58
+ // the error data lives in the external props storage (WeakMap) — read it
59
+ // via getProps(error), typed as ErrorProps below.
60
+ export type MnemonicaError = Error;
61
+
62
+ // Error-instance data props, stored externally (see api/types/Props.ts)
63
+ // and exposed through getProps(error) — never defined on the error object
64
+ export interface ErrorProps {
65
+ args?: unknown[];
66
+ originalError?: Error;
67
+ instance?: object;
68
+ exceptionReason?: unknown;
69
+ reasons?: unknown[];
70
+ surplus?: unknown[];
71
+ error?: unknown;
58
72
  }
59
73
 
60
74
  /**
@@ -581,6 +595,31 @@ export interface RegistryHolderBase<
581
595
  T & Record<ChildPath, StoredConstructor<F, ChildPath>>,
582
596
  ChildPath
583
597
  >;
598
+
599
+ // Explicit lazy getter: .lazy(() => Constructor, config?)
600
+ lazy<SubType extends object>(
601
+ this: RegistryHolderBase<T, Parent, Path>,
602
+ getter: LazyDef<SubType>,
603
+ config?: constructorOptions
604
+ ): IDefinitorInstance<SubType>;
605
+
606
+ // Named lazy getter: .lazy(TypeName, () => Constructor, config?)
607
+ lazy<
608
+ const Name extends string,
609
+ N extends object,
610
+ F extends Proto<Parent, N> = Proto<Parent, N>,
611
+ ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
612
+ >(
613
+ this: RegistryHolderBase<T, Parent, Path>,
614
+ TypeName: Name,
615
+ getter: LazyDef<N>,
616
+ config?: constructorOptions
617
+ ): IDefinitorInstance<
618
+ F,
619
+ InstanceResult<F>,
620
+ T & Record<ChildPath, StoredConstructor<F, ChildPath>>,
621
+ ChildPath
622
+ >;
584
623
  }
585
624
 
586
625
  // Full registry holder - adds typed .lookup() for collections that are not
@@ -624,6 +663,8 @@ export interface IDefinitorInstance<
624
663
 
625
664
  lookup: TypeLookup<Registry>;
626
665
 
666
+ decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
667
+
627
668
  registerHook(hookType: hooksTypes, cb: hook): void;
628
669
 
629
670
  subtypes: SubtypesMap;
@@ -652,6 +693,47 @@ export interface TypeAbsorber extends CallableFunction {
652
693
  ): IDefinitorInstance<T>;
653
694
  }
654
695
 
696
+ // Lazy absorber function type - used for explicit lazy getter definitions
697
+ export interface LazyAbsorber extends CallableFunction {
698
+ <T extends object>(
699
+ this: unknown,
700
+ getter: LazyDef<T>,
701
+ config?: constructorOptions
702
+ ): IDefinitorInstance<T>;
703
+ <T extends object>(
704
+ this: unknown,
705
+ TypeName: string,
706
+ getter: LazyDef<T>,
707
+ config?: constructorOptions
708
+ ): IDefinitorInstance<T>;
709
+ <T extends object>(
710
+ this: unknown,
711
+ arg1: string | LazyDef<T>,
712
+ arg2: LazyDef<T> | constructorOptions,
713
+ arg3?: constructorOptions
714
+ ): IDefinitorInstance<T>;
715
+ <T extends object>(
716
+ this: unknown,
717
+ source: RegistryHolderBase<object, object, string>,
718
+ getter: LazyDef<T>,
719
+ config?: constructorOptions
720
+ ): IDefinitorInstance<T>;
721
+ <T extends object>(
722
+ this: unknown,
723
+ source: RegistryHolderBase<object, object, string>,
724
+ TypeName: string,
725
+ getter: LazyDef<T>,
726
+ config?: constructorOptions
727
+ ): IDefinitorInstance<T>;
728
+ <T extends object>(
729
+ this: unknown,
730
+ source: RegistryHolderBase<object, object, string>,
731
+ arg1: string | LazyDef<T>,
732
+ arg2: LazyDef<T> | constructorOptions,
733
+ arg3?: constructorOptions
734
+ ): IDefinitorInstance<T>;
735
+ }
736
+
655
737
  // TypesCollection interface for createTypesCollection
656
738
  // This represents the actual return type of createTypesCollection
657
739
  export interface TypesCollection<
@@ -661,6 +743,16 @@ export interface TypesCollection<
661
743
  >
662
744
  extends RegistryHolder<T, Parent, Path>, Hookable {
663
745
  subtypes: SubtypesMap;
746
+
747
+ // Decorator factory: .decorate() or .decorate(config)
748
+ // Returns a class decorator that registers the class in this collection's registry.
749
+ decorate(
750
+ config?: constructorOptions
751
+ ): <U extends Constructor<object>>(cstr: U) => DecoratedClass<
752
+ U,
753
+ T & Record<ConstructorName<U>, StoredConstructor<InstanceType<U>, ConstructorName<U>>>
754
+ >;
755
+
664
756
  [key: string]: unknown;
665
757
  }
666
758
 
@@ -702,12 +794,21 @@ export interface TypeDescriptorDefine extends CallableFunction {
702
794
  ): TypeClass;
703
795
  }
704
796
 
797
+ export interface TypeDescriptorLazy extends CallableFunction {
798
+ (
799
+ getter: CallableFunction,
800
+ config?: object
801
+ ): TypeClass;
802
+ }
803
+
705
804
  export interface TypeDescriptorLookup extends CallableFunction {
706
805
  (TypeNestedPath: string): TypeClass | undefined;
707
806
  }
708
807
 
709
808
  export type TypeDescriptorInstance = {
710
809
  define: TypeDescriptorDefine;
810
+ lazy: TypeDescriptorLazy;
811
+ decorate: (config?: constructorOptions) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
711
812
  lookup: TypeDescriptorLookup;
712
813
  subtypes: Map<string, object>;
713
814
  TypeName: string;
@@ -723,18 +824,22 @@ export type ConstructorName<T extends Constructor<object>> =
723
824
 
724
825
  // Decorated class type - includes call signature for decorator pattern and
725
826
  // aligns with `IDefinitorInstance` so decorated classes expose the same
726
- // constructor/lookup surface as builder-created types. It still relies on the
727
- // global `TypeRegistry` for typed lookups because decorators cannot carry a
728
- // local accumulating registry across independent class declarations.
729
- export type DecoratedClass<T extends Constructor<object>> =
827
+ // constructor/lookup surface as builder-created types.
828
+ // `Registry` defaults to the global `TypeRegistry`; for custom collections it
829
+ // is the collection's local registry so `lookup()` and nested `.decorate()`
830
+ // stay inside that collection.
831
+ export type DecoratedClass<
832
+ T extends Constructor<object>,
833
+ Registry extends object = TypeRegistry
834
+ > =
730
835
  T &
731
836
  Omit<
732
- IDefinitorInstance<InstanceType<T>, InstanceType<T>, TypeRegistry, ConstructorName<T>>,
837
+ IDefinitorInstance<InstanceType<T>, InstanceType<T>, Registry, ConstructorName<T>>,
733
838
  'define' | 'lookup'
734
839
  > &
735
- (<U extends Constructor<object>>(target: U) => DecoratedClass<U>) & {
840
+ (<U extends Constructor<object>>(target: U) => DecoratedClass<U, Registry>) & {
736
841
  define: TypeAbsorber;
737
- lookup: TypeLookup<TypeRegistry>;
842
+ lookup: TypeLookup<Registry>;
738
843
  };
739
844
 
740
845
  // Helper: merge parent entity (E) and child constructor instance (T) into a
@@ -809,7 +914,7 @@ export interface MnemonicaModule<Registry extends object = {}>
809
914
  decorate: <T extends Constructor<object> | constructorOptions | undefined = undefined>(
810
915
  target?: T,
811
916
  config?: constructorOptions
812
- ) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U>;
917
+ ) => <U extends Constructor<object>>(cstr: U) => DecoratedClass<U, Registry>;
813
918
  registerHook: <T extends object>(Constructor: IDEF<T>, hookType: hooksTypes, cb: hook) => void;
814
919
 
815
920
  // Descriptors
@@ -40,12 +40,56 @@ export function parent <T extends object> (instance: T, path?: string): object |
40
40
  return p;
41
41
  }
42
42
 
43
- const { constructor: { name } } = p;
43
+ const segments = path.split('.');
44
+ const last = segments.length - 1;
44
45
 
45
46
  // seek throuh parent instances
46
47
  // about the fist constructor with this name
47
- const result = name === path ?
48
- p : parent( p as object, path );
49
- return result;
48
+ // dotted paths must match contiguously upwards:
49
+ // each leading segment must be the direct parent
50
+ // of the instance matched by the next one,
51
+ // and the instance itself is never a candidate
52
+ let current = p as object;
53
+ for ( ;; ) {
54
+
55
+ const { constructor: { name } } = current as { constructor: { name: string } };
56
+
57
+ if ( name === segments[ last ] ) {
58
+
59
+ if ( last === 0 ) {
60
+ return current;
61
+ }
62
+
63
+ let ancestor = current;
64
+ let matched = true;
65
+ for ( let i = last - 1; i >= 0; i-- ) {
66
+ const ancestorProps = _getProps( ancestor ) as Props | undefined;
67
+ if ( !ancestorProps ) {
68
+ matched = false;
69
+ break;
70
+ }
71
+ ancestor = ancestorProps.__parent__ as object;
72
+ const { constructor: { name: ancestorName } } = ancestor as { constructor: { name: string } };
73
+ if ( ancestorName !== segments[ i ] ) {
74
+ matched = false;
75
+ break;
76
+ }
77
+ }
78
+
79
+ if ( matched ) {
80
+ return current;
81
+ }
82
+
83
+ }
84
+
85
+ // every props-bearing instance chains up to an object,
86
+ // the props-less root instance terminates the scan
87
+ const currentProps = _getProps( current ) as Props | undefined;
88
+ if ( !currentProps ) {
89
+ return;
90
+ }
91
+ current = currentProps.__parent__ as object;
92
+
93
+ }
50
94
 
51
95
  }