mnemonica 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/SKILL.md +214 -0
  2. package/package.json +6 -5
package/SKILL.md ADDED
@@ -0,0 +1,214 @@
1
+ ---
2
+ name: mnemonica-core
3
+ description: |
4
+ Instance inheritance for JavaScript/TypeScript: types built from parent
5
+ INSTANCES, so the prototype chain back to the root is the data's own
6
+ lineage. Use when the user mentions mnemonica, define(), lookup(),
7
+ TypeRegistry, tactica, @decorate(), instance inheritance, or when writing
8
+ code that declares or constructs mnemonica types. Covers: the builder and
9
+ tactica lineages, lookup, subtype construction, hooks, async
10
+ constructors, the instance utilities, and observability with dive + otel.
11
+ metadata:
12
+ tags: [mnemonica, typescript, prototype-inheritance, type-system, nodejs, tactica, observability]
13
+ ---
14
+
15
+ # mnemonica — usage skill for agents
16
+
17
+ mnemonica is a JavaScript runtime library for **modelling data as lineage**:
18
+ each type is constructed from an instance of its parent, so every object
19
+ carries the story of how it came to be — who it came from, with which
20
+ arguments, in which order.
21
+
22
+ ## When to use
23
+
24
+ - The code imports `mnemonica` or `@mnemonica/*`
25
+ - The task declares types (`define()`, `lazy()`, `@decorate()`) or
26
+ constructs instances
27
+ - TypeScript complains about a mnemonica constructor or subtype — see
28
+ **NEVER cast** below before touching anything
29
+
30
+ ## The model
31
+
32
+ `define(name, handler)` declares a type and returns its constructor.
33
+ A subtype is declared on its parent type and constructed **from a parent
34
+ instance**: `new parent.Child(args)`. The child's prototype chain runs
35
+ through the parent instance — it inherits the parent's data, and
36
+ `instanceof` answers every ancestor. Construction context (arguments,
37
+ parent, type) is readable with `getProps(instance)`. Do not copy parent
38
+ fields into the child by hand; the chain already carries them.
39
+
40
+ mnemonica is a **runtime** library: `define()` registers types as a side
41
+ effect, into one type graph, whatever style declared them. TypeScript
42
+ cannot see a graph built by runtime calls — the two lineages below are the
43
+ two ways to make it sound for TypeScript.
44
+
45
+ ## Rule zero: reach every constructor through `lookup()`
46
+
47
+ Get constructors with `lookup()` — never by importing them from a model
48
+ file, never by casting. Which `lookup` depends on the lineage:
49
+
50
+ | Lineage | Call | Typed from |
51
+ |---|---|---|
52
+ | Builder | `App.lookup('User.Admin')` | the builder value's own registry |
53
+ | Builder | `User.lookup('Admin')` on a type's own constructor | the builder registry; resolves **relative first** (its own subtypes), then the same string as an absolute path from the root |
54
+ | Tactica | `lookup('User.Admin')` — the free function | the global `TypeRegistry`, generated by tactica |
55
+
56
+ Paths are dot-separated nested names — the same strings everywhere.
57
+ **The builder has no free-function style**: it is `App.lookup` /
58
+ `Type.lookup` only. The free `define` / `lookup` and `@decorate()` are
59
+ what tactica exists for.
60
+
61
+ ## Lineage 1 — Builder (no tooling)
62
+
63
+ Chain `.define()` on the exported `mnemonica` object or on a
64
+ `createTypesCollection()` (an isolated collection). The returned value
65
+ carries a local registry, so everything is typed with no build step.
66
+
67
+ ```typescript
68
+ import { mnemonica } from 'mnemonica';
69
+
70
+ interface UserShape { name: string }
71
+ interface AdminShape extends UserShape { role: string }
72
+
73
+ export const App = mnemonica
74
+ .define('User', function (this: UserShape, data: { name: string }) {
75
+ this.name = data.name;
76
+ })
77
+ // names given to a constructor's .define() are relative to it
78
+ .define('Admin', function (this: AdminShape, data: { role: string }) {
79
+ this.role = data.role;
80
+ });
81
+
82
+ const User = App.lookup('User');
83
+ const user = new User({ name: 'Ada' });
84
+ const admin = new user.Admin({ role: 'root' }); // admin.name === 'Ada'
85
+ ```
86
+
87
+ Across files: export the chain and continue it where needed
88
+ (`export const Admin = User.define('Admin', …)`); the registry rides in
89
+ the value's type. Keep imports tree-shaped — no cycles.
90
+
91
+ The builder cannot type `@decorate()` classes (a class decorator never
92
+ changes the class binding's type) and cannot type the free `lookup()`.
93
+ For those, use tactica.
94
+
95
+ ## Lineage 2 — Tactica (codegen + declaration merging)
96
+
97
+ For `@decorate()`, the free `define` / `lookup`, or a codebase with free
98
+ `define()` calls everywhere. Tactica scans your code and generates the
99
+ `TypeRegistry` augmentation; TypeScript merges it into mnemonica's
100
+ declarations. **Declaration merging only works when both steps are done:**
101
+
102
+ | MUST | Why |
103
+ |---|---|
104
+ | Run `npx tactica` (or `npx tactica --watch`) after changing any `define()` / `@decorate()` | `.tactica/registry.ts` is what types `lookup()`; a stale file means stale or missing types |
105
+ | Add `.tactica/**/*` to `tsconfig.json` `include` | without it the augmentation is invisible and `lookup()` silently falls back to `TypeClass \| undefined` — the most common failure |
106
+
107
+ ```json
108
+ {
109
+ "include": ["src/**/*", ".tactica/**/*"]
110
+ }
111
+ ```
112
+
113
+ ```typescript
114
+ import { define, lookup } from 'mnemonica';
115
+
116
+ define('User', function (this: UserShape, data: { name: string }) {
117
+ this.name = data.name;
118
+ });
119
+ define('User.Admin', function (this: AdminShape, data: { role: string }) {
120
+ this.role = data.role;
121
+ });
122
+
123
+ const User = lookup('User'); // typed by .tactica
124
+ const user = new User({ name: 'Ada' });
125
+ const admin = new user.Admin({ role: 'root' });
126
+ ```
127
+
128
+ - **Runtime values** → `lookup('Name')`. **Type annotations** →
129
+ `import type { User, User_Admin } from '../.tactica/types'`.
130
+ - Never edit `.tactica/` — change the `define()` call and regenerate.
131
+ - `InstanceType<typeof X>` aliases belong only inside the model
132
+ definition files themselves; everywhere else use `.tactica/types`.
133
+ - `@decorate()` declares a root type, `@decorate(User)` a subtype of
134
+ `User`; reach them the same way, `lookup('User.Admin')`. See
135
+ [docs/decorate.md](./docs/decorate.md).
136
+
137
+ ## NEVER cast
138
+
139
+ **NEVER write `as unknown as`, `as any`, or `new (instance as any).Child(…)`
140
+ on mnemonica types.** Both lineages type everything; a cast only hides why
141
+ the types are missing.
142
+
143
+ If you feel you need one: **stop and ask the user**, explaining what
144
+ fails and that constructors must come from `lookup()`. The cause is
145
+ almost always one of:
146
+
147
+ 1. tactica was not re-run after the types changed — run `npx tactica`;
148
+ 2. `.tactica/` is not in `tsconfig.json` `include`;
149
+ 3. a constructor is imported from a model file instead of `lookup()`;
150
+ 4. a subtype is constructed from a constructor, not from its parent
151
+ instance (`new user.Admin(…)`).
152
+
153
+ ## Constructing subtypes
154
+
155
+ - Subtypes are constructed from a parent instance: `new user.Admin(…)`.
156
+ `new` on a looked-up subtype constructor without a parent instance
157
+ fails at runtime; to attach to an existing instance explicitly use
158
+ `apply(parent, Ctor, args)` / `call` / `bind`.
159
+ - `strictChain: true` (default) requires the immediate parent type;
160
+ `strictChain: false` also accepts instances further up the chain.
161
+ - A constructor resolved through a getter (circular imports) uses
162
+ `lazy()`: `User.lazy('Guest', () => GuestHandler)`.
163
+ - Match the style already in the codebase — `define()` and `@decorate()`
164
+ are runtime equivalents.
165
+
166
+ ## Features you will reach for
167
+
168
+ | Need | API | Read |
169
+ |---|---|---|
170
+ | Declare / find types | `define`, `lazy`, `lookup`, `createTypesCollection` | [FOR_HUMANS.md](./FOR_HUMANS.md) API reference |
171
+ | Construct on an existing object | `apply`, `call`, `bind` | [FOR_HUMANS.md](./FOR_HUMANS.md) |
172
+ | React to construction | `registerHook(Type, 'preCreation' \| 'postCreation' \| 'creationError', cb)` | [FOR_HUMANS.md](./FOR_HUMANS.md) |
173
+ | Async constructors | `await new Type(…)`, `awaitReturn` | [docs/async-constructors.md](./docs/async-constructors.md) |
174
+ | Type config | `strictChain`, `blockErrors`, … | [FOR_HUMANS.md](./FOR_HUMANS.md) |
175
+ | Read lineage / data | `getProps`, `utils.extract`, `pick`, `parent`, `fork` | [docs/UTILS.md](./docs/UTILS.md) |
176
+ | What sits under an instance | — | [docs/prototype-chain.md](./docs/prototype-chain.md) |
177
+ | Runtime field enforcement | typeomatica | [docs/typeomatica.md](./docs/typeomatica.md) |
178
+ | Observability | dive, otel, nestjs | [@mnemonica/dive](https://www.npmjs.com/package/@mnemonica/dive), [@mnemonica/otel](https://www.npmjs.com/package/@mnemonica/otel) |
179
+ | Live tooling | strategy, mnemographica | [docs/tooling.md](./docs/tooling.md) |
180
+
181
+ ## Where to read more
182
+
183
+ - [docs/typed-lookup.md](./docs/typed-lookup.md) — both lineages in full
184
+ - [FOR_HUMANS.md](./FOR_HUMANS.md) — the manual: every signature and option
185
+ - [README.md](./README.md) — the thesis: what mnemonica fixes and why
186
+ - [@mnemonica/tactica](https://www.npmjs.com/package/@mnemonica/tactica) —
187
+ CLI options and generated files
188
+
189
+ ## Observability: mnemonica + dive + otel
190
+
191
+ This is the reason the lineage matters in production. `@mnemonica/dive`
192
+ records execution flow over the objects themselves — no
193
+ AsyncLocalStorage, no monkey-patching — and `@mnemonica/otel` wires every
194
+ mnemonica construction into it and exports OpenTelemetry spans. Together
195
+ they give Node.js userland code near-complete observability:
196
+
197
+ - **Every construction is an edge.** With otel's `attachHooks`, each
198
+ `new` of a mnemonica type is recorded under its data-flow parent — the
199
+ instance **is** the context.
200
+ - **A failure carries its story.** An Error thrown in a wrapped call
201
+ carries its flow and its data: `getFlow(error)` and
202
+ `getErrorInstance(error)` work anywhere, including a process-level
203
+ `uncaughtException` handler.
204
+ - **Across time.** Wrap timer, queue and emitter callbacks with the
205
+ instance they serve (`dive.wrap(fn, instance)`); a callback that fires
206
+ long after its request finished still reports that request's chain.
207
+ - **No retention to tune.** History lives exactly as long as something
208
+ holds it, and is collected after — no buffers to size.
209
+
210
+ Start here: [@mnemonica/dive](https://www.npmjs.com/package/@mnemonica/dive)
211
+ (where to wrap, reading flows) and
212
+ [@mnemonica/otel](https://www.npmjs.com/package/@mnemonica/otel)
213
+ (`attachHooks`, OpenTelemetry providers). NestJS apps:
214
+ [@mnemonica/nestjs](https://www.npmjs.com/package/@mnemonica/nestjs).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mnemonica",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "AI-agent ready instance inheritance system for JavaScript/TypeScript via prototype chains",
5
5
  "type": "commonjs",
6
6
  "main": "./build/index.js",
@@ -21,6 +21,7 @@
21
21
  },
22
22
  "files": [
23
23
  "README.md",
24
+ "SKILL.md",
24
25
  "FOR_HUMANS.md",
25
26
  "LICENSE",
26
27
  "docs",
@@ -97,9 +98,9 @@
97
98
  "@eslint/js": "^9.21.0",
98
99
  "@mnemonica/tactica": "^0.1.15",
99
100
  "@types/jest": "^29.5.14",
100
- "@types/node": "^22.20.2",
101
- "@typescript-eslint/eslint-plugin": "^8.70.0",
102
- "@typescript-eslint/parser": "^8.70.0",
101
+ "@types/node": "^22.20.4",
102
+ "@typescript-eslint/eslint-plugin": "^8.70.1",
103
+ "@typescript-eslint/parser": "^8.70.1",
103
104
  "chai": "^5.3.3",
104
105
  "eslint": "^9.39.5",
105
106
  "eslint-plugin-import": "^2.32.0",
@@ -115,7 +116,7 @@
115
116
  "mocha-lcov-reporter": "^1.3.0",
116
117
  "nyc": "^17.1.0",
117
118
  "oxlint": "^0.16.12",
118
- "ts-jest": "^29.4.12",
119
+ "ts-jest": "^29.4.14",
119
120
  "typeomatica": "^0.3.63",
120
121
  "typescript": "^6.0.3"
121
122
  },