mnemonica 1.0.9 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/.ai/AGENTS.md +31 -18
  2. package/.ai/DEBUG.md +7 -8
  3. package/.ai/ONBOARDING.md +23 -6
  4. package/.ai/PROTOTYPE-CHAIN.md +21 -0
  5. package/.ai/{ask/AGENTS.md → mode-ask.md} +1 -1
  6. package/.ai/{orchestrator/AGENTS.md → mode-orchestrator.md} +2 -19
  7. package/{docs → .ai}/performance-vs-security.md +1 -1
  8. package/.ai/{rules-skill/async-constructors.md → rules-async-constructors.md} +66 -0
  9. package/.ai/{rules/CODING.md → rules-coding.md} +19 -2
  10. package/.ai/{rules/CONTEXT-CONDENSING.md → rules-context-condensing.md} +2 -2
  11. package/.ai/{rules-skill/contributing.md → rules-contributing.md} +35 -0
  12. package/.ai/{rules-skill/instance-methods.md → rules-instance-methods.md} +1 -1
  13. package/.ai/rules-reminders.md +19 -0
  14. package/.ai/{rules-skill/type-system.md → rules-type-system.md} +1 -1
  15. package/{docs → .ai}/theory-of-operations.md +4 -4
  16. package/AGENTS.md +96 -24
  17. package/FOR_HUMANS.md +32 -76
  18. package/README.md +35 -20
  19. package/SKILL.md +20 -20
  20. package/build/api/index.d.ts +2 -2
  21. package/build/api/types/index.js +8 -3
  22. package/build/descriptors/types/index.js +2 -2
  23. package/build/index.d.ts +10 -3
  24. package/build/index.js +17 -5
  25. package/build/types/index.d.ts +60 -21
  26. package/build/types/index.js +1 -1
  27. package/docs/ai-learning-trajectory.md +2 -3
  28. package/docs/decorate.md +123 -0
  29. package/docs/hott-primer.md +1 -1
  30. package/docs/purpose.md +3 -3
  31. package/{.ai/TACTICA-DEEP-DIVE.md → docs/tactica-deep-dive.md} +45 -2
  32. package/docs/tactica-pattern.md +1 -2
  33. package/docs/typed-lookup.md +337 -93
  34. package/package.json +6 -6
  35. package/src/api/types/index.ts +14 -4
  36. package/src/descriptors/types/index.ts +63 -58
  37. package/src/index.ts +103 -12
  38. package/src/types/index.ts +241 -41
  39. package/.ai/CODE.md +0 -111
  40. package/.ai/TACTICA-RULES.md +0 -103
  41. package/.ai/async_init.md +0 -94
  42. package/.ai/rules/REMINDERS.md +0 -63
  43. package/.ai/rules-skill/lookup-typed.md +0 -82
  44. package/.ai/rules-skill/proxy-architecture.md +0 -63
  45. package/.ai/task-templates/new-feature.md +0 -46
  46. /package/.ai/{rules-skill/code-style.md → rules-code-style.md} +0 -0
  47. /package/.ai/{rules-skill/define-patterns.md → rules-define-patterns.md} +0 -0
  48. /package/.ai/{rules-skill/ecosystem.md → rules-ecosystem.md} +0 -0
  49. /package/.ai/{rules-skill/error-system.md → rules-error-system.md} +0 -0
  50. /package/.ai/{rules-skill/hooks.md → rules-hooks.md} +0 -0
  51. /package/.ai/{rules-skill/philosophy.md → rules-philosophy.md} +0 -0
  52. /package/.ai/{rules-skill/testing.md → rules-testing.md} +0 -0
@@ -1,161 +1,405 @@
1
- # Typed lookup with `lookup()` — with or without tactica
1
+ # Type-safe `lookup()` and `define()` — with or without `@mnemonica/tactica`
2
2
 
3
- `lookup()` is a runtime lookup with compile-time type safety. The type safety comes from one mechanism: declaration merging on the `TypeRegistry` interface that mnemonica exports. The path argument may use dots, slashes, colons, or bracket notation (e.g. `'UserType.AdminType'`, `'UserType/AdminType'`, `'UserType:AdminType'`, or `'UserType[AdminType]'`), and a single-segment name is resolved relative to the collection or type constructor the lookup is called on.
3
+ `mnemonica` has exactly **one runtime**. Every `define()` call registers into the
4
+ same type graph, and every `lookup()` resolves against it, no matter which API
5
+ style you use.
6
+
7
+ TypeScript, however, cannot see a type graph built by runtime calls. There are
8
+ **three ways** to hand the registry to the compiler, and they compose:
9
+
10
+ | Approach | Extra tooling | `new instance.SubType()` typed | Free `lookup()` typed | `@decorate()` typed |
11
+ |---|---|---|---|---|
12
+ | **Builder mode** (default) | **None** | Yes | No (unless bridged) | No |
13
+ | **Registry bridge** (`RegistryOf`) | One hand-written line | Yes | **Yes** | No |
14
+ | **Tactica** (codegen) | `npx tactica` build step | Yes | Yes | **Yes** |
15
+
16
+ > **Rule of thumb:** start with builder mode — zero dependencies, zero codegen,
17
+ > types work out of the box. Add the one-line bridge the moment you want free
18
+ > `lookup()` typed. Reach for Tactica when you need `@decorate()`, or when a
19
+ > large codebase already uses free `define()` calls everywhere.
20
+
21
+ ---
22
+
23
+ ## Builder mode (default)
24
+
25
+ Import the module object or create a custom collection, then chain `.define()` calls. The returned object carries a local type registry.
4
26
 
5
27
  ```typescript
6
- // src/index.ts (mnemonica core)
7
- export interface TypeRegistry {
8
- // Intentionally empty. Augment this interface via declaration merging
9
- // (hand-written or generated by @mnemonica/tactica).
28
+ import { mnemonica } from 'mnemonica';
29
+
30
+ interface UserShape {
31
+ name: string;
32
+ }
33
+
34
+ interface AdminShape extends UserShape {
35
+ role: string;
10
36
  }
11
37
 
12
- export function lookup<const K extends keyof TypeRegistry>(
13
- this: unknown,
14
- TypeNestedPath: K
15
- ): TypeRegistry[K];
16
- export function lookup(
17
- this: unknown,
18
- TypeNestedPath: string
19
- ): TypeClass | undefined;
38
+ const App = mnemonica
39
+ .define('User', function (this: UserShape, data: { name: string }) {
40
+ this.name = data.name;
41
+ })
42
+ // Names passed to a constructor's `.define()` are relative to that constructor.
43
+ .define('Admin', function (this: AdminShape, data: { role: string }) {
44
+ this.role = data.role;
45
+ });
46
+
47
+ // `App.lookup('User')` is typed from the builder registry.
48
+ const User = App.lookup('User');
49
+ const user = new User({ name: 'Ada' });
50
+
51
+ // Subtypes are constructed from a parent instance.
52
+ const admin = new user.Admin({ role: 'root' });
20
53
  ```
21
54
 
22
- Until you (or a tool) augment `TypeRegistry`, `lookup('Anything')` falls back to the broad `TypeClass | undefined` return type.
55
+ ### Custom collections
23
56
 
24
- Two ways to do the augmentation: **by hand**, or via **[`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica)**. Runtime behavior is identical. The trade-off is purely maintenance overhead.
57
+ `createTypesCollection()` works the same way and is fully isolated from the default collection.
25
58
 
26
- ---
59
+ ```typescript
60
+ import { createTypesCollection } from 'mnemonica';
61
+
62
+ const AppCollection = createTypesCollection()
63
+ .define('User', function (this: UserShape, data: { name: string }) {
64
+ this.name = data.name;
65
+ })
66
+ .define('Admin', function (this: AdminShape, data: { role: string }) {
67
+ this.role = data.role;
68
+ });
69
+
70
+ const User = AppCollection.lookup('User');
71
+ const user = new User({ name: 'Ada' });
72
+ const admin = new user.Admin({ role: 'root' });
73
+ ```
27
74
 
28
- ## Without tactica — hand-augmented `TypeRegistry`
75
+ ### Lookup resolution on a constructor
29
76
 
30
- Define your types as usual:
77
+ A constructor's own `lookup()` resolves in two steps: **relative first, then
78
+ root fallback**. It searches the type's own subtypes (a relative path), and if
79
+ nothing matches it resolves the same string as an absolute path from the
80
+ collection root. These are all equivalent:
31
81
 
32
82
  ```typescript
33
- // src/types.ts
34
- import { define } from 'mnemonica';
83
+ const AdminA = App.lookup('User.Admin'); // absolute, via root fallback
84
+ const AdminB = App.lookup('User').lookup('Admin'); // root, then relative
85
+ const AdminC = App.lookup('Admin'); // works only if App has a descendant named Admin
86
+ ```
35
87
 
36
- export const Person = define('Person', function (this: { name: string }, data: { name: string }) {
37
- this.name = data.name;
38
- });
88
+ Relative resolution wins when both could match: if a type has its own subtype
89
+ named `Admin`, `lookup('Admin')` returns that subtype, never a different
90
+ root-level `Admin`.
91
+
92
+ You can also look up a root constructor and call `.define()` on it directly:
93
+
94
+ ```typescript
95
+ const User = App.lookup('User');
39
96
 
40
- export const Employee = Person.define('Employee', function (this: { role: string }, data: { role: string }) {
41
- this.role = data.role;
97
+ User.define('Guest', function (this: GuestShape, data: { token: string }) {
98
+ this.token = data.token;
42
99
  });
43
100
  ```
44
101
 
45
- Write a `.d.ts` file (or any `.ts` file outside the build output) with the augmentation and the matching instance shapes:
102
+ ### Constructing subtypes
103
+
104
+ Subtypes must be created from a parent instance. Do not call `new` on a constructor returned by `lookup()` for a subtype.
105
+
106
+ ```typescript
107
+ // OK
108
+ const user = new User({ name: 'Ada' });
109
+ const admin = new user.Admin({ role: 'root' });
110
+
111
+ // WRONG at runtime
112
+ const Admin = App.lookup('User.Admin');
113
+ const admin2 = new Admin({ role: 'root' }); // fails: no parent instance
114
+ ```
115
+
116
+ Direct `new` on a constructor returned by `.define()` works for **root types** only. For subtypes the parent instance is required (or use `apply(parentInstance, Ctor, args)`). With `strictChain: true` (the default) the parent must be the immediate type; with `strictChain: false` ancestor instances further up the chain are also accepted.
117
+
118
+ ---
119
+
120
+ ## Multi-file usage: registry threading
121
+
122
+ **Yes — an exported builder value works across files.** Two reasons:
123
+
124
+ - **Runtime:** every chain registers into the same collection singleton
125
+ (`defaultTypes`, or the one `createTypesCollection()` you share), so imports
126
+ are irrelevant to what exists at runtime.
127
+ - **Compile time:** the registry rides in the *value's type*
128
+ (`IDefinitorInstance<..., Registry, Path>`), and values cross files freely
129
+ through `export` / `import`.
130
+
131
+ Split the chain through exports:
46
132
 
47
133
  ```typescript
48
- // types/mnemonica-registry.d.ts
49
- import type { ProtoFlat } from 'mnemonica';
134
+ // models/user.ts
135
+ import { mnemonica } from 'mnemonica';
136
+ export const User = mnemonica.define('User', function (this: UserShape, data: { name: string }) {
137
+ this.name = data.name;
138
+ });
139
+
140
+ // models/admin.ts
141
+ import { User } from './user';
142
+ export const Admin = User.define('Admin', function (this: AdminShape, data: { role: string }) {
143
+ this.role = data.role;
144
+ });
145
+ // Admin's type carries the accumulated registry: 'User' + 'User.Admin'
146
+
147
+ // anywhere.ts
148
+ import { Admin } from './models/admin';
149
+ const AdminCtor = Admin.lookup('User.Admin'); // typed
150
+ ```
151
+
152
+ Rule: thread the chain through exports and keep imports tree-shaped (no
153
+ cycles). Each file sees exactly the types registered up to its point in the
154
+ chain.
155
+
156
+ ---
50
157
 
51
- // Instance shapes — what `lookup` returns will construct.
52
- export type PersonInstance = {
53
- name : string;
54
- // Subtypes available on instances of this type:
55
- Employee : new (data: { role: string }) => EmployeeInstance;
56
- };
158
+ ## The registry bridge: `RegistryOf`
57
159
 
58
- export type EmployeeInstance = ProtoFlat<PersonInstance, {
59
- role : string;
60
- Employee : undefined; // can't re-instantiate the same level
61
- }>;
160
+ The builder registry is local to the value that carries it. If you also want
161
+ the **free** `lookup()` typed — without importing the builder everywhere — merge
162
+ the local registry into the global `TypeRegistry` with one hand-written line:
163
+
164
+ ```typescript
165
+ // registry.ts
166
+ import type { RegistryOf } from 'mnemonica';
167
+ import type { App } from './models';
62
168
 
63
169
  declare module 'mnemonica' {
64
- interface TypeRegistry {
65
- 'Person' : new (data: { name: string }) => PersonInstance;
66
- 'Person.Employee' : new (data: { role: string }) => EmployeeInstance;
67
- }
170
+ interface TypeRegistry extends RegistryOf<typeof App> {}
68
171
  }
69
172
  ```
70
173
 
71
- Include the `.d.ts` in `tsconfig.json`:
174
+ ```typescript
175
+ // anywhere.ts — import './registry' once (side effect)
176
+ import './registry';
177
+ import { lookup } from 'mnemonica';
72
178
 
73
- ```json
74
- {
75
- "include": ["src/**/*.ts", "types/**/*.d.ts"]
76
- }
179
+ const Admin = lookup('User.Admin'); // fully typed, no Tactica
77
180
  ```
78
181
 
79
- Use it:
182
+ Why prefer this over hand-written augmentation (below): **single source of
183
+ truth**. The builder chain is the registry; the interface just re-exports it
184
+ into the global slot. Rename or restructure a type in the chain and the bridge
185
+ follows automatically — no second file to keep in sync, no codegen step.
186
+
187
+ Caveats:
188
+
189
+ - Keep the bridge in its own module (`registry.ts`). If a file both defines
190
+ the chain and uses free `lookup()`, import order decides whether the
191
+ augmentation is visible — a dedicated module avoids the question.
192
+ - The bridge cannot type `@decorate()` classes. A class decorator's return
193
+ type never changes the class binding's declared type (TypeScript limitation,
194
+ not mnemonica's), so decorated classes have no local registry to extract —
195
+ that is what Tactica is for.
196
+
197
+ ---
198
+
199
+ ## Explicit source: `lookup(source, path)` and `define(source, name, handler)`
200
+
201
+ The free `lookup()` resolves against the global `TypeRegistry`. The free
202
+ `define()` resolves against the default collection. When you hold a builder
203
+ value and want the free-function style anyway, pass it as the first argument —
204
+ the registry is then inferred from the source, not from the global interface:
80
205
 
81
206
  ```typescript
82
- import { lookup } from 'mnemonica';
207
+ import { lookup, define } from 'mnemonica';
208
+
209
+ // typed from App's registry, never a silent `TypeClass | undefined`
210
+ const Admin = lookup(App, 'User.Admin');
83
211
 
84
- const Person = lookup('Person'); // fully typed
85
- const Employee = lookup('Person.Employee'); // fully typed
212
+ // same semantics as User.define('Guest', ...) — a subtype of User
213
+ const Guest = define(User, 'Guest', function (this: GuestShape, data: { token: string }) {
214
+ this.token = data.token;
215
+ });
86
216
 
87
- const alice = new Person({ name: 'Alice' });
88
- const engineer = new alice.Employee({ role: 'Engineer' }); // alice.Employee is typed
217
+ // same semantics as collection.define('NewRoot', ...) — a root type
218
+ const NewRoot = define(AppCollection, 'NewRoot', function (this: NewRootShape) { /* ... */ });
89
219
  ```
90
220
 
91
- `ProtoFlat<Parent, OwnProps>` is exported from `mnemonica` and merges the parent's instance shape with the subtype's own properties. The pattern is exactly what tactica generates — see the real example at `finecut/express/.tactica/types.ts` in this project's ecosystem.
221
+ Semantics follow the source object: a **collection** source defines a root
222
+ type, a **constructor** source defines a subtype. `lookup(source, path)`
223
+ resolves exactly like `source.lookup(path)` — relative first, root fallback.
92
224
 
93
225
  ---
94
226
 
95
- ## With tactica — generated augmentation
227
+ ## Augmented mode (Tactica): for `@decorate()` and free-`define` codebases
96
228
 
97
- Install:
229
+ If you use Tactica, you can write plain free functions and class decorators. Tactica scans your code and generates a `TypeRegistry` augmentation file.
98
230
 
99
- ```bash
100
- npm install --save-dev @mnemonica/tactica
101
- ```
231
+ ```typescript
232
+ import { define, lookup, decorate } from 'mnemonica';
233
+
234
+ interface UserShape {
235
+ name: string;
236
+ }
237
+
238
+ interface AdminShape extends UserShape {
239
+ role: string;
240
+ }
241
+
242
+ // Tactica will add these to TypeRegistry automatically.
243
+ define('User', function (this: UserShape, data: { name: string }) {
244
+ this.name = data.name;
245
+ });
102
246
 
103
- Install and run the CLI:
247
+ define('User.Admin', function (this: AdminShape, data: { role: string }) {
248
+ this.role = data.role;
249
+ });
104
250
 
105
- ```bash
106
- npm install --save-dev @mnemonica/tactica
107
- npx tactica --output ./.tactica --include "src/**/*.ts"
251
+ // Now the free lookup is typed.
252
+ const Admin = lookup('User.Admin');
253
+ const admin = new Admin({ role: 'root' });
108
254
  ```
109
255
 
110
- Then include the generated files in `tsconfig.json`:
256
+ ### With `@decorate()`
111
257
 
112
- ```json
113
- {
114
- "include": ["src/**/*.ts", ".tactica/**/*.ts"]
258
+ > The canonical decorator guide is [`docs/decorate.md`](./decorate.md) —
259
+ > setup, examples, and the TypeØmatica integration live there. The short
260
+ > version:
261
+
262
+ ```typescript
263
+ import { decorate } from 'mnemonica';
264
+
265
+ @decorate()
266
+ class User {
267
+ name: string;
268
+ constructor(data: { name: string }) {
269
+ this.name = data.name;
270
+ }
115
271
  }
116
- ```
117
272
 
118
- Tactica scans your `define()` and `@decorate()` calls and generates `.tactica/types.ts` + `.tactica/registry.ts`. The generated registry has the same shape as the hand-written one above, but tactica also fills in instance-level subtypes (`alice.Employee`) automatically and uses `ProtoFlat<Parent, ...>` for nested types so they merge correctly. You can re-run the CLI manually or from a build script; mnemographica can also run it on your behalf.
273
+ @decorate(User)
274
+ class Admin {
275
+ role: string;
276
+ constructor(data: { role: string }) {
277
+ this.role = data.role;
278
+ }
279
+ }
119
280
 
120
- You write nothing for the augmentation; tactica regenerates as your types evolve. See [`@mnemonica/tactica`](https://www.npmjs.com/package/@mnemonica/tactica) for the full configuration surface.
281
+ const AdminCtor = lookup('User.Admin');
282
+ ```
283
+
284
+ > **Note:** `@decorate()` requires `TypeRegistry` augmentation (Tactica or
285
+ > hand-written). The builder API and the bridge cannot cover decorators,
286
+ > because TypeScript never applies a class decorator's return type to the
287
+ > class binding — there is no local registry to carry or extract.
121
288
 
122
289
  ---
123
290
 
124
- ## When to choose which
291
+ ## Hand-written `TypeRegistry` augmentation
125
292
 
126
- **Hand-augmented** is fine for:
293
+ If you are not using Tactica and the bridge does not fit (for example, types
294
+ defined only through free `define()` calls), you can write the augmentation
295
+ yourself.
127
296
 
128
- - Small projects (a handful of types)
129
- - Bootstrap / learning — writing the augmentation by hand is the fastest way to understand what `lookup()` actually does
130
- - Projects that want to keep the `TypeRegistry` surface stable and curated, independent of source-code churn
131
- - Environments where running a CLI/codegen utility is inconvenient
297
+ ```typescript
298
+ import type { TypeConstructor } from 'mnemonica';
132
299
 
133
- **Tactica** is the right choice when:
300
+ declare module 'mnemonica' {
301
+ interface TypeRegistry {
302
+ 'User': TypeConstructor<UserShape>;
303
+ 'User.Admin': TypeConstructor<AdminShape>;
304
+ }
305
+ }
306
+ ```
307
+
308
+ Once augmented, `lookup('User')` and `lookup('User.Admin')` resolve against `TypeRegistry` instead of falling back to `TypeClass | undefined`. For builder projects the `RegistryOf` bridge above is the same thing with one line and no duplication.
134
309
 
135
- - You have many types or expect the type graph to grow
136
- - You don't want to maintain the augmentation by hand as `define()` calls are added or refactored
137
- - You want full instance-subtype typing (`alice.Employee`) without writing it
138
- - You're already running tactica as part of your build or IDE workflow
310
+ ---
139
311
 
140
- The runtime is identical either way. There is no "tactica path" vs "non-tactica path" in mnemonica core itself — there is only the `TypeRegistry` augmentation, and two ways to produce it.
312
+ ## Which type system wins when both are present?
313
+
314
+ Adding Tactica (or a bridge) to a project that already uses the builder API does **not** replace the builder's local type system. The systems are orthogonal and apply to different APIs.
315
+
316
+ | Call you write | Type system used | Why |
317
+ |---|---|---|
318
+ | `const App = mnemonica.define(...)` | Builder registry | The returned object carries its own type map. |
319
+ | `App.lookup('User')` | Builder registry | `lookup()` is a method on the builder object. |
320
+ | `lookup(App, 'User')` | Builder registry | The source argument carries the registry. |
321
+ | `new user.Admin(...)` | Builder registry | Subtypes are resolved from the instance's inferred chain type. |
322
+ | `define('User', ...)` free import | `TypeRegistry` augmentation | The free export has no local registry, so it relies on the global interface. |
323
+ | `lookup('User')` free import | `TypeRegistry` augmentation | Same as above — populated by Tactica, hand-written, or the `RegistryOf` bridge. |
324
+
325
+ So if you keep using `mnemonica.define(...)` after installing Tactica, **the builder types stay in control**. Tactica only becomes relevant when you switch to the free `define` / `lookup` imports or use `@decorate()`.
326
+
327
+ At runtime the results are identical. At compile time, TypeScript picks whichever type system matches the exact identifier you used. If two sources disagree on the same path, the mismatch surfaces only on the API that resolves against `TypeRegistry`.
141
328
 
142
329
  ---
143
330
 
144
- ## What is forbidden either way
331
+ ## Why two type systems exist
332
+
333
+ It feels like there should be one type system. In an ideal world, every `define()` call would add its type to the same registry, and `lookup()` would always be typed. TypeScript does not allow that.
334
+
335
+ ### The hard limitation
145
336
 
146
- Regardless of how you produce the augmentation, two patterns are anti-patterns. They typically show up as workarounds when the augmentation is missing, and the fix is to augment correctly (manually or via tactica), not to cast around the type system:
337
+ TypeScript cannot extend a global interface from inside a function signature. This code is impossible in TypeScript:
147
338
 
148
339
  ```typescript
149
- // ❌ never
150
- const requestData = new RequestData({ ... }) as unknown as RequestDataT;
151
- const child = new (instance as any).Child({ ... });
340
+ function define<N extends string, T>(name: N, handler: (this: T) => void) {
341
+ // There is no type-level statement that says:
342
+ // "Add N to the global TypeRegistry interface."
343
+ // The language simply does not have it.
344
+ }
152
345
  ```
153
346
 
347
+ Interfaces can only be extended by explicit declarations:
348
+
154
349
  ```typescript
155
- // ✓ instead
156
- const RequestData = lookup('RequestData');
157
- const requestData = new RequestData({ ... });
158
- const child = new requestData.Child({ ... });
350
+ declare module 'mnemonica' {
351
+ interface TypeRegistry {
352
+ 'User': TypeConstructor<UserShape>;
353
+ }
354
+ }
159
355
  ```
160
356
 
161
- See [`../.ai/TACTICA-RULES.md`](../.ai/TACTICA-RULES.md) for the full anti-pattern list.
357
+ Function bodies, return types, and generic parameters cannot produce declarations. That is why Tactica generates them: it is a build-time tool that pretends to be a programmer who wrote all the augmentation blocks by hand. The `RegistryOf` bridge is the same declaration, written once, with its contents *computed* from the builder value instead of repeated by hand.
358
+
359
+ ### What the builder mode can and cannot do
360
+
361
+ The builder mode solves the problem locally by carrying the registry in the returned object:
362
+
363
+ ```typescript
364
+ const App = mnemonica.define('User', ...).define('Admin', ...);
365
+ // App's type is roughly { lookup: { User: ..., 'User.Admin': ... } }
366
+ ```
367
+
368
+ This works because the function's return type can be computed from the generic parameters. The registry is local to the value — but the value can be exported, so the registry travels across files (registry threading), and it can be merged into the global interface (the `RegistryOf` bridge).
369
+
370
+ What it still cannot handle:
371
+
372
+ - **`@decorate()` classes** — a class decorator's return type never changes the class binding's declared type, so there is no channel for a local registry at all.
373
+
374
+ ### What the augmented mode can and cannot do
375
+
376
+ The augmented mode uses the global `TypeRegistry` interface. It can handle:
377
+
378
+ - Free `define()` and `lookup()` anywhere in the project.
379
+ - `@decorate()` classes.
380
+ - Cross-file references because the interface is globally visible.
381
+
382
+ But it requires external tooling (Tactica) or a hand-written declaration (directly, or via the `RegistryOf` bridge). Without that, the interface is empty and free `lookup()` falls back to `TypeClass | undefined`.
383
+
384
+ ### Why not unify them?
385
+
386
+ The split is a consequence of TypeScript's design, not mnemonica's. You cannot have one mechanism that covers all three cases:
387
+
388
+ 1. Chained builder calls.
389
+ 2. Free function calls.
390
+ 3. Decorated classes.
391
+
392
+ So mnemonica exposes the builder API for case 1, the bridge and two-arg
393
+ overloads to connect cases 1 and 2, and the augmented API for cases 2 and 3.
394
+ The runtime is the same everywhere; only the way TypeScript discovers the
395
+ types differs.
396
+
397
+ ---
398
+
399
+ ## Summary
400
+
401
+ - **Default:** builder mode — `mnemonica.define(...)` or `createTypesCollection()`, `.lookup()` on the result. No tooling.
402
+ - **Multiple files:** thread the chain through exports; the registry type travels with the value.
403
+ - **Want free `lookup()` typed:** add the one-line `RegistryOf` bridge in a `registry.ts`.
404
+ - **Mixing styles:** `lookup(source, path)` / `define(source, name, handler)` infer the registry from the source value.
405
+ - **Need `@decorate()` typed:** use Tactica — it is the only path, and the reason is a TypeScript limitation, not a missing feature.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mnemonica",
3
- "version": "1.0.9",
3
+ "version": "1.2.0",
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",
@@ -95,15 +95,15 @@
95
95
  },
96
96
  "homepage": "https://github.com/wentout/mnemonica#readme",
97
97
  "devDependencies": {
98
- "@eslint/eslintrc": "^3.3.5",
98
+ "@eslint/eslintrc": "^3.3.6",
99
99
  "@eslint/js": "^9.21.0",
100
100
  "@mnemonica/tactica": "^0.1.3",
101
101
  "@types/jest": "^29.5.14",
102
- "@types/node": "^22.19.21",
103
- "@typescript-eslint/eslint-plugin": "^8.61.1",
104
- "@typescript-eslint/parser": "^8.61.1",
102
+ "@types/node": "^22.20.1",
103
+ "@typescript-eslint/eslint-plugin": "^8.64.0",
104
+ "@typescript-eslint/parser": "^8.64.0",
105
105
  "chai": "^5.3.3",
106
- "eslint": "^9.39.4",
106
+ "eslint": "^9.39.5",
107
107
  "eslint-plugin-import": "^2.32.0",
108
108
  "eslint-plugin-mocha": "^10.5.0",
109
109
  "eslint-plugin-no-arrow-this": "^1.2.0",
@@ -212,11 +212,21 @@ TypeDescriptor.prototype.lookup = function (
212
212
  this: TypeDescriptorInstance,
213
213
  TypeNestedPath: string
214
214
  ) {
215
- const result = lookup.call(
216
- this.subtypes as TypesMap,
217
- TypeNestedPath
215
+ // relative first: resolve among this type's own subtypes,
216
+ // e.g. AdminType.lookup('SubType') or 'SubType.DeepSubType'
217
+ const relativeResult = lookup.call(
218
+ this.subtypes as TypesMap,
219
+ TypeNestedPath
218
220
  );
219
- return result;
221
+ if (relativeResult) {
222
+ return relativeResult;
223
+ }
224
+ // root fallback: resolve absolute registry paths from the
225
+ // collection, e.g. AdminType.lookup('UserType.AdminType'),
226
+ // which is what the TypeLookup<Registry> type contract promises
227
+ const collection = this.collection as CollectionDef;
228
+ const rootResult = collection.lookup(TypeNestedPath);
229
+ return rootResult;
220
230
  };
221
231
 
222
232
  odp(