mnemonica 1.1.0 → 1.2.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.
- package/.ai/AGENTS.md +31 -18
- package/.ai/DEBUG.md +7 -8
- package/.ai/ONBOARDING.md +24 -7
- package/.ai/PROTOTYPE-CHAIN.md +33 -2
- 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} +146 -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/define-patterns.md → rules-define-patterns.md} +39 -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 +76 -32
- package/CONTRIBUTING.md +4 -18
- package/FOR_HUMANS.md +123 -83
- package/README.md +65 -28
- package/SKILL.md +27 -22
- package/build/api/errors/index.js +7 -5
- package/build/api/index.d.ts +1 -0
- package/build/api/index.js +2 -1
- package/build/api/types/InstanceCreator.js +16 -5
- package/build/api/types/Mnemosyne.d.ts +3 -14
- package/build/api/types/Mnemosyne.js +21 -8
- package/build/api/types/Props.js +1 -2
- package/build/api/types/TypeProxy.d.ts +1 -1
- package/build/api/types/TypeProxy.js +9 -9
- package/build/api/types/compileNewModificatorFunctionBody.js +3 -5
- package/build/api/types/index.d.ts +5 -0
- package/build/api/types/index.js +77 -33
- package/build/api/utils/index.d.ts +1 -0
- package/build/api/utils/index.js +13 -3
- package/build/descriptors/types/index.js +46 -2
- package/build/index.d.ts +14 -3
- package/build/index.js +34 -7
- package/build/types/index.d.ts +24 -4
- package/build/types/index.js +1 -1
- package/build/utils/parent.js +35 -5
- package/docs/ai-learning-trajectory.md +2 -3
- package/docs/async-constructors.md +1 -1
- package/docs/decorate.md +123 -0
- package/docs/empathy-in-ai.md +2 -3
- package/docs/hott-primer.md +1 -1
- package/docs/prototype-chain.md +4 -0
- package/docs/purpose.md +3 -3
- package/{.ai/TACTICA-DEEP-DIVE.md → docs/tactica-deep-dive.md} +44 -1
- package/docs/tactica-pattern.md +1 -2
- package/docs/typed-lookup.md +183 -50
- package/package.json +8 -8
- package/src/api/errors/index.ts +10 -4
- package/src/api/index.ts +2 -8
- package/src/api/types/InstanceCreator.ts +28 -6
- package/src/api/types/Mnemosyne.ts +34 -15
- package/src/api/types/Props.ts +4 -4
- package/src/api/types/TypeProxy.ts +10 -9
- package/src/api/types/compileNewModificatorFunctionBody.ts +12 -4
- package/src/api/types/index.ts +136 -69
- package/src/api/utils/index.ts +27 -9
- package/src/descriptors/types/index.ts +81 -3
- package/src/index.ts +150 -7
- package/src/types/index.ts +120 -10
- package/src/utils/parent.ts +48 -4
- 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/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/utils/parent.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
}
|
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
|
package/.ai/rules/REMINDERS.md
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# Critical Reminders for AI Agents
|
|
2
|
-
|
|
3
|
-
## Before Writing Any Code
|
|
4
|
-
|
|
5
|
-
### 1. Type vs Interface - CRITICAL
|
|
6
|
-
**ALWAYS** check AGENTS.md section "CRITICAL: Type vs Interface vs Instance" before defining types:
|
|
7
|
-
|
|
8
|
-
```typescript
|
|
9
|
-
// ✅ CORRECT - Instance data uses TYPE
|
|
10
|
-
type UserData = { name: string; age: number; };
|
|
11
|
-
|
|
12
|
-
// ❌ WRONG - Instance is NOT an interface!
|
|
13
|
-
interface UserData { name: string; age: number; }
|
|
14
|
-
|
|
15
|
-
// ✅ CORRECT - Constructor/Prototype contract uses INTERFACE
|
|
16
|
-
interface MnemonicaInstance {
|
|
17
|
-
extract(): object;
|
|
18
|
-
parent(): object | null;
|
|
19
|
-
}
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
**Rule of thumb:**
|
|
23
|
-
- **Interface** = Black-box contract (constructors, callables, `implements`)
|
|
24
|
-
- **Type** = Runtime data structure/shape (instances, POJOs)
|
|
25
|
-
|
|
26
|
-
### 2. Indentation - TABS ONLY
|
|
27
|
-
- **NEVER** use spaces for indentation
|
|
28
|
-
- Tab width: 4
|
|
29
|
-
- Enforced by eslint
|
|
30
|
-
|
|
31
|
-
### 3. Function Spacing
|
|
32
|
-
```typescript
|
|
33
|
-
function myFunc () { } // ✅ Space before parentheses
|
|
34
|
-
function myFunc() { } // ❌ Wrong
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
### 4. Key Spacing
|
|
38
|
-
```typescript
|
|
39
|
-
{ key: value } // ✅ Space after colon
|
|
40
|
-
{ key:value } // ❌ Wrong
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### 5. Before Each Task
|
|
44
|
-
1. Read AGENTS.md if context seems incomplete
|
|
45
|
-
2. Check existing code patterns in the same directory
|
|
46
|
-
3. Run `npm run build` before completing (core/ directory)
|
|
47
|
-
4. Run `npm run test:cov` for coverage validation (core/ directory)
|
|
48
|
-
|
|
49
|
-
## Quick Reference
|
|
50
|
-
|
|
51
|
-
| Decision | Check |
|
|
52
|
-
|----------|-------|
|
|
53
|
-
| Type vs Interface | AGENTS.md |
|
|
54
|
-
| Build commands | AGENTS.md |
|
|
55
|
-
| Code style | AGENTS.md |
|
|
56
|
-
| Error handling | AGENTS.md |
|
|
57
|
-
| Context condensing | CONTEXT-CONDENSING.md |
|
|
58
|
-
|
|
59
|
-
## File-Specific Notes
|
|
60
|
-
|
|
61
|
-
- `core/src/types/index.ts` - Export types here, use `type` for instances
|
|
62
|
-
- `core/src/api/types/*.ts` - Internal implementations, check existing patterns
|
|
63
|
-
- Any `provider.ts` files - Check AGENTS.md for type/interface rules
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: mnemonica-lookup-typed
|
|
3
|
-
description: |
|
|
4
|
-
Type-safe lookup with lookup() and TypeRegistry augmentation.
|
|
5
|
-
Use when the user asks about lookup, TypeRegistry, tactica-generated types,
|
|
6
|
-
declaration merging, or type-safe constructor retrieval in mnemonica.
|
|
7
|
-
metadata:
|
|
8
|
-
tags: [mnemonica, lookup, types, tactica, declaration-merging]
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
# Type-Safe Lookup with `lookup()`
|
|
12
|
-
|
|
13
|
-
## `lookup()` with and without `TypeRegistry` augmentation
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { lookup } from 'mnemonica';
|
|
17
|
-
|
|
18
|
-
// Without augmentation: returns TypeClass | undefined
|
|
19
|
-
const SomeType = lookup('SomeType');
|
|
20
|
-
const instance = new SomeType({ ... }); // no type safety
|
|
21
|
-
|
|
22
|
-
// With augmentation: returns the typed constructor
|
|
23
|
-
const SomeType = lookup('SomeType');
|
|
24
|
-
const instance = new SomeType({ ... }); // TypeScript knows the signature
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## TypeRegistry Pattern
|
|
28
|
-
|
|
29
|
-
The default `TypeRegistry` is an empty interface. Application code or
|
|
30
|
-
`@mnemonica/tactica` augments it through TypeScript declaration merging.
|
|
31
|
-
|
|
32
|
-
```typescript
|
|
33
|
-
// In mnemonica core
|
|
34
|
-
export interface TypeRegistry {
|
|
35
|
-
// Intentionally empty. Augment via declaration merging.
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Generated by tactica in .tactica/registry.ts
|
|
39
|
-
declare module 'mnemonica' {
|
|
40
|
-
interface TypeRegistry {
|
|
41
|
-
'UserType': TypeConstructor<UserTypeInstance>;
|
|
42
|
-
'Parent.SubType': TypeConstructor<SubTypeInstance>;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Unaugmented vs Augmented `TypeRegistry`
|
|
48
|
-
|
|
49
|
-
The axis that actually matters is whether `TypeRegistry` has been augmented for your types — not whether tactica is involved. Tactica is one way to produce the augmentation; hand-writing it is another. Runtime behaviour is identical either way.
|
|
50
|
-
|
|
51
|
-
| Feature | Unaugmented `TypeRegistry` | Augmented `TypeRegistry` |
|
|
52
|
-
|---------|---------------------------|--------------------------|
|
|
53
|
-
| Constructor retrieval | `lookup('Name')` returns `TypeClass \| undefined` | `lookup('Name')` returns typed constructor |
|
|
54
|
-
| Type safety | Runtime only | Compile-time + runtime |
|
|
55
|
-
| Registry default | Empty interface | Per-key constructor signatures |
|
|
56
|
-
| Instance properties | `any` / `unknown` | Fully typed |
|
|
57
|
-
|
|
58
|
-
Two ways to augment: hand-written `.d.ts` (small projects, learning) or [`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica) (auto-generated, recommended for non-trivial projects). See [`../../docs/typed-lookup.md`](../../docs/typed-lookup.md) for both paths side by side.
|
|
59
|
-
|
|
60
|
-
## Before/After: Adding a Type to TypeRegistry
|
|
61
|
-
|
|
62
|
-
**Before** (untyped lookup)
|
|
63
|
-
```typescript
|
|
64
|
-
const UserType = lookup('UserType');
|
|
65
|
-
const user = new UserType({ name: 'John' });
|
|
66
|
-
// user is typed as object — no IntelliSense for properties
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
**After** (TypeRegistry augmented — either hand-written or tactica-generated; runtime is identical)
|
|
70
|
-
```typescript
|
|
71
|
-
// In a .d.ts file (hand-written) or .tactica/registry.ts (tactica-generated)
|
|
72
|
-
declare module 'mnemonica' {
|
|
73
|
-
interface TypeRegistry {
|
|
74
|
-
'UserType': new (...args: [UserData]) => UserTypeInstance;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// In application code
|
|
79
|
-
const UserType = lookup('UserType');
|
|
80
|
-
const user = new UserType({ name: 'John' });
|
|
81
|
-
// user is fully typed as UserTypeInstance
|
|
82
|
-
```
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: mnemonica-proxy-architecture
|
|
3
|
-
description: |
|
|
4
|
-
The Proxy-based construction pipeline: TypeProxy, InstanceCreator, Mnemosyne,
|
|
5
|
-
and createInstanceModificator. Use when the user asks about how mnemonica
|
|
6
|
-
creates instances, the prototype chain, TypeProxy, InstanceCreator, or the
|
|
7
|
-
internal construction mechanism.
|
|
8
|
-
metadata:
|
|
9
|
-
tags: [mnemonica, proxy, architecture, construction, prototype]
|
|
10
|
-
---
|
|
11
|
-
|
|
12
|
-
# Proxy-Based Architecture
|
|
13
|
-
|
|
14
|
-
## Construction Pipeline (8 Stages)
|
|
15
|
-
|
|
16
|
-
```
|
|
17
|
-
define(TypeName, ctor) → new TypeDescriptor() → new TypeProxy()
|
|
18
|
-
│
|
|
19
|
-
▼
|
|
20
|
-
new InstanceCreator(type, parent, args)
|
|
21
|
-
│
|
|
22
|
-
▼
|
|
23
|
-
makeInstanceModificator(self)
|
|
24
|
-
│
|
|
25
|
-
▼
|
|
26
|
-
ModificationConstructor.call(parent, ModificatorType, proto, _addProps)
|
|
27
|
-
│
|
|
28
|
-
▼
|
|
29
|
-
user constructor runs (new ModificatorType(...args))
|
|
30
|
-
│
|
|
31
|
-
▼
|
|
32
|
-
postProcessing() → invokePostHooks()
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## Key Components
|
|
36
|
-
|
|
37
|
-
### TypeProxy
|
|
38
|
-
Wraps type constructors. Handles property access, `.define()`, `.lookup()`,
|
|
39
|
-
and subtype resolution.
|
|
40
|
-
|
|
41
|
-
### InstanceCreator
|
|
42
|
-
Orchestrates the full instance construction lifecycle:
|
|
43
|
-
1. **Setup** — extract type, config, prepare ModificationConstructor
|
|
44
|
-
2. **Stack** — collect creation stack if `submitStack` or chained
|
|
45
|
-
3. **blockErrors** — if parent is Error, optionally block construction
|
|
46
|
-
4. **Pre-hooks** — invoke `preCreation` hooks on collection and type
|
|
47
|
-
5. **Build** — `makeInstanceModificator()` wires prototype chain
|
|
48
|
-
6. **Construct** — `new InstanceModificator(...args)` runs user constructor
|
|
49
|
-
7. **Async** — if result is Promise, attach awaiter and subtype factories
|
|
50
|
-
8. **Post-proc** — validate inheritance, set `__self__`, invoke `postCreation` hooks
|
|
51
|
-
|
|
52
|
-
### Mnemosyne
|
|
53
|
-
Handles instance method access via Proxy. Provides:
|
|
54
|
-
- `extract()` — get all enumerable properties
|
|
55
|
-
- `pick(...keys)` — select specific properties
|
|
56
|
-
- `parent()` — traverse prototype chain
|
|
57
|
-
- `fork(...args)` — create new instance with same or different args
|
|
58
|
-
- `clone` — alias for fork()
|
|
59
|
-
|
|
60
|
-
### createInstanceModificator (ModificationConstructor)
|
|
61
|
-
The default ModificationConstructor that wires the prototype chain by creating
|
|
62
|
-
a Mnemosyne object inheriting from the parent instance, attaching internal props,
|
|
63
|
-
and linking `ModificatorType.prototype` to it.
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# Task Template: New Feature Implementation
|
|
2
|
-
|
|
3
|
-
## Pre-Flight Checklist
|
|
4
|
-
|
|
5
|
-
- [ ] Read AGENTS.md section "CRITICAL: Type vs Interface vs Instance" (lines 244-279)
|
|
6
|
-
- [ ] Read AGENTS.md section "Build & Test Commands" (lines 102-116)
|
|
7
|
-
- [ ] Check existing code patterns in the target directory
|
|
8
|
-
- [ ] Run `npm run build` to ensure clean baseline (core/ directory)
|
|
9
|
-
|
|
10
|
-
## Implementation Steps
|
|
11
|
-
|
|
12
|
-
1. **Design Phase**
|
|
13
|
-
- [ ] Define types (use `type` for instances, `interface` for constructors/contracts)
|
|
14
|
-
- [ ] Check REMINDERS.md in `.ai/rules/`
|
|
15
|
-
|
|
16
|
-
2. **Coding Phase**
|
|
17
|
-
- [ ] Follow indentation: TABS ONLY (width 4)
|
|
18
|
-
- [ ] Function spacing: `function myFunc () { }`
|
|
19
|
-
- [ ] Key spacing: `{ key: value }`
|
|
20
|
-
|
|
21
|
-
3. **Testing Phase**
|
|
22
|
-
- [ ] Run `npm run test:cov` (validates build + 100% coverage)
|
|
23
|
-
- [ ] Verify no ESLint warnings
|
|
24
|
-
- [ ] Read and analyse full content of edited file as if it would be code-review
|
|
25
|
-
|
|
26
|
-
4. **Completion**
|
|
27
|
-
- [ ] Update TODO list
|
|
28
|
-
- [ ] Run final build
|
|
29
|
-
- [ ] Use attempt_completion tool
|
|
30
|
-
|
|
31
|
-
## Context Condensing Protocol
|
|
32
|
-
|
|
33
|
-
**CRITICAL:** If you notice yourself forgetting established patterns:
|
|
34
|
-
|
|
35
|
-
→ **READ** `.ai/rules/CONTEXT-CONDENSING.md` immediately
|
|
36
|
-
|
|
37
|
-
## Quick References
|
|
38
|
-
|
|
39
|
-
| Check | Location |
|
|
40
|
-
|-------|----------|
|
|
41
|
-
| Type vs Interface | AGENTS.md |
|
|
42
|
-
| Build commands | AGENTS.md |
|
|
43
|
-
| Code style | AGENTS.md |
|
|
44
|
-
| Error handling | AGENTS.md |
|
|
45
|
-
| Lessons Learned | AGENTS.md |
|
|
46
|
-
| Context condensing | CONTEXT-CONDENSING.md |
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|