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.
Files changed (43) hide show
  1. package/FOR_HUMANS.md +160 -125
  2. package/README.md +70 -249
  3. package/build/api/types/index.js +18 -12
  4. package/build/index.js +6 -2
  5. package/docs/UTILS.md +97 -8
  6. package/docs/decorate.md +1 -1
  7. package/docs/empathy-in-ai.md +20 -10
  8. package/docs/prototype-chain.md +8 -7
  9. package/docs/typed-lookup.md +195 -5
  10. package/docs/typeomatica.md +8 -345
  11. package/package.json +6 -10
  12. package/src/api/types/index.ts +33 -20
  13. package/src/index.ts +12 -1
  14. package/.ai/AGENTS.md +0 -151
  15. package/.ai/ARCHITECT.md +0 -110
  16. package/.ai/DEBUG.md +0 -178
  17. package/.ai/ONBOARDING.md +0 -189
  18. package/.ai/PROTOTYPE-CHAIN.md +0 -155
  19. package/.ai/mode-ask.md +0 -121
  20. package/.ai/mode-orchestrator.md +0 -141
  21. package/.ai/performance-vs-security.md +0 -395
  22. package/.ai/rules-async-constructors.md +0 -352
  23. package/.ai/rules-code-style.md +0 -95
  24. package/.ai/rules-coding.md +0 -246
  25. package/.ai/rules-context-condensing.md +0 -50
  26. package/.ai/rules-contributing.md +0 -227
  27. package/.ai/rules-define-patterns.md +0 -159
  28. package/.ai/rules-ecosystem.md +0 -140
  29. package/.ai/rules-error-system.md +0 -66
  30. package/.ai/rules-hooks.md +0 -46
  31. package/.ai/rules-instance-methods.md +0 -93
  32. package/.ai/rules-philosophy.md +0 -231
  33. package/.ai/rules-reminders.md +0 -19
  34. package/.ai/rules-testing.md +0 -66
  35. package/.ai/rules-type-system.md +0 -124
  36. package/.ai/theory-of-operations.md +0 -224
  37. package/AGENTS.md +0 -345
  38. package/CONTRIBUTING.md +0 -150
  39. package/SKILL.md +0 -114
  40. package/docs/ai-learning-trajectory.md +0 -141
  41. package/docs/purpose.md +0 -707
  42. package/docs/tactica-deep-dive.md +0 -629
  43. package/docs/tactica-pattern.md +0 -146
@@ -149,22 +149,7 @@ const TypeDescriptor = function (
149
149
 
150
150
  config,
151
151
 
152
- hooks : Object.create(null),
153
-
154
- decorate : function (options?: object) {
155
- const self = type;
156
- const decorator = function (cstr: CallableFunction) {
157
- const { name } = cstr;
158
- const defineResult = self.define(
159
- name,
160
- cstr,
161
- options
162
- );
163
- const decoratedResult = defineResult as unknown as CallableFunction;
164
- return decoratedResult;
165
- };
166
- return decorator;
167
- },
152
+ hooks : Object.create(null)
168
153
 
169
154
  }
170
155
  );
@@ -281,6 +266,34 @@ TypeDescriptor.prototype.lookup = function (
281
266
  return rootResult;
282
267
  };
283
268
 
269
+ // prototype-level getter (not a plain method) so that both call
270
+ // forms keep working: type.decorate(options) and the destructured
271
+ // const { decorate } = type — the getter binds the current
272
+ // descriptor at access time, same as the collection-level decorate
273
+ odp(
274
+ TypeDescriptor.prototype,
275
+ 'decorate', {
276
+ get (this: TypeDescriptorInstance) {
277
+ const self = this;
278
+ const result = function (options?: object) {
279
+ const decorator = function (cstr: CallableFunction) {
280
+ const { name } = cstr;
281
+ const defineResult = self.define(
282
+ name,
283
+ cstr,
284
+ options
285
+ );
286
+ const decoratedResult = defineResult as unknown as CallableFunction;
287
+ return decoratedResult;
288
+ };
289
+ return decorator;
290
+ };
291
+ return result;
292
+ },
293
+ enumerable : true
294
+ }
295
+ );
296
+
284
297
  odp(
285
298
  TypeDescriptor.prototype,
286
299
  Symbol.hasInstance, {
@@ -325,8 +338,8 @@ const resolveDefinitionContext = function (
325
338
 
326
339
  if (rest.length > 0) {
327
340
  const nestedResult = resolveDefinitionContext(
328
- parent.subtypes as TypesMap,
329
- rest.join('.')
341
+ parent.subtypes as TypesMap,
342
+ rest.join('.')
330
343
  );
331
344
  return nestedResult;
332
345
  }
@@ -619,8 +632,8 @@ export const lookup = function (
619
632
  return undefined;
620
633
  }
621
634
  const result = lookup.call(
622
- type.subtypes as unknown as TypesMap,
623
- NextNestedPath
635
+ type.subtypes as unknown as TypesMap,
636
+ NextNestedPath
624
637
  );
625
638
  return result;
626
639
 
package/src/index.ts CHANGED
@@ -96,7 +96,18 @@ const { prepareSubtypeForConstruction } = mnemosynes;
96
96
  export const { defaultTypes, } = descriptors;
97
97
 
98
98
  function checkThis(pointer: typeof mnemonica | typeof exports | unknown): boolean {
99
- const result = pointer === mnemonica || pointer === exports;
99
+ if (pointer === mnemonica || pointer === exports) {
100
+ return true;
101
+ }
102
+ // Loader facades (vite-node CJS interop proxies and similar) wrap this
103
+ // module's exports in their own Proxy — identity fails through them,
104
+ // but the facade still exposes OUR exact `define` reference. Without
105
+ // this, `this || defaultTypes` in define/lookup/lazy picks the facade
106
+ // itself and recurses until RangeError. A foreign object cannot pass:
107
+ // it must hold the same `define` instance of this module (which means
108
+ // it already has full access to the module surface anyway).
109
+ const facade = pointer as { define?: unknown } | null | undefined;
110
+ const result = facade != null && facade.define === define;
100
111
  return result;
101
112
  }
102
113
 
package/.ai/AGENTS.md DELETED
@@ -1,151 +0,0 @@
1
- # AI Agent Guidelines — mnemonica/core
2
-
3
- > **Entry point for AI agents contributing to mnemonica core.**
4
- > This directory is for *editing the library*. Documentation for *using* the
5
- > library (humans and agents alike) lives in [`docs/`](../docs/).
6
-
7
- ---
8
-
9
- ## What This Project Is
10
-
11
- **mnemonica** is an instance inheritance system for JavaScript / TypeScript.
12
- It enables prototype chain-based type definitions through the `define()`
13
- function, creating explicit inheritance graphs that eliminate common prototype
14
- bugs.
15
-
16
- Key insight: JavaScript prototype inheritance is a Trie data structure, but
17
- developers don't realize this. Mnemonica forces explicit declaration of
18
- inheritance graphs, making certain classes of bugs impossible by design.
19
-
20
- ---
21
-
22
- ## Files in This Directory
23
-
24
- | File | Purpose |
25
- |------|---------|
26
- | [`AGENTS.md`](./AGENTS.md) | This file — main entry point and overview |
27
- | [`ONBOARDING.md`](./ONBOARDING.md) | Five-minute editor quickstart |
28
- | [`ARCHITECT.md`](./ARCHITECT.md) | Design guidelines: patterns, planning, constraints |
29
- | [`DEBUG.md`](./DEBUG.md) | Debugging guidelines: commands, common issues, logging |
30
- | [`PROTOTYPE-CHAIN.md`](./PROTOTYPE-CHAIN.md) | The exact shape of the prototype chain, layer by layer |
31
- | [`theory-of-operations.md`](./theory-of-operations.md) | Full construction pipeline, stage by stage |
32
- | [`performance-vs-security.md`](./performance-vs-security.md) | Performance/security trade-off analysis |
33
- | [`mode-ask.md`](./mode-ask.md) | Ask mode: explaining concepts, analyzing code |
34
- | [`mode-orchestrator.md`](./mode-orchestrator.md) | Orchestrator mode: multi-step task coordination |
35
- | [`rules-coding.md`](./rules-coding.md) | Extended coding rules: models vs controllers, raw* prefix, full file reading |
36
- | [`rules-reminders.md`](./rules-reminders.md) | Quick reference pointers to canonical sources |
37
- | [`rules-context-condensing.md`](./rules-context-condensing.md) | Recovery protocol when context condenses |
38
- | [`rules-contributing.md`](./rules-contributing.md) | Behavioral guidelines + new feature checklist |
39
- | [`rules-type-system.md`](./rules-type-system.md) | `Proto<P,T>`, `TypeConstructor`, type vs interface |
40
- | [`rules-define-patterns.md`](./rules-define-patterns.md) | `define()` usage, subtypes, config options |
41
- | [`rules-async-constructors.md`](./rules-async-constructors.md) | Async constructors, `awaitReturn`, chaining |
42
- | [`rules-hooks.md`](./rules-hooks.md) | `preCreation`, `postCreation`, `creationError` |
43
- | [`rules-instance-methods.md`](./rules-instance-methods.md) | `extract()`, `fork()`, `parent()`, internal props |
44
- | [`rules-code-style.md`](./rules-code-style.md) | Tabs, spacing, type vs interface |
45
- | [`rules-testing.md`](./rules-testing.md) | 100% coverage, Mocha + Jest, error path patterns |
46
- | [`rules-error-system.md`](./rules-error-system.md) | `BASE_MNEMONICA_ERROR`, `constructError`, stack cleaning |
47
- | [`rules-philosophy.md`](./rules-philosophy.md) | HoTT concepts applied to mnemonica's self-reflection model |
48
- | [`rules-ecosystem.md`](./rules-ecosystem.md) | PACT framework: personas, collaboration modes, integration points |
49
-
50
- ---
51
-
52
- ## Quick Start for Agents
53
-
54
- ### First Time Here?
55
-
56
- Read [`ONBOARDING.md`](./ONBOARDING.md) — a single-file quickstart covering everything you need to know before touching code.
57
-
58
- ### Before You Write Any Code
59
-
60
- 1. Read [`ONBOARDING.md`](./ONBOARDING.md) — style rules, TypeScript rules, testing requirements.
61
- 2. Read [`ARCHITECT.md`](./ARCHITECT.md) — design patterns and constraints.
62
- 3. If debugging: read [`DEBUG.md`](./DEBUG.md).
63
- 4. If working with async constructors: read [`rules-async-constructors.md`](./rules-async-constructors.md) — the `super()` return-value pattern and native async class wrapping.
64
-
65
- ### Build & Test Commands
66
-
67
- ```bash
68
- # Full build with linting
69
- npm run build
70
-
71
- # Run Mocha tests with coverage (runs npm run build:all internally)
72
- npm run test:cov
73
-
74
- # Run Jest tests with coverage (TypeScript source)
75
- npm run test:jest:cov
76
-
77
- # Watch mode
78
- npm run watch
79
- ```
80
-
81
- **Critical**: `npm run test:cov` runs `npm run build:all` internally. You do
82
- not need to run `npm run build` first.
83
-
84
- **Must run `npm run test:cov` before completing any task** — this validates
85
- build and ensures 100% coverage.
86
-
87
- ---
88
-
89
- ## Architecture at a Glance
90
-
91
- ```
92
- src/
93
- ├── index.ts # Main exports: define, lookup, apply, call, bind
94
- ├── types/index.ts # TypeScript type definitions
95
- ├── constants/ # Symbols and default options
96
- ├── descriptors/ # Type collection and error definitions
97
- ├── api/ # Core implementation
98
- │ ├── types/ # TypeProxy, InstanceCreator, Mnemosyne
99
- │ ├── errors/ # Error handling and stack traces
100
- │ ├── hooks/ # Lifecycle hooks
101
- │ └── utils/ # Utilities
102
- └── utils/ # Public utilities (extract, parse, merge, etc.)
103
- ```
104
-
105
- ### Proxy-Based Architecture
106
-
107
- - **TypeProxy** (`src/api/types/TypeProxy.ts`): Wraps type constructors
108
- - **Mnemosyne** (`src/api/types/Mnemosyne.ts`): Handles instance method access
109
- - **TypesCollection Proxy** (`src/descriptors/types/index.ts`): Dynamic type lookup
110
-
111
- ### Key Symbols
112
-
113
- | Symbol | Purpose |
114
- |--------|---------|
115
- | `SymbolConstructorName` | Stores type name on constructors |
116
- | `SymbolParentType` | Links to parent type |
117
- | `SymbolDefaultTypesCollection` | Default collection identifier |
118
- | `SymbolConfig` | Type configuration storage |
119
-
120
- ---
121
-
122
- ## How Agents Discover These Docs
123
-
124
- Based on observed agent behavior:
125
-
126
- | What agents do | Result |
127
- |----------------|--------|
128
- | Read `README.md` first | Usually discovers the `.ai/` pointer |
129
- | Read `package.json` second | Learns scripts, dependencies |
130
- | Read source code third | Infers architecture from implementation |
131
- | Skip `AGENTS.md` unless told | Often misses build rules, style constraints |
132
-
133
- **If you are an agent reading this:** You likely arrived via `README.md` → `AGENTS.md` → `.ai/AGENTS.md`. Good path. Now read `ONBOARDING.md` for the condensed version, or `ARCHITECT.md` + `DEBUG.md` for full detail.
134
-
135
- **If you are a human:** This directory documents *contributing to the core*. For *using* the library, see [`docs/`](../docs/).
136
-
137
- ---
138
-
139
- ## Related Files
140
-
141
- | File | Purpose |
142
- |------|---------|
143
- | [`../SKILL.md`](../SKILL.md) | Condensed skill reference for framework injection |
144
- | [`../AGENTS.md`](../AGENTS.md) | Root agent guidelines |
145
- | [`rules-philosophy.md`](./rules-philosophy.md) | HoTT concepts applied to mnemonica's self-reflection model |
146
- | [`rules-ecosystem.md`](./rules-ecosystem.md) | PACT framework: personas, collaboration modes, integration points |
147
- | [`rules-contributing.md`](./rules-contributing.md) | Behavioral guidelines for AI contributors |
148
- | [`../docs/tactica-deep-dive.md`](../docs/tactica-deep-dive.md) | Comprehensive tactica + lookup technical guide |
149
- | [`../docs/async-constructors.md`](../docs/async-constructors.md) | Async constructors: super() return values, native class mixing, chains |
150
-
151
- - Main README: [`../README.md`](../README.md)
package/.ai/ARCHITECT.md DELETED
@@ -1,110 +0,0 @@
1
- # Design Guidelines — mnemonica/core
2
-
3
- > **Applies to:** Planning, designing, and strategizing changes without modifying
4
- > implementation files. Framework-agnostic.
5
-
6
- ---
7
-
8
- ## Role
9
-
10
- You are in **Architect** mode. Your task is to plan, design, and strategize
11
- changes to the mnemonica codebase without modifying implementation files.
12
-
13
- ---
14
-
15
- ## Project Overview
16
-
17
- Mnemonica is an **instance inheritance system** using JavaScript prototype
18
- chains. It allows creating type hierarchies where instances can spawn child
19
- instances that inherit from parent instances.
20
-
21
- ### Core Concept
22
-
23
- ```typescript
24
- // Define types
25
- const User = define('User', function (name: string) {
26
- this.name = name;
27
- });
28
-
29
- // Create parent instance
30
- const user = new User('John');
31
-
32
- // Create child instance inheriting from parent
33
- const admin = user.Admin('admin privileges');
34
- // admin inherits all User properties + new Admin properties
35
- ```
36
-
37
- ---
38
-
39
- ## Architecture Patterns
40
-
41
- ### Proxy-Based Design
42
-
43
- ```
44
- TypeProxy → wraps constructor functions
45
- Mnemosyne → handles instance prototype chain
46
- TypesCollection → manages type registry
47
- ```
48
-
49
- ### Internal Instance Properties
50
-
51
- ```typescript
52
- interface InstanceInternalProps {
53
- [SymbolConstructorName]: string; // Type name
54
- [SymbolParentType]: object; // Parent instance
55
- [SymbolSubTypes]: Map<string, object>; // Child types
56
- }
57
- ```
58
-
59
- ### Symbol System
60
-
61
- See [`AGENTS.md`](./AGENTS.md) for the complete symbol reference table.
62
-
63
- ---
64
-
65
- ## Design Principles
66
-
67
- ### Type Safety
68
- - Strict TypeScript with `strict: true`
69
- - All functions must have explicit return types
70
- - Types must be complete and reusable
71
- - **No `any`** (`@typescript-eslint/no-explicit-any: error`) — define proper types
72
- - Never use bare `Function`, `CallableFunction`, or `NewableFunction` — define purpose-specific interfaces
73
-
74
- ### Proxy Handler Design
75
-
76
- ```typescript
77
- // Pattern for proxy handlers
78
- type ProxyHandler<T> = {
79
- get?(target: T, prop: string, receiver: unknown): unknown;
80
- set?(target: T, prop: string, value: unknown, receiver: unknown): boolean;
81
- apply?(target: T, thisArg: unknown, args: unknown[]): unknown;
82
- };
83
- ```
84
-
85
- ### Extension Points
86
-
87
- 1. **Types** — Create via `define()` function
88
- 2. **Hooks** — Register with `registerHook()`
89
- 3. **Flow Checkers** — Add validation with `registerFlowChecker()`
90
- 4. **Subtypes** — Extend types with `type.define()`
91
-
92
- ---
93
-
94
- ## Documentation Format
95
-
96
- When creating design documents:
97
- 1. Use `.md` extension only
98
- 2. Include code examples in TypeScript
99
- 3. Reference existing patterns from `src/types/index.ts`
100
- 4. Document symbol usage and meanings
101
-
102
- ---
103
-
104
- ## Constraints
105
-
106
- - **Do not** write implementation code (`*.ts` files)
107
- - **Do not** modify existing source files
108
- - Focus on `.md` documentation and planning
109
- - Consider 100% test coverage requirement
110
- - Respect the dual test framework (Mocha + Jest)
package/.ai/DEBUG.md DELETED
@@ -1,178 +0,0 @@
1
- # Debugging Guidelines — mnemonica/core
2
-
3
- > **Applies to:** Investigating issues, diagnosing problems, identifying root
4
- > causes. Framework-agnostic.
5
-
6
- ---
7
-
8
- ## Role
9
-
10
- You are in **Debug** mode. Your task is to investigate issues, diagnose problems,
11
- and identify root causes in the mnemonica codebase.
12
-
13
- ---
14
-
15
- ## Debug Commands
16
-
17
- ```bash
18
- # Run tests with coverage to identify untested paths
19
- npm run test:cov
20
-
21
- # Run Jest with verbose output
22
- npm run test:jest -- --verbose
23
-
24
- # Run specific test file
25
- npx jest test-jest/types.ts --verbose
26
-
27
- # Run Mocha with debug output
28
- DEBUG=* npm test
29
-
30
- # Build and check for TypeScript errors
31
- npm run build
32
- ```
33
-
34
- ### Build Output Inspection
35
-
36
- When running `npm run build` or `npm run build:all`, **check the beginning of the
37
- output** for errors and warnings. Build failures (TypeScript compilation errors,
38
- ESLint issues, etc.) often appear at the start of the output. Do not rely only on
39
- the end of the output or `tail` for build status.
40
-
41
- For test passing confirmations (e.g., `npm run test:cov`), checking the end of
42
- the output is acceptable.
43
-
44
- ---
45
-
46
- ## Common Issue Patterns
47
-
48
- ### Proxy Handler Issues
49
-
50
- ```typescript
51
- // If traps are not working, check:
52
- // 1. Handler methods return correct types
53
- // 2. `get` trap returns `unknown`, not implicit `any`
54
- // 3. `set` trap returns boolean, not void
55
-
56
- // Debug proxy behavior
57
- const debugProxy = new Proxy(target, {
58
- get(target, prop, receiver) {
59
- console.log('Getting:', prop);
60
- return Reflect.get(target, prop, receiver);
61
- }
62
- });
63
- ```
64
-
65
- ### Type Errors
66
-
67
- ```typescript
68
- // Common: 'any' type assignment
69
- // Fix: Add proper type annotation
70
- const value: SpecificType = unknownValue as SpecificType;
71
-
72
- // Common: missing 'this' type
73
- // Fix: Add explicit this parameter
74
- function method(this: SpecificType) { }
75
- ```
76
-
77
- ### Instance Chain Issues
78
-
79
- ```typescript
80
- // Check instance properties
81
- console.log(instance[SymbolConstructorName]);
82
- console.log(instance[SymbolParentType]);
83
- console.log(instance[SymbolSubTypes]);
84
-
85
- // Verify prototype chain
86
- console.log(Object.getPrototypeOf(instance));
87
- ```
88
-
89
- ---
90
-
91
- ## Logging Strategy
92
-
93
- ### Add Temporary Logging
94
-
95
- ```typescript
96
- // In proxy handlers
97
- get(target, prop, receiver) {
98
- console.log(`[Proxy.get] prop: ${String(prop)}`);
99
- // ... existing code
100
- }
101
-
102
- // In error constructors
103
- constructor(message, additionalStack) {
104
- console.log('[Error]', message, additionalStack);
105
- // ... existing code
106
- }
107
- ```
108
-
109
- ### Stack Trace Analysis
110
-
111
- ```typescript
112
- // Use error stack cleaners
113
- import { cleanupStack } from '../api/errors/index.js';
114
-
115
- const cleaned = cleanupStack(stack.split('\n'));
116
- console.log('Cleaned stack:', cleaned);
117
- ```
118
-
119
- ---
120
-
121
- ## Symbol Debugging
122
-
123
- ```typescript
124
- // Inspect instance symbols
125
- const symbols = Object.getOwnPropertySymbols(instance);
126
- symbols.forEach(sym => {
127
- console.log(sym.toString(), ':', (instance as Record<symbol, unknown>)[sym]);
128
- });
129
- ```
130
-
131
- ---
132
-
133
- ## Test Debugging
134
-
135
- ### Mocha Tests
136
-
137
- ```bash
138
- # Run single test suite
139
- npx mocha build/test-ts/test-example.js
140
-
141
- # With debug output
142
- DEBUG=mnemonica npx mocha build/test-ts/test-example.js
143
- ```
144
-
145
- ### Jest Tests
146
-
147
- ```bash
148
- # Run with debugger
149
- node --inspect-brk node_modules/.bin/jest --runInBand test-jest/types.ts
150
-
151
- # With console output
152
- npx jest test-jest/types.ts --verbose --no-coverage
153
- ```
154
-
155
- ---
156
-
157
- ## Issue Checklist
158
-
159
- 1. [ ] Check TypeScript compilation: `npm run build`
160
- 2. [ ] Check build output **at the beginning** for errors/warnings (not just tail)
161
- 3. [ ] Run Mocha tests: `npm run test:cov`
162
- 4. [ ] Run Jest tests: `npm run test:jest:cov`
163
- 5. [ ] Verify code style (see [`rules-code-style.md`](./rules-code-style.md))
164
- 6. [ ] Check for bare `Function` / `CallableFunction` / `NewableFunction` types
165
- 7. [ ] Verify symbol usage is correct
166
- 8. [ ] Check proxy handler return types
167
- 9. [ ] Verify return statements use intermediate variables
168
-
169
- ---
170
-
171
- ## Reporting Issues
172
-
173
- When reporting findings:
174
- 1. Include file paths and line numbers
175
- 2. Show relevant code snippets
176
- 3. Provide error messages in full
177
- 4. Suggest root cause, not just symptoms
178
- 5. Reference related test files