mnemonica 1.0.9 → 1.2.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/.ai/AGENTS.md +31 -18
- package/.ai/DEBUG.md +7 -8
- package/.ai/ONBOARDING.md +23 -6
- package/.ai/PROTOTYPE-CHAIN.md +21 -0
- package/.ai/{ask/AGENTS.md → mode-ask.md} +1 -1
- package/.ai/{orchestrator/AGENTS.md → mode-orchestrator.md} +2 -19
- package/{docs → .ai}/performance-vs-security.md +1 -1
- package/.ai/{rules-skill/async-constructors.md → rules-async-constructors.md} +66 -0
- package/.ai/{rules/CODING.md → rules-coding.md} +19 -2
- package/.ai/{rules/CONTEXT-CONDENSING.md → rules-context-condensing.md} +2 -2
- package/.ai/{rules-skill/contributing.md → rules-contributing.md} +35 -0
- package/.ai/{rules-skill/instance-methods.md → rules-instance-methods.md} +1 -1
- package/.ai/rules-reminders.md +19 -0
- package/.ai/{rules-skill/type-system.md → rules-type-system.md} +1 -1
- package/{docs → .ai}/theory-of-operations.md +4 -4
- package/AGENTS.md +96 -24
- package/FOR_HUMANS.md +32 -76
- package/README.md +35 -20
- package/SKILL.md +20 -20
- package/build/api/index.d.ts +2 -2
- package/build/api/types/index.js +8 -3
- package/build/descriptors/types/index.js +2 -2
- package/build/index.d.ts +10 -3
- package/build/index.js +17 -5
- package/build/types/index.d.ts +60 -21
- package/build/types/index.js +1 -1
- package/docs/ai-learning-trajectory.md +2 -3
- package/docs/decorate.md +123 -0
- package/docs/hott-primer.md +1 -1
- package/docs/purpose.md +3 -3
- package/{.ai/TACTICA-DEEP-DIVE.md → docs/tactica-deep-dive.md} +45 -2
- package/docs/tactica-pattern.md +1 -2
- package/docs/typed-lookup.md +337 -93
- package/package.json +6 -6
- package/src/api/types/index.ts +14 -4
- package/src/descriptors/types/index.ts +63 -58
- package/src/index.ts +103 -12
- package/src/types/index.ts +241 -41
- package/.ai/CODE.md +0 -111
- package/.ai/TACTICA-RULES.md +0 -103
- package/.ai/async_init.md +0 -94
- package/.ai/rules/REMINDERS.md +0 -63
- package/.ai/rules-skill/lookup-typed.md +0 -82
- package/.ai/rules-skill/proxy-architecture.md +0 -63
- package/.ai/task-templates/new-feature.md +0 -46
- /package/.ai/{rules-skill/code-style.md → rules-code-style.md} +0 -0
- /package/.ai/{rules-skill/define-patterns.md → rules-define-patterns.md} +0 -0
- /package/.ai/{rules-skill/ecosystem.md → rules-ecosystem.md} +0 -0
- /package/.ai/{rules-skill/error-system.md → rules-error-system.md} +0 -0
- /package/.ai/{rules-skill/hooks.md → rules-hooks.md} +0 -0
- /package/.ai/{rules-skill/philosophy.md → rules-philosophy.md} +0 -0
- /package/.ai/{rules-skill/testing.md → rules-testing.md} +0 -0
package/src/types/index.ts
CHANGED
|
@@ -253,10 +253,139 @@ export type CollectionDef = Hookable & {
|
|
|
253
253
|
};
|
|
254
254
|
|
|
255
255
|
// Type lookup function type: may have augmentation by Tactica re-definition
|
|
256
|
-
|
|
257
|
-
|
|
256
|
+
// Global registry shape used as the default for TypeLookup. It combines any
|
|
257
|
+
// augmented TypeRegistry entries with a broad string index signature so the
|
|
258
|
+
// generic TypeLookup constraint is satisfied.
|
|
259
|
+
export type GlobalRegistry = TypeRegistry & Record<string, TypeConstructorBase>;
|
|
260
|
+
|
|
261
|
+
// Extract the instance type returned by a mnemonica-compatible constructor.
|
|
262
|
+
// Falls back to `object` so generic helpers that require an object constraint
|
|
263
|
+
// do not fail when the constructor shape is not yet narrowed.
|
|
264
|
+
export type ExtractConstructorInstance<C> =
|
|
265
|
+
C extends { new (...args: unknown[]): infer I }
|
|
266
|
+
? I extends object ? I : object
|
|
267
|
+
: object;
|
|
268
|
+
|
|
269
|
+
// Map dotted registry paths that extend `${Path}.${Child}` into child
|
|
270
|
+
// constructor properties on the parent instance. This lets typed lookups
|
|
271
|
+
// return a constructor whose instances carry their subtypes, e.g.
|
|
272
|
+
// `new (App.lookup('User'))(...).Admin`.
|
|
273
|
+
export type SubTypeConstructors<
|
|
274
|
+
Registry extends object,
|
|
275
|
+
Path extends string
|
|
276
|
+
> = {
|
|
277
|
+
[K in keyof Registry as K extends `${Path}.${infer Child}` ? Child : never]:
|
|
278
|
+
LookupResult<Registry, K & string>;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
// Reconstruct a constructor with a new instance type while preserving every
|
|
282
|
+
// non-`prototype`, non-`lookup` property (`define`, hooks, etc.). `lookup` is
|
|
283
|
+
// replaced separately so it can be scoped to the constructor's own type path.
|
|
284
|
+
export type ReplaceConstructorInstance<
|
|
285
|
+
C,
|
|
286
|
+
NewInstance extends object
|
|
287
|
+
> = C extends {
|
|
288
|
+
new (...args: infer A): unknown;
|
|
289
|
+
(...args: infer A2): unknown;
|
|
290
|
+
readonly prototype: unknown;
|
|
291
|
+
}
|
|
292
|
+
? {
|
|
293
|
+
new (...args: A): NewInstance;
|
|
294
|
+
(this: NewInstance, ...args: A2): NewInstance;
|
|
295
|
+
readonly prototype: NewInstance & {
|
|
296
|
+
readonly constructor: ReplaceConstructorInstance<C, NewInstance>;
|
|
297
|
+
};
|
|
298
|
+
} & Omit<C, 'prototype' | 'lookup'>
|
|
299
|
+
: never;
|
|
300
|
+
|
|
301
|
+
// Instance type produced by a typed lookup: the constructor's own instance
|
|
302
|
+
// shape plus child-constructor properties for every registered subtype.
|
|
303
|
+
// Kept as a named alias so hover tooltips show the base shape first instead of
|
|
304
|
+
// expanding the whole registry.
|
|
305
|
+
export type WithSubTypes<
|
|
306
|
+
Instance extends object,
|
|
307
|
+
Registry extends object,
|
|
308
|
+
Path extends string
|
|
309
|
+
> = Instance & SubTypeConstructors<Registry, Path>;
|
|
310
|
+
|
|
311
|
+
// Constructor returned by a typed lookup, with its instance type augmented by
|
|
312
|
+
// the subtypes registered under that path. Kept as a named alias so the
|
|
313
|
+
// recursive reference from `SubTypeConstructors` resolves cleanly.
|
|
314
|
+
export type AugmentedConstructor<
|
|
315
|
+
Registry extends object,
|
|
316
|
+
Path extends keyof Registry & string
|
|
317
|
+
> = ReplaceConstructorInstance<
|
|
318
|
+
Registry[Path],
|
|
319
|
+
WithSubTypes<ExtractConstructorInstance<Registry[Path]>, Registry, Path>
|
|
320
|
+
>;
|
|
321
|
+
|
|
322
|
+
// Result of a typed lookup: the augmented constructor plus a `lookup` method
|
|
323
|
+
// scoped to the constructor's own type path for relative subtype lookups.
|
|
324
|
+
export type LookupResult<
|
|
325
|
+
Registry extends object,
|
|
326
|
+
Path extends keyof Registry & string
|
|
327
|
+
> = Registry[Path] extends TypeConstructorBase
|
|
328
|
+
? AugmentedConstructor<Registry, Path> & {
|
|
329
|
+
lookup: NestedTypeLookup<Registry, Path>;
|
|
330
|
+
}
|
|
331
|
+
: never;
|
|
332
|
+
|
|
333
|
+
export interface TypeLookup<T extends object = GlobalRegistry> extends CallableFunction {
|
|
334
|
+
<const K extends keyof T & string>(this: unknown, TypeNestedPath: K): LookupResult<T, K>;
|
|
335
|
+
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
258
336
|
}
|
|
259
337
|
|
|
338
|
+
// Extracts the accumulated registry map from a builder-chain value
|
|
339
|
+
// (IDefinitorInstance, TypesCollection, or MnemonicaModule). Enables the
|
|
340
|
+
// one-line bridge that merges a builder's local registry into the global
|
|
341
|
+
// TypeRegistry without Tactica:
|
|
342
|
+
//
|
|
343
|
+
// declare module 'mnemonica' {
|
|
344
|
+
// interface TypeRegistry extends RegistryOf<typeof App> {}
|
|
345
|
+
// }
|
|
346
|
+
export type RegistryOf<T> =
|
|
347
|
+
T extends IDefinitorInstance<object, InstanceResult<object>, infer Registry, string> ? Registry
|
|
348
|
+
: T extends TypesCollection<infer Registry, object, string> ? Registry
|
|
349
|
+
: T extends MnemonicaModule<infer Registry> ? Registry
|
|
350
|
+
: never;
|
|
351
|
+
|
|
352
|
+
// Relative keys of a registry under a given dotted path. For a root path (`''`)
|
|
353
|
+
// this is all string keys; for a non-empty path it is the tail after `${Path}.`.
|
|
354
|
+
export type RelativeKeys<
|
|
355
|
+
Registry extends object,
|
|
356
|
+
Path extends string
|
|
357
|
+
> = Path extends ''
|
|
358
|
+
? keyof Registry & string
|
|
359
|
+
: (keyof Registry extends infer K
|
|
360
|
+
? K extends `${Path}.${infer Child}` ? Child : never
|
|
361
|
+
: never);
|
|
362
|
+
|
|
363
|
+
// Full registry key that corresponds to a relative key `K` under `Path`.
|
|
364
|
+
export type FullKey<
|
|
365
|
+
Registry extends object,
|
|
366
|
+
Path extends string,
|
|
367
|
+
K extends string
|
|
368
|
+
> = Path extends '' ? K : Extract<keyof Registry & string, `${Path}.${K}`>;
|
|
369
|
+
|
|
370
|
+
// Lookup function type for constructors and registry holders that know their
|
|
371
|
+
// own dotted type path. Root holders use `Path = ''` and behave like the global
|
|
372
|
+
// `TypeLookup`; child constructors use their own path for relative lookups.
|
|
373
|
+
export type NestedTypeLookup<
|
|
374
|
+
Registry extends object,
|
|
375
|
+
Path extends string = ''
|
|
376
|
+
> = (Path extends ''
|
|
377
|
+
? {
|
|
378
|
+
<const K extends keyof Registry & string>(this: unknown, TypeNestedPath: K): LookupResult<Registry, K>;
|
|
379
|
+
}
|
|
380
|
+
: {
|
|
381
|
+
<const K extends RelativeKeys<Registry, Path> & string>(
|
|
382
|
+
this: unknown,
|
|
383
|
+
TypeNestedPath: K
|
|
384
|
+
): LookupResult<Registry, Extract<keyof Registry & string, `${Path}.${K}`>>;
|
|
385
|
+
}) & {
|
|
386
|
+
(this: unknown, TypeNestedPath: string): TypeClass | undefined;
|
|
387
|
+
};
|
|
388
|
+
|
|
260
389
|
// Specification for chained subtype creation with .then()
|
|
261
390
|
export interface ThenSpec {
|
|
262
391
|
subtype: object;
|
|
@@ -297,7 +426,12 @@ export interface InstanceCreatorContext {
|
|
|
297
426
|
*
|
|
298
427
|
* Result: child T gets all its own props, plus parent's non-overlapping props.
|
|
299
428
|
*/
|
|
300
|
-
|
|
429
|
+
// When the parent contributes no properties, collapse to the child type so
|
|
430
|
+
// hover tooltips stay readable.
|
|
431
|
+
export type Proto<P extends object, T extends object> =
|
|
432
|
+
[Exclude<keyof P, keyof T>] extends [never]
|
|
433
|
+
? T
|
|
434
|
+
: T & Pick<P, Exclude<keyof P, keyof T>>;
|
|
301
435
|
|
|
302
436
|
|
|
303
437
|
// export type ProtoFlat<
|
|
@@ -403,13 +537,75 @@ export type InstanceResult<
|
|
|
403
537
|
N extends object,
|
|
404
538
|
> = { [K in keyof N]: N[K] };
|
|
405
539
|
|
|
540
|
+
// Lightweight constructor type stored inside a typed registry map.
|
|
541
|
+
// It preserves the dotted `Path` so that `.define()` on a looked-up constructor
|
|
542
|
+
// computes the correct child path, but it omits the full `GlobalRegistry`
|
|
543
|
+
// generic to keep hover tooltips readable.
|
|
544
|
+
export type StoredConstructor<
|
|
545
|
+
F extends object,
|
|
546
|
+
Path extends string = ''
|
|
547
|
+
> = _Internal_TC_<F> & RegistryHolderBase<{}, F, Path>;
|
|
548
|
+
|
|
549
|
+
// Registry holder base - provides the accumulating .define() method.
|
|
550
|
+
// `Parent` tracks the instance type of the last defined constructor, so chained
|
|
551
|
+
// subtypes get the correct merged `Proto<Parent, N>` instance type.
|
|
552
|
+
// `Path` is the dotted type path of the holder (empty for root holders).
|
|
553
|
+
export interface RegistryHolderBase<
|
|
554
|
+
T extends object = {},
|
|
555
|
+
Parent extends object = object,
|
|
556
|
+
Path extends string = ''
|
|
557
|
+
> {
|
|
558
|
+
// Legacy overload: define(constructHandler, config?)
|
|
559
|
+
define<SubType extends object>(
|
|
560
|
+
this: RegistryHolderBase<T, Parent, Path>,
|
|
561
|
+
TypeOrTypeName: CallableFunction,
|
|
562
|
+
constructHandlerOrConfig?: IDEF<SubType> | object | boolean | CallableFunction,
|
|
563
|
+
configOrUndefined?: constructorOptions | CallableFunction | boolean
|
|
564
|
+
): IDefinitorInstance<SubType>;
|
|
565
|
+
|
|
566
|
+
// Modern overload: define(TypeName, constructHandler, config?)
|
|
567
|
+
define<
|
|
568
|
+
const Name extends string,
|
|
569
|
+
N extends object,
|
|
570
|
+
Args extends unknown[],
|
|
571
|
+
F extends Proto<Parent, N> = Proto<Parent, N>,
|
|
572
|
+
ChildPath extends string = Path extends '' ? Name : `${Path}.${Name}`
|
|
573
|
+
>(
|
|
574
|
+
this: RegistryHolderBase<T, Parent, Path>,
|
|
575
|
+
TypeName: Name,
|
|
576
|
+
constructHandler?: IDEF<N, Args>,
|
|
577
|
+
config?: constructorOptions
|
|
578
|
+
): IDefinitorInstance<
|
|
579
|
+
F,
|
|
580
|
+
InstanceResult<F>,
|
|
581
|
+
T & Record<ChildPath, StoredConstructor<F, ChildPath>>,
|
|
582
|
+
ChildPath
|
|
583
|
+
>;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// Full registry holder - adds typed .lookup() for collections that are not
|
|
587
|
+
// also constructors (createTypesCollection, the mnemonica module object).
|
|
588
|
+
export interface RegistryHolder<
|
|
589
|
+
T extends object = {},
|
|
590
|
+
Parent extends object = object,
|
|
591
|
+
Path extends string = ''
|
|
592
|
+
>
|
|
593
|
+
extends RegistryHolderBase<T, Parent, Path> {
|
|
594
|
+
lookup: NestedTypeLookup<T, Path>;
|
|
595
|
+
}
|
|
596
|
+
|
|
406
597
|
// Definitor instance - the constructor function returned by define
|
|
407
598
|
// N = instance type (properties available on instances)
|
|
408
|
-
//
|
|
599
|
+
// R = wrapped result instance type
|
|
600
|
+
// Registry = typed registry map for lookup()
|
|
601
|
+
// Path = dotted type path of this constructor (empty for root types)
|
|
409
602
|
export interface IDefinitorInstance<
|
|
410
603
|
N extends object,
|
|
411
|
-
R extends InstanceResult<N> = InstanceResult<N
|
|
412
|
-
|
|
604
|
+
R extends InstanceResult<N> = InstanceResult<N>,
|
|
605
|
+
Registry extends object = GlobalRegistry,
|
|
606
|
+
Path extends string = ''
|
|
607
|
+
>
|
|
608
|
+
extends RegistryHolderBase<Registry, N, Path> {
|
|
413
609
|
|
|
414
610
|
TypeName: string;
|
|
415
611
|
prototype: N;
|
|
@@ -426,27 +622,12 @@ export interface IDefinitorInstance<
|
|
|
426
622
|
// the line below should make is a @decorate decorator working
|
|
427
623
|
(...args: unknown[]): IDefinitorInstance<R>;
|
|
428
624
|
|
|
625
|
+
lookup: TypeLookup<Registry>;
|
|
429
626
|
|
|
430
|
-
// Define method that combines parent N with new type T using Proto
|
|
431
|
-
define<
|
|
432
|
-
T extends object,
|
|
433
|
-
|
|
434
|
-
// this will be instance type for sub-type !!!
|
|
435
|
-
// F extends Flatten<Proto<N, T>>
|
|
436
|
-
F extends Proto<N, T>
|
|
437
|
-
>(
|
|
438
|
-
TypeOrTypeName: string | CallableFunction,
|
|
439
|
-
constructHandlerOrConfig?: IDEF<T> | object | boolean | CallableFunction,
|
|
440
|
-
configOrUndefined?: constructorOptions | CallableFunction | boolean
|
|
441
|
-
): IDefinitorInstance<F>;
|
|
442
|
-
|
|
443
|
-
lookup: TypeLookup;
|
|
444
|
-
|
|
445
627
|
registerHook(hookType: hooksTypes, cb: hook): void;
|
|
446
|
-
|
|
447
|
-
|
|
628
|
+
|
|
448
629
|
subtypes: SubtypesMap;
|
|
449
|
-
|
|
630
|
+
|
|
450
631
|
// Internal properties accessed by tests
|
|
451
632
|
__type__?: TypeDef;
|
|
452
633
|
collection?: CollectionDef;
|
|
@@ -473,9 +654,12 @@ export interface TypeAbsorber extends CallableFunction {
|
|
|
473
654
|
|
|
474
655
|
// TypesCollection interface for createTypesCollection
|
|
475
656
|
// This represents the actual return type of createTypesCollection
|
|
476
|
-
export interface TypesCollection
|
|
477
|
-
|
|
478
|
-
|
|
657
|
+
export interface TypesCollection<
|
|
658
|
+
T extends object = {},
|
|
659
|
+
Parent extends object = object,
|
|
660
|
+
Path extends string = ''
|
|
661
|
+
>
|
|
662
|
+
extends RegistryHolder<T, Parent, Path>, Hookable {
|
|
479
663
|
subtypes: SubtypesMap;
|
|
480
664
|
[key: string]: unknown;
|
|
481
665
|
}
|
|
@@ -489,11 +673,18 @@ export interface Hookable {
|
|
|
489
673
|
}
|
|
490
674
|
|
|
491
675
|
// createTypesCollection function type
|
|
492
|
-
export type CreateTypesCollectionFunction =
|
|
676
|
+
export type CreateTypesCollectionFunction =
|
|
677
|
+
<T extends object = {}, Parent extends object = object>(
|
|
678
|
+
config?: constructorOptions
|
|
679
|
+
) => TypesCollection<T, Parent>;
|
|
493
680
|
|
|
494
681
|
// Type class - base type constructor
|
|
495
682
|
export type TypeClass = IDefinitorInstance<object>;
|
|
496
683
|
|
|
684
|
+
// Generic registry map used by typed collections and the mnemonica builder.
|
|
685
|
+
// Keys are dotted type paths; values are mnemonica-compatible constructors.
|
|
686
|
+
export type TypeRegistryMap = Record<string, TypeConstructorBase>;
|
|
687
|
+
|
|
497
688
|
// Mnemonica constructor — the constructor function returned by constructHandler()
|
|
498
689
|
// Has SymbolConstructorName attached and is both newable and callable
|
|
499
690
|
export interface MnemonicaConstructor extends NewableFunction {
|
|
@@ -520,19 +711,30 @@ export type TypeDescriptorInstance = {
|
|
|
520
711
|
lookup: TypeDescriptorLookup;
|
|
521
712
|
subtypes: Map<string, object>;
|
|
522
713
|
TypeName: string;
|
|
714
|
+
collection: CollectionDef;
|
|
523
715
|
};
|
|
524
716
|
|
|
525
717
|
// Constructor type for decorate function
|
|
526
718
|
export type Constructor<T = object> = new (...args: unknown[]) => T;
|
|
527
719
|
|
|
528
|
-
//
|
|
720
|
+
// Name of a constructor as a string literal type.
|
|
721
|
+
export type ConstructorName<T extends Constructor<object>> =
|
|
722
|
+
T extends { name: infer N } ? N extends string ? N : string : string;
|
|
723
|
+
|
|
724
|
+
// Decorated class type - includes call signature for decorator pattern and
|
|
725
|
+
// 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.
|
|
529
729
|
export type DecoratedClass<T extends Constructor<object>> =
|
|
530
730
|
T &
|
|
731
|
+
Omit<
|
|
732
|
+
IDefinitorInstance<InstanceType<T>, InstanceType<T>, TypeRegistry, ConstructorName<T>>,
|
|
733
|
+
'define' | 'lookup'
|
|
734
|
+
> &
|
|
531
735
|
(<U extends Constructor<object>>(target: U) => DecoratedClass<U>) & {
|
|
532
736
|
define: TypeAbsorber;
|
|
533
|
-
|
|
534
|
-
lookup: TypeLookup;
|
|
535
|
-
TypeName: string;
|
|
737
|
+
lookup: TypeLookup<TypeRegistry>;
|
|
536
738
|
};
|
|
537
739
|
|
|
538
740
|
// Helper: merge parent entity (E) and child constructor instance (T) into a
|
|
@@ -595,14 +797,12 @@ export interface UtilsCollection {
|
|
|
595
797
|
[key: string]: CallableFunction;
|
|
596
798
|
}
|
|
597
799
|
|
|
598
|
-
// Main mnemonica module interface - represents the exported module object
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
<const K extends keyof TypeRegistry>(TypeNestedPath: K): TypeRegistry[K];
|
|
605
|
-
};
|
|
800
|
+
// Main mnemonica module interface - represents the exported module object.
|
|
801
|
+
// The optional Registry generic lets the object act as a typed registry holder
|
|
802
|
+
// when chaining .define() calls.
|
|
803
|
+
export interface MnemonicaModule<Registry extends object = {}>
|
|
804
|
+
extends RegistryHolder<Registry, object> {
|
|
805
|
+
// Core functions not covered by RegistryHolder
|
|
606
806
|
apply: ApplyFunction;
|
|
607
807
|
call: CallFunction;
|
|
608
808
|
bind: BindFunction;
|
|
@@ -613,7 +813,7 @@ export interface MnemonicaModule {
|
|
|
613
813
|
registerHook: <T extends object>(Constructor: IDEF<T>, hookType: hooksTypes, cb: hook) => void;
|
|
614
814
|
|
|
615
815
|
// Descriptors
|
|
616
|
-
defaultTypes: TypesCollection
|
|
816
|
+
defaultTypes: TypesCollection<Registry>;
|
|
617
817
|
|
|
618
818
|
// Errors
|
|
619
819
|
BASE_MNEMONICA_ERROR: MnemonicaErrorConstructor;
|
package/.ai/CODE.md
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
# Coding Standards — mnemonica/core
|
|
2
|
-
|
|
3
|
-
> **Applies to:** All code changes. Framework-agnostic.
|
|
4
|
-
> See also `.ai/rules/CODING.md` for extended coding rules.
|
|
5
|
-
|
|
6
|
-
---
|
|
7
|
-
|
|
8
|
-
## Build & Test Commands
|
|
9
|
-
|
|
10
|
-
See [`rules-skill/testing.md`](./rules-skill/testing.md) for the full reference. Summary:
|
|
11
|
-
```bash
|
|
12
|
-
npm run build # full build with linting
|
|
13
|
-
npm run test:cov # Mocha + coverage (runs build:all internally)
|
|
14
|
-
npm run test:jest:cov # Jest on TypeScript source
|
|
15
|
-
npm run watch # watch mode
|
|
16
|
-
```
|
|
17
|
-
**Must run `npm run test:cov` before completing any task.**
|
|
18
|
-
|
|
19
|
-
---
|
|
20
|
-
|
|
21
|
-
## Code Style Rules
|
|
22
|
-
|
|
23
|
-
See [`rules-skill/code-style.md`](./rules-skill/code-style.md) for the full style guide (tabs, function spacing, aligned colons, TypeScript strictness, no `any`).
|
|
24
|
-
|
|
25
|
-
The return-statement rule is design-critical and is documented in full in [`../AGENTS.md`](../AGENTS.md#return-statement-design-rule). Summary: **always use an intermediate variable before returning** so Chrome DevTools can inspect the value at a breakpoint.
|
|
26
|
-
|
|
27
|
-
---
|
|
28
|
-
|
|
29
|
-
## TypeScript Type Rules
|
|
30
|
-
|
|
31
|
-
**Never use bare `Function`, `CallableFunction`, or `NewableFunction` as parameter or return types.** Define a purpose-specific interface that extends them. See [`rules-skill/code-style.md`](./rules-skill/code-style.md) for examples and allowed exceptions.
|
|
32
|
-
|
|
33
|
-
---
|
|
34
|
-
|
|
35
|
-
## Build Output Inspection
|
|
36
|
-
|
|
37
|
-
When running `npm run build` or `npm run build:all`, **check the beginning of the output** for errors and warnings. Build failures (TypeScript compilation errors, ESLint issues, etc.) often appear at the start of the output. Do not rely only on the end of the output or `tail` for build status.
|
|
38
|
-
|
|
39
|
-
For test passing confirmations (e.g., `npm run test:cov`), checking the end of the output is acceptable.
|
|
40
|
-
|
|
41
|
-
---
|
|
42
|
-
|
|
43
|
-
## Type Patterns
|
|
44
|
-
|
|
45
|
-
### Proxy Handlers
|
|
46
|
-
```typescript
|
|
47
|
-
type ProxyHandler<T> = {
|
|
48
|
-
get?(target: T, prop: string, receiver: unknown): unknown;
|
|
49
|
-
set?(target: T, prop: string, value: unknown, receiver: unknown): boolean;
|
|
50
|
-
};
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
### Internal Instance Properties
|
|
54
|
-
```typescript
|
|
55
|
-
interface MnemonicaInstance {
|
|
56
|
-
[SymbolConstructorName]?: string;
|
|
57
|
-
[SymbolParentType]?: object;
|
|
58
|
-
[SymbolSubTypes]?: Map<string, object>;
|
|
59
|
-
}
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
---
|
|
63
|
-
|
|
64
|
-
## Testing Requirements
|
|
65
|
-
|
|
66
|
-
See [`rules-skill/testing.md`](./rules-skill/testing.md) for full details. 100% coverage required on both Mocha and Jest. Jest tests should mirror Mocha patterns from `test/environment.js`.
|
|
67
|
-
|
|
68
|
-
---
|
|
69
|
-
|
|
70
|
-
## Symbols Reference
|
|
71
|
-
|
|
72
|
-
See [`AGENTS.md`](./AGENTS.md) for the complete symbol reference table.
|
|
73
|
-
|
|
74
|
-
---
|
|
75
|
-
|
|
76
|
-
## Adding Type Definitions
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
// 1. Define type in src/types/index.ts
|
|
80
|
-
export interface NewTypeConfig {
|
|
81
|
-
property: string;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// 2. Update existing interfaces if needed
|
|
85
|
-
export interface MnemonicaInstance {
|
|
86
|
-
newProperty?: NewTypeConfig;
|
|
87
|
-
}
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
---
|
|
91
|
-
|
|
92
|
-
## Error Handling
|
|
93
|
-
|
|
94
|
-
```typescript
|
|
95
|
-
// Use MnemonicaErrorConstructor for custom errors
|
|
96
|
-
import { constructError } from '../api/errors/index.js';
|
|
97
|
-
|
|
98
|
-
const MyError = constructError('MY_ERROR', 'Error message');
|
|
99
|
-
throw new MyError('additional info', stack);
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
---
|
|
103
|
-
|
|
104
|
-
## Configuration Files
|
|
105
|
-
|
|
106
|
-
**Disallowed without explicit approval:**
|
|
107
|
-
- Modifying `./tsconfig.json`
|
|
108
|
-
- Modifying `./eslint.config.js`
|
|
109
|
-
|
|
110
|
-
These configuration files define the project's strict standards. Any changes
|
|
111
|
-
require user approval first.
|
package/.ai/TACTICA-RULES.md
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
# mnemonica + `lookup`: rules and the underlying pattern
|
|
2
|
-
|
|
3
|
-
> **The real rule:** use `lookup()` plus an augmented `TypeRegistry` — never `import { X } from './collections/...'` plus `as unknown as` casts. The augmentation can be hand-written or generated by [`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica). Tactica is the productivity tool; the **augmentation** is the requirement. For the full mechanism and a side-by-side example, see [`../docs/typed-lookup.md`](../docs/typed-lookup.md).
|
|
4
|
-
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
## What this file is
|
|
8
|
-
|
|
9
|
-
The anti-patterns that show up when working with a mnemonica project — and what to do instead. The rules apply regardless of whether you use tactica or hand-augment `TypeRegistry`; the runtime is identical either way.
|
|
10
|
-
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
## Why the type-safe path matters
|
|
14
|
-
|
|
15
|
-
mnemonica creates a Trie of constructors at runtime:
|
|
16
|
-
|
|
17
|
-
```
|
|
18
|
-
RequestData
|
|
19
|
-
└── RouteData
|
|
20
|
-
└── PageData
|
|
21
|
-
└── RenderData
|
|
22
|
-
└── ResponseData
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
At runtime, this works perfectly — `new requestData.RouteData({ ... })` just works.
|
|
26
|
-
|
|
27
|
-
But TypeScript does not see this Trie. The `define()` function returns a constructor TypeScript understands as a single function — not as something that grows `.RouteData`, `.PageData`, etc. on each instance.
|
|
28
|
-
|
|
29
|
-
The temptation is to cast around the gap:
|
|
30
|
-
|
|
31
|
-
```typescript
|
|
32
|
-
// ❌ never
|
|
33
|
-
const requestData = new RequestData({ ... }) as unknown as RequestDataT;
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
That bypasses the type system. The right answer is to teach TypeScript what the Trie looks like — which is exactly what `lookup()` + a `TypeRegistry` augmentation does.
|
|
37
|
-
|
|
38
|
-
---
|
|
39
|
-
|
|
40
|
-
## The mechanism in one paragraph
|
|
41
|
-
|
|
42
|
-
`mnemonica` exports an empty `interface TypeRegistry {}`. `lookup<const K extends keyof TypeRegistry>(path: K)` returns `TypeRegistry[K]`. So a `declare module 'mnemonica' { interface TypeRegistry { ... } }` augmentation in your project — by hand, or generated by tactica — gives `lookup('Path.To.Type')` a fully typed return value. Zero casts.
|
|
43
|
-
|
|
44
|
-
After augmentation, TypeScript knows:
|
|
45
|
-
- The constructor arguments for each type
|
|
46
|
-
- The instance properties
|
|
47
|
-
- The sub-constructors on each instance (`.RouteData`, `.PageData`, etc.)
|
|
48
|
-
|
|
49
|
-
See [`../docs/typed-lookup.md`](../docs/typed-lookup.md) for both paths with examples.
|
|
50
|
-
|
|
51
|
-
---
|
|
52
|
-
|
|
53
|
-
## When to insist on what
|
|
54
|
-
|
|
55
|
-
When asked to work with a mnemonica project:
|
|
56
|
-
|
|
57
|
-
1. **Check whether `TypeRegistry` is augmented somewhere.** This can be:
|
|
58
|
-
- A `.tactica/` directory with `registry.ts` (tactica-generated), OR
|
|
59
|
-
- A `.d.ts` or `.ts` file containing `declare module 'mnemonica' { interface TypeRegistry { ... } }` (hand-written)
|
|
60
|
-
|
|
61
|
-
If neither exists, the project has no type-safe path. Ask the user before writing code: do they want tactica set up, or to hand-augment? Either is fine — but pick one before writing types.
|
|
62
|
-
|
|
63
|
-
2. **If `.tactica/` exists, check `tsconfig.json` includes it.** If missing, add `.tactica/**/*.ts` to `include`.
|
|
64
|
-
|
|
65
|
-
3. **Check that `lookup` is being used.** Direct imports from collection files + casts are the smell that augmentation is missing or unused.
|
|
66
|
-
|
|
67
|
-
4. **Never accept `as unknown as` as a solution.** It is a symptom of the augmentation being missing, not a fix.
|
|
68
|
-
|
|
69
|
-
---
|
|
70
|
-
|
|
71
|
-
## Quick reference
|
|
72
|
-
|
|
73
|
-
| Situation | Wrong | Right |
|
|
74
|
-
|---|---|---|
|
|
75
|
-
| Get a constructor | `import { T } from './collections/T.js'` | `const T = lookup('T')` |
|
|
76
|
-
| Create instance | `new T({ ... }) as unknown as TT` | `new T({ ... })` |
|
|
77
|
-
| Chain to child | `new (instance as any).Child({ ... })` | `new instance.Child({ ... })` |
|
|
78
|
-
| Type not found | Add `as any` | Augment `TypeRegistry` (manually or run `tactica`) |
|
|
79
|
-
|
|
80
|
-
---
|
|
81
|
-
|
|
82
|
-
## Build verification
|
|
83
|
-
|
|
84
|
-
After any change:
|
|
85
|
-
|
|
86
|
-
```bash
|
|
87
|
-
npm run tactica # only if using tactica — regenerate .tactica/
|
|
88
|
-
npx tsc --noEmit # verify zero TS errors
|
|
89
|
-
npx eslint src/ # verify zero lint errors
|
|
90
|
-
npm test # verify all tests pass
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
If you are not using tactica, `npm run tactica` is not required; the hand-written augmentation is the source of truth.
|
|
94
|
-
|
|
95
|
-
---
|
|
96
|
-
|
|
97
|
-
## Deeper reading
|
|
98
|
-
|
|
99
|
-
- [`../docs/typed-lookup.md`](../docs/typed-lookup.md) — `lookup` with or without tactica (canonical reference, side-by-side)
|
|
100
|
-
- [`../docs/tactica-pattern.md`](../docs/tactica-pattern.md) — human-facing explanation of declaration merging
|
|
101
|
-
- [`./TACTICA-DEEP-DIVE.md`](./TACTICA-DEEP-DIVE.md) — comprehensive technical guide
|
|
102
|
-
|
|
103
|
-
The key insight: **the runtime constructor is identical whether you import it directly or look it up. The only difference is TypeScript's compile-time knowledge.** `lookup` + augmented `TypeRegistry` teaches TypeScript what mnemonica already knows at runtime — and the augmentation can come from anywhere.
|
package/.ai/async_init.md
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
# Async Class Constructor Support in Mnemonica
|
|
2
|
-
|
|
3
|
-
## Context
|
|
4
|
-
|
|
5
|
-
JavaScript class constructors cannot be declared `async`. However, a constructor **can return a Promise**. When a base class constructor returns `Promise.resolve(this)`, the subclass `this` becomes the Promise object — class fields initialize on the Promise wrapper, not the real instance. This breaks field inheritance in raw JS.
|
|
6
|
-
|
|
7
|
-
Mnemonica handles this correctly through its wrapper architecture. See also the [test suite](../test_async/index.js) showing the problem and wrapper fix in practice.
|
|
8
|
-
|
|
9
|
-
## How Mnemonica Preserves Class Fields
|
|
10
|
-
|
|
11
|
-
In `src/api/types/compileNewModificatorFunctionBody.ts:54-66`:
|
|
12
|
-
|
|
13
|
-
```ts
|
|
14
|
-
const getClassConstructor = (ConstructHandler, CreationHandler) => {
|
|
15
|
-
return class extends ConstructHandler {
|
|
16
|
-
constructor(...args) {
|
|
17
|
-
const answer = super(...args); // user class runs, fields init on REAL this
|
|
18
|
-
const result = CreationHandler.call(this, answer);
|
|
19
|
-
return result;
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
The **wrapper class** (not the user class) is the subclass in JS's class hierarchy. When `super()` runs the user constructor, fields like `childField = 'value'` initialize on the wrapper's `this`. If the user constructor returns a Promise, `CreationHandler` processes it via `makeAwaiter`, but the fields are already on the correct object.
|
|
26
|
-
|
|
27
|
-
**Key finding:** Class fields survive async initialization in mnemonica because the wrapper ensures they initialize on the real instance before the Promise is returned.
|
|
28
|
-
|
|
29
|
-
## `Symbol.hasInstance` — Nominal Typing by Name
|
|
30
|
-
|
|
31
|
-
Mnemonica replaces standard JS `instanceof` with **nominal typing** checked by `getTypeChecker` (`src/api/utils/index.ts:37`):
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
const getTypeChecker = (TypeName: string) => {
|
|
35
|
-
return (instance: object) => {
|
|
36
|
-
// For Promise-returning constructors, check SymbolConstructorName directly
|
|
37
|
-
if (Reflect.getPrototypeOf(instance).constructor.name === 'Promise') {
|
|
38
|
-
return instance[SymbolConstructorName] === TypeName;
|
|
39
|
-
}
|
|
40
|
-
// Normal case: walk prototype chain collecting constructor names
|
|
41
|
-
const constructors = collectConstructors(instance);
|
|
42
|
-
return constructors[TypeName] || false;
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
`collectConstructors` (`src/utils/collectConstructors.ts`) walks the prototype chain upward, collecting `constructor.name` values into a lookup object like `{ AsyncInitParent: true, AsyncInitChild: true, Mnemonica: true, Object: true }`. It stops at `Mnemonica`.
|
|
48
|
-
|
|
49
|
-
Three places define `Symbol.hasInstance`:
|
|
50
|
-
|
|
51
|
-
1. **`TypeDescriptor.prototype`** (`src/api/types/index.ts:210`) — for `instanceof TypeName` where `TypeName` is what `define()` returned
|
|
52
|
-
2. **`Mnemosyne.prototype`** (`src/api/types/Mnemosyne.ts:381`) — for `instanceof` checks on the instance itself
|
|
53
|
-
3. **`makeSubTypeProxy`** (`src/api/types/Mnemosyne.ts:205`) — for subtype access like `parent.SubType()`
|
|
54
|
-
|
|
55
|
-
### Why This Matters for Pre-existing Class Hierarchies
|
|
56
|
-
|
|
57
|
-
When passing a pre-existing class hierarchy (`class Extended extends Base`) to `define()`:
|
|
58
|
-
|
|
59
|
-
- **Top-level** `define('PreExt', Extended)`: `PreExt.prototype === Extended.prototype`. The instance chain includes `Extended.prototype` and `Base.prototype`. Both `instanceof PreExt` and `instanceof Extended` work.
|
|
60
|
-
- **Sub-type** `Root.define('Sub', Extended)`: `Sub.prototype === Extended.prototype` is still true, BUT mnemonica's subtype wiring inserts the parent type (`Root`) into the instance chain **before** `Extended.prototype`. The original class hierarchy is skipped for standard JS `instanceof`, but mnemonica's name-based `getTypeChecker` still recognizes it because `collectConstructors` sees the names in the chain.
|
|
61
|
-
|
|
62
|
-
Actually — correction: in the sub-type case, standard JS `instanceof Extended` returns **false** because the prototype chain is wired to `Root`, not `Extended.prototype`. But mnemonica's `instanceof Sub` and `instanceof Root` still work because `collectConstructors` finds their names.
|
|
63
|
-
|
|
64
|
-
## Test Suite
|
|
65
|
-
|
|
66
|
-
Tests live in `test_async/index.js` and run via `npm run test:async_init`.
|
|
67
|
-
|
|
68
|
-
### Acronyms Used
|
|
69
|
-
|
|
70
|
-
- **WOReturn** = **W**ith**O**ut **Return** — Promise resolves to `undefined` instead of `this`
|
|
71
|
-
- **NAR** = **N**o **A**wait **R**eturn** — `awaitReturn: false` disables the guard
|
|
72
|
-
|
|
73
|
-
### Test Coverage
|
|
74
|
-
|
|
75
|
-
1. **WOReturn guard for classes** — async class constructor returning Promise without `this` throws `WRONG_MODIFICATION_PATTERN`
|
|
76
|
-
2. **NAR bypass** — `awaitReturn: false` allows the same without throwing
|
|
77
|
-
3. **Field inheritance through mnemonica chain** — parent/child fields preserved across async constructors
|
|
78
|
-
4. **Pre-existing class hierarchy at top-level** — `define('Type', ExtendedClass)` preserves fields and `instanceof` against original classes
|
|
79
|
-
5. **Pre-existing class hierarchy as sub-type** — root type defines sub-type using `ExtendedClass`; sub instance gets root fields + class hierarchy fields, but standard JS `instanceof` against original classes is broken (mnemonica `instanceof` still works)
|
|
80
|
-
|
|
81
|
-
## Relevant Source Files
|
|
82
|
-
|
|
83
|
-
- `src/api/types/compileNewModificatorFunctionBody.ts` — wrapper class creation
|
|
84
|
-
- `src/api/utils/index.ts` — `getTypeChecker`
|
|
85
|
-
- `src/utils/collectConstructors.ts` — prototype chain walker
|
|
86
|
-
- `src/api/types/Mnemosyne.ts` — `Symbol.hasInstance` on instances and sub-type proxy
|
|
87
|
-
- `src/api/types/index.ts` — `Symbol.hasInstance` on `TypeDescriptor.prototype`
|
|
88
|
-
- `src/api/types/InstanceCreator.ts` — async routing via `makeAwaiter`
|
|
89
|
-
|
|
90
|
-
## Related
|
|
91
|
-
|
|
92
|
-
- [`test_async/index.js`](../test_async/index.js) — test suite covering async initialization scenarios
|
|
93
|
-
- [TC39 proposal-async-init issue #3](https://github.com/tc39/proposal-async-init/issues/3) — language-level discussion of the same problem
|
|
94
|
-
- [Mnemonica README](../README.md) — project overview and API documentation
|