@seanhogg/builderforce-memory-engine 2026.6.20 → 2026.6.28

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 (72) hide show
  1. package/dist/index.d.ts +9 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +8 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/kernels/limbic_affect.d.ts +2 -0
  6. package/dist/kernels/limbic_affect.d.ts.map +1 -0
  7. package/dist/kernels/limbic_affect.js +74 -0
  8. package/dist/kernels/limbic_affect.js.map +1 -0
  9. package/dist/limbic/index.d.ts +14 -0
  10. package/dist/limbic/index.d.ts.map +1 -0
  11. package/dist/limbic/index.js +11 -0
  12. package/dist/limbic/index.js.map +1 -0
  13. package/dist/limbic/limbic_model.d.ts +111 -0
  14. package/dist/limbic/limbic_model.d.ts.map +1 -0
  15. package/dist/limbic/limbic_model.js +299 -0
  16. package/dist/limbic/limbic_model.js.map +1 -0
  17. package/dist/limbic/limbic_trainer.d.ts +62 -0
  18. package/dist/limbic/limbic_trainer.d.ts.map +1 -0
  19. package/dist/limbic/limbic_trainer.js +172 -0
  20. package/dist/limbic/limbic_trainer.js.map +1 -0
  21. package/dist/limbic/regions.d.ts +79 -0
  22. package/dist/limbic/regions.d.ts.map +1 -0
  23. package/dist/limbic/regions.js +132 -0
  24. package/dist/limbic/regions.js.map +1 -0
  25. package/dist/lm/evermind_lm.d.ts +148 -0
  26. package/dist/lm/evermind_lm.d.ts.map +1 -0
  27. package/dist/lm/evermind_lm.js +479 -0
  28. package/dist/lm/evermind_lm.js.map +1 -0
  29. package/dist/lm/index.d.ts +6 -0
  30. package/dist/lm/index.d.ts.map +1 -0
  31. package/dist/lm/index.js +5 -0
  32. package/dist/lm/index.js.map +1 -0
  33. package/dist/model/attention_block.js +1 -1
  34. package/dist/model/attention_block.js.map +1 -1
  35. package/dist/model/mamba_model.js +1 -1
  36. package/dist/model/mamba_model.js.map +1 -1
  37. package/dist/moe/index.d.ts +10 -0
  38. package/dist/moe/index.d.ts.map +1 -0
  39. package/dist/moe/index.js +7 -0
  40. package/dist/moe/index.js.map +1 -0
  41. package/dist/moe/moe_model.d.ts +134 -0
  42. package/dist/moe/moe_model.d.ts.map +1 -0
  43. package/dist/moe/moe_model.js +415 -0
  44. package/dist/moe/moe_model.js.map +1 -0
  45. package/dist/moe/moe_package.d.ts +81 -0
  46. package/dist/moe/moe_package.d.ts.map +1 -0
  47. package/dist/moe/moe_package.js +157 -0
  48. package/dist/moe/moe_package.js.map +1 -0
  49. package/dist/moe/moe_trainer.d.ts +53 -0
  50. package/dist/moe/moe_trainer.d.ts.map +1 -0
  51. package/dist/moe/moe_trainer.js +93 -0
  52. package/dist/moe/moe_trainer.js.map +1 -0
  53. package/dist/optim/adamw.d.ts +32 -0
  54. package/dist/optim/adamw.d.ts.map +1 -0
  55. package/dist/optim/adamw.js +52 -0
  56. package/dist/optim/adamw.js.map +1 -0
  57. package/package.json +1 -1
  58. package/src/index.ts +59 -0
  59. package/src/kernels/limbic_affect.ts +74 -0
  60. package/src/limbic/index.ts +28 -0
  61. package/src/limbic/limbic_model.ts +373 -0
  62. package/src/limbic/limbic_trainer.ts +253 -0
  63. package/src/limbic/regions.ts +141 -0
  64. package/src/lm/evermind_lm.ts +558 -0
  65. package/src/lm/index.ts +6 -0
  66. package/src/model/attention_block.ts +1 -1
  67. package/src/model/mamba_model.ts +1 -1
  68. package/src/moe/index.ts +23 -0
  69. package/src/moe/moe_model.ts +475 -0
  70. package/src/moe/moe_package.ts +205 -0
  71. package/src/moe/moe_trainer.ts +134 -0
  72. package/src/optim/adamw.ts +72 -0
@@ -0,0 +1,62 @@
1
+ /**
2
+ * limbic_trainer.ts – LimbicTrainer: gradient-based training for the LimbicModel.
3
+ *
4
+ * Trains the affect head to predict affect deltas and reward from
5
+ * (experience embedding, current state) pairs, using full-batch gradient
6
+ * descent with AdamW. When a GPUDevice is supplied the optimiser step runs on
7
+ * the GPU via the shared WEIGHT_UPDATE_WGSL kernel (real WebGPU training); with
8
+ * no device it uses a numerically-equivalent CPU AdamW so training works
9
+ * everywhere (CI, Node without @webgpu/node, etc.).
10
+ *
11
+ * The objective is MSE (regression), not the cross-entropy used by the
12
+ * language-model {@link MambaTrainer} — a limbic experience has continuous
13
+ * targets, not a next-token distribution.
14
+ */
15
+ import type { LimbicModel } from "./limbic_model.js";
16
+ /** One training example: an experience and the affect change it should produce. */
17
+ export interface LimbicSample {
18
+ /** Experience embedding (length = model.inputDim). */
19
+ input: ArrayLike<number>;
20
+ /** Affective state at the time of the experience (length = model.stateDim). */
21
+ state: ArrayLike<number>;
22
+ /** Observed affect delta target in (-1, 1) per state dim (length = model.stateDim). */
23
+ deltaTarget: ArrayLike<number>;
24
+ /** Observed scalar reward for the experience. */
25
+ reward: number;
26
+ }
27
+ export interface LimbicTrainOptions {
28
+ learningRate?: number;
29
+ epochs?: number;
30
+ weightDecay?: number;
31
+ beta1?: number;
32
+ beta2?: number;
33
+ eps?: number;
34
+ /** Max global gradient L2 norm before the optimiser step. Default 1.0. */
35
+ maxGradNorm?: number;
36
+ onEpochEnd?: ((epoch: number, loss: number) => void) | null;
37
+ }
38
+ export declare class LimbicTrainer {
39
+ readonly model: LimbicModel;
40
+ readonly device: GPUDevice | null;
41
+ private _moments;
42
+ private _step;
43
+ private readonly _adamwPipeline;
44
+ constructor(model: LimbicModel, device?: GPUDevice | null);
45
+ /** Whether the optimiser step runs on the GPU. */
46
+ get gpuTraining(): boolean;
47
+ private _initMoments;
48
+ /**
49
+ * Train on a batch of samples for `epochs` passes. Returns the per-epoch mean
50
+ * loss (monotonically decreasing on a learnable mapping). Full-batch: grads
51
+ * accumulate across the whole sequence (recurrent hidden carried, reset per
52
+ * epoch), are averaged, clipped, then applied once per epoch.
53
+ */
54
+ train(samples: LimbicSample[], opts?: LimbicTrainOptions): Promise<number[]>;
55
+ /** Mean MSE loss over samples (no weight update). Hidden resets per sample. */
56
+ evaluate(samples: LimbicSample[]): number;
57
+ private _clipGradients;
58
+ private _adamwStepCpu;
59
+ /** AdamW on the GPU via the shared WEIGHT_UPDATE_WGSL kernel. Awaited per step. */
60
+ private _adamwStepGpu;
61
+ }
62
+ //# sourceMappingURL=limbic_trainer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"limbic_trainer.d.ts","sourceRoot":"","sources":["../../src/limbic/limbic_trainer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAYH,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,mBAAmB,CAAC;AAElE,mFAAmF;AACnF,MAAM,WAAW,YAAY;IAC3B,sDAAsD;IACtD,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,+EAA+E;IAC/E,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IACzB,uFAAuF;IACvF,WAAW,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;IAC/B,iDAAiD;IACjD,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,0EAA0E;IAC1E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,CAAC,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;CAC7D;AAuBD,qBAAa,aAAa;IACxB,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAC5B,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,IAAI,CAAC;IAClC,OAAO,CAAC,QAAQ,CAA6B;IAC7C,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA4B;gBAE/C,KAAK,EAAE,WAAW,EAAE,MAAM,GAAE,SAAS,GAAG,IAAW;IAQ/D,kDAAkD;IAClD,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED,OAAO,CAAC,YAAY;IAQpB;;;;;OAKG;IACG,KAAK,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,IAAI,GAAE,kBAAuB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IA6DtF,+EAA+E;IAC/E,QAAQ,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,MAAM;IAkBzC,OAAO,CAAC,cAAc;IAUtB,OAAO,CAAC,aAAa;IAqBrB,mFAAmF;YACrE,aAAa;CAkC5B"}
@@ -0,0 +1,172 @@
1
+ /**
2
+ * limbic_trainer.ts – LimbicTrainer: gradient-based training for the LimbicModel.
3
+ *
4
+ * Trains the affect head to predict affect deltas and reward from
5
+ * (experience embedding, current state) pairs, using full-batch gradient
6
+ * descent with AdamW. When a GPUDevice is supplied the optimiser step runs on
7
+ * the GPU via the shared WEIGHT_UPDATE_WGSL kernel (real WebGPU training); with
8
+ * no device it uses a numerically-equivalent CPU AdamW so training works
9
+ * everywhere (CI, Node without @webgpu/node, etc.).
10
+ *
11
+ * The objective is MSE (regression), not the cross-entropy used by the
12
+ * language-model {@link MambaTrainer} — a limbic experience has continuous
13
+ * targets, not a next-token distribution.
14
+ */
15
+ import { createUniformBuffer, createStorageBuffer, createComputePipeline, createBindGroup, dispatchKernel, readBuffer, cdiv, } from "../utils/gpu_utils.js";
16
+ import { WEIGHT_UPDATE_WGSL } from "../kernels/weight_update.js";
17
+ function packAdamParams(numElements, lr, beta1, beta2, eps, weightDecay, beta1_t, beta2_t) {
18
+ const buf = new ArrayBuffer(32);
19
+ new Uint32Array(buf, 0, 1).set([numElements]);
20
+ new Float32Array(buf, 4, 7).set([lr, beta1, beta2, eps, weightDecay, beta1_t, beta2_t]);
21
+ return buf;
22
+ }
23
+ export class LimbicTrainer {
24
+ model;
25
+ device;
26
+ _moments = null;
27
+ _step = 0;
28
+ _adamwPipeline;
29
+ constructor(model, device = null) {
30
+ this.model = model;
31
+ this.device = device;
32
+ this._adamwPipeline = device
33
+ ? createComputePipeline(device, WEIGHT_UPDATE_WGSL, "adamw_update")
34
+ : null;
35
+ }
36
+ /** Whether the optimiser step runs on the GPU. */
37
+ get gpuTraining() {
38
+ return this.device != null && this._adamwPipeline != null;
39
+ }
40
+ _initMoments() {
41
+ if (this._moments)
42
+ return;
43
+ this._moments = this.model.parameters().map((p) => ({
44
+ m: new Float32Array(p.numel),
45
+ v: new Float32Array(p.numel),
46
+ }));
47
+ }
48
+ /**
49
+ * Train on a batch of samples for `epochs` passes. Returns the per-epoch mean
50
+ * loss (monotonically decreasing on a learnable mapping). Full-batch: grads
51
+ * accumulate across the whole sequence (recurrent hidden carried, reset per
52
+ * epoch), are averaged, clipped, then applied once per epoch.
53
+ */
54
+ async train(samples, opts = {}) {
55
+ if (samples.length === 0)
56
+ throw new Error("LimbicTrainer.train: no samples");
57
+ const { learningRate = 0.05, epochs = 50, weightDecay = 0.0, beta1 = 0.9, beta2 = 0.999, eps = 1e-8, maxGradNorm = 1.0, onEpochEnd = null, } = opts;
58
+ this._initMoments();
59
+ const losses = [];
60
+ for (let epoch = 0; epoch < epochs; epoch++) {
61
+ this.model.zeroGrad();
62
+ let epochLoss = 0;
63
+ // Each sample is one experience appraisal: cross-experience memory flows
64
+ // through the affective state `s` (fed back by the runtime), not through
65
+ // the hidden scratch `h`, so the hidden state resets per sample. This also
66
+ // makes BPTT(1) exact — there is no truncated carry.
67
+ for (const s of samples) {
68
+ const { loss } = this.model.backwardStep(s.input, this.model.initHidden(), s.state, s.deltaTarget, s.reward);
69
+ epochLoss += loss;
70
+ }
71
+ // Average gradients over the batch.
72
+ const grads = this.model.gradients();
73
+ const invN = 1 / samples.length;
74
+ for (const g of grads) {
75
+ for (let i = 0; i < g.data.length; i++)
76
+ g.data[i] *= invN;
77
+ }
78
+ this._clipGradients(grads, maxGradNorm);
79
+ this._step++;
80
+ const beta1_t = Math.pow(beta1, this._step);
81
+ const beta2_t = Math.pow(beta2, this._step);
82
+ const hp = { learningRate, weightDecay, beta1, beta2, eps, beta1_t, beta2_t };
83
+ if (this.gpuTraining) {
84
+ await this._adamwStepGpu(grads, hp);
85
+ }
86
+ else {
87
+ this._adamwStepCpu(grads, hp);
88
+ }
89
+ const avg = epochLoss / samples.length;
90
+ losses.push(avg);
91
+ if (onEpochEnd)
92
+ onEpochEnd(epoch + 1, avg);
93
+ }
94
+ return losses;
95
+ }
96
+ /** Mean MSE loss over samples (no weight update). Hidden resets per sample. */
97
+ evaluate(samples) {
98
+ if (samples.length === 0)
99
+ return 0;
100
+ let total = 0;
101
+ const { rewardWeight } = this.model.config;
102
+ for (const s of samples) {
103
+ const f = this.model.forward(s.input, this.model.initHidden(), s.state);
104
+ let loss = 0;
105
+ for (let k = 0; k < f.delta.length; k++) {
106
+ const d = f.delta[k] - (s.deltaTarget[k] ?? 0);
107
+ loss += 0.5 * d * d;
108
+ }
109
+ const rd = f.reward - s.reward;
110
+ loss += 0.5 * rewardWeight * rd * rd;
111
+ total += loss;
112
+ }
113
+ return total / samples.length;
114
+ }
115
+ _clipGradients(grads, maxNorm) {
116
+ let normSq = 0;
117
+ for (const g of grads)
118
+ for (let i = 0; i < g.data.length; i++)
119
+ normSq += g.data[i] * g.data[i];
120
+ const norm = Math.sqrt(normSq);
121
+ if (norm > maxNorm && norm > 0) {
122
+ const scale = maxNorm / norm;
123
+ for (const g of grads)
124
+ for (let i = 0; i < g.data.length; i++)
125
+ g.data[i] *= scale;
126
+ }
127
+ }
128
+ _adamwStepCpu(grads, hp) {
129
+ const params = this.model.parameters();
130
+ const { learningRate: lr, weightDecay: wd, beta1, beta2, eps, beta1_t, beta2_t } = hp;
131
+ for (let pi = 0; pi < params.length; pi++) {
132
+ const p = params[pi].data;
133
+ const g = grads[pi].data;
134
+ const mom = this._moments[pi];
135
+ for (let i = 0; i < p.length; i++) {
136
+ const gi = g[i];
137
+ mom.m[i] = beta1 * mom.m[i] + (1 - beta1) * gi;
138
+ mom.v[i] = beta2 * mom.v[i] + (1 - beta2) * gi * gi;
139
+ const mHat = mom.m[i] / (1 - beta1_t);
140
+ const vHat = mom.v[i] / (1 - beta2_t);
141
+ p[i] = p[i] * (1 - lr * wd) - (lr * mHat) / (Math.sqrt(vHat) + eps);
142
+ }
143
+ }
144
+ }
145
+ /** AdamW on the GPU via the shared WEIGHT_UPDATE_WGSL kernel. Awaited per step. */
146
+ async _adamwStepGpu(grads, hp) {
147
+ const device = this.device;
148
+ const pipeline = this._adamwPipeline;
149
+ const params = this.model.parameters();
150
+ const { learningRate: lr, weightDecay: wd, beta1, beta2, eps, beta1_t, beta2_t } = hp;
151
+ for (let pi = 0; pi < params.length; pi++) {
152
+ const p = params[pi];
153
+ const mom = this._moments[pi];
154
+ const paramBuf = createStorageBuffer(device, p.data, true);
155
+ const gradBuf = createStorageBuffer(device, grads[pi].data, false);
156
+ const mBuf = createStorageBuffer(device, mom.m, true);
157
+ const vBuf = createStorageBuffer(device, mom.v, true);
158
+ const uni = createUniformBuffer(device, packAdamParams(p.numel, lr, beta1, beta2, eps, wd, beta1_t, beta2_t));
159
+ const bg = createBindGroup(device, pipeline, [uni, paramBuf, gradBuf, mBuf, vBuf]);
160
+ dispatchKernel(device, pipeline, bg, [cdiv(p.numel, 256), 1, 1]);
161
+ p.data.set((await readBuffer(device, paramBuf, p.numel * 4)).subarray(0, p.numel));
162
+ mom.m.set((await readBuffer(device, mBuf, p.numel * 4)).subarray(0, p.numel));
163
+ mom.v.set((await readBuffer(device, vBuf, p.numel * 4)).subarray(0, p.numel));
164
+ paramBuf.destroy();
165
+ gradBuf.destroy();
166
+ mBuf.destroy();
167
+ vBuf.destroy();
168
+ uni.destroy();
169
+ }
170
+ }
171
+ }
172
+ //# sourceMappingURL=limbic_trainer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":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EAAE,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YACnF,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YAC9E,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YAE9E,QAAQ,CAAC,OAAO,EAAE,CAAC;YACnB,OAAO,CAAC,OAAO,EAAE,CAAC;YAClB,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,GAAG,CAAC,OAAO,EAAE,CAAC;QAChB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,79 @@
1
+ /**
2
+ * regions.ts – Limbic-system region map and affective state schema.
3
+ *
4
+ * The limbic model is the *dynamic* affective/motivational layer that rides on
5
+ * top of the (static) psychometric personality. Where the hippocampus
6
+ * (HybridMambaModel + MemoryStore) holds *what the agent knows*, the limbic
7
+ * model holds *how the agent currently feels and what it is driven toward* —
8
+ * and learns the dynamics of that, in WebGPU, from experience.
9
+ *
10
+ * This file is the single source of truth for the affective state vector that
11
+ * every other limbic module (model, trainer, runtime service) indexes into.
12
+ * Keep the dimension ids in sync with the runtime compiler in
13
+ * `agent-runtime/src/builderforce/limbic.ts` — the two are coupled solely by
14
+ * these string ids and the {@link LIMBIC_DIM} indices.
15
+ *
16
+ * Region → state mapping (mirrors the labelled diagram):
17
+ * • Amygdala → salience / threat appraisal → drives valence + arousal
18
+ * • Hypothalamus → homeostatic drives (curiosity, caution, effort, social)
19
+ * • Thalamus → attention gate (how much incoming signal is admitted)
20
+ * • Basal ganglia → action selection bias (explore vs. exploit)
21
+ * • Hippocampus → reused (existing SSM memory); feeds the experience input
22
+ */
23
+ /** The five modelled limbic regions. Hippocampus is reused, not re-modelled. */
24
+ export declare const REGION: {
25
+ readonly amygdala: "amygdala";
26
+ readonly hypothalamus: "hypothalamus";
27
+ readonly thalamus: "thalamus";
28
+ readonly basalGanglia: "basal_ganglia";
29
+ readonly hippocampus: "hippocampus";
30
+ };
31
+ export type Region = (typeof REGION)[keyof typeof REGION];
32
+ /**
33
+ * Canonical indices into the affective state vector. The vector is a dense
34
+ * Float32Array of length {@link LIMBIC_STATE_DIM}. Core affect (valence,
35
+ * arousal) is the 2D summary; the remaining dims are the per-region drives the
36
+ * agent's behaviour is modulated by.
37
+ */
38
+ export declare const LIMBIC_DIM: {
39
+ /** Core affect — pleasantness. Range [-1, +1] (negative .. positive). */
40
+ readonly valence: 0;
41
+ /** Core affect — activation. Range [0, 1] (calm .. activated). */
42
+ readonly arousal: 1;
43
+ /** Hypothalamus drive — appetite for novelty/exploration. Range [0, 1]. */
44
+ readonly driveCuriosity: 2;
45
+ /** Hypothalamus drive — appetite for safety/guardrails. Range [0, 1]. */
46
+ readonly driveCaution: 3;
47
+ /** Hypothalamus drive — available energy. Range [0, 1] (fatigued .. fresh). */
48
+ readonly driveEffort: 4;
49
+ /** Hypothalamus drive — appetite for communication/collaboration. Range [0, 1]. */
50
+ readonly driveSocial: 5;
51
+ /** Thalamus — attention gain on incoming signal. Range [0, 1]. */
52
+ readonly attention: 6;
53
+ /** Basal ganglia — explore(1) vs. exploit(0) action-selection bias. Range [0, 1]. */
54
+ readonly exploration: 7;
55
+ };
56
+ export type LimbicDimName = keyof typeof LIMBIC_DIM;
57
+ /** Length of the affective state vector. */
58
+ export declare const LIMBIC_STATE_DIM = 8;
59
+ /** Ordered dim names, index-aligned with {@link LIMBIC_DIM}. */
60
+ export declare const LIMBIC_DIM_NAMES: LimbicDimName[];
61
+ /** Inclusive [min, max] bounds per state dim, index-aligned. Valence is signed. */
62
+ export declare const LIMBIC_BOUNDS: ReadonlyArray<readonly [number, number]>;
63
+ /**
64
+ * Neutral resting state — the default homeostatic setpoint before personality
65
+ * pulls it anywhere. Calm, mildly positive, balanced drives, full attention,
66
+ * slightly exploit-biased.
67
+ */
68
+ export declare const NEUTRAL_STATE: ReadonlyArray<number>;
69
+ /** Clamp a single state dim to its bounds. */
70
+ export declare function clampDim(index: number, value: number): number;
71
+ /** Clamp a whole state vector in place and return it. */
72
+ export declare function clampState(state: Float32Array): Float32Array;
73
+ /** A fresh neutral state vector. */
74
+ export declare function neutralState(): Float32Array;
75
+ /** Build a labelled record from a dense state vector (for logging / transport). */
76
+ export declare function stateToRecord(state: ArrayLike<number>): Record<LimbicDimName, number>;
77
+ /** Build a dense state vector from a (possibly partial) labelled record. */
78
+ export declare function recordToState(rec: Partial<Record<LimbicDimName, number>>): Float32Array;
79
+ //# sourceMappingURL=regions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"regions.d.ts","sourceRoot":"","sources":["../../src/limbic/regions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,gFAAgF;AAChF,eAAO,MAAM,MAAM;;;;;;CAMT,CAAC;AACX,MAAM,MAAM,MAAM,GAAG,CAAC,OAAO,MAAM,CAAC,CAAC,MAAM,OAAO,MAAM,CAAC,CAAC;AAE1D;;;;;GAKG;AACH,eAAO,MAAM,UAAU;IACrB,yEAAyE;;IAEzE,kEAAkE;;IAElE,2EAA2E;;IAE3E,yEAAyE;;IAEzE,+EAA+E;;IAE/E,mFAAmF;;IAEnF,kEAAkE;;IAElE,qFAAqF;;CAE7E,CAAC;AACX,MAAM,MAAM,aAAa,GAAG,MAAM,OAAO,UAAU,CAAC;AAEpD,4CAA4C;AAC5C,eAAO,MAAM,gBAAgB,IAAI,CAAC;AAElC,gEAAgE;AAChE,eAAO,MAAM,gBAAgB,EAAE,aAAa,EAS3C,CAAC;AAEF,mFAAmF;AACnF,eAAO,MAAM,aAAa,EAAE,aAAa,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CASlE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,aAAa,EAAE,aAAa,CAAC,MAAM,CAS/C,CAAC;AAEF,8CAA8C;AAC9C,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAK7D;AAED,yDAAyD;AACzD,wBAAgB,UAAU,CAAC,KAAK,EAAE,YAAY,GAAG,YAAY,CAK5D;AAED,oCAAoC;AACpC,wBAAgB,YAAY,IAAI,YAAY,CAE3C;AAED,mFAAmF;AACnF,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,CAMrF;AAED,4EAA4E;AAC5E,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,CAAC,MAAM,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC,GAAG,YAAY,CAOvF"}
@@ -0,0 +1,132 @@
1
+ /**
2
+ * regions.ts – Limbic-system region map and affective state schema.
3
+ *
4
+ * The limbic model is the *dynamic* affective/motivational layer that rides on
5
+ * top of the (static) psychometric personality. Where the hippocampus
6
+ * (HybridMambaModel + MemoryStore) holds *what the agent knows*, the limbic
7
+ * model holds *how the agent currently feels and what it is driven toward* —
8
+ * and learns the dynamics of that, in WebGPU, from experience.
9
+ *
10
+ * This file is the single source of truth for the affective state vector that
11
+ * every other limbic module (model, trainer, runtime service) indexes into.
12
+ * Keep the dimension ids in sync with the runtime compiler in
13
+ * `agent-runtime/src/builderforce/limbic.ts` — the two are coupled solely by
14
+ * these string ids and the {@link LIMBIC_DIM} indices.
15
+ *
16
+ * Region → state mapping (mirrors the labelled diagram):
17
+ * • Amygdala → salience / threat appraisal → drives valence + arousal
18
+ * • Hypothalamus → homeostatic drives (curiosity, caution, effort, social)
19
+ * • Thalamus → attention gate (how much incoming signal is admitted)
20
+ * • Basal ganglia → action selection bias (explore vs. exploit)
21
+ * • Hippocampus → reused (existing SSM memory); feeds the experience input
22
+ */
23
+ /** The five modelled limbic regions. Hippocampus is reused, not re-modelled. */
24
+ export const REGION = {
25
+ amygdala: "amygdala",
26
+ hypothalamus: "hypothalamus",
27
+ thalamus: "thalamus",
28
+ basalGanglia: "basal_ganglia",
29
+ hippocampus: "hippocampus",
30
+ };
31
+ /**
32
+ * Canonical indices into the affective state vector. The vector is a dense
33
+ * Float32Array of length {@link LIMBIC_STATE_DIM}. Core affect (valence,
34
+ * arousal) is the 2D summary; the remaining dims are the per-region drives the
35
+ * agent's behaviour is modulated by.
36
+ */
37
+ export const LIMBIC_DIM = {
38
+ /** Core affect — pleasantness. Range [-1, +1] (negative .. positive). */
39
+ valence: 0,
40
+ /** Core affect — activation. Range [0, 1] (calm .. activated). */
41
+ arousal: 1,
42
+ /** Hypothalamus drive — appetite for novelty/exploration. Range [0, 1]. */
43
+ driveCuriosity: 2,
44
+ /** Hypothalamus drive — appetite for safety/guardrails. Range [0, 1]. */
45
+ driveCaution: 3,
46
+ /** Hypothalamus drive — available energy. Range [0, 1] (fatigued .. fresh). */
47
+ driveEffort: 4,
48
+ /** Hypothalamus drive — appetite for communication/collaboration. Range [0, 1]. */
49
+ driveSocial: 5,
50
+ /** Thalamus — attention gain on incoming signal. Range [0, 1]. */
51
+ attention: 6,
52
+ /** Basal ganglia — explore(1) vs. exploit(0) action-selection bias. Range [0, 1]. */
53
+ exploration: 7,
54
+ };
55
+ /** Length of the affective state vector. */
56
+ export const LIMBIC_STATE_DIM = 8;
57
+ /** Ordered dim names, index-aligned with {@link LIMBIC_DIM}. */
58
+ export const LIMBIC_DIM_NAMES = [
59
+ "valence",
60
+ "arousal",
61
+ "driveCuriosity",
62
+ "driveCaution",
63
+ "driveEffort",
64
+ "driveSocial",
65
+ "attention",
66
+ "exploration",
67
+ ];
68
+ /** Inclusive [min, max] bounds per state dim, index-aligned. Valence is signed. */
69
+ export const LIMBIC_BOUNDS = [
70
+ [-1, 1], // valence
71
+ [0, 1], // arousal
72
+ [0, 1], // driveCuriosity
73
+ [0, 1], // driveCaution
74
+ [0, 1], // driveEffort
75
+ [0, 1], // driveSocial
76
+ [0, 1], // attention
77
+ [0, 1], // exploration
78
+ ];
79
+ /**
80
+ * Neutral resting state — the default homeostatic setpoint before personality
81
+ * pulls it anywhere. Calm, mildly positive, balanced drives, full attention,
82
+ * slightly exploit-biased.
83
+ */
84
+ export const NEUTRAL_STATE = [
85
+ 0.0, // valence
86
+ 0.2, // arousal
87
+ 0.5, // driveCuriosity
88
+ 0.5, // driveCaution
89
+ 0.8, // driveEffort
90
+ 0.5, // driveSocial
91
+ 0.7, // attention
92
+ 0.5, // exploration (centred → resting state is behaviourally inert)
93
+ ];
94
+ /** Clamp a single state dim to its bounds. */
95
+ export function clampDim(index, value) {
96
+ const b = LIMBIC_BOUNDS[index];
97
+ if (!b)
98
+ return value;
99
+ if (Number.isNaN(value))
100
+ return b[0];
101
+ return Math.max(b[0], Math.min(b[1], value));
102
+ }
103
+ /** Clamp a whole state vector in place and return it. */
104
+ export function clampState(state) {
105
+ for (let i = 0; i < state.length && i < LIMBIC_STATE_DIM; i++) {
106
+ state[i] = clampDim(i, state[i]);
107
+ }
108
+ return state;
109
+ }
110
+ /** A fresh neutral state vector. */
111
+ export function neutralState() {
112
+ return Float32Array.from(NEUTRAL_STATE);
113
+ }
114
+ /** Build a labelled record from a dense state vector (for logging / transport). */
115
+ export function stateToRecord(state) {
116
+ const out = {};
117
+ for (let i = 0; i < LIMBIC_DIM_NAMES.length; i++) {
118
+ out[LIMBIC_DIM_NAMES[i]] = state[i] ?? 0;
119
+ }
120
+ return out;
121
+ }
122
+ /** Build a dense state vector from a (possibly partial) labelled record. */
123
+ export function recordToState(rec) {
124
+ const s = neutralState();
125
+ for (let i = 0; i < LIMBIC_DIM_NAMES.length; i++) {
126
+ const v = rec[LIMBIC_DIM_NAMES[i]];
127
+ if (typeof v === "number" && !Number.isNaN(v))
128
+ s[i] = clampDim(i, v);
129
+ }
130
+ return s;
131
+ }
132
+ //# sourceMappingURL=regions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"regions.js","sourceRoot":"","sources":["../../src/limbic/regions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,gFAAgF;AAChF,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,QAAQ,EAAE,UAAU;IACpB,YAAY,EAAE,cAAc;IAC5B,QAAQ,EAAE,UAAU;IACpB,YAAY,EAAE,eAAe;IAC7B,WAAW,EAAE,aAAa;CAClB,CAAC;AAGX;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,yEAAyE;IACzE,OAAO,EAAE,CAAC;IACV,kEAAkE;IAClE,OAAO,EAAE,CAAC;IACV,2EAA2E;IAC3E,cAAc,EAAE,CAAC;IACjB,yEAAyE;IACzE,YAAY,EAAE,CAAC;IACf,+EAA+E;IAC/E,WAAW,EAAE,CAAC;IACd,mFAAmF;IACnF,WAAW,EAAE,CAAC;IACd,kEAAkE;IAClE,SAAS,EAAE,CAAC;IACZ,qFAAqF;IACrF,WAAW,EAAE,CAAC;CACN,CAAC;AAGX,4CAA4C;AAC5C,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC;AAElC,gEAAgE;AAChE,MAAM,CAAC,MAAM,gBAAgB,GAAoB;IAC/C,SAAS;IACT,SAAS;IACT,gBAAgB;IAChB,cAAc;IACd,aAAa;IACb,aAAa;IACb,WAAW;IACX,aAAa;CACd,CAAC;AAEF,mFAAmF;AACnF,MAAM,CAAC,MAAM,aAAa,GAA6C;IACrE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,UAAU;IACnB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE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K,CAAC,CAAC,CAAE,CAAC,CAAC;IACpC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,oCAAoC;AACpC,MAAM,UAAU,YAAY;IAC1B,OAAO,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;AAC1C,CAAC;AAED,mFAAmF;AACnF,MAAM,UAAU,aAAa,CAAC,KAAwB;IACpD,MAAM,GAAG,GAAG,EAAmC,CAAC;IAChD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,gBAAgB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACjD,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAE,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC5C,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,aAAa,CAAC,GAA2C;IACvE,MAAM,CAAC,GAAG,YAAY,EAAE,CAAC;IACzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,gBAAgB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACjD,MAAM,CAAC,GAAG,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAE,CAAC,CAAC;QACpC,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;YAAE,CAAC,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACvE,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC"}
@@ -0,0 +1,148 @@
1
+ /**
2
+ * evermind_lm.ts — EvermindLM: a small but complete generative language model.
3
+ *
4
+ * This is what turns a trained checkpoint into an *AI that generates text* (the
5
+ * thing a marketplace buyer actually runs). Architecture (Mamba-flavoured, the
6
+ * minimal exact-gradient CPU reference):
7
+ *
8
+ * x_t = Embed[token_t]
9
+ * per layer:
10
+ * x_t += DepthwiseCausalConv(x)_t // temporal mixing (short conv)
11
+ * x_t += SharedExpertMoE(x_t) // per-position channel mixing (sparse)
12
+ * logits_t = x_t · Embedᵀ // tied output head
13
+ *
14
+ * The token mixer is a depthwise causal convolution (each channel sees a short
15
+ * window of its own past — Mamba's pre-conv) and the channel mixer is the
16
+ * shared-expert MoE, so the model is genuinely sparse. Embeddings are tied
17
+ * (input lookup == output head), which the gradient code accounts for.
18
+ *
19
+ * Pure CPU, exact forward + backward (finite-difference checked), reusing the
20
+ * engine's MoE, cross-entropy, and AdamW. The WGSL/WebGPU path is a future
21
+ * acceleration with the same shapes.
22
+ */
23
+ import { type AdamWOptions } from "../optim/adamw.js";
24
+ export interface EvermindLMConfig {
25
+ /** Vocabulary size. */
26
+ vocabSize: number;
27
+ /** Model (channel) dimension. Default 64. */
28
+ dModel: number;
29
+ /** Number of (conv + MoE) blocks. Default 2. */
30
+ numLayers: number;
31
+ /** Causal conv kernel width. Default 3. */
32
+ convKernel: number;
33
+ /** Hidden width of each MoE expert FFN. Default 2·dModel. */
34
+ hiddenDim: number;
35
+ /** Routed experts per MoE layer. Default 4. */
36
+ numExperts: number;
37
+ /** Experts activated per token. Default 2. */
38
+ topK: number;
39
+ /** Deterministic init seed. */
40
+ seed?: number;
41
+ }
42
+ export declare const DEFAULT_LM_CONFIG: Required<Omit<EvermindLMConfig, "seed" | "vocabSize">>;
43
+ export declare const DEFAULT_LM_SEED = 1163283533;
44
+ interface MoECacheLike {
45
+ x: Float32Array;
46
+ route: {
47
+ experts: number[];
48
+ gates: number[];
49
+ probs: Float32Array;
50
+ };
51
+ sharedPre: Float32Array;
52
+ sharedH: Float32Array;
53
+ expertOut: Float32Array[];
54
+ expertPre: Float32Array[];
55
+ expertH: Float32Array[];
56
+ }
57
+ interface LayerCache {
58
+ layerIn: Float32Array[];
59
+ normedConv: Float32Array[];
60
+ rmsConv: number[];
61
+ afterConv: Float32Array[];
62
+ rmsMoe: number[];
63
+ moeCache: MoECacheLike[];
64
+ }
65
+ interface ForwardCache {
66
+ tokens: number[];
67
+ layers: LayerCache[];
68
+ finalX: Float32Array[];
69
+ }
70
+ /** A tokenizer the LM can read/write text through (the engine's `BPETokenizer` fits). */
71
+ export interface TextCodec {
72
+ encode(text: string): number[];
73
+ decode(ids: number[]): string;
74
+ }
75
+ export interface LMGenerateOptions {
76
+ maxNewTokens: number;
77
+ /** Sampling temperature; ≤0 ⇒ greedy argmax. Default 0 (greedy). */
78
+ temperature?: number;
79
+ /** Deterministic sampler seed (only used when temperature > 0). */
80
+ seed?: number;
81
+ /** Stop generating when this token id is produced. */
82
+ stopToken?: number;
83
+ }
84
+ export declare class EvermindLM {
85
+ readonly config: Required<Omit<EvermindLMConfig, "seed">>;
86
+ /** Tied token embedding / output head: vocabSize × dModel (row-major). */
87
+ emb: Float32Array;
88
+ private gEmb;
89
+ /** Per-layer depthwise causal conv kernels: dModel × convKernel. */
90
+ private readonly conv;
91
+ private readonly gConv;
92
+ /** Per-layer pre-conv / pre-MoE RMSNorm gains (dModel each). */
93
+ private readonly nConv;
94
+ private readonly gNConv;
95
+ private readonly nMoe;
96
+ private readonly gNMoe;
97
+ /** Per-layer channel mixer. */
98
+ private readonly moe;
99
+ constructor(config: EvermindLMConfig);
100
+ /** Run the model over a token sequence; returns per-position logits + a cache. */
101
+ forward(tokens: number[]): {
102
+ logits: Float32Array[];
103
+ cache: ForwardCache;
104
+ };
105
+ /**
106
+ * Next-token cross-entropy over the sequence (predict tokens[t+1] from
107
+ * position t), accumulating exact gradients. Returns the mean loss. Call
108
+ * {@link zeroGrad} before and an optimiser step after.
109
+ */
110
+ lossAndBackward(tokens: number[]): number;
111
+ /**
112
+ * Text-level generation: encode the prompt, generate, decode. `codec` is any
113
+ * tokenizer exposing encode/decode (the engine's `BPETokenizer` satisfies it),
114
+ * so the LM consumes and emits real text rather than raw token ids. The model's
115
+ * `vocabSize` must match the codec's vocabulary.
116
+ */
117
+ generateText(prompt: string, codec: TextCodec, opts: LMGenerateOptions): string;
118
+ /** Greedy / temperature-sampled autoregressive generation. Returns NEW token ids. */
119
+ generate(prompt: number[], opts: LMGenerateOptions): number[];
120
+ /** All trainable parameters as {data} (AdamW-compatible), canonical order. */
121
+ parameters(): {
122
+ data: Float32Array;
123
+ }[];
124
+ /** Gradient buffers, index-aligned with {@link parameters}. */
125
+ gradients(): {
126
+ data: Float32Array;
127
+ }[];
128
+ zeroGrad(): void;
129
+ /** Serialise to an "EVL0" binary (fp16 or f32), params in {@link parameters} order. */
130
+ exportWeights(opts?: {
131
+ fp16?: boolean;
132
+ }): ArrayBuffer;
133
+ /** Load weights from an "EVL0" binary. Validates magic + dims. */
134
+ loadWeights(buffer: ArrayBuffer): void;
135
+ }
136
+ /** Minimal sequence trainer: AdamW over next-token cross-entropy. */
137
+ export declare class EvermindLMTrainer {
138
+ private readonly model;
139
+ private readonly opts;
140
+ private readonly adam;
141
+ constructor(model: EvermindLM, opts?: AdamWOptions & {
142
+ epochs?: number;
143
+ });
144
+ /** Train on a set of token sequences; returns per-epoch mean loss. */
145
+ fit(sequences: number[][]): number[];
146
+ }
147
+ export {};
148
+ //# sourceMappingURL=evermind_lm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evermind_lm.d.ts","sourceRoot":"","sources":["../../src/lm/evermind_lm.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAIH,OAAO,EAAS,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAI7D,MAAM,WAAW,gBAAgB;IAC/B,uBAAuB;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,6CAA6C;IAC7C,MAAM,EAAE,MAAM,CAAC;IACf,gDAAgD;IAChD,SAAS,EAAE,MAAM,CAAC;IAClB,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAC;IACnB,6DAA6D;IAC7D,SAAS,EAAE,MAAM,CAAC;IAClB,+CAA+C;IAC/C,UAAU,EAAE,MAAM,CAAC;IACnB,8C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