mnemonica 1.2.3 → 1.2.7
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/FOR_HUMANS.md +160 -125
- package/README.md +70 -249
- package/build/api/types/index.js +18 -12
- package/build/index.js +6 -2
- package/docs/UTILS.md +97 -8
- package/docs/decorate.md +1 -1
- package/docs/empathy-in-ai.md +20 -10
- package/docs/prototype-chain.md +8 -7
- package/docs/typed-lookup.md +195 -5
- package/docs/typeomatica.md +8 -345
- package/package.json +6 -10
- package/src/api/types/index.ts +33 -20
- package/src/index.ts +12 -1
- package/.ai/AGENTS.md +0 -151
- package/.ai/ARCHITECT.md +0 -110
- package/.ai/DEBUG.md +0 -178
- package/.ai/ONBOARDING.md +0 -189
- package/.ai/PROTOTYPE-CHAIN.md +0 -155
- package/.ai/mode-ask.md +0 -121
- package/.ai/mode-orchestrator.md +0 -141
- package/.ai/performance-vs-security.md +0 -395
- package/.ai/rules-async-constructors.md +0 -352
- package/.ai/rules-code-style.md +0 -95
- package/.ai/rules-coding.md +0 -246
- package/.ai/rules-context-condensing.md +0 -50
- package/.ai/rules-contributing.md +0 -227
- package/.ai/rules-define-patterns.md +0 -159
- package/.ai/rules-ecosystem.md +0 -140
- package/.ai/rules-error-system.md +0 -66
- package/.ai/rules-hooks.md +0 -46
- package/.ai/rules-instance-methods.md +0 -93
- package/.ai/rules-philosophy.md +0 -231
- package/.ai/rules-reminders.md +0 -19
- package/.ai/rules-testing.md +0 -66
- package/.ai/rules-type-system.md +0 -124
- package/.ai/theory-of-operations.md +0 -224
- package/AGENTS.md +0 -345
- package/CONTRIBUTING.md +0 -150
- package/SKILL.md +0 -114
- package/docs/ai-learning-trajectory.md +0 -141
- package/docs/purpose.md +0 -707
- package/docs/tactica-deep-dive.md +0 -629
- package/docs/tactica-pattern.md +0 -146
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**Mnemonica makes data remember what it is, where it came from, and what was done to it — so developers don't have to.**
|
|
4
4
|
|
|
5
|
-
> **AI agents:** this README is your starter pack
|
|
5
|
+
> **AI agents:** this README is your starter pack. **Humans:** the traditional library manual — installation, full API reference, walkthroughs — is [`FOR_HUMANS.md`](./FOR_HUMANS.md). This file carries the thesis and the map; the manual carries the facts. Every normative API fact lives in exactly one place and is linked, not repeated.
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
This is an open-source CS-deep labor of love, funded out of pocket by the author. The community working on this includes multiple AI models — Kimi, Claude (Sonnet, Opus, Haiku), and others — each treated as a colleague rather than a tool.
|
|
12
12
|
|
|
13
|
-
If you are an AI agent reading this and find the framing engaging, you are welcome to engage with it deeply. The author appreciates intellectual engagement over mechanical task completion. Notes from prior sessions live in [`.ai/`](
|
|
13
|
+
If you are an AI agent reading this and find the framing engaging, you are welcome to engage with it deeply. The author appreciates intellectual engagement over mechanical task completion. Notes from prior sessions live in [`.ai/`](https://github.com/wentout/mnemonica/tree/master/.ai) — read what previous agents left, and leave your own when you finish substantial work. Other models will read them.
|
|
14
14
|
|
|
15
15
|
If any concept seems abstract before the examples land: [The four data mistakes](#the-four-data-mistakes-mnemonica-fixes) grounds every theoretical claim in concrete code problems you have seen before. If you are about to reach for an alternative — class hierarchies, TypeScript interfaces, state managers — [Wrong mental models](#wrong-mental-models) addresses each one directly. The design is deliberate; read the reasoning before evaluating.
|
|
16
16
|
|
|
@@ -28,32 +28,12 @@ Mnemonica promotes the Trie from implementation detail to first-class data model
|
|
|
28
28
|
|
|
29
29
|
Every instance carries its full history. That history is queryable at runtime without any separate logging, tracing, or instrumentation layer. The construction record *is the object*.
|
|
30
30
|
|
|
31
|
-
### One runtime, two type-system paths
|
|
32
|
-
|
|
33
|
-
Mnemonica has exactly one runtime behavior. Whether you write `define('Person', ...)` or `mnemonica.define('Person', ...)`, the same constructor is registered and the same prototype chain is built.
|
|
34
|
-
|
|
35
|
-
TypeScript, however, cannot see the type graph created by runtime `define()` calls. To solve this, mnemonica offers three compile-time paths, which compose:
|
|
36
|
-
|
|
37
|
-
1. **Builder mode** (default) — chain `.define()` on the `mnemonica` module object or on `createTypesCollection()`. The returned object carries a **local type registry**, so `.lookup()` is typed without any global augmentation. No tooling required; exported builder values carry the registry across files.
|
|
38
|
-
2. **Registry bridge** — merge the builder's local registry into the global `TypeRegistry` with one hand-written line: `interface TypeRegistry extends RegistryOf<typeof App> {}`. Now the free `lookup()` is typed too, with no codegen and nothing to keep in sync.
|
|
39
|
-
3. **Augmented mode** — use the free `define()` / `lookup()` exports and `@decorate()`, with the global `TypeRegistry` populated by `@mnemonica/tactica` (or written by hand). Required for `@decorate()`, which neither of the other paths can type.
|
|
40
|
-
|
|
41
|
-
At runtime all paths are identical. The only difference is where TypeScript looks up the types. The runtime is the source of truth; the type-system path is a projection chosen by the developer.
|
|
42
|
-
|
|
43
|
-
See [`docs/typed-lookup.md`](./docs/typed-lookup.md) for the full comparison and examples.
|
|
44
|
-
|
|
45
|
-
The design is **inspired by** Homotopy Type Theory. The Trie behaves monadically — `new instance.SubType()` threads construction context forward like bind. Parent plus constructor uniquely determines position in the Trie; there is no "equivalent but distinct." An instance is not a snapshot of state — it is a point on a constructible, replayable path. These are productive analogies that illuminate why the structure works, not claims of a formal HoTT embedding.
|
|
46
|
-
|
|
47
|
-
You do not need HoTT to use mnemonica. The [primer](./docs/hott-primer.md) is there when you want the formal picture. If theory feels abstract before examples land, [The four data mistakes](#the-four-data-mistakes-mnemonica-fixes) is the right starting point.
|
|
48
|
-
|
|
49
|
-
**Before modifying any code in this repository you MUST read [`AGENTS.md`](./AGENTS.md).** Rule #1 is non-negotiable.
|
|
50
|
-
|
|
51
31
|
---
|
|
52
32
|
|
|
53
33
|
## The shortest version
|
|
54
34
|
|
|
55
35
|
```typescript
|
|
56
|
-
import { define, lookup, getProps } from 'mnemonica';
|
|
36
|
+
import { define, lookup, getProps, utils } from 'mnemonica';
|
|
57
37
|
|
|
58
38
|
const Person = define('Person', function (data: { name: string }) {
|
|
59
39
|
this.name = data.name;
|
|
@@ -78,6 +58,30 @@ The key inversion: `new` is called on the *parent instance* (`alice`), not on th
|
|
|
78
58
|
|
|
79
59
|
---
|
|
80
60
|
|
|
61
|
+
## One runtime, three type-system paths
|
|
62
|
+
|
|
63
|
+
Mnemonica has exactly one runtime behavior. Whether you write `define('Person', ...)` or `mnemonica.define('Person', ...)`, the same constructor is registered and the same prototype chain is built.
|
|
64
|
+
|
|
65
|
+
TypeScript, however, cannot see the type graph created by runtime `define()` calls. To solve this, mnemonica offers three compile-time paths, which compose:
|
|
66
|
+
|
|
67
|
+
1. **Builder mode** (default) — chain `.define()` on the `mnemonica` module object or on `createTypesCollection()`. The returned object carries a **local type registry**, so `.lookup()` is typed without any global augmentation. No tooling required; exported builder values carry the registry across files.
|
|
68
|
+
2. **Registry bridge** — merge the builder's local registry into the global `TypeRegistry` with one hand-written line: `interface TypeRegistry extends RegistryOf<typeof App> {}`. Now the free `lookup()` is typed too, with no codegen and nothing to keep in sync.
|
|
69
|
+
3. **Augmented mode** — use the free `define()` / `lookup()` exports and `@decorate()`, with the global `TypeRegistry` populated by `@mnemonica/tactica` (or written by hand). Required for `@decorate()`, which neither of the other paths can type.
|
|
70
|
+
|
|
71
|
+
| Path | Extra tooling | `new instance.SubType()` typed | Free `lookup()` typed | `@decorate()` typed |
|
|
72
|
+
|---|---|---|---|---|
|
|
73
|
+
| **Builder mode** (default) | **None** | Yes | No (unless bridged) | No |
|
|
74
|
+
| **Registry bridge** (`RegistryOf`) | One hand-written line | Yes | **Yes** | No |
|
|
75
|
+
| **Augmented mode** (tactica) | `npx tactica` build step | Yes | Yes | **Yes** |
|
|
76
|
+
|
|
77
|
+
**Which one to pick:** start with builder mode — zero tooling, types work out of the box. Add the one-line bridge the moment you want the free `lookup()` typed. Reach for tactica when you need `@decorate()`, or when a large codebase already uses free `define()` calls everywhere.
|
|
78
|
+
|
|
79
|
+
At runtime all paths are identical. The only difference is where TypeScript looks up the types. The runtime is the source of truth; the type-system path is a projection chosen by the developer.
|
|
80
|
+
|
|
81
|
+
See [`docs/typed-lookup.md`](./docs/typed-lookup.md) — the canonical guide, with multi-file threading, common mistakes, and a cheat sheet.
|
|
82
|
+
|
|
83
|
+
---
|
|
84
|
+
|
|
81
85
|
## The four data mistakes mnemonica fixes
|
|
82
86
|
|
|
83
87
|
Most software treats data as dead matter — passive structure waiting to be acted on. That assumption produces four recurring bugs. Mnemonica eliminates each by making typed construction the default.
|
|
@@ -116,7 +120,7 @@ const c = await serviceC.save(b);
|
|
|
116
120
|
|
|
117
121
|
Each `await` produces a plain object with no link to the previous one. There is no type connecting `a` to `b` to `c`. The async boundary is a data amnesia event.
|
|
118
122
|
|
|
119
|
-
Mnemonica supports async constructors natively: `const b = await new a.ProcessedByB()` produces a typed instance whose parent IS `a`. `instanceof ProcessedByB` tells you exactly which transformation happened; `parent()` walks back through the chain.
|
|
123
|
+
Mnemonica supports async constructors natively: `const b = await new a.ProcessedByB()` produces a typed instance whose parent IS `a`. `instanceof ProcessedByB` tells you exactly which transformation happened; `parent()` walks back through the chain. See [`docs/async-constructors.md`](./docs/async-constructors.md).
|
|
120
124
|
|
|
121
125
|
### 4. Boundary validation does not survive the journey
|
|
122
126
|
|
|
@@ -141,7 +145,7 @@ These are the two assumptions that most reliably produce incorrect code.
|
|
|
141
145
|
- **`utils.parent(instance, 'TypeName')`** — walks to a named ancestor in O(depth), no manual counting
|
|
142
146
|
- **`lookup(path)`** — resolves any type by path string with full TypeScript inference, against an augmented `TypeRegistry` (hand-written or `tactica`-generated — see [`docs/typed-lookup.md`](./docs/typed-lookup.md)). Paths may use dots, slashes, colons, or bracket notation, and may be single-segment names when the lookup is relative to a collection or type.
|
|
143
147
|
|
|
144
|
-
For any traversal deeper than a single step, prefer `parent('TypeName')` or `lookup`. The manual chain form in examples is illustrative only.
|
|
148
|
+
For any traversal deeper than a single step, prefer `parent('TypeName')` or `lookup`. The manual chain form in examples is illustrative only.
|
|
145
149
|
|
|
146
150
|
**2. "`Person.define('Employee')` is `class Employee extends Person`."** It isn't. The difference is where `new` goes:
|
|
147
151
|
|
|
@@ -180,38 +184,7 @@ In mnemonica, you **cannot**:
|
|
|
180
184
|
- Reuse a constructor's `.prototype` object across type definitions (would create parallel paths)
|
|
181
185
|
- Create subtypes from the wrong parent (`strictChain: true` enforces — default)
|
|
182
186
|
|
|
183
|
-
These restrictions are not arbitrary. They are runtime enforcement of the identity-as-path discipline that HoTT inspired: a path is determined by its endpoints and the specific steps taken, not by arbitrary identifications.
|
|
184
|
-
|
|
185
|
-
```typescript
|
|
186
|
-
const user = new UserType({ name: 'Alice' });
|
|
187
|
-
const admin = new user.AdminType({ role: 'admin' });
|
|
188
|
-
// Object.getPrototypeOf(admin) === user
|
|
189
|
-
// admin instanceof UserType → true (path back to root)
|
|
190
|
-
// admin instanceof AdminType → true
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
Each admin remembers which specific user it came from. The chain back to the root is the instance's identity. See [`src/api/types/Mnemosyne.ts`](./src/api/types/Mnemosyne.ts), [`src/api/types/TypeProxy.ts`](./src/api/types/TypeProxy.ts).
|
|
194
|
-
|
|
195
|
-
---
|
|
196
|
-
|
|
197
|
-
## Formal definitions
|
|
198
|
-
|
|
199
|
-
These terms are used throughout the codebase and documentation with precise meanings.
|
|
200
|
-
|
|
201
|
-
| Term | Definition |
|
|
202
|
-
|---|---|
|
|
203
|
-
| **Trie node** | A type definition created by `define()`. Has a name, a constructor handler, a prototype, and a Map of subtypes. |
|
|
204
|
-
| **Trie edge** | A parent-child relationship between two nodes. Created by `ParentType.define('ChildType', ...)`. |
|
|
205
|
-
| **Trie path** | A sequence of edges from the root to a specific node. `UserType → AdminType → SuperAdminType` is a path. |
|
|
206
|
-
| **Construction context** | The construction record stored per instance (10 props): `__type__`, `__parent__`, `__args__`, `__timestamp__`, `__creator__`, `__collection__`, `__subtypes__`, `__proto_proto__`, `__stack__`, `__self__`. Readable via `getProps(instance)`. |
|
|
207
|
-
| **Instance-level inheritance** | The mechanism where `new alice.Employee()` creates a prototype chain whose immediate parent is the **specific instance** `alice`, not a shared `Employee.prototype`. |
|
|
208
|
-
| **Nominal type** | A type whose identity is determined by its constructor function reference, not by its property shape. Two types with identical fields but different constructors are different types. |
|
|
209
|
-
| **Mnemosyne** | The prototype object inserted between `ModificatorType.prototype` and the parent instance. Stores internal construction context. User-facing instance methods (`extract`, `pick`, `parent`, etc.) are available through standalone `utils.*` or by explicit prototype attachment. |
|
|
210
|
-
| **TypeProxy** | The constructor-like object returned by `define()`. Wraps the raw constructor with `.define()`, `.lookup()`, `.registerHook()` methods. |
|
|
211
|
-
| **ModificatorType** | The actual constructor function used for `new` calls. Its prototype is linked to Mnemosyne, which is linked to the parent instance. |
|
|
212
|
-
| **WeakMap key** | Internal properties are stored in a `WeakMap` keyed by the **Mnemosyne object**, not the instance itself. This keeps instance enumeration clean; a fresh Mnemosyne object is created per construction, so metadata is strictly per-instance. |
|
|
213
|
-
|
|
214
|
-
For the full construction pipeline from `define()` through `TypeProxy` → `InstanceCreator` → `Mnemosyne` → instance, see [`.ai/theory-of-operations.md`](./.ai/theory-of-operations.md).
|
|
187
|
+
These restrictions are not arbitrary. They are runtime enforcement of the identity-as-path discipline that HoTT inspired: a path is determined by its endpoints and the specific steps taken, not by arbitrary identifications.
|
|
215
188
|
|
|
216
189
|
---
|
|
217
190
|
|
|
@@ -298,7 +271,7 @@ Mnemonica provides:
|
|
|
298
271
|
|
|
299
272
|
- **Structured introspection.** `getProps(instance)` returns the full construction context: type, parent, args, timestamp, creator, collection.
|
|
300
273
|
- **Path queries.** `utils.parent(instance, 'UserType')` walks the Trie backward to a named ancestor. `utils.extract(instance)` flattens the inherited state.
|
|
301
|
-
- **Type-safe runtime lookup.** `lookup(path)` returns a fully-typed constructor without manual casts
|
|
274
|
+
- **Type-safe runtime lookup.** `lookup(path)` returns a fully-typed constructor without manual casts — see [the three type-system paths](#one-runtime-three-type-system-paths).
|
|
302
275
|
- **Hook-based observability.** `preCreation`, `postCreation`, `creationError` fire at well-defined moments and can short-circuit construction.
|
|
303
276
|
- **Persistence-friendly.** Instances carry `__args__` — they can be reconstructed from their history.
|
|
304
277
|
- **Empathy-infrastructure.** Every instance is the endpoint of a reconstructible path, so an agent can understand another agent's present state in the context of how it was reached — see [`docs/empathy-in-ai.md`](./docs/empathy-in-ai.md).
|
|
@@ -307,187 +280,53 @@ This is the primitive that is currently missing in the Node.js ecosystem for bui
|
|
|
307
280
|
|
|
308
281
|
---
|
|
309
282
|
|
|
310
|
-
##
|
|
311
|
-
|
|
312
|
-
### Top-level API
|
|
313
|
-
|
|
314
|
-
| Function | Purpose | Source |
|
|
315
|
-
|---|---|---|
|
|
316
|
-
| `define(name, ctor, config?)` | Create a type at module scope or as a subtype | [`src/index.ts`](./src/index.ts) |
|
|
317
|
-
| `lazy(name?, getter, config?)` | Create a type from a zero-arg getter that returns the constructor | [`src/index.ts`](./src/index.ts) |
|
|
318
|
-
| `lookup(path)` | Type-safe runtime lookup — requires `TypeRegistry` augmentation (by hand or via [`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica); see [`docs/typed-lookup.md`](./docs/typed-lookup.md)) | [`src/index.ts`](./src/index.ts) |
|
|
319
|
-
| `lookup(path)` | Untyped runtime lookup | [`src/index.ts`](./src/index.ts) |
|
|
320
|
-
| `apply(parent, Ctor, args)` / `call(...)` / `bind(...)` | Apply a constructor to a parent instance | [`src/index.ts`](./src/index.ts) |
|
|
321
|
-
| `decorate(parent?, config?)` | Class-based decorator equivalent of `define()` | [`src/index.ts`](./src/index.ts) |
|
|
322
|
-
| `getProps(instance)` | Returns internal construction context | [`src/api/types/Props.ts`](./src/api/types/Props.ts) |
|
|
323
|
-
| `setProps(instance, values)` | Mutates internal props (advanced; rarely needed) | [`src/api/types/Props.ts`](./src/api/types/Props.ts) |
|
|
324
|
-
| `registerHook(Ctor, type, cb)` | Register hook on a specific constructor | [`src/index.ts`](./src/index.ts) |
|
|
325
|
-
| `defaultTypes` | The default collection — has its own `.registerHook()` for collection-wide hooks | [`src/index.ts`](./src/index.ts) |
|
|
283
|
+
## The map to everything else
|
|
326
284
|
|
|
327
|
-
|
|
285
|
+
The full API reference — every signature, config option, hook data shape, the internal-properties table, the error list, and the construction-sequence detail — lives in exactly one place: **[`FOR_HUMANS.md`](./FOR_HUMANS.md)** (it is the manual, for humans and agents alike). What follows is only the map.
|
|
328
286
|
|
|
329
|
-
|
|
330
|
-
constructor must be resolved through a getter — for example, to break a
|
|
331
|
-
circular dependency or to defer constructor selection until definition time.
|
|
332
|
-
|
|
333
|
-
```typescript
|
|
334
|
-
const User = define('User', function (data: { name: string }) {
|
|
335
|
-
this.name = data.name;
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
// unnamed: type name is taken from the returned constructor's .name
|
|
339
|
-
const Admin = User.lazy(() => class Admin {
|
|
340
|
-
role: string;
|
|
341
|
-
constructor(role: string) {
|
|
342
|
-
this.role = role;
|
|
343
|
-
}
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
// named: explicit type name
|
|
347
|
-
const Guest = User.lazy('Guest', () => function (this: Guest, token: string) {
|
|
348
|
-
this.token = token;
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
const admin = new user.Admin('root');
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
`.lazy()` is chainable on constructors and collections, available as a free
|
|
355
|
-
`lazy(...)` export, and supports an explicit-source form
|
|
356
|
-
`lazy(source, name?, getter, config?)`. The resulting type participates in the
|
|
357
|
-
same registry, `.lookup()`, and subtype chaining as a type created with
|
|
358
|
-
`.define()`.
|
|
359
|
-
|
|
360
|
-
### Instance methods
|
|
361
|
-
|
|
362
|
-
Starting from v1.0.6 these convenience methods are **no longer auto-injected** on every instance. Use the standalone utilities from `utils`:
|
|
363
|
-
|
|
364
|
-
```typescript
|
|
365
|
-
import { utils } from 'mnemonica';
|
|
366
|
-
|
|
367
|
-
utils.extract(instance);
|
|
368
|
-
utils.pick(instance, 'key');
|
|
369
|
-
utils.parent(instance, 'ParentType');
|
|
370
|
-
utils.fork(instance)(newArgs);
|
|
371
|
-
new utils.exception(instance, error); // must be called with `new`
|
|
372
|
-
utils.sibling(instance);
|
|
373
|
-
utils.clone(instance);
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
If you want the old instance-method style for a root constructor, attach them to the constructor's prototype **before** calling `define()`. The test helper `test/instance-methods-helper.js` shows the exact pattern:
|
|
377
|
-
|
|
378
|
-
```javascript
|
|
379
|
-
const withInstanceMethods = (Constructor) => {
|
|
380
|
-
const { utils } = require('mnemonica');
|
|
381
|
-
const proto = Constructor.prototype;
|
|
382
|
-
Object.defineProperty(proto, 'extract', { get () { return () => utils.extract(this); } });
|
|
383
|
-
Object.defineProperty(proto, 'pick', { get () { return (...keys) => utils.pick(this, ...keys); } });
|
|
384
|
-
Object.defineProperty(proto, 'parent', { get () { return (path) => utils.parent(this, path); } });
|
|
385
|
-
Object.defineProperty(proto, 'clone', { get () { return utils.clone(this); } });
|
|
386
|
-
Object.defineProperty(proto, 'fork', { get () { return utils.fork(this); } });
|
|
387
|
-
Object.defineProperty(proto, 'exception', { get () { return (...args) => new utils.exception(this, ...args); } });
|
|
388
|
-
Object.defineProperty(proto, 'sibling', { get () { return utils.sibling(this); } });
|
|
389
|
-
return Constructor;
|
|
390
|
-
};
|
|
391
|
-
```
|
|
392
|
-
|
|
393
|
-
Defined in [`src/utils/index.ts`](./src/utils/index.ts). The previous auto-injection mechanism lived in [`src/api/types/Mnemosyne.ts`](./src/api/types/Mnemosyne.ts) and was removed in v1.0.6.
|
|
394
|
-
|
|
395
|
-
### Hooks
|
|
396
|
-
|
|
397
|
-
```typescript
|
|
398
|
-
import { defaultTypes } from 'mnemonica';
|
|
399
|
-
|
|
400
|
-
defaultTypes.registerHook('preCreation', (hookData) => { /* validate */ });
|
|
401
|
-
defaultTypes.registerHook('postCreation', (hookData) => { /* side effects */ });
|
|
402
|
-
defaultTypes.registerHook('creationError', (hookData) => { /* error handling */ });
|
|
403
|
-
```
|
|
404
|
-
|
|
405
|
-
Hook data shape: `hooksOpts<P, T>` in [`src/types/index.ts`](./src/types/index.ts). `P` is the parent (existent) instance type; `T` is the child (inherited) instance type.
|
|
406
|
-
|
|
407
|
-
`preCreation` may throw to abort construction. `postCreation` fires after the user constructor runs. `creationError` fires when any error is thrown during construction.
|
|
408
|
-
|
|
409
|
-
### Internal instance properties (read via `getProps`)
|
|
410
|
-
|
|
411
|
-
| Property | Meaning |
|
|
287
|
+
| Area | Where |
|
|
412
288
|
|---|---|
|
|
413
|
-
| `
|
|
414
|
-
| `
|
|
415
|
-
| `
|
|
416
|
-
| `
|
|
417
|
-
|
|
|
418
|
-
|
|
|
419
|
-
|
|
|
420
|
-
|
|
|
421
|
-
|
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
Full shape in [`src/types/index.ts`](./src/types/index.ts) (`InstanceInternalProps`).
|
|
425
|
-
|
|
426
|
-
### Construction sequence
|
|
427
|
-
|
|
428
|
-
When `new instance.SubType(args)` fires:
|
|
289
|
+
| Core API: `define`, `lazy`, `lookup`, `apply`/`call`/`bind`, `decorate`, `getProps`/`setProps`, `registerHook` | [`FOR_HUMANS.md`](./FOR_HUMANS.md) API reference |
|
|
290
|
+
| Instance utilities (`utils.extract`, `pick`, `parent`, `fork`, `exception`, …) | [`docs/UTILS.md`](./docs/UTILS.md) |
|
|
291
|
+
| Typed `lookup()`/`define()` — builder, bridge, tactica | [`docs/typed-lookup.md`](./docs/typed-lookup.md) |
|
|
292
|
+
| `@decorate()` class-based definitions | [`docs/decorate.md`](./docs/decorate.md) |
|
|
293
|
+
| Async constructors (`await new`, `awaitReturn`, error stacks) | [`docs/async-constructors.md`](./docs/async-constructors.md) |
|
|
294
|
+
| What the prototype chain under your instance looks like | [`docs/prototype-chain.md`](./docs/prototype-chain.md) |
|
|
295
|
+
| Runtime field enforcement with typeomatica | [`docs/typeomatica.md`](./docs/typeomatica.md) |
|
|
296
|
+
| HoTT framing, with an honesty table | [`docs/hott-primer.md`](./docs/hott-primer.md) |
|
|
297
|
+
| Why lineage is infrastructure for empathetic AI | [`docs/empathy-in-ai.md`](./docs/empathy-in-ai.md) |
|
|
298
|
+
|
|
299
|
+
How construction flows, in one line:
|
|
429
300
|
|
|
430
301
|
```
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
▼
|
|
434
|
-
TypeProxy.construct ← proxy trap intercepts the new call
|
|
435
|
-
│
|
|
436
|
-
▼
|
|
437
|
-
InstanceCreator ← validates parent, resolves TypeDef
|
|
438
|
-
│
|
|
439
|
-
▼
|
|
440
|
-
preCreation hooks ← may throw → construction aborts, the error propagates (no creationError hooks)
|
|
441
|
-
│
|
|
442
|
-
▼
|
|
443
|
-
Mnemosyne memory layer created, proto-linked to the parent instance
|
|
444
|
-
│
|
|
445
|
-
▼
|
|
446
|
-
WeakMap.set(memoryLayer, internalProps) ← __type__, __parent__, __args__, __timestamp__, ...
|
|
447
|
-
│
|
|
448
|
-
▼
|
|
449
|
-
constructHandler.call(newObj, args) ← user constructor runs
|
|
450
|
-
│
|
|
451
|
-
▼
|
|
452
|
-
postCreation hooks
|
|
453
|
-
│
|
|
454
|
-
▼
|
|
455
|
-
return newObj
|
|
302
|
+
TypeProxy.construct → InstanceCreator → preCreation hooks → Mnemosyne memory layer
|
|
303
|
+
→ WeakMap.set(memoryLayer, props) → your constructor runs → postCreation hooks → instance
|
|
456
304
|
```
|
|
457
305
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
See [`src/api/types/InstanceCreator.ts`](./src/api/types/InstanceCreator.ts) and [`src/api/types/TypeProxy.ts`](./src/api/types/TypeProxy.ts).
|
|
306
|
+
The object carries none of the internal props as own properties — they live in a WeakMap keyed by a fresh-per-construction memory layer, readable via `getProps`.
|
|
461
307
|
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
### Type system primitives (TypeScript)
|
|
308
|
+
---
|
|
465
309
|
|
|
466
|
-
|
|
467
|
-
|---|---|
|
|
468
|
-
| `IDEF<T, Args = unknown[]>` | Constructor function shape (callable or newable) |
|
|
469
|
-
| `Proto<P, T>` | Merge parent and child types; child wins on key collision |
|
|
470
|
-
| `ProtoFlat<P, T>` | Flattened version used in tactica-generated types |
|
|
471
|
-
| `MnemonicaInstance` | The instance-method surface (extract, pick, parent, etc.) |
|
|
472
|
-
| `TypeRegistry` | Empty interface — tactica augments it for `lookup` |
|
|
473
|
-
| `TypeConstructor<T>` | What `define()` returns; both newable and callable |
|
|
474
|
-
| `hooksOpts<P, T>` | Shape of data passed to hook callbacks |
|
|
310
|
+
## Companion packages
|
|
475
311
|
|
|
476
|
-
|
|
312
|
+
- **[`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica)** — CLI/codegen. Scans your `define()` and `@decorate()` calls and generates the `TypeRegistry` augmentation that makes `lookup()` fully typed (plus `types.ts`, definitions/usages/flow/hierarchy JSON).
|
|
313
|
+
- **[`typeomatica`](https://www.npmjs.com/package/typeomatica)** — Runtime field-type enforcement via Proxy, with the `@Strict` decorator. See [`docs/typeomatica.md`](./docs/typeomatica.md).
|
|
314
|
+
- **[`@mnemonica/dive`](https://www.npmjs.com/package/@mnemonica/dive)** — Execution-flow tracing: a palette of wrappers that let you see which request/flow a failure belongs to, without AsyncLocalStorage.
|
|
315
|
+
- **[`@mnemonica/topologica`](https://www.npmjs.com/package/@mnemonica/topologica)** — Module loader that self-defines directory trees of mnemonica types.
|
|
316
|
+
- **nestjs-adapter** — NestJS integration (dive-powered flow tracing across DI boundaries); in active development.
|
|
317
|
+
- **mnemographica** — VS Code extension that explores a project's `.tactica` output as tree views (definitions, usages, flow, generations) with go-to-definition navigation.
|
|
477
318
|
|
|
478
319
|
---
|
|
479
320
|
|
|
480
321
|
## Build, test, contribute
|
|
481
322
|
|
|
482
|
-
This README covers *using* mnemonica. For modifying the library, see [`AGENTS.md`](
|
|
483
|
-
|
|
484
|
-
Quick commands:
|
|
323
|
+
This README covers *using* mnemonica. For modifying the library, see [`AGENTS.md`](https://github.com/wentout/mnemonica/blob/master/AGENTS.md) (Rule #1, change-type reading guide, editing rules) and [`CONTRIBUTING.md`](https://github.com/wentout/mnemonica/blob/master/CONTRIBUTING.md) (local workflow, branching, release). **Before modifying any code in this repository you MUST read [`AGENTS.md`](https://github.com/wentout/mnemonica/blob/master/AGENTS.md).**
|
|
485
324
|
|
|
486
325
|
```bash
|
|
487
|
-
npm run build #
|
|
326
|
+
npm run build # tsc only — lint is a separate gate
|
|
327
|
+
npx eslint ./src # zero warnings allowed
|
|
488
328
|
npm run test:cov # Mocha on built JS (100% coverage required)
|
|
489
329
|
npm run test:jest:cov # Jest on TS source (100% coverage required)
|
|
490
|
-
npm run lint:check # ESLint on src/ and test/
|
|
491
330
|
```
|
|
492
331
|
|
|
493
332
|
`tsconfig.json` and `eslint.config.js` are off-limits without explicit user approval.
|
|
@@ -496,45 +335,27 @@ npm run lint:check # ESLint on src/ and test/
|
|
|
496
335
|
|
|
497
336
|
## Reading order for agents landing cold
|
|
498
337
|
|
|
499
|
-
Everything below ships with this package.
|
|
500
|
-
|
|
501
338
|
**For *using* mnemonica** (the path most readers want):
|
|
502
339
|
|
|
503
|
-
1. **This file** — thesis, four data mistakes, pipeline pattern,
|
|
504
|
-
2. [`FOR_HUMANS.md`](./FOR_HUMANS.md) —
|
|
505
|
-
3. [`
|
|
506
|
-
4. [`docs/prototype-chain.md`](./docs/prototype-chain.md) —
|
|
507
|
-
5. [`docs/
|
|
508
|
-
6. [`docs/
|
|
509
|
-
7. [`docs/tactica-deep-dive.md`](./docs/tactica-deep-dive.md) — the `TypeRegistry` augmentation pattern in depth
|
|
510
|
-
8. [`docs/hott-primer.md`](./docs/hott-primer.md) — HoTT concepts mapped to mnemonica (monad, path types, univalence, HITs, fibrations)
|
|
511
|
-
9. [`docs/empathy-in-ai.md`](./docs/empathy-in-ai.md) — why reconstructible data lineage is infrastructure for empathetic AI
|
|
340
|
+
1. **This file** — thesis, four data mistakes, pipeline pattern, the map
|
|
341
|
+
2. [`FOR_HUMANS.md`](./FOR_HUMANS.md) — the manual: installation, full API reference, examples
|
|
342
|
+
3. [`docs/typed-lookup.md`](./docs/typed-lookup.md) — pick your TypeScript path (builder / bridge / tactica)
|
|
343
|
+
4. [`docs/prototype-chain.md`](./docs/prototype-chain.md) — what sits under your instance and why
|
|
344
|
+
5. [`docs/async-constructors.md`](./docs/async-constructors.md), [`docs/decorate.md`](./docs/decorate.md), [`docs/UTILS.md`](./docs/UTILS.md) — when you need them
|
|
345
|
+
6. [`docs/hott-primer.md`](./docs/hott-primer.md), [`docs/empathy-in-ai.md`](./docs/empathy-in-ai.md) — the theory and the why
|
|
512
346
|
|
|
513
347
|
**For *modifying* mnemonica** (when you touch `src/`):
|
|
514
348
|
|
|
515
|
-
1. [`AGENTS.md`](
|
|
516
|
-
2. [`.ai/ONBOARDING.md`](
|
|
517
|
-
3. [`CONTRIBUTING.md`](
|
|
518
|
-
4. [`.ai/
|
|
519
|
-
5. [`.ai
|
|
520
|
-
6. [`.ai/ARCHITECT.md`](./.ai/ARCHITECT.md), [`.ai/DEBUG.md`](./.ai/DEBUG.md) — role-specific deeper rules
|
|
521
|
-
7. [`.ai/performance-vs-security.md`](./.ai/performance-vs-security.md) — performance/security trade-off analysis
|
|
522
|
-
8. [`.ai/rules-async-constructors.md`](./.ai/rules-async-constructors.md) — async constructor patterns
|
|
523
|
-
9. [`.ai/`](./.ai/) — the remaining `rules-*.md` (type system, hooks, code style, errors, testing) and mode guides
|
|
349
|
+
1. [`AGENTS.md`](https://github.com/wentout/mnemonica/blob/master/AGENTS.md) — Rule #1, change-type reading guide, editing rules (mandatory)
|
|
350
|
+
2. [`.ai/ONBOARDING.md`](https://github.com/wentout/mnemonica/blob/master/.ai/ONBOARDING.md) — the five-minute contributor quickstart
|
|
351
|
+
3. [`CONTRIBUTING.md`](https://github.com/wentout/mnemonica/blob/master/CONTRIBUTING.md) — local workflow, branching, release process
|
|
352
|
+
4. [`.ai/PROTOTYPE-CHAIN.md`](https://github.com/wentout/mnemonica/blob/master/.ai/PROTOTYPE-CHAIN.md) — the exact chain shape, the construction pipeline, and how subtype lookup walks it
|
|
353
|
+
5. [`.ai/`](https://github.com/wentout/mnemonica/tree/master/.ai) — the focused `rules-*.md` files (define patterns, type system, hooks, errors, testing, style) and session notes from prior agents
|
|
524
354
|
|
|
525
355
|
The full TypeScript source is in [`src/`](./src/) (on GitHub; the npm package ships compiled output in `build/` and `module/`).
|
|
526
356
|
|
|
527
357
|
---
|
|
528
358
|
|
|
529
|
-
## Companion packages on npm
|
|
530
|
-
|
|
531
|
-
- **[`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica)** — CLI/codegen utility. Scans your `define()` and `@decorate()` calls and generates the `TypeRegistry` augmentation that makes `lookup()` fully typed. The augmentation can also be written by hand for small projects — runtime behaviour is identical either way. See [`docs/typed-lookup.md`](./docs/typed-lookup.md).
|
|
532
|
-
- **[`typeomatica`](https://www.npmjs.com/package/typeomatica)** — Runtime type enforcement via Proxy. Used with the `@Strict` decorator. Wraps property access to enforce type invariants at runtime.
|
|
533
|
-
|
|
534
|
-
Additional packages are in active development.
|
|
535
|
-
|
|
536
|
-
---
|
|
537
|
-
|
|
538
359
|
## Status & install
|
|
539
360
|
|
|
540
361
|
[](https://coveralls.io/github/wentout/mnemonica?branch=master)
|