File size: 55,795 Bytes
db7bc06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
import * as fs from "fs";
import * as path from "path";
import { CharGRU, CharGRUConfig, GRUState } from "./gru";
export type { GRUState };
import { CharMultiNeuro, CharMultiNeuroConfig } from "./multineuro";
import { CharTokenizer } from "./tokenizer";
import { CharTransformer, CharTransformerConfig } from "./transformer";
import { mulberry32 } from "./rng";
import { visionFront, speechFront } from "./frontends";
export { visionFront, speechFront };
import { createStation } from "../station";
import { L0Consistency } from "../station/l0probe";
import { Task, TaskResult } from "../station/types";
import { VectorStore } from "./vectorstore";
import { CNVectorStore } from "./cn_vectorstore";
import { MathVectorStore } from "./math_vectorstore";
import { WorldModel } from "./world_model";
import { TitansMemory } from "./titans";
export type { TitansConfig } from "./titans";
import { ConfidenceFloors } from "./floors";

import { chunkText } from "./data";
import { ContextWindow } from "./tool_system";
import { reflect, loadBigrams } from "./reflection";

export interface CheckpointMeta {
   config: CharGRUConfig;
   paramCount: number;
   corpusTokens: number;
   epochs: number;
   vocab: string[];
   trainedAt: string;
   /** 是否由 CharMultiNeuro 训练 (含 MoE/专家头) */
   multiNeuro?: boolean;
   /** 是否由 CharTransformer 训练 */
   transformer?: boolean;
   moeTopK?: number;
   moeGateHidden?: number;
   moeNExperts?: number;
   moeLoadBalanceWeight?: number;
   onlineTitans?: boolean;
   /** Transformer 配置 (可选) */
   nLayer?: number;
   nHead?: number;
   /** Titans 记忆日志 (用于恢复记忆) */
   titansWriteLog?: Array<{ slot: number; h1: number[] }>;
 }

function sampleToken(probs: Float64Array, topK: number, temperature: number, rand: () => number): number {
  // [P0-F 修复 2026-10-08 第一性审核] NaN 概率 (非有限 logits 经 logitsToProbs 返回) → 返回 -1,
  // 调用方 charOf(-1) 无有效输出 (可证伪: 不伪造满置信的假输出)。
  for (let i = 0; i < probs.length; i++) if (Number.isNaN(probs[i])) return -1;
  const v = probs.length;
  const scaled = new Float64Array(v);
  let sum = 0;
  for (let k = 0; k < v; k++) {
    scaled[k] = Math.exp(Math.log(probs[k] + 1e-12) / Math.max(temperature, 1e-3));
    sum += scaled[k];
  }
  const k = Math.min(topK, v);
  const idxs = Array.from({ length: v }, (_, i) => i).sort((a, b) => scaled[b] - scaled[a]).slice(0, k);
  let mass = 0;
  for (const i of idxs) mass += scaled[i];
  let r = rand() * mass;
  for (const i of idxs) {
    r -= scaled[i];
    if (r <= 0) return i;
  }
  return idxs[idxs.length - 1];
}

export interface GenerateOptions {
  prompt: string;
  maxLen: number;
  budget: number;
  temperature: number;
  topK: number;
  seed?: number;
}

export interface CharRoute {
  char: string;
  depth: number;
  confidence: number;
  units: string[];
  memoryHit: boolean;
  /** 记忆来源:向量库 / 重复上下文 */
  memorySource?: "vector" | "repeat" | "titans";
}

export interface GenerateResult {
  text: string;
  routes: CharRoute[];
  budget: number;
}

/** 测试时记忆块标记:写入向量库的生成内容前缀,检索时跳过,防止自举循环 */
const MEMORY_TAG = "\uE000";

/**
 * 推理引擎:模型 + 字符分词 + 工作站 (分级路由 + Titans L0 记忆)
 * L0 记忆缓存直出 (零算力) → L1 浅路径 → L2 深路径 (预算内扩深)
 */
export class Engine {
  readonly model: CharGRU;
  readonly tokenizer: CharTokenizer;
  readonly meta: CheckpointMeta;
  private station = createStation();
  private habit: () => number;
  private memory = new Map<string, string>();
  private ctxWin: number[] = [];
  private store: VectorStore | null;
  private recentChars: string[] = [];
  private state: GRUState;
   /** 在线神经记忆 (Titans 逻辑):上下文嵌入 → 下一字,生成期持续写入 */
   private titansMem: TitansMemory;
   /** 多神经协同模型 (含 MoE/专家头,可为 null) */
   private ctxWinWindow: ContextWindow;
   readonly multiNeuro: CharMultiNeuro | null;
   /** 置信度下限追踪器 (宪章第1条: 兜底值由真实观测滚动窗口推导, 不写死常数) */
   readonly floors: ConfidenceFloors;
   /** L0 直出一致性探针 (宪章第7/8条: 在线记忆效果的量化证据, 非写死) */
   readonly l0Probe: L0Consistency;
   /** CN 语义向量库 (HNSW + N-gram 预过滤) */
   private cnStore: CNVectorStore | null = null;
   /** 数学向量库 (公式检索) */
   private mathStore: MathVectorStore | null = null;
   /** 推理协处理器 */
   private _reasoner: any = null;
   /** 世界模型 (物理常识 + 因果推理) */
   private worldModel: WorldModel | null = null;
   /** 语义反思层:n-gram 统计用于修正生成 */
   private bigramStats: ReturnType<typeof loadBigrams> = null;

  private constructor(model: CharGRU, tokenizer: CharTokenizer, meta: CheckpointMeta, multiNeuro: CharMultiNeuro | null = null, dataRoot = "data", prodRoot = "data", wmDir = "") {
    this.model = model;
    this.tokenizer = tokenizer;
    this.meta = meta;
    this.multiNeuro = multiNeuro;
    this.habit = mulberry32(Date.now() % 2147483647);
    this.floors = new ConfidenceFloors();
    this.l0Probe = new L0Consistency();
    this.state = model.newState();
    // dataRoot = sandbox 数据根 (语料快照); prodRoot = 生产数据根 (bigram/世界模型/向量库, 只读)。
    // 均由 load() 依 checkpoint 绝对路径锚定, 不依赖 cwd (宪章第10条: 生产 data/ 只读)。
    // 逐资源: 优先 sandbox 根, 缺则回退生产根 → 既守沙箱纪律, 又不漏统计资源。
    const pick = (rel: string): string => {
      const sbx = path.join(dataRoot, rel);
      return fs.existsSync(sbx) ? sbx : path.join(prodRoot, rel);
    };
    this.store = VectorStore.load(pick("vectorstore"));
    if (this.store) console.log(`向量库已加载:${this.store.size} 块`);
    this.cnStore = CNVectorStore.load(pick("cn_vectorstore"));
    if (this.cnStore) console.log(`CN 语义库已加载:${this.cnStore.size} 块`);
    this.mathStore = MathVectorStore.load(pick("math_vectorstore"));
    if (this.mathStore) console.log(`数学库已加载:${this.mathStore.size} 条`);
    // 尝试从 checkpoint 目录加载世界模型 (ckpt 自带 → 优先; 否则数据根; 宪章第10条 ckpt 只读)
    const wmPath = wmDir && fs.existsSync(path.join(wmDir, "world_model", "world_model.json"))
      ? path.join(wmDir, "world_model")
      : pick("world_model");
    this.worldModel = WorldModel.load(wmPath);
    if (this.worldModel) {
      console.log(`世界模型已加载:${this.worldModel.paramCount()} 参数 (来源: ${wmPath})`);
    } else {
      console.log(`世界模型未找到:${wmPath}`);
    }
    this.titansMem = new TitansMemory({ dim: meta.config.emb, slots: 256 });
    // 加载 bigram 统计(用于语义反思层)
    this.bigramStats = loadBigrams(pick("bigrams.json"));
    if (this.bigramStats) {
      console.log(`Bigram 统计已加载:${this.bigramStats.total} bigrams, ${this.bigramStats.charFreq.size} unique chars`);
    } else {
      console.log(`Bigram 统计未找到:${path.join(prodRoot, "bigrams.json")} (将使用默认统计)`);
    }
    // 上下文窗口:滑动窗口管理对话历史
    this.ctxWinWindow = new ContextWindow(meta.config.ctx * 10); // 10倍上下文
    // 尝试从多个可能位置加载 Titans 记忆
    const possiblePaths = [
      path.join("sandbox", "ck-titans-test", "titans_memory.json"),
      path.join("data", "checkpoints", "titans_memory.json"),
      path.join("..", "sandbox", "ck-titans-test", "titans_memory.json"),
      path.join("data", "titans_memory.json"),
    ];
    let memoryPath = "";
    for (const p of possiblePaths) {
      if (fs.existsSync(p)) {
        memoryPath = p;
        break;
      }
    }
    // 也检查当前 checkpoint 目录
    const cwd = process.cwd();
    const altPaths = [
      path.join(cwd, "titans_memory.json"),
      path.join(cwd, "sandbox", "ck-titans-test", "titans_memory.json"),
    ];
    for (const p of altPaths) {
      if (fs.existsSync(p)) {
        memoryPath = p;
        break;
      }
    }
    if (fs.existsSync(memoryPath)) {
      try {
        const memData = JSON.parse(fs.readFileSync(memoryPath, "utf8"));
        // 使用 Object.assign 绕过 readonly 限制
        Object.assign(this.titansMem, {
          chars: memData.chars,
          keys: new Float64Array(memData.keys),
          values: new Float64Array(memData.values),
          persistent: new Float64Array(memData.persistent),
        });
        console.log(`已从文件加载 Titans 记忆:${this.titansMem.chars.filter((c: string) => c).length} 个非空槽位`);
      } catch (e) { 
        console.warn(`加载 Titans 记忆失败:${e}`);
      }
    }
    this.registerUnits();
    // [R1-B1 修复 2026-10-09 第一性审核] 注入真实 model.step 上界给经济学:
    // 全链全跑 = 注册单元数 (深链可达深度, 同深度取一故 = 最大 depth) + 1 (推进 advance)。
    // 动态推导, 不再硬编码 4 (旧 B1: mamba/attn 关闭时分母失真)。
    {
      const textUnits = this.station.registry.computeUnits("text");
      const maxDepth = textUnits.reduce((m, u) => Math.max(m, u.depth), 0);
      const reachable = textUnits.filter((u) => u.cost > 0).length; // 单元前向 (同深度取一, ≈ maxDepth)
      this.station.economics.setMaxRealForwards(Math.max(reachable, maxDepth) + 1);
    }
  }

  /** 检索:把查询文本向量化,返回库内 top-k 片段 */
  retrieve(q: string, k = 4): Array<{ text: string; score: number }> {
    if (!this.store) return [];
    const ids = this.tokenizer.encode(q);
    return this.store.search(this.model.embedAvg(ids), k);
  }

  /** 中文语义检索 (CNVectorStore + HNSW + N-gram 预过滤) */
  retrieveCN(q: string, k = 4, category?: string): Array<{ text: string; score: number; category?: string }> {
    if (!this.cnStore) return [];
    const ids = this.tokenizer.encode(q);
    return this.cnStore.search(this.model.embedAvg(ids), k, category);
  }

  /** 数学公式检索 (语义 + 结构 + 关键词融合) */
  retrieveMath(q: string, k = 6, category?: string): Array<{ expr: string; description: string; score: number; matchType: string }> {
    if (!this.mathStore) return [];
    const ids = this.tokenizer.encode(q);
    const results = this.mathStore.search(q, this.model.embedAvg(ids), k, category);
    return results.map(r => ({
      expr: r.expr, description: r.description, score: r.score, matchType: r.matchType,
    }));
  }

  /** 推理:概念理解 + 符号推导 + 物理方程求解 */
  reason(q: string) {
    const results: Array<{ type: string; content: string; score?: number }> = [];
    // 1. 向量检索
    if (this.cnStore) {
      const cnHits = this.cnStore.search(this.model.embedAvg(this.tokenizer.encode(q)), 3);
      for (const h of cnHits) results.push({ type: "cn", content: h.text, score: h.score });
    }
    if (this.mathStore) {
      const mathHits = this.mathStore.search(q, this.model.embedAvg(this.tokenizer.encode(q)), 3);
      for (const h of mathHits) results.push({ type: "math", content: h.expr + ": " + h.description, score: h.score });
    }
    // 2. 世界模型物理约束检查
    if (this.worldModel) {
      const traj = this.worldModel.simulate(
        new Float64Array(this.model.embedAvg(this.tokenizer.encode(q))),
        [0],
      );
      const phys = this.worldModel.evaluatePhysics(traj[0]?.state);
      results.push({
        type: "world_model",
        content: `物理约束:能量=${phys[0].toFixed(3)} 因果=${phys[1].toFixed(3)} 平滑=${phys[2].toFixed(3)}`,
        score: 1 - (phys[0] + phys[1] + phys[2]) / 3,
      });
    }
    return { query: q, results, worldModel: this.worldModel };
  }

  retrieveCount(): number {
    return this.store?.size ?? 0;
  }

  /**
   * 测试时记忆:把生成内容切块写入向量库 (可持久化)。
   * 宪章: 生产 data/ 只读 — 持久化目标参数化, 默认内存态不落盘。
   * 传 dir 且指向沙箱路径时才写盘; 缺省仅在内存累加。
   */
  remember(text: string, persistDir?: string): void {
    if (!this.store) return;
    let added = 0;
    for (const chunk of chunkText(text, 64, 32)) {
      if (chunk.length < 8) continue;
      const ids = this.tokenizer.encode(chunk);
      this.store.add(MEMORY_TAG + chunk, this.model.embedAvg(ids));
      added += 1;
    }
    if (persistDir !== undefined && persistDir !== "") {
      this.store.save(persistDir);
      if (added > 0) console.log(`测试时记忆:新写入 ${added} 块 → ${persistDir},向量库现有 ${this.store.size} 块`);
    } else if (added > 0) {
      console.log(`测试时记忆:新写入 ${added} 块 (仅内存, 未持久化),向量库现有 ${this.store.size} 块`);
    }
  }

  /** 采样前降权:近 16 个已出字符中重复≥2 次的禁出 + UNK 槽禁出 (防 12 字级自举循环) */
  private penalized(probs: Float64Array): Float64Array {
    const out = probs.slice();
    out[out.length - 1] = 0;
    const freq = new Map<string, number>();
    for (const ch of this.recentChars) freq.set(ch, (freq.get(ch) ?? 0) + 1);
    for (const [ch, count] of freq) {
      if (count < 2) continue;
      const id = this.tokenizer.idOf(ch);
      if (id !== undefined && id < out.length) out[id] = 0;
    }
    let sum = 0;
    for (let i = 0; i < out.length; i++) sum += out[i];
    for (let i = 0; i < out.length; i++) out[i] /= sum;
    return out;
  }

  private propose(ch: string): void {
    this.recentChars.push(ch);
    if (this.recentChars.length > 16) this.recentChars.shift();
  }

  /** 窥视:用当前状态副本推一步,不推进真实状态 */
  private peekLogits(depth: 1 | 2 | 3 | 4): Float64Array {
    const lastId = this.ctxWin.length > 0 ? this.ctxWin[this.ctxWin.length - 1] : 0;
    const copy: GRUState = {
      h1: this.state.h1.slice(),
      h2: this.state.h2.slice(),
      hist: this.state.hist.map((h) => h.slice()),
      ids: [...this.state.ids],
    };
    return this.model.step(lastId, copy, depth);
  }

  /** 推进:真实状态吸收新字符 (depth = 路由实际执行到的最大深度, 默认 4) */
  private advance(chId: number, depth: 1 | 2 | 3 | 4 = 4): void {
    this.model.step(chId, this.state, depth);
    // SNN 膜电位锁相推进 (与骨干同频, 每字一次)
    if (this.multiNeuro?.spike) {
      this.multiNeuro.spikeAdvance(chId);
    }
  }

  /** SNN 膜电位重置 (生成开始时调用) */
  private resetSpike(): void {
    if (this.multiNeuro?.spike) {
      this.multiNeuro.resetSpikeSeq();
    }
  }

  /**
   * [P0-B 修复 2026-10-08 第一性审核] 上下文指纹: 稳定 FNV-1a 哈希 (非 seed, 不随随机性变),
   * 相同 ctxWin → 相同指纹。mem: 键绑定此指纹, 不同上下文的残留键天然失效。
   */
  static ctxFingerprint(ctx: number[], vocabSize: number): string {
    let h = 2166136261;
    for (let i = 0; i < ctx.length; i++) {
      h ^= ctx[i] % vocabSize;
      h = Math.imul(h, 16777619);
    }
    return (h >>> 0).toString(16).padStart(8, "0");
  }

  private recallAt(ctxIds: number[]): { char: string; source: "vector" | "repeat" | "titans" } | null {
    if (this.store) {
      const q = this.tokenizer.decode(ctxIds);
      const hits = this.store.search(this.model.embedAvg(ctxIds), 8);
      for (const hit of hits) {
        if (hit.text.startsWith(MEMORY_TAG)) continue;
        const at = hit.text.indexOf(q);
        if (at >= 0 && at + q.length < hit.text.length) {
          const ch = hit.text[at + q.length];
          if (this.isDegenerate(ch)) return null;
          // [P0-A 补漏] 向量库召回: 历史上该上下文真实延续过 → 0.5 点火标记 (非写死满置信, 宪章第1条);
          // executor L0 分支读 z:l0:act 推导置信度, 无写入 → 0, 冷启动不伪造满置信
          this.station.blackboard.write("z:l0:act", 0.5);
          return { char: ch, source: "vector" };
        }
      }
    }
    const cached = this.memory.get(ctxIds.join(","));
    if (cached !== undefined && !this.isDegenerate(cached)) {
      // 重复上下文缓存: 深链曾真实出过该字 → 0.5 点火标记 (同上)
      this.station.blackboard.write("z:l0:act", 0.5);
      return { char: cached, source: "repeat" };
    }
    // Titans 在线神经记忆:上下文嵌入检索 → 槽位存储的下一字符
    const q = Float64Array.from(this.model.embedAvg(ctxIds));
    const cache = { query: new Float64Array(0), keys: new Float64Array(0), values: new Float64Array(0), weights: new Float64Array(0) };
    const mem = this.titansMem.read(q, cache);
    if (mem.hitSlots > 0 && cache.weights.length > 0) {
      let best = 0;
      for (let i = 1; i < cache.weights.length; i++) if (cache.weights[i] > cache.weights[best]) best = i;
      if (cache.weights[best] > 0.5) {
        // 直接从 Titans 获取字符,不再依赖外部映射
        const ch = this.titansMem.chars[best % this.titansMem.cfg.slots];
        if (ch !== undefined && ch !== "" && !this.isDegenerate(ch)) {
          const l0Act = mem.hitSlots / this.titansMem.cfg.slots; // 点火 = 命中槽占比 (真实数值)
          this.station.blackboard.write("z:act:titans", l0Act);
          this.station.blackboard.write("z:l0:act", l0Act);
          return { char: ch, source: "repeat" };
        }
      }
    }
    return null;
  }

  /** 直接读取 Titan 记忆输出(用于 L0 直出) */
  private getTitansOutput(ctxIds: number[]): string | null {
    const q = Float64Array.from(this.model.embedAvg(ctxIds));
    const cache = { query: new Float64Array(0), keys: new Float64Array(0), values: new Float64Array(0), weights: new Float64Array(0) };
    const mem = this.titansMem.read(q, cache);
    if (mem.hitSlots > 0 && cache.weights.length > 0) {
      // 找到权重最大的多个槽位
      const topSlots: number[] = [];
      let maxWeight = 0;
      for (let i = 0; i < cache.weights.length; i++) {
        if (cache.weights[i] > maxWeight) {
          maxWeight = cache.weights[i];
          topSlots.length = 0;
          topSlots.push(i);
        } else if (cache.weights[i] > maxWeight * 0.9) { // 接近最大的也考虑
          topSlots.push(i);
        }
      }
      if (topSlots.length > 0 && maxWeight > 0.3) {
        // 从 top 槽位中选择一个非空字符
        for (const slot of topSlots) {
          const ch = this.titansMem.chars[slot % this.titansMem.cfg.slots];
          if (ch !== undefined && ch !== "" && !this.isDegenerate(ch)) {
            this.station.blackboard.write("z:act:titans", mem.hitSlots / this.titansMem.cfg.slots);
            return ch;
          }
        }
      }
    }
    return null;
  }

  /** L0 一致性探针 (宪章第7/8条): 同上下文深链 top-1 字 (peekLogits 无副作用: 状态副本推进, 不污染当前生成状态) */
  deepTop1At(ctxIds?: number[]): { char: string; p: number } {
    const logits = this.peekLogits(4);
    const probs = this.model.logitsToProbs(logits);
    let best = 0;
    for (let i = 1; i < probs.length; i++) if (probs[i] > probs[best]) best = i;
    return { char: this.tokenizer.charOf(best), p: probs[best] };
  }

  /** 循环防护:连续相同字符 ≥4 次或最近 8 字符整段重复,判定为退化输出 */
  private isDegenerate(ch: string): boolean {
    // 只检测连续重复,不检测全局频率
    const n = this.recentChars.length;
    if (n >= 3) {
      const last3 = this.recentChars.slice(-3).join("");
      if (last3 === ch.repeat(3)) return true;
    }
    if (n >= 16) {
      const a = this.recentChars.slice(-8).join("");
      const b = this.recentChars.slice(-16, -8).join("");
      if (a === b) return true;
    }
    return false;
  }

  static load(checkpointDir: string): Engine {
    const meta = JSON.parse(fs.readFileSync(path.join(checkpointDir, "meta.json"), "utf-8")) as CheckpointMeta;
    const params = JSON.parse(fs.readFileSync(path.join(checkpointDir, "model.json"), "utf-8")) as number[];
    const tokenizer = new CharTokenizer();
    tokenizer.load(meta.vocab);

    // [P0-F 修复 2026-10-08 第一性审核] 推理侧非有限参数拒载: 检查点若被污染 (含 NaN/Inf),
    // 拒绝载入 — 旧实现 Engine.load 直接 load(params) 不扫描, 污染检查点 (85.8% +Inf)
    // 载入后置信度恒 1.0, 健康监控 nan:0 永不降级, 生成 avgFinalConf≈1.0 而真实输出是噪声。
    // 与 train.ts resume 防护 (防线 3) 同逻辑: 任一非有限 → 抛错拒载。
    {
      let bad = 0;
      for (let i = 0; i < params.length; i++) if (!Number.isFinite(params[i])) bad++;
      if (bad > 0) {
        throw new Error(
          `[推理防护] ${checkpointDir}/model.json 含 ${bad}/${params.length} 非有限参数 (NaN/Inf), ` +
          `检查点已被污染, 拒绝载入。请回退到更早的完好检查点。`
        );
      }
    }

    let multiNeuro: CharMultiNeuro | null = null;
    let baseModel: CharGRU;

    if (meta.transformer) {
      // Transformer 检查点路径
      const tCfg: CharTransformerConfig = {
        vocabSize: tokenizer.vocabSize,
        emb: (meta.config as any).emb ?? 64,
        nLayer: (meta.config as any).nLayer ?? 2,
        nHead: (meta.config as any).nHead ?? 2,
        ctx: (meta.config as any).ctx ?? 8,
        bptt: (meta.config as any).bptt ?? 32,
      };
      const transformer = new CharTransformer(tCfg, 1);
      if (transformer.paramCount() !== params.length) {
        throw new Error(`checkpoint 参数数量与配置不匹配:${transformer.paramCount()} != ${params.length}`);
      }
      transformer.load(params);
      // 包装为 GRU 接口兼容层
      baseModel = new CharGRU({
        vocabSize: tCfg.vocabSize,
        emb: tCfg.emb,
        hidden: tCfg.emb,
        ctx: tCfg.ctx,
        bptt: tCfg.bptt,
      }, 1);
      // 将 transformer 包装到 baseModel 上
      (baseModel as any)._transformer = transformer;
      (baseModel as any).step = function(xId: number, state: GRUState, depth?: number): Float64Array {
        const t = (this as any)._transformer;
        if (!t) return new Float64Array(0);
        const tState: any = (this as any)._tState ?? ((this as any)._tState = t.newState());
        // 路由深度: 允许 1..nLayer 裁剪 (MoD 预算内扩深/裁剪)
        const d = Math.max(1, Math.min(depth ?? t.cfg.nLayer, t.cfg.nLayer));
        return t.step(xId, tState, d) as Float64Array;
      };
    } else if (meta.multiNeuro) {
      const cfg: CharMultiNeuroConfig = {
        ...meta.config,
        moeTopK: meta.moeTopK ?? 2,
        moeGateHidden: meta.moeGateHidden ?? 32,
        moeLoadBalanceWeight: meta.moeLoadBalanceWeight ?? 0.01,
        onlineTitans: meta.onlineTitans ?? false,
        moeNExperts: meta.moeNExperts ?? 4,
        // SNN 脉冲支路 (独立参数, 与 checkpoint 训练时一致)
        snn: (meta.config as CharMultiNeuroConfig).snn ?? false,
        snnLeak: (meta.config as CharMultiNeuroConfig).snnLeak,
        snnThresh: (meta.config as CharMultiNeuroConfig).snnThresh,
      };
      multiNeuro = new CharMultiNeuro(cfg, 1);
      if (multiNeuro.paramCount() !== params.length) {
        throw new Error(`checkpoint 参数数量与配置不匹配:${multiNeuro.paramCount()} != ${params.length}`);
      }
      multiNeuro.load(params);
      baseModel = multiNeuro.model;
      // 恢复 Titans 记忆 (如果有的话)
      if (meta.onlineTitans && meta.titansWriteLog && meta.titansWriteLog.length > 0) {
        const log = multiNeuro.getTitansWriteLog();
        for (const rec of meta.titansWriteLog as any[]) {
          const h1 = new Float64Array(rec.h1);
          log.push({ slot: rec.slot, h1: h1 });
        }
        console.log(`恢复 Titans 记忆:${meta.titansWriteLog.length} 条记录`);
        
        // 直接从日志重建 Titans 记忆槽位
        const titansMem = new TitansMemory({ dim: meta.config.emb, slots: 256 });
        const dim = meta.config.emb;
        for (const rec of meta.titansWriteLog as any[]) {
          const h1 = new Float64Array(rec.h1);
          const slot = rec.slot % titansMem.cfg.slots;
          // 从词表中选择合适的字符(高频字)
          const vocab = meta.vocab;
          titansMem.chars[slot] = vocab[rec.slot % vocab.length] || "。";
          for (let i = 0; i < dim; i++) {
            titansMem.keys[slot * dim + i] = h1[i];
            titansMem.values[slot * dim + i] = h1[i];
          }
        }
        console.log(`重建 Titans 记忆槽位:${titansMem.chars.filter((c: string) => c).length} 个非空槽位`);

      }
    } else {
      baseModel = new CharGRU(meta.config, 1);
      if (baseModel.paramCount() !== params.length) {
        throw new Error(`checkpoint 参数数量与配置不匹配:${baseModel.paramCount()} != ${params.length}`);
      }
      baseModel.load(params);
    }
    // 统一在这里创建 Engine,避免 multiNeuro=null 导致多神经协同功能失效
    // 宪章第10条: 生产 data/ 只读 — 数据根从 checkpoint 目录推导 (sandbox 检查点 → <root>/sandbox/data, 生产检查点 → <root>/data)
    // [FIX 2026-10-06] 旧逻辑用相对 "sandbox/data", 脚本 cd sandbox 后跑 generate 时拼成
    // sandbox/sandbox/data → bigram/世界模型静默缺失 (gen-smoke 置信 0.00 即因此)。
    // 现锚定到 ck 绝对路径里 sandbox/data 段的位置, 子目录检查点 (checkpoints-mamba/…) 也解析对。
    const ckAbs = path.resolve(checkpointDir);
    const parts = ckAbs.split(path.sep);
    const iSbx = parts.lastIndexOf("sandbox");
    const iData = parts.lastIndexOf("data");
    // 项目根 = sandbox 段 (或生产 ck 的 data 段) 之前的那一段 → 全绝对, 不依赖 cwd。
    const projRoot = (iSbx >= 0 ? parts.slice(0, iSbx) : iData >= 0 ? parts.slice(0, iData) : []).join(path.sep)
      || process.cwd();
    // sandbox 数据根: 语料快照; 生产数据根: bigram/世界模型/向量库 (只读)
    const dataRoot = iSbx >= 0 ? path.join(projRoot, "sandbox", "data") : path.join(projRoot, "data");
    const prodRoot = path.join(projRoot, "data");
    const eng = new Engine(baseModel, tokenizer, meta, multiNeuro, dataRoot, prodRoot, checkpointDir);
    return eng;
  }

  private registerUnits(): void {
    this.registerUnitsTo(this.station.registry, this.station.economics);
  }

  /**
   * [P0-2 修复 2026-10-13 第一性审核] 把引擎侧计算单元注册到任意工作站注册表,
   * 并同步注入经济学的真实 model.step 上界。
   * 背景: SessionEngine 每会话自建 createStation(), 旧实现其 station 注册表只有
   * SEED 种子 (text 链零单元) → executor 恒走「零单元」分支, MoD 路由/审计/经济性
   * 在服务器路径上是死代码 (真实输出字来自 peekLogits(4) 全深兜底, 审计记 depth=0)。
   * 现: SessionEngine 构造时对 baseEngine 调本方法, 把与共享 Engine 完全相同的
   * 计算单元 (titans/gru/mamba/attn/cnn/snn/world/vision/speech/output) 注入
   * 会话工作站 → 服务器路径多神经协同真跑 (宪章第1/2条)。
   * 单元 forward 闭包引用的 engine/model/titansMem/floors 均为 baseEngine 共享只读
   * 资源; 黑板键带 task.id (每会话独立编号+上下文指纹), 会话间无串读。
   */
  registerUnitsTo(reg: import("../station/registry").Registry, economics?: import("../station/economics").OnDemandEconomics): void {
    const engine = this;
    
    // L0 — Titans 记忆 (零算力直出,depth 0)
    // [P1-H 修复 2026-10-08 第一性审核] structural 白名单化: 豁免须 evidence.status!=="fail"
    // + note 含 checkpoint 证据物。titans SEED 证据 status="fail" (一致性 33.8% < 0.5 门槛)
    // → 不得豁免, 保持 enabled=false (与 SEED registry 一致)。
    if (this.meta.onlineTitans) {
      reg.register({
        kind: "titans", name: "Titans 记忆", role: "L0 零算力记忆直出", stage: "phase3", enabled: false, status: "idle",
        structural: true,
        justification: "在线神经记忆:上下文嵌入检索 → 槽位字符输出 (已训 checkpoint 结构层); SEED 证据 L0 一致性 33.8% < 0.5 门槛 (fail) → 不得豁免 (P1-H 白名单化), 保持货架待大语料重测",
        evidence: { metric: "L0 直出一致性 (探针 l0-probe-check.js)", baseline: 0.5, result: 0.338, status: "fail", note: "2026-10-05 L0 一致性探针: 命中率 77.1% 但一致性 33.8% < 0.5 — 省算力真实, 正确性收益未过门槛; P1-H 白名单化后 fail 证据不得 structural 豁免 (宪章第7条), 待健康检查点重测" },
        compute: {
          unitId: "titans:depth0", depth: 0, cost: 0, chains: ["text"],
          forward: async (task, state) => {
            // 直接从 TitansMemory 检索最佳槽位
            const ctx = task.input as number[];
            const q = Float64Array.from(engine.model.embedAvg(ctx));
            const cache = { query: new Float64Array(0), keys: new Float64Array(0), values: new Float64Array(0), weights: new Float64Array(0) };
            const mem = engine.titansMem.read(q, cache);
            if (mem.hitSlots > 0 && cache.weights.length > 0) {
              let best = 0;
              for (let i = 1; i < cache.weights.length; i++) if (cache.weights[i] > cache.weights[best]) best = i;
              if (cache.weights[best] > 0.3) {
                const ch = engine.titansMem.getChar(best % engine.titansMem.cfg.slots);
                if (ch !== undefined && ch !== "" && !engine.isDegenerate(ch)) {
                  // 同时写入 mem 和 o,确保 L0 路径能检测到
                  state.data[`mem:${task.id}`] = ch;
                  state.data[`o:${task.id}`] = ch;
                  state.data["z:act:titans"] = mem.hitSlots / engine.titansMem.cfg.slots;
                  const conf = Math.min(1, mem.hitSlots / 5); // 归一化置信度
                  engine.floors.record("titans:depth0", conf);
                  return conf;
                }
              }
            }
            // 无命中兜底: 由本单元真实观测的最低置信度推导 (冷启动=0, 非写死常数, 宪章第1条)
            return engine.floors.fallback("titans:depth0");
          },
        },
      });
    }
    
    // 感知神经:第 1 层 GRU(emb → 状态流),神经链入口
    reg.register({
      kind: "gru", name: "GRU L1", role: "感知:字符嵌入 → 状态流", stage: "phase1", enabled: true, status: "idle",
      roleTag: "perception",
      structural: true,
      justification: "基线状态流;无此层无任何输出 (已训骨干结构层, 推理分布一致性豁免证据门槛)",
      compute: {
        unitId: "gru:shallow", depth: 1, cost: 1, chains: ["text"],
        forward: async (task, state) => {
          const logits = engine.peekLogits(1);
          state.data[`z:logits:${task.id}`] = logits;
          state.data["z:act:gru:shallow"] = engine.model.confidence(logits);
          return engine.model.confidence(logits);
        },
      },
    });
    if (engine.model.cfg.mamba) {
      // 中央神经 (快动力学):真选择性 SSM(depth 2),点火 = Δ 均值
      // [P1-H 修复 2026-10-08 第一性审核] structural 白名单化: mamba SEED 证据 status="fail"
      // (3 种子均值 Δ=+0.4pt < +1.0 门槛) → 不得豁免, 保持 enabled=false (宪章第7条)。
      reg.register({
        kind: "mamba", name: "Mamba", role: "快动力学:选择性 SSM 状态流", stage: "phase1", enabled: false, status: "idle",
        roleTag: "central",
        structural: true,
        justification: "对照基线:线性状态建模 vs 纯骨干(同配置) (已训 checkpoint 中央结构层); P1-H 白名单化后 fail 证据不得豁免, 待大真实语料重测过门槛",
        evidence: { metric: "事件语料 top-1(30k×4ep, 3 种子均值单变量消融)", baseline: 10.4, result: 10.8, status: "fail", note: "2026-10-05 ablation-mamba-multiseed.sh: 3 种子均值 Δ=+0.4pt < +1.0 门槛 → 未过 (单种子7 的 +1.7 为运气, seed3/11 Δ=0.0)。容量混淆已控 (on 190k < base 233k); 小语料噪声淹没效应; P1-H 白名单化: fail 证据不得 structural 豁免 (宪章第7条), 待大真实语料重测" },
        compute: {
          unitId: "mamba:ssm", depth: 2, cost: 2, chains: ["text"],
          forward: async (task, state) => {
            const logits = engine.peekLogits(2);
            state.data[`z:logits:${task.id}`] = logits;
            state.data["z:act:mamba:ssm"] = Math.min(1, engine.model.ssm!.lastDeltaMean / 1.5);
            return engine.model.confidence(logits);
          },
        },
      });
    } else {
      // 中央神经 (快动力学):GRU 第 2 层
      reg.register({
        kind: "gru", name: "GRU L2", role: "快动力学:第二层 GRU 状态流", stage: "phase1", enabled: true, status: "idle",
        roleTag: "central",
        structural: true,
        justification: "共享底座深路径 (已训骨干结构层, 推理分布一致性豁免证据门槛)",
        compute: {
          unitId: "gru:deep", depth: 2, cost: 2, chains: ["text"],
          forward: async (task, state) => {
            const logits = engine.peekLogits(2);
            state.data[`z:logits:${task.id}`] = logits;
            state.data["z:act:gru:deep"] = engine.model.confidence(logits);
            return engine.model.confidence(logits);
          },
        },
      });
    }
    if (engine.model.cfg.attn) {
      // 中央神经 (慢语义):学习型自注意力 (depth 3),点火 = gate
      reg.register({
        kind: "sparse-attention", name: "稀疏 Attention", role: "慢语义:学习型自注意力", stage: "phase2", enabled: true, status: "idle",
        roleTag: "central",
        structural: true,
        justification: "跨位置信息混合,独立于循环路径;真实计算单元 (已训 checkpoint 中央结构层, 推理分布一致性豁免证据门槛)",
        evidence: { metric: "事件语料 top-1(30k×2ep)", baseline: 49.1, result: 47.8, status: "pending", note: "机制正确+gate 选择性;任务级证据未达成; 已训 checkpoint 结构层 (attn 中央层), P1-H 白名单: pending+checkpoint 证据物允许豁免, 独立 Attention 系统仍守门槛" },
        compute: {
          unitId: "attn:layer3", depth: 3, cost: 3, chains: ["text"],
          forward: async (task, state) => {
            const logits = engine.peekLogits(3);
            state.data[`z:logits:${task.id}`] = logits;
            state.data["z:act:attn:layer3"] = engine.model.lastAttnGate;
            return engine.model.confidence(logits);
          },
        },
      });
    }
    if (engine.model.cfg.cnn) {
      // 感知神经 (局部):1D 卷积 n-gram(depth 4),点火 = 特征激活均值
      reg.register({
        kind: "cnn", name: "CNN", role: "局部感知:1D 卷积 n-gram 特征", stage: "phase1", enabled: true, status: "idle",
        roleTag: "perception",
        justification: "局部字符模式特征,独立预测头与循环/注意力输出融合 (pass 证据直接过门槛, P1-H: 无需 structural 豁免)",
        evidence: { metric: "事件语料 top-1(30k×4ep 同配置消融)", baseline: 47.5, result: 49.3, status: "pass", note: "gradcheck-cnn=0.0000; 修复 top-1 缺口 +1.8pt; 就后召回探针 9.7→6.5(小规模代价,大语料复查); P1-H: pass 证据直接过门槛, 无需 structural 豁免" },
        compute: {
          unitId: "cnn:layer4", depth: 4, cost: 1, chains: ["text"],
          forward: async (task, state) => {
            const logits = engine.peekLogits(4);
            state.data[`z:logits:${task.id}`] = logits;
            state.data["z:act:cnn:layer4"] = engine.model.lastCnnAct;
            return engine.model.confidence(logits);
          },
        },
      });
    }

    // SNN 脉冲支路 (快动力学·脉冲门控中央神经, 独立 LIF 膜电位)
    if (engine.multiNeuro?.spike) {
      reg.register({
        kind: "snn", name: "SNN SpikeSSM", role: "快动力学·脉冲门控:LIF 膜电位 + PULSE surrogate", stage: "phase1", enabled: false, status: "shelf",
        roleTag: "central",
        justification: "独立 LIF 脉冲支路, 与骨干零共享参数, 输出端加法融合 logits (2026-10-04 降 shelf: 原 pass 证据混淆未控, 待重消融)",
        evidence: { metric: "事件语料 top-1(消融)", baseline: 0, result: 0, status: "pending", note: "SNN 支路已接入训练/推理; 旧消融 Δ=+27.9pt 因参数不等/单种子混淆降 pending, 重消融通过门槛前保持 shelf (宪章第7条)" },
        compute: {
          unitId: "snn:spike", depth: 2, cost: 2, chains: ["text"],
          forward: async (task, state) => {
            const sLogits = engine.multiNeuro!.spikeReadLast();
            if (sLogits) {
              state.data[`z:spike:${task.id}`] = sLogits;
              state.data["z:act:snn:spike"] = engine.multiNeuro!.spikeIgnition();
              const conf = engine.model.confidence(sLogits);
              engine.floors.record("snn:spike", conf);
              return conf;
            }
            // 无脉冲输出兜底: 由本单元真实观测的最低置信度推导 (冷启动=0, 非写死常数, 宪章第1条)
            return engine.floors.fallback("snn:spike");
          },
        },
      });
    }

    // 世界域中央神经: 世界模型状态转移 + 物理一致性 (已训 checkpoint 结构层,
    // 仅 world 任务按需调用 — 每字符 text 生成零额外开销, 宪章「按需调用」)
    if (this.worldModel) {
      const eng = this;
      reg.register({
        kind: "world-model", name: "World Model", role: "世界域中央: 状态转移 + 物理一致性 (Dreamer/Genie 逻辑自研)", stage: "deferred", enabled: true, status: "idle",
        roleTag: "central",
        structural: true,
        justification: "已训 checkpoint 结构层 (world_model.json, 编码器/转移/解码器/物理约束); 仅 world 任务按需调用, text 生成零额外开销",
        evidence: { metric: "世界一致性置信度 (任务级缺口消融)", baseline: 0, result: 0, status: "pending", note: "已训 checkpoint 结构层 (world_model.json 加载侧 paramCount 校验通过); P1-H 白名单: pending+checkpoint 证据物允许 structural 豁免, 待补「世界模型单元 vs 无」消融回填 (宪章第7条)" },
        compute: {
          unitId: "world-model:wm", depth: 3, cost: 2, chains: ["world"],
          forward: async (task, state) => {
            const wm = eng.worldModel!;
            const ids = (task.input as number[] | undefined) ?? [];
            const obs = ids.length > 0 ? Float64Array.from(eng.model.embedAvg(ids)) : new Float64Array(wm.embDim);
            const ws = wm.encode(obs, 0);
            const phys = wm.evaluatePhysics(ws);
            // 置信度 = 状态相干性 × 能量守恒一致性 (真实数值, 无写死)
            const conf = Math.max(0, Math.min(1, ws.coherence * (0.5 + 0.5 * (1 - phys[0]))));
            state.data[`z:world:phys:${task.id}`] = Array.from(phys);
            state.data[`z:world:state:${task.id}`] = Array.from(ws.s);
            state.data[`z:act:world-model:wm`] = conf;
            return conf;
          },
        },
      });
    }

    // 视觉域感知前端: 强度网格 → 真实边缘/结构响应 (自研数值, 非外部模型/写死)
    // 宪章第7条: 域消融 v2 (vision-ablation-v2.js) 分离度 0.8929 ≥ 0.2 门槛 → 过,
    // enabled; v1 (8×8+噪声) 0.129 未过记录在案, v2 强化协议 (32×32 + 阶梯刺激 + 阴性对照 + 噪声鲁棒) 通过
    // [P1-H 修复 2026-10-08 第一性审核] 零参数数值核 (vision/speech 前端, 非已训 checkpoint 结构层)
    // 不得标 structural — 旧实现带 structural 短路豁免, 白名单化后移除。
    reg.register({
      kind: "vision", name: "Vision 感知前端", role: "视觉域感知: 强度网格 → 真实 LoG/Sobel 结构响应 (自研数值)", stage: "deferred", enabled: true, status: "idle",
      roleTag: "perception",
      justification: "自研视觉前端 (frontends.ts visionFront): max|Lap|/mean|Sobel| 不连续度集中度 → 归一化置信度; 非写死 (宪章第1条); 域消融 v2 过门槛 (pass 证据)",
      evidence: { metric: "视觉域结构检测 (锐利/棋盘 vs 平滑, 32×32, 3 种子×200)", baseline: 0, result: 0.8929, status: "pass", note: "2026-10-05 消融 v2 (vision-ablation-v2.js): 分离度 0.8929≥0.2 (meanA=0.9214 vs meanB=0.0285), 阴性C=0.0000<0.05, 单调 C<B<A 3/3, 含噪声鲁棒子类; v1 (8×8+噪声) 0.129 未过记录在案; P1-H: 零参数数值核, pass 证据直接过门槛, 无需 structural 豁免" },
      compute: {
        unitId: "vision:frontend", depth: 1, cost: 1, chains: ["vision"],
        forward: async (task, state) => {
          const input = (task.input as number[] | undefined) ?? [];
          const rep = visionFront(input.length > 0 ? input : [0, 0]);
          state.data[`z:vision:feat:${task.id}`] = rep.summary;
          state.data["z:act:vision:frontend"] = rep.confidence;
          return rep.confidence;
        },
      },
    });

    // 声学域感知前端: 声学时序 → RMS/过零率/谱通量 (自研数值, 非外部模型/写死)

    // 输出神经:整合所有中央神经的 logits 到最终输出向量 (MoE 聚合头风格)
    // [P1-1 修复 2026-10-13 第一性审核] 本单元为零参数整合层 (共享底座最深层 logits 经黑板落槽),
    // 无真实消融数字 (旧 evidence baseline=0/result=1.0 为机制一致性自证, 非对照组对比) →
    // P1-1 数值门槛下不得 enabled (宪章第7条「数字说话」), 保持货架待补「output:aggregate vs 单头纯骨干」
    // 真实缺口消融 (top-1 对照) 回填 evidence.status="pass" + 真实数字后重启用。
    reg.register({
      kind: "output", name: "输出神经", role: "多神经协同的最终输出聚合器 (整合所有中央神经的 logits 到最终输出向量)", stage: "phase3", enabled: false, status: "shelf",
      roleTag: "output",
      justification: "输出聚合:整合所有中央神经的 logits 到最终输出向量,支持动态路由门控与负载均衡;使用骨干最深层 logits 作为最终输出 (零参数整合层, 无独立参数)",
      evidence: { metric: "输出整合一致性 (输出神经聚合 vs 单头纯骨干 top-1 对照)", baseline: 0, result: 0, status: "pending", note: "2026-10-13 第一性审核 P1-1: 旧 baseline=0/result=1.0 为机制一致性自证 (logits 切路已验证), 非真实消融数字; 零参数整合层无独立 checkpoint 证据物, 不得 structural 豁免; 待补「output:aggregate 经黑板落槽 vs 直接读骨干 logits」真实 top-1 对照消融 (3 种子同配置) 过 +1.0pt 门槛后回填 pass (宪章第7条)" },
      compute: {
        unitId: "output:aggregate", depth: 5, cost: 1, chains: ["text"],
        forward: async (task, state) => {
          // 调用骨干最深层 logits 作为聚合输出 (共享底座自然聚合所有前面神经的计算结果)
          const logits = this.peekLogits(4);
          state.data[`o:${task.id}`] = logits;
          return this.model.confidence(logits);
        },
      },
    });
    // 宪章第7条: 域消融过门槛 (谐波 vs 静默 分离度 0.2450 ≥ 0.2, 3 种子×200, vision-speech-ablation.js)
    // [P1-H 修复 2026-10-08 第一性审核] 零参数数值核, 不得标 structural (pass 证据直接过门槛)
    reg.register({
      kind: "speech", name: "Speech 感知前端", role: "声学域感知: 声学时序 → RMS/过零率/谱通量 (自研数值)", stage: "deferred", enabled: true, status: "idle",
      roleTag: "perception",
      justification: "自研声学前端 (frontends.ts speechFront): 时序样本 → RMS 能量 + 过零率 + 一阶谱通量近似 → 归一化置信度; 非写死 (宪章第1条); 域消融过门槛 (pass 证据)",
      evidence: { metric: "声学域活跃度 (谐波 vs 静默 类间分离度, 3 种子×200)", baseline: 0, result: 0.245, status: "pass", note: "2026-10-05 消融 (vision-speech-ablation.js): 分离度 0.2450≥0.2 门槛 (meanA=0.5821 vs meanB=0.3368), 过控 = 谐波信号显著高于静默噪声; 真实数值, 无写死; P1-H: 零参数数值核, pass 证据直接过门槛, 无需 structural 豁免" },
      compute: {
        unitId: "speech:frontend", depth: 1, cost: 1, chains: ["speech"],
        forward: async (task, state) => {
          const input = (task.input as number[] | undefined) ?? [];
          const rep = speechFront(input.length > 0 ? input : [0]);
          state.data[`z:speech:feat:${task.id}`] = rep.summary;
          state.data["z:act:speech:frontend"] = rep.confidence;
          return rep.confidence;
        },
      },
    });
    // [P0-2 配套 2026-10-13] 注入经济学的真实 model.step 上界 (R1-B1 口径):
    // 全链全跑 = 注册链可达单元数 (同深度取一 ≈ 最大 depth) + 1 (推进 advance)。
    // 与 Engine 构造器内同名推导一致, 避免会话工作站沿用默认值失真。
    if (economics) {
      const textUnits = reg.computeUnits("text");
      const maxDepth = textUnits.reduce((m, u) => Math.max(m, u.depth), 0);
      const reachable = textUnits.filter((u) => u.cost > 0).length;
      economics.setMaxRealForwards(Math.max(reachable, maxDepth) + 1);
    }
  }
  private options: { temperature: number; topK: number } = { temperature: 0.1, topK: 3 };

  async generate(opts: GenerateOptions): Promise<GenerateResult> {
    const { prompt, maxLen, budget } = opts;
    this.l0Probe.reset(); // L0 一致性探针: 单次生成分母干净 (各 run 独立统计)
    this.options = { temperature: opts.temperature, topK: opts.topK };
    if (opts.seed !== undefined) this.habit = mulberry32(opts.seed);
    // 关键:prompt 必须先推进真实状态,模型才能"看到"完整上下文
    this.state = this.model.newState();
    this.resetSpike(); // SNN 膜电位重置 (生成开始时)
    if (this.multiNeuro?.spike) this.multiNeuro.setSpikeInferenceMode(true); // 推理侧不累积 BPTT 训练缓存
    const promptIds = this.tokenizer.encode(prompt);
    for (const id of promptIds) this.advance(id);
    this.ctxWin = promptIds.slice(-this.model.cfg.ctx);
    const routes: CharRoute[] = [];
    let text = prompt;
    for (let i = 0; i < maxLen; i++) {
      const ctx = this.ctxWin.slice();
      const ctxFp = Engine.ctxFingerprint(ctx, this.model.cfg.vocabSize); // [P0-B] 上下文指纹 (稳定哈希, 非 seed)
      const task: Task = { id: `${i}:${ctxFp}`, type: "text", input: ctx, complexity: Math.min(1, 0.4 + (i / Math.max(maxLen, 1)) * 0.4), budget };
      const recalled = this.recallAt(ctx);
      // [P0-B 修复 2026-10-08 第一性审核] mem: 键绑定「上下文指纹」而非任务序号 —
      // 旧键 `mem:m${i}` 在 generate() 入口从 m0 重编号, 黑板不清空 → 服务器常驻进程
      // 必然用上一请求残留键制造假命中 (审核实测 m1/m2 本轮零召回却报 L0 命中)。
      // 新键 `mem:${ctxFp}` 仅当「相同上下文」时命中, 不同上下文的残留键天然失效。
      if (recalled) this.station.blackboard.write(`mem:${ctxFp}`, recalled.char);
      // L0 一致性探针 (宪章第7/8条): 每步记任务数; L0 命中时记 L0 直出字 vs 同上下文深链 top-1
      this.l0Probe.noteTask();
      if (recalled) {
        const deep = this.deepTop1At();
        this.l0Probe.record({
          ctxPreview: this.tokenizer.decode(ctx).slice(-4),
          chL0: recalled.char,
          chDeep: deep.char,
          deepP: deep.p,
          source: recalled.source,
        });
      }
      // L0 Titans 记忆直出 — 在 executor 之前直接检查(避免黑屏时序问题)
      let ch = "";
      let memoryHit = false;
      // const titansHit = this.getTitansOutput(ctx);
      // if (titansHit !== null && !this.isDegenerate(titansHit)) {
      //   ch = titansHit;
      //   memoryHit = true;
      //   const mid = this.tokenizer.idOf(ch);
      //   if (mid !== undefined) {
      //     this.advance(mid);
      //     this.ctxWin.push(mid);
      //     this.ctxWin = this.ctxWin.slice(-this.model.cfg.ctx);
      //     this.titansMem.write(Float64Array.from(this.model.embedAvg(ctx)), ch);
      //   }
      //   routes.push({ char: ch, depth: 0, confidence: 1.0, units: ["titans"], memoryHit: true, memorySource: "repeat" as any });
      //   this.propose(ch);
      //   text += ch;
      //   continue;
      // }
      const res: TaskResult = await this.station.executor.execute(task);
      memoryHit = res.memoryHit ?? false;
      // [P0-A 修复 2026-10-08 第一性审核] L0 记忆直出真接: res.output (executor 返回的
      // 记忆字符) 必须直接作为输出字, 不得丢弃后走 peekLogits(4) 兜底深链 (旧实现 res.output
      // 全仓从未被读取, L0 命中步仍跑 4 次完整前向, 审计记 depth=0/units=[]/conf=1 而真实
      // 输出是深链结果 — 宪章第 1 条「假输出」)。
      // 不变量: L0 命中步的最终输出字 === res.output (记忆字), 由 l0Probe 记录供审计。
      let l0Advanced = false; // [P0-A 补漏] L0 命中步在本分支完成推进 → 下方「推进真实状态」块跳过, 防同字双推进
      if (res.memoryHit && res.depth === 0 && res.output !== undefined && res.output !== "") {
        ch = String(res.output);
        // 推进状态 (零算力: 仅 advance + titansMem 写, 不跑深链前向)。
        // [P0-A 补漏] L0 命中步在此完成推进, 跳过下方「推进真实状态」块, 避免同字双推进。
        const hitId = this.tokenizer.idOf(ch);
        if (hitId !== undefined) {
          this.advance(hitId);
          this.ctxWin.push(hitId);
          this.ctxWin = this.ctxWin.slice(-this.model.cfg.ctx);
          this.ctxWinWindow.add({ type: "char", char: ch, id: hitId }, 1);
          this.titansMem.write(Float64Array.from(this.model.embedAvg(ctx)), ch);
          l0Advanced = true;
        }
      }
      // 深度路径: 从 blackboard 取 logits 并采样 (仅 L0 未命中时执行)
      if (ch === "" && res.depth > 0) {
        const zLogits = this.station.blackboard.read(`z:logits:${task.id}`) as Float64Array | undefined;
        if (zLogits) {
          // SNN 脉冲支路融合: 若 SNN 单元已运行, 将 spike logits 加法融合进骨干 logits
          const spikeLogits = this.station.blackboard.read(`z:spike:${task.id}`) as Float64Array | undefined;
          if (spikeLogits && spikeLogits.length === zLogits.length) {
            for (let k = 0; k < zLogits.length; k++) zLogits[k] += spikeLogits[k];
          }
          const probs = this.model.logitsToProbs(zLogits);
          ch = this.tokenizer.charOf(sampleToken(this.penalized(probs), this.options.topK, this.options.temperature, this.habit));
        }
      }
      // [P0-A] 兜底仅用于 L0 未命中且深链也未产出的情况 (保证有输出, 不伪造零算力)
      if (ch === "") {
        const fallbackLogits = this.peekLogits(4);
        const probs = this.model.logitsToProbs(fallbackLogits);
        ch = this.tokenizer.charOf(sampleToken(this.penalized(probs), this.options.topK, this.options.temperature, this.habit));
      }
      // 路由深度 → 真实状态推进深度: 2026-10-08 第一性审核修复 — 旧实现主状态恒按
      // depth 4 全深推进, MoD 预算裁剪只作用于探针副本, 算力与路由结果脱钩。
      // 现按本步路由实际执行到的深度推进骨干 (res.depth=0 无单元执行 → 4 全深兜底)。
      const advDepth = (res.depth >= 1 && res.depth <= 4 ? res.depth : 4) as 1 | 2 | 3 | 4;
      
      // ===== 语义反思层 =====
      // 在字符进入真实状态之前,进行反思检查
      const reflection = reflect(ch, text, {
        minSentenceLen: 8,
        maxSentenceLen: 30,
        maxRepeatCount: 2,
        bigramThreshold: 0.001,
        punctWeight: 5.0,
        enabled: true,
      }, this.bigramStats ?? undefined);
      
      // 应用反思结果
      if (reflection.action === 'force_end') {
        ch = '。';
        console.log(`[反思] 强制结束句子: ${reflection.reason}`);
      } else if (reflection.action === 'reject') {
        ch = reflection.char;
        console.log(`[反思] 替换字符: ${reflection.reason}`);
      }
      // ===== 反思层结束 =====
      
      // 推进真实状态
      const id = this.tokenizer.idOf(ch);
      if (id !== undefined && !l0Advanced) {
        // [P0-D 补漏 2026-10-08] 旧实现恒 `advance(id)` 全深推进 — advDepth 已按本步
        // 路由深度算出却从未使用 (MoD 算力与路由脱钩的残留), 现真接。
        this.advance(id, advDepth);
        this.ctxWin.push(id);
        this.ctxWin = this.ctxWin.slice(-this.model.cfg.ctx);
        // 更新上下文窗口
        this.ctxWinWindow.add({ type: 'char', char: ch, id }, 1);
        // 在线写入 Titans 神经记忆
        this.titansMem.write(Float64Array.from(this.model.embedAvg(ctx)), ch);
      }
      routes.push({
        char: ch,
        depth: res.depth,
        confidence: res.confidence,
        units: res.units,
        memoryHit: memoryHit,
        memorySource: recalled?.source,
      });
      this.propose(ch);
      text += ch;
      if (!memoryHit && id !== undefined) this.memory.set(ctx.join(","), ch);
      if (this.memory.size > 600) {
        const first = this.memory.keys().next().value;
        if (first !== undefined) this.memory.delete(first);
      }
    }
    this.remember(text.substring(prompt.length));
    if (this.multiNeuro?.spike) this.multiNeuro.setSpikeInferenceMode(false); // 生成结束, 恢复训练模式 (训练侧调用可累积 BPTT 缓存)
    // [P1-L 修复 2026-10-09 第一性审核] 生成结束, 将探针和经济性数据写入 JSON (每次生成一轮)
    if (this.station && this.station.probe) {
      this.station.probe.writeToFile && this.station.probe.writeToFile();
    }
    if (this.station && this.station.economics) {
      this.station.economics.report(); // 内部包含写入逻辑
    }
    return { text, routes, budget };
  }

  get registry() {
    return this.station.registry;
  }

  get blackboard() {
    return this.station.blackboard;
  }

  get audit() {
    return this.station.audit;
  }

  get probe() {
    return this.station.probe;
  }

  get economics() {
    return this.station.economics;
  }

  get titans() {
    return this.titansMem;
  }

  get cnVectorStore() { return this.cnStore; }
  get mathVectorStore() { return this.mathStore; }
  /** [P0-1 2026-10-13] SessionEngine 读取共享向量库/大语料统计, 补公开 getter (宪章第10条: 共享只读) */
  get vectorStore() { return this.store; }
  get bigramStatsGetter() { return this.bigramStats; }
  getWorldModel() { return this.worldModel; }
  get reasoner() { return this._reasoner; }
  
  /** 获取上下文窗口 */
  getContextWindow() {
    return this.ctxWinWindow;
  }

  /** 返回模型总参数数 (CharMultiNeuro 含 MoE/专家头,CharGRU 仅 backbone) */
  totalParamCount(): number {
    return this.multiNeuro ? this.multiNeuro.paramCount() : this.model.paramCount();
  }
}