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/AGENTS.md
CHANGED
|
@@ -10,17 +10,26 @@ project, start with [`README.md`](./README.md).
|
|
|
10
10
|
|
|
11
11
|
**This is the highest-priority rule. It overrides everything else in this file.**
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
STOP and ask the user before proceeding when ANY of these is true:
|
|
14
14
|
|
|
15
|
-
1. **An
|
|
16
|
-
results, or any tool returned an error.
|
|
15
|
+
1. **An error occurred** — `Edit` failed, `Write` produced unexpected
|
|
16
|
+
results, or any tool returned an error. A successful retry does NOT
|
|
17
|
+
cancel this rule: report the error AND how you recovered.
|
|
17
18
|
2. **You are uncertain** — about what change to make, how a function works,
|
|
18
19
|
or what the user intended.
|
|
19
20
|
3. **You are filling gaps with assumption** — "probably", "likely",
|
|
20
21
|
"I think", "it should work" are signals to stop.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
4. **A tool or environment constraint conflicts with the user's stated
|
|
23
|
+
preference** — surface the conflict verbatim; never silently satisfy
|
|
24
|
+
the tool at the user's expense.
|
|
25
|
+
5. **You are about to create or duplicate a file the user did not
|
|
26
|
+
explicitly request** — ask first. Never copy content when `mv`,
|
|
27
|
+
rename, or a reference suffices.
|
|
28
|
+
|
|
29
|
+
When in doubt: STOP, ask one clear specific question, WAIT for the answer.
|
|
30
|
+
Do not invent workarounds (no `sed`, no `python -c`, no console hacks,
|
|
31
|
+
no rewriting files to bypass a tool restriction).
|
|
32
|
+
**Silent recovery is a violation, even when the outcome looks fine.**
|
|
24
33
|
|
|
25
34
|
The reason this rule exists: wrong assumptions waste both your time and the
|
|
26
35
|
user's. The library encodes non-obvious design intent (data-flow vs control-flow,
|
|
@@ -30,11 +39,17 @@ Confident guesses produce code that compiles but corrupts the design.
|
|
|
30
39
|
---
|
|
31
40
|
|
|
32
41
|
> **Note:** Framework-agnostic rules are also available in `.ai/`:
|
|
33
|
-
> [`AGENTS.md`](./.ai/AGENTS.md),
|
|
34
|
-
> [`ARCHITECT.md`](./.ai/ARCHITECT.md), [`DEBUG.md`](./.ai/DEBUG.md)
|
|
35
|
-
> [`async_init.md`](./.ai/async_init.md).
|
|
42
|
+
> [`AGENTS.md`](./.ai/AGENTS.md),
|
|
43
|
+
> [`ARCHITECT.md`](./.ai/ARCHITECT.md), [`DEBUG.md`](./.ai/DEBUG.md).
|
|
36
44
|
> These rules apply to all agent frameworks.
|
|
37
45
|
|
|
46
|
+
> **Document locations:** this repository is agent-tools-agnostic. Documents
|
|
47
|
+
> about *contributing to the core* live in [`.ai/`](./.ai/); documents about
|
|
48
|
+
> *using the library* (for humans and agents alike) live in
|
|
49
|
+
> [`docs/`](./docs/). Tool-specific directories (`.kilo/`, `.kilocode/`,
|
|
50
|
+
> `.opencode/`, etc.) are for tool configuration only — never put project
|
|
51
|
+
> documents (plans, rules, guides) there.
|
|
52
|
+
|
|
38
53
|
## What and why
|
|
39
54
|
|
|
40
55
|
For the library's thesis, the four data mistakes it eliminates, the Trie observation, the pipeline pattern, and the HoTT framing, read [`README.md`](./README.md). This file assumes you have.
|
|
@@ -48,26 +63,26 @@ Load the docs that match your change type. The wrong context produces broken cod
|
|
|
48
63
|
| Change type | Read before starting |
|
|
49
64
|
|---|---|
|
|
50
65
|
| Any `src/` change | This file + [`.ai/ONBOARDING.md`](./.ai/ONBOARDING.md) |
|
|
51
|
-
| Involves `define()` / type graph | + [`.ai/rules-
|
|
52
|
-
| Involves hooks | + [`.ai/rules-
|
|
53
|
-
| Involves async constructors | + [`.ai/rules-
|
|
54
|
-
| Involves TypeScript types | + [`.ai/rules-
|
|
55
|
-
| Involves proxy internals | + [`.ai/
|
|
56
|
-
| Uses tactica / `lookup` | + [
|
|
66
|
+
| Involves `define()` / type graph | + [`.ai/rules-define-patterns.md`](./.ai/rules-define-patterns.md) |
|
|
67
|
+
| Involves hooks | + [`.ai/rules-hooks.md`](./.ai/rules-hooks.md) |
|
|
68
|
+
| Involves async constructors | + [`.ai/rules-async-constructors.md`](./.ai/rules-async-constructors.md) |
|
|
69
|
+
| Involves TypeScript types | + [`.ai/rules-type-system.md`](./.ai/rules-type-system.md) |
|
|
70
|
+
| Involves proxy internals | + [`.ai/PROTOTYPE-CHAIN.md`](./.ai/PROTOTYPE-CHAIN.md) |
|
|
71
|
+
| Uses tactica / `lookup` | + [`docs/tactica-deep-dive.md`](./docs/tactica-deep-dive.md) |
|
|
57
72
|
| Docs-only change | README section you're touching only |
|
|
58
73
|
|
|
59
74
|
**This file + `.ai/ONBOARDING.md` are the always-required baseline for any `src/` edit.**
|
|
60
75
|
|
|
61
76
|
### Framework-specific rules
|
|
62
77
|
|
|
63
|
-
Mode-specific files in `.ai
|
|
64
|
-
- [`.ai/rules
|
|
65
|
-
- [`.ai/rules
|
|
66
|
-
- [`.ai/rules
|
|
78
|
+
Mode-specific files in `.ai/`:
|
|
79
|
+
- [`.ai/rules-coding.md`](./.ai/rules-coding.md) — universal coding rules
|
|
80
|
+
- [`.ai/rules-reminders.md`](./.ai/rules-reminders.md) — type vs interface, spacing reminders
|
|
81
|
+
- [`.ai/rules-context-condensing.md`](./.ai/rules-context-condensing.md) — context recovery protocol
|
|
67
82
|
|
|
68
83
|
## Build/Test Commands
|
|
69
84
|
|
|
70
|
-
See [`.ai/rules-
|
|
85
|
+
See [`.ai/rules-testing.md`](./.ai/rules-testing.md) for the full command reference, dual-framework details, and coverage requirements. Summary:
|
|
71
86
|
|
|
72
87
|
```bash
|
|
73
88
|
npm run build # full build with linting
|
|
@@ -82,7 +97,7 @@ npm run watch # watch mode
|
|
|
82
97
|
|
|
83
98
|
## Code Style (Project-Specific)
|
|
84
99
|
|
|
85
|
-
See [`.ai/rules-
|
|
100
|
+
See [`.ai/rules-code-style.md`](./.ai/rules-code-style.md) for the full style reference. Key rules: tabs only, space before function parens, colons aligned in object literals, `strict: true`, **no `any`** (`no-explicit-any: error`).
|
|
86
101
|
|
|
87
102
|
## Architecture Patterns
|
|
88
103
|
|
|
@@ -94,7 +109,7 @@ The core API is `define(TypeName, constructHandler, config?)` in `src/index.ts`.
|
|
|
94
109
|
|
|
95
110
|
### The `lookup()` Function
|
|
96
111
|
|
|
97
|
-
For user-facing semantics, see [`README.md`](./README.md) and [
|
|
112
|
+
For user-facing semantics, see [`README.md`](./README.md) and [`docs/tactica-deep-dive.md`](./docs/tactica-deep-dive.md). The contributor-relevant detail is the implementation pattern: `TypeRegistry` starts empty, and `lookup()` uses overloads so augmented keys return the typed constructor while unaugmented keys fall back to `TypeClass | undefined`.
|
|
98
113
|
|
|
99
114
|
```typescript
|
|
100
115
|
// In mnemonica core (src/index.ts)
|
|
@@ -136,12 +151,18 @@ Runtime behavior is identical whether `TypeRegistry` is augmented or not; the on
|
|
|
136
151
|
|
|
137
152
|
### Typed registry builders
|
|
138
153
|
|
|
139
|
-
Mnemonica has
|
|
154
|
+
Mnemonica has three compile-time paths for the same runtime API:
|
|
140
155
|
|
|
141
|
-
- **Builder mode** — chain `.define()` on the exported `mnemonica` object or on
|
|
156
|
+
- **Builder mode** (default) — chain `.define()` on the exported `mnemonica` object or on
|
|
142
157
|
a `createTypesCollection()` result. No `TypeRegistry` augmentation, no Tactica.
|
|
158
|
+
- **Registry bridge** — one hand-written line merges a builder's local registry
|
|
159
|
+
into the global `TypeRegistry`:
|
|
160
|
+
`interface TypeRegistry extends RegistryOf<typeof App> {}`
|
|
161
|
+
(inside `declare module 'mnemonica'`, in a dedicated `registry.ts`).
|
|
143
162
|
- **Augmented mode** — use free `define()`/`lookup()` or `@decorate()`, and let
|
|
144
|
-
Tactica (or a hand-written file) populate the global `TypeRegistry`.
|
|
163
|
+
Tactica (or a hand-written file) populate the global `TypeRegistry`. Required
|
|
164
|
+
for `@decorate()`: TypeScript never applies a class decorator's return type
|
|
165
|
+
to the class binding, so no local-registry mechanism can type decorators.
|
|
145
166
|
|
|
146
167
|
Public types involved:
|
|
147
168
|
|
|
@@ -149,6 +170,8 @@ Public types involved:
|
|
|
149
170
|
- `MnemonicaModule<Registry>` — type of the exported `mnemonica` object.
|
|
150
171
|
- `IDefinitorInstance<N, R, Registry, Path>` — type of constructors returned by
|
|
151
172
|
`.define()`.
|
|
173
|
+
- `RegistryOf<T>` — extracts the accumulated `Registry` from any of the above,
|
|
174
|
+
for the bridge.
|
|
152
175
|
|
|
153
176
|
Quick builder example:
|
|
154
177
|
|
|
@@ -168,11 +191,19 @@ const user = new User({ name: 'Ada' });
|
|
|
168
191
|
const admin = new user.Admin({ role: 'root' });
|
|
169
192
|
```
|
|
170
193
|
|
|
171
|
-
|
|
172
|
-
|
|
194
|
+
Constructor `.lookup()` resolves **relative first, then root fallback**: the
|
|
195
|
+
type's own subtypes are searched first, then the same string is resolved as an
|
|
196
|
+
absolute path from the collection root. Exported builder values carry the
|
|
197
|
+
registry across files (registry threading).
|
|
198
|
+
|
|
199
|
+
The free `define()`/`lookup()` exports resolve against the global
|
|
200
|
+
`TypeRegistry` (augmentation or bridge). When you hold a builder value, prefer
|
|
201
|
+
the explicit-source forms — `lookup(source, path)` and
|
|
202
|
+
`define(source, name, handler)` — which infer the registry from the source
|
|
203
|
+
instead of falling back to the global interface.
|
|
173
204
|
|
|
174
|
-
For the full guide —
|
|
175
|
-
|
|
205
|
+
For the full guide — multi-file threading, the bridge, two-arg overloads,
|
|
206
|
+
`strictChain` notes, and `@decorate` limitations — see
|
|
176
207
|
[`docs/typed-lookup.md`](./docs/typed-lookup.md).
|
|
177
208
|
|
|
178
209
|
### Type System Structure
|
|
@@ -198,7 +229,7 @@ The library makes heavy use of JavaScript Proxies:
|
|
|
198
229
|
|
|
199
230
|
### Internal Instance Properties
|
|
200
231
|
|
|
201
|
-
Stored in a `WeakMap` keyed by the instance
|
|
232
|
+
Stored in a `WeakMap` keyed by the instance's **Mnemosyne memory layer** — the prototype object created per construction in `createInstanceModificator` — not as own properties on the instance itself. `_getProps` reaches it by walking the prototype chain from the instance. Access via `getProps(instance)`. For the full property list (9 entries, with meanings) see the **Internal instance properties** table in [`README.md`](./README.md) — that table is the canonical reference.
|
|
202
233
|
|
|
203
234
|
`setProps(instance, values)` is the mutating counterpart; rarely needed and considered advanced.
|
|
204
235
|
|
|
@@ -257,7 +288,7 @@ This applies to **all** `return` statements where the expression is anything oth
|
|
|
257
288
|
|
|
258
289
|
## TypeScript Type Rules
|
|
259
290
|
|
|
260
|
-
**Never use bare `Function`, `CallableFunction`, or `NewableFunction` as types** — always define a purpose-specific interface that extends them. See [`.ai/rules-
|
|
291
|
+
**Never use bare `Function`, `CallableFunction`, or `NewableFunction` as types** — always define a purpose-specific interface that extends them. See [`.ai/rules-code-style.md`](./.ai/rules-code-style.md) for examples and allowed exceptions.
|
|
261
292
|
|
|
262
293
|
## Preserving Design Comments and Memory Notes
|
|
263
294
|
|
|
@@ -275,7 +306,20 @@ If a comment becomes technically inaccurate after a change, update it rather tha
|
|
|
275
306
|
|
|
276
307
|
## Testing Requirements
|
|
277
308
|
|
|
278
|
-
See [`.ai/rules-
|
|
309
|
+
See [`.ai/rules-testing.md`](./.ai/rules-testing.md) for full coverage requirements and patterns. 100% required on both Mocha and Jest. Must run `npm run test:cov` before completing any task.
|
|
310
|
+
|
|
311
|
+
## Before Saying a Task Is Done
|
|
312
|
+
|
|
313
|
+
When a task uses a TODO list, the list is part of the deliverable:
|
|
314
|
+
|
|
315
|
+
1. **Before reporting completion, update the TODO list** so every item is
|
|
316
|
+
marked done (struck through). A stale `in_progress` item reads as
|
|
317
|
+
unfinished work even when the work itself is complete.
|
|
318
|
+
2. **Only mark items done that are verified** — tests green, coverage at
|
|
319
|
+
100% on both suites, `lint:md` clean where docs changed. Never mark an
|
|
320
|
+
item done just to tidy the list.
|
|
321
|
+
3. **When the task concludes, wipe the TODO list.** A concluded task leaves
|
|
322
|
+
no list behind; the next task starts fresh.
|
|
279
323
|
|
|
280
324
|
## Common Patterns
|
|
281
325
|
|
package/CONTRIBUTING.md
CHANGED
|
@@ -116,21 +116,7 @@ links and broken anchors across all Markdown files in the repo.
|
|
|
116
116
|
|
|
117
117
|
## Releasing
|
|
118
118
|
|
|
119
|
-
### 1.
|
|
120
|
-
|
|
121
|
-
In `CHANGELOG.md`, move everything under `## [Unreleased]` into a
|
|
122
|
-
new versioned section:
|
|
123
|
-
|
|
124
|
-
```markdown
|
|
125
|
-
## [1.0.1] - 2026-05-22
|
|
126
|
-
|
|
127
|
-
### Fixed
|
|
128
|
-
- ...
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
Leave a fresh empty `## [Unreleased]` block at the top for the next cycle.
|
|
132
|
-
|
|
133
|
-
### 2. Bump the version
|
|
119
|
+
### 1. Bump the version
|
|
134
120
|
|
|
135
121
|
```bash
|
|
136
122
|
npm version patch # 1.0.0 → 1.0.1
|
|
@@ -140,7 +126,7 @@ npm version patch # 1.0.0 → 1.0.1
|
|
|
140
126
|
`npm version` updates `package.json` and creates a git commit + tag
|
|
141
127
|
(`v1.0.1`) automatically. Do not edit `package.json` by hand for this.
|
|
142
128
|
|
|
143
|
-
###
|
|
129
|
+
### 2. Verify
|
|
144
130
|
|
|
145
131
|
```bash
|
|
146
132
|
npm run verify # build + lint:check
|
|
@@ -150,14 +136,14 @@ npm run lint:md # no dead links or broken anchors
|
|
|
150
136
|
|
|
151
137
|
All three must pass before publishing.
|
|
152
138
|
|
|
153
|
-
###
|
|
139
|
+
### 3. Inspect the tarball
|
|
154
140
|
|
|
155
141
|
```bash
|
|
156
142
|
npm pack --dry-run
|
|
157
143
|
```
|
|
158
144
|
|
|
159
145
|
The tarball must include: `build/`, `module/`, `src/`, `docs/`, `.ai/`,
|
|
160
|
-
`
|
|
146
|
+
`README.md`, `FOR_HUMANS.md`, `AGENTS.md`, `SKILL.md`,
|
|
161
147
|
`CONTRIBUTING.md`, `LICENSE`.
|
|
162
148
|
|
|
163
149
|
It must **not** include: `test/`, `test-jest/`, `test-ts/`, `test_async/`,
|
package/FOR_HUMANS.md
CHANGED
|
@@ -57,7 +57,7 @@ Mnemonica has one runtime behavior, but TypeScript cannot see the type graph cre
|
|
|
57
57
|
1. **Builder mode** — chain `.define()` on the `mnemonica` object or on `createTypesCollection()`. The returned object carries a local type registry, so `.lookup()` is typed without any global augmentation.
|
|
58
58
|
2. **Augmented mode** — use the free `define()` / `lookup()` exports and augment the global `TypeRegistry` by hand or with `@mnemonica/tactica`.
|
|
59
59
|
|
|
60
|
-
At runtime
|
|
60
|
+
There is also a one-line middle path: the `RegistryOf` bridge merges a builder's local registry into the global `TypeRegistry` by hand. At runtime all paths are identical — the only difference is where TypeScript looks up the types. See [`docs/typed-lookup.md`](./docs/typed-lookup.md) for details.
|
|
61
61
|
|
|
62
62
|
---
|
|
63
63
|
|
|
@@ -122,7 +122,7 @@ The key inversion: `new` is called on the **parent instance** (`alice`), not on
|
|
|
122
122
|
- **Curious about the theory?** Read [`README.md`](./README.md) — the HoTT framing, the Trie observation, the pipeline pattern, the AI-agent angle.
|
|
123
123
|
- **Want examples you can run?** See the [Examples](#examples) section below.
|
|
124
124
|
- **Going deeper on philosophy?** Read [`docs/purpose.md`](./docs/purpose.md).
|
|
125
|
-
- **Background reading:** [Inheritance in JavaScript: Factory of Constructors with Prototype Chain](https://github.com/mythographica/stash/blob/master/inheritance.md) · [Architecture of Prototype Inheritance in JavaScript](https://
|
|
125
|
+
- **Background reading:** [Inheritance in JavaScript: Factory of Constructors with Prototype Chain](https://github.com/mythographica/stash/blob/master/inheritance.md) · [Architecture of Prototype Inheritance in JavaScript](https://github.com/mythographica/stash/blob/master/inheritance_architecture.md) · [Dead Simple type checker for JavaScript](https://github.com/mythographica/typologica)
|
|
126
126
|
|
|
127
127
|
---
|
|
128
128
|
|
|
@@ -204,35 +204,12 @@ console.log(admin.role); // 'admin' (own property)
|
|
|
204
204
|
|
|
205
205
|
### Class-Based (TypeScript)
|
|
206
206
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
@decorate()
|
|
214
|
-
class UserType {
|
|
215
|
-
name: string;
|
|
216
|
-
email: string;
|
|
217
|
-
constructor (data: { name: string; email: string }) {
|
|
218
|
-
Object.assign(this, data);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Create an instance
|
|
223
|
-
const user = new UserType({ name: 'John', email: 'john@example.com' });
|
|
224
|
-
|
|
225
|
-
// Define a subtype
|
|
226
|
-
@decorate(UserType)
|
|
227
|
-
class AdminType {
|
|
228
|
-
role: string = 'admin';
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// Create a nested instance (inherits from user)
|
|
232
|
-
const admin = new user.AdminType();
|
|
233
|
-
console.log(admin.name); // 'John' (inherited)
|
|
234
|
-
console.log(admin.role); // 'admin' (own property)
|
|
235
|
-
```
|
|
207
|
+
A class-based style also exists via the `@decorate()` decorator, and it works
|
|
208
|
+
identically to `define()` at runtime. It is **not the default path**: it
|
|
209
|
+
requires [`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica)
|
|
210
|
+
(or hand-written augmentation) to be typed, because TypeScript never applies a
|
|
211
|
+
class decorator's return type to the class binding. See
|
|
212
|
+
[`docs/decorate.md`](./docs/decorate.md) for the full guide and examples.
|
|
236
213
|
|
|
237
214
|
### ESM
|
|
238
215
|
|
|
@@ -249,12 +226,12 @@ import { define, lookup } from 'mnemonica/module';
|
|
|
249
226
|
| Change type | Read before starting |
|
|
250
227
|
|---|---|
|
|
251
228
|
| Any `src/` change | [`AGENTS.md`](./AGENTS.md) + [`.ai/ONBOARDING.md`](./.ai/ONBOARDING.md) |
|
|
252
|
-
| Involves `define()` / type graph | + [`.ai/rules-
|
|
253
|
-
| Involves hooks | + [`.ai/rules-
|
|
254
|
-
| Involves async constructors | + [`.ai/rules-
|
|
255
|
-
| Involves TypeScript types | + [`.ai/rules-
|
|
256
|
-
| Involves proxy internals | + [`.ai/
|
|
257
|
-
| Uses tactica / `lookup` | + [
|
|
229
|
+
| Involves `define()` / type graph | + [`.ai/rules-define-patterns.md`](./.ai/rules-define-patterns.md) |
|
|
230
|
+
| Involves hooks | + [`.ai/rules-hooks.md`](./.ai/rules-hooks.md) |
|
|
231
|
+
| Involves async constructors | + [`.ai/rules-async-constructors.md`](./.ai/rules-async-constructors.md) |
|
|
232
|
+
| Involves TypeScript types | + [`.ai/rules-type-system.md`](./.ai/rules-type-system.md) |
|
|
233
|
+
| Involves proxy internals | + [`.ai/PROTOTYPE-CHAIN.md`](./.ai/PROTOTYPE-CHAIN.md) |
|
|
234
|
+
| Uses tactica / `lookup` | + [`docs/tactica-deep-dive.md`](./docs/tactica-deep-dive.md) |
|
|
258
235
|
| Docs-only change | README section you're touching only |
|
|
259
236
|
| Unfamiliar with the codebase | [`.ai/ONBOARDING.md`](./.ai/ONBOARDING.md) first — one file, five minutes |
|
|
260
237
|
|
|
@@ -516,13 +493,14 @@ const AsyncTypeNoReturn = define('AsyncType', async function () {
|
|
|
516
493
|
| I want to... | Use |
|
|
517
494
|
|---|---|
|
|
518
495
|
| Define a type | `define('Name', ctor)` |
|
|
496
|
+
| Define a type lazily | `lazy('Name', () => ctor)` or `Type.lazy(() => ctor)` |
|
|
519
497
|
| Create from instance | `new instance.SubType(args)` |
|
|
520
|
-
| Look up a type | `lookup('Name')` or `lookup('
|
|
498
|
+
| Look up a type | `lookup('Name')` or `Type.lookup('Path')` |
|
|
521
499
|
| Read construction history | `getProps(instance)` |
|
|
522
|
-
| Get parent instance | `utils.parent(instance)` or `utils.parent(instance, '
|
|
500
|
+
| Get parent instance | `utils.parent(instance)`, `utils.parent(instance, 'TypeName')` or `utils.parent(instance, 'Root.Parent')` |
|
|
523
501
|
| Flatten to plain object | `utils.extract(instance)` |
|
|
524
502
|
| Add lifecycle hooks | `type.registerHook('postCreation', cb)` |
|
|
525
|
-
| Use classes |
|
|
503
|
+
| Use classes (requires Tactica) | see [`docs/decorate.md`](./docs/decorate.md) |
|
|
526
504
|
|
|
527
505
|
### Core Functions
|
|
528
506
|
|
|
@@ -540,10 +518,44 @@ const MyType = define('MyType', function (data) {
|
|
|
540
518
|
```
|
|
541
519
|
|
|
542
520
|
**Parameters:**
|
|
543
|
-
- `typeName` (string): Name of the type (optional if using factory function)
|
|
521
|
+
- `typeName` (string): Name of the type (optional if using factory function). Must be non-empty, start with an uppercase letter, and must not be one of the reserved names `Mnemonica` or `Mnemosyne`. May be a nested path (`'Parent.Child'`) to define a subtype under an existing type.
|
|
544
522
|
- `constructHandler` (Function): Constructor function
|
|
545
523
|
- `config` (object, optional): Configuration options
|
|
546
524
|
|
|
525
|
+
#### `lazy(typeName?, getter, config?)`
|
|
526
|
+
|
|
527
|
+
Defines a type whose constructor is resolved through a zero-arg getter. Useful
|
|
528
|
+
for breaking circular dependencies or deferring constructor selection until
|
|
529
|
+
definition time. The resulting type behaves like a type created with `define()`.
|
|
530
|
+
|
|
531
|
+
```js
|
|
532
|
+
const { lazy } = require('mnemonica');
|
|
533
|
+
|
|
534
|
+
// unnamed: type name is taken from the returned constructor's .name
|
|
535
|
+
const LazyType = lazy(() => function LazyType(data) {
|
|
536
|
+
Object.assign(this, data);
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
// named: explicit type name
|
|
540
|
+
const NamedType = lazy('NamedType', () => function (data) {
|
|
541
|
+
Object.assign(this, data);
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
// chainable on constructors and collections
|
|
545
|
+
const SubType = MyType.lazy('SubType', () => function (data) {
|
|
546
|
+
this.extra = data;
|
|
547
|
+
});
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
**Parameters:**
|
|
551
|
+
- `typeName` (string, optional): Name of the type. If omitted, the name is taken from the getter's returned constructor.
|
|
552
|
+
- `getter` (Function): Zero-arg function returning the constructor.
|
|
553
|
+
- `config` (object, optional): Configuration options
|
|
554
|
+
|
|
555
|
+
**Note:** the getter is invoked **once at definition time** (to determine the
|
|
556
|
+
type name, class mode, and prototype) and then again on every construction.
|
|
557
|
+
Keep it free of side effects that must not run at `define()` time.
|
|
558
|
+
|
|
547
559
|
#### `lookup(typeNestedPath)`
|
|
548
560
|
|
|
549
561
|
Looks up a type by its nested path.
|
|
@@ -554,6 +566,11 @@ const SomeType = lookup('SomeType');
|
|
|
554
566
|
const SomeNestedType = lookup('SomeType.SomeNestedType');
|
|
555
567
|
```
|
|
556
568
|
|
|
569
|
+
Path segments may be separated by `.`, `/`, or `:`, and whitespace inside the
|
|
570
|
+
path is ignored — `lookup('A/B')`, `lookup('A:B')`, and `lookup(' A . B ')`
|
|
571
|
+
all resolve like `lookup('A.B')`. The same path forms work everywhere a
|
|
572
|
+
nested type path is accepted (`define()`, `utils.parent()`, hooks, etc.).
|
|
573
|
+
|
|
557
574
|
#### `lookup(typeNestedPath)`
|
|
558
575
|
|
|
559
576
|
Type-safe lookup function. Requires a `TypeRegistry` augmentation — hand-written for small projects or generated by [`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica). See [`docs/typed-lookup.md`](./docs/typed-lookup.md) for both paths side by side.
|
|
@@ -607,49 +624,20 @@ const subInstance = createSub('arg1', 'arg2');
|
|
|
607
624
|
|
|
608
625
|
#### `decorate(target?, config?)`
|
|
609
626
|
|
|
610
|
-
TypeScript decorator for class-based definitions
|
|
627
|
+
TypeScript decorator for class-based definitions; runtime-equivalent to
|
|
628
|
+
`define()`. Requires Tactica (or hand-written `TypeRegistry` augmentation) to
|
|
629
|
+
be typed — see [`docs/decorate.md`](./docs/decorate.md) for the full guide,
|
|
630
|
+
including nested decoration, configuration, and the TypeØmatica integration.
|
|
611
631
|
|
|
612
632
|
```typescript
|
|
613
633
|
import { decorate } from 'mnemonica';
|
|
614
634
|
|
|
615
|
-
// Basic decoration
|
|
616
635
|
@decorate()
|
|
617
636
|
class MyClass {
|
|
618
637
|
field: number = 123;
|
|
619
638
|
}
|
|
620
|
-
|
|
621
|
-
// Nested decoration (define as subtype)
|
|
622
|
-
@decorate()
|
|
623
|
-
class ParentClass {
|
|
624
|
-
parentField: string = 'parent';
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
@decorate(ParentClass)
|
|
628
|
-
class ChildClass {
|
|
629
|
-
childField: string = 'child';
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
// Create parent instance, then child from it
|
|
633
|
-
const parent = new ParentClass();
|
|
634
|
-
const child = new parent.ChildClass();
|
|
635
639
|
```
|
|
636
640
|
|
|
637
|
-
**With configuration:**
|
|
638
|
-
|
|
639
|
-
```typescript
|
|
640
|
-
@decorate({ strictChain: false, blockErrors: true })
|
|
641
|
-
class ConfiguredClass {
|
|
642
|
-
field: number = 123;
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
@decorate(ParentClass, { strictChain: false })
|
|
646
|
-
class ConfiguredChildClass {
|
|
647
|
-
field: number = 123;
|
|
648
|
-
}
|
|
649
|
-
```
|
|
650
|
-
|
|
651
|
-
**Note:** After a class is decorated with `@decorate()`, it can be used as a decorator for nested types (advanced pattern; may require `@ts-ignore` due to TypeScript limitations with callable class types).
|
|
652
|
-
|
|
653
641
|
#### `registerHook(Constructor, hookType, callback)`
|
|
654
642
|
|
|
655
643
|
Registers a hook for a specific constructor.
|
|
@@ -671,9 +659,10 @@ For advanced TypeScript usage, the following types are exported from `mnemonica`
|
|
|
671
659
|
| Type | Description | Usage |
|
|
672
660
|
|------|-------------|-------|
|
|
673
661
|
| `IDEF<T>` | Base constructor function type | `define('Name', fn: IDEF<MyType>)` |
|
|
662
|
+
| `LazyDef<T>` | Zero-arg getter returning a constructor | `lazy('Name', fn: LazyDef<MyType>)` |
|
|
674
663
|
| `MnemonicaInstance` | Optional helper interface | Can be used when attaching the legacy instance methods to your own prototype |
|
|
675
664
|
| `TypeClass` | Base type constructor | `const MyType: TypeClass = define(...)` |
|
|
676
|
-
| `DecoratedClass<T>` | Decorated class type | `@decorate() class MyClass {}` |
|
|
665
|
+
| `DecoratedClass<T>` | Decorated class type | `@decorate() class MyClass {}` (see [`docs/decorate.md`](./docs/decorate.md)) |
|
|
677
666
|
| `IDefinitorInstance<N, S>` | Constructor with subtypes | Returned by `define()` with `.define()` method |
|
|
678
667
|
| `ConstructorFunction<T>` | Constructor with prototype | Generic constructor function signature |
|
|
679
668
|
| `constructorOptions` | Configuration options | `{ strictChain: true, blockErrors: true }` |
|
|
@@ -784,6 +773,11 @@ myCollection.registerHook('preCreation', (opts) => {
|
|
|
784
773
|
const FoundType = myCollection.lookup('MyType');
|
|
785
774
|
```
|
|
786
775
|
|
|
776
|
+
Only known option keys are applied to a collection: unknown keys are
|
|
777
|
+
silently dropped, and a value whose type does not match the option's default
|
|
778
|
+
falls back to that default. Note that `asClass` is a per-`define()` option —
|
|
779
|
+
it is not part of the collection-level defaults and is ignored here.
|
|
780
|
+
|
|
787
781
|
#### `getProps(instance)` / `setProps(instance, values)`
|
|
788
782
|
|
|
789
783
|
Get or set internal properties of an instance.
|
|
@@ -794,10 +788,12 @@ const { getProps, setProps } = require('mnemonica');
|
|
|
794
788
|
const props = getProps(instance);
|
|
795
789
|
console.log(props.__type__, props.__args__);
|
|
796
790
|
|
|
797
|
-
// Set properties
|
|
798
|
-
setProps(instance, {
|
|
791
|
+
// Set custom properties
|
|
792
|
+
setProps(instance, { myFlag: true });
|
|
799
793
|
```
|
|
800
794
|
|
|
795
|
+
`setProps` only accepts non-reserved names: the internal `__…__` properties (`__type__`, `__args__`, `__timestamp__`, etc.) are filtered out and cannot be overwritten. Also note that `getProps` returns the *live* internal object when no custom properties are set — mutating the result mutates the instance's construction record. Once `setProps` has been used, `getProps` returns a merged copy instead.
|
|
796
|
+
|
|
801
797
|
---
|
|
802
798
|
|
|
803
799
|
### Instance Methods
|
|
@@ -844,13 +840,26 @@ const pickedArray = utils.pick(instance, ['email', 'password']);
|
|
|
844
840
|
|
|
845
841
|
#### `utils.parent(instance, constructorName?)`
|
|
846
842
|
|
|
847
|
-
Gets the parent instance. If `constructorName` is provided, walks up the chain
|
|
843
|
+
Gets the parent instance. If `constructorName` is provided, walks up the chain
|
|
844
|
+
and returns the nearest ancestor with that constructor name. The instance itself
|
|
845
|
+
is never a candidate — only ancestors are searched.
|
|
846
|
+
|
|
847
|
+
A **dotted path** (`'GrandParent.Parent'`) requires the segments to match
|
|
848
|
+
contiguously upwards: each leading segment must be the direct parent of the
|
|
849
|
+
instance matched by the next one. The return value is the instance matched by
|
|
850
|
+
the **last** segment. This disambiguates lineages where the same type name
|
|
851
|
+
appears more than once (possible with `strictChain: false` re-construction).
|
|
852
|
+
If no contiguous match exists, the result is `undefined`.
|
|
853
|
+
|
|
848
854
|
The structural return type is `object | undefined`; a specific nominal parent
|
|
849
855
|
type requires a `TypeRegistry` augmentation (see [`docs/typed-lookup.md`](./docs/typed-lookup.md)).
|
|
850
856
|
|
|
851
857
|
```ts
|
|
852
858
|
const immediateParent = utils.parent(instance);
|
|
853
859
|
const specificParent = utils.parent(instance, 'UserType'); // object | undefined
|
|
860
|
+
// lineage request → route → page → response:
|
|
861
|
+
const route = utils.parent(response, 'RequestData.RouteData'); // route instance
|
|
862
|
+
const nearest = utils.parent(response, 'RouteData'); // page's parent, as before
|
|
854
863
|
```
|
|
855
864
|
|
|
856
865
|
#### `utils.clone(instance)`
|
|
@@ -943,7 +952,7 @@ explicit `<T>` cast is required for ordinary use.
|
|
|
943
952
|
| `utils.fork(instance)` | `(this: object, ...args: unknown[]) => T` |
|
|
944
953
|
| `utils.parent(instance, path?)` | `object \| undefined` |
|
|
945
954
|
| `utils.sibling(instance)` | `SiblingAccessor` |
|
|
946
|
-
| `utils.merge(A, B, ...args)` | `InstanceResult<Merge<B, A
|
|
955
|
+
| `utils.merge(A, B, ...args)` | `InstanceResult<Merge<B, A>>` |
|
|
947
956
|
| `utils.parse(instance)` | `Parsed<T>` |
|
|
948
957
|
| `utils.toJSON(instance)` | `string` |
|
|
949
958
|
| `utils.collectConstructors(instance, flat?)` | `(CallableFunction \| string)[]` |
|
|
@@ -1000,7 +1009,7 @@ interface HookData {
|
|
|
1000
1009
|
args: unknown[]; // Arguments passed to constructor
|
|
1001
1010
|
existentInstance: object; // Parent instance
|
|
1002
1011
|
inheritedInstance: object; // New instance (postCreation only)
|
|
1003
|
-
throwModificationError(error: Error): void; // Throw error from hook
|
|
1012
|
+
throwModificationError(error: Error): void; // Throw error from hook (not available in preCreation)
|
|
1004
1013
|
}
|
|
1005
1014
|
```
|
|
1006
1015
|
|
|
@@ -1013,6 +1022,13 @@ MyType.registerHook('postCreation', (hookData) => {
|
|
|
1013
1022
|
|
|
1014
1023
|
**Note:** In preCreation hooks, `existentInstance` refers to the parent; in postCreation hooks, it refers to the instance used for inheritance.
|
|
1015
1024
|
|
|
1025
|
+
**Error wrapping boundaries.** `preCreation` hooks do not receive
|
|
1026
|
+
`throwModificationError` — throw normally to abort construction; the error
|
|
1027
|
+
propagates unwrapped. The same is true for errors thrown by a lazy type's
|
|
1028
|
+
getter at setup and by a custom `ModificationConstructor` during prototype
|
|
1029
|
+
wiring: they propagate as-is. Only failures of the user constructor body are
|
|
1030
|
+
wrapped into mnemonica errored instances (when `blockErrors` is `true`).
|
|
1031
|
+
|
|
1016
1032
|
---
|
|
1017
1033
|
|
|
1018
1034
|
### Error Handling
|
|
@@ -1066,6 +1082,10 @@ const { defineStackCleaner } = require('mnemonica');
|
|
|
1066
1082
|
defineStackCleaner(/node_modules\/some-package/);
|
|
1067
1083
|
```
|
|
1068
1084
|
|
|
1085
|
+
Multiple cleaners may be registered; a stack line is removed if **any** of
|
|
1086
|
+
them matches. If cleaning would remove every line, the original stack is
|
|
1087
|
+
kept instead.
|
|
1088
|
+
|
|
1069
1089
|
---
|
|
1070
1090
|
|
|
1071
1091
|
### Symbols & Constants
|
|
@@ -1109,6 +1129,26 @@ define('SomeType', function () {}, {
|
|
|
1109
1129
|
| `asClass` | `boolean` | `auto` | Force class mode detection. Usually auto-detected from constructor syntax. |
|
|
1110
1130
|
| `ModificationConstructor` | `Function` | - | Custom constructor function for internal instance modification. |
|
|
1111
1131
|
|
|
1132
|
+
### What `strictChain` actually guards
|
|
1133
|
+
|
|
1134
|
+
`strictChain` is checked at two independent points, and re-constructing a
|
|
1135
|
+
predecessor type on a deeper instance (`apply(deepInstance, AncestorType)`)
|
|
1136
|
+
requires **both** of them to be off:
|
|
1137
|
+
|
|
1138
|
+
1. **Subtype resolution** checks the config of the **entity's type**: may this
|
|
1139
|
+
instance construct a subtype that is not its own direct subtype? With
|
|
1140
|
+
`strictChain: false`, the search walks up the parent chain and a subtype
|
|
1141
|
+
registered on any ancestor type becomes constructible
|
|
1142
|
+
(`Mnemosyne.prepareSubtypeForConstruction` → `findSubTypeFromParent`).
|
|
1143
|
+
2. **Construction validation** checks the config of the **target type**: must
|
|
1144
|
+
the new instance's parent be an instance of the type's declared parent
|
|
1145
|
+
kind? With `strictChain: false`, a parent of a different kind is accepted
|
|
1146
|
+
(`InstanceCreator.postProcessing`).
|
|
1147
|
+
|
|
1148
|
+
This is how one lineage can end up carrying the same type name more than
|
|
1149
|
+
once — which is exactly the case `utils.parent(instance, 'A.B')` dotted
|
|
1150
|
+
paths were made for.
|
|
1151
|
+
|
|
1112
1152
|
### Instance Method Opt-In
|
|
1113
1153
|
|
|
1114
1154
|
Starting from v1.0.6 instance methods are **not** exposed on instances by default. Use the standalone `utils` export or attach the methods to your constructor's prototype before calling `define()`:
|
|
@@ -1142,7 +1182,7 @@ defaultTypes[SymbolConfig].blockErrors = false;
|
|
|
1142
1182
|
|
|
1143
1183
|
## Usage with TypeØmatica
|
|
1144
1184
|
|
|
1145
|
-
TypeØmatica is a companion library that provides strict runtime type checking using JavaScript Proxies. It enforces types at runtime exactly as TypeScript expects at compile time.
|
|
1185
|
+
TypeØmatica is a companion library that provides strict runtime type checking using JavaScript Proxies. It enforces types at runtime exactly as TypeScript expects at compile time. The `@decorate()` usage below requires Tactica — see [`docs/decorate.md`](./docs/decorate.md).
|
|
1146
1186
|
|
|
1147
1187
|
```typescript
|
|
1148
1188
|
import { BaseClass } from 'typeomatica';
|
|
@@ -1324,8 +1364,8 @@ rules live in [AGENTS.md](AGENTS.md).
|
|
|
1324
1364
|
|
|
1325
1365
|
**Related reading:**
|
|
1326
1366
|
- [Inheritance in JavaScript: Factory of Constructors with Prototype Chain](https://github.com/mythographica/stash/blob/master/inheritance.md)
|
|
1327
|
-
- [Architecture of Prototype Inheritance in JavaScript](https://
|
|
1328
|
-
- [Dead Simple type checker for JavaScript](https://
|
|
1367
|
+
- [Architecture of Prototype Inheritance in JavaScript](https://github.com/mythographica/stash/blob/master/inheritance_architecture.md)
|
|
1368
|
+
- [Dead Simple type checker for JavaScript](https://github.com/mythographica/typologica)
|
|
1329
1369
|
|
|
1330
1370
|
---
|
|
1331
1371
|
|