import { Engine, GenerateOptions, GenerateResult, CharRoute } from "../ml/engine"; import type { GRUState } from "../ml/engine"; import { createStation } from "../station"; import { Task, TaskResult } from "../station/types"; import { chunkText } from "../ml/data"; import { reflect } from "../ml/reflection"; import { mulberry32 } from "../ml/rng"; import { ContextWindow } from "../ml/tool_system"; import { ConfidenceFloors } from "../ml/floors"; import { TitansMemory } from "../ml/titans"; import { L0Consistency } from "../station/l0probe"; // SessionState holds all mutable state per session interface SessionState { // Core generation state state: GRUState; recentChars: string[]; memory: Map; titansMem: TitansMemory; floors: ConfidenceFloors; l0Probe: L0Consistency; ctxWinWindow: ContextWindow; habit: () => number; options: { temperature: number; topK: number }; ctxWin: number[]; } /** * SessionEngine - 每个会话独立的引擎实例 * 包装一个共享的 Engine 实例(只读部分),维护每个会话自己的 mutable state * 解决并发请求共享可变引擎状态问题 * * [P0-1 修复 2026-10-13 第一性审核] 原实现存在 9 处编译断裂: * 1. import GRUState 自 engine (未导出) → 现 engine 已 re-export * 2. import BeautyOfWords/Characterizer 自不存在的 ../ml/beauty_of_words / characterizer → 删 (死引用) * 3. import TitansMemoryConfig (titans.ts 只导出 TitansConfig) → 现 titans.ts 补别名 * 4. import { l0Probe } (l0probe 模块无实例导出) → 现 l0probe 补共享实例; 会话侧用 per-session L0Consistency * 5. class 字段在 baseEngine 之前初始化 (TS2729) → 现改 getter 延迟读取 * 6. 直接访问 Engine 私有成员 (store/cnStore/mathStore/worldModel/bigramStats/resetSpike) → 现走公开 getter / multiNeuro 直调 * 7. advance(id, advDepth, ss) 参数顺序错 (签名 advance(chId, ss, depth)) → 现 advance(id, ss, advDepth) * 8. routes 类型与 CharRoute.memorySource 窄化不符 → 现直用 CharRoute[] * 9. baseEngine.resetSpike 私有 → 现 baseEngine.multiNeuro?.resetSpikeSeq() 直调 * * [P0-2 修复 2026-10-13] 会话工作站单元注入: * 旧实现会话 station 只有 SEED 种子 (text 链零单元) → executor 恒「零单元」分支, * MoD 路由/审计/经济性在服务器路径死代码 (真实输出全来自 peekLogits(4) 兜底, 审计恒 depth=0)。 * 现构造时 baseEngine.registerUnitsTo(station.registry, station.economics), * 把与共享 Engine 完全相同的计算单元 (gru/mamba/attn/cnn/snn/titans/world/vision/speech/output) * 注入会话工作站 → 服务器路径多神经协同真跑 (宪章第1/2条)。 * 会话黑板键: 带 task.id (= `${i}:${ctxFp}`) 与 mem:${ctxFp}, 每会话独立编号+上下文指纹, 无串读。 */ export class SessionEngine { /** 会话 ID */ public readonly sessionId: string; /** * [P0-1 修复 2026-10-13] 每会话专用 Station — 旧实现为类字段初始化器 * `station = createStation()` (TS 字段初始化器在 baseEngine 参数赋值前执行, * 且与旧字段 `model = this.baseEngine.model` 同列, 构造即崩溃 "reading 'model' of undefined")。 * 现改延迟 getter: 首次访问时创建, 构造时 registerUnitsTo 注入共享引擎单元。 */ private _station: import("../station").Station | null = null; public get station(): import("../station").Station { if (!this._station) this._station = createStation(); return this._station; } /** 每会话可变状态 */ private sessionState: SessionState; // [P0-1] 共享只读资源改 getter 延迟读取 (旧 class 字段在 baseEngine 之前初始化 → TS2729) private get model() { return this.baseEngine.model; } private get tokenizer() { return this.baseEngine.tokenizer; } private get bigramStats() { return this.baseEngine.bigramStatsGetter; } constructor(private baseEngine: Engine, sessionId: string) { this.sessionId = sessionId; // [P0-2] 注入工作站单元 (titans/gru/mamba/attn/cnn/snn/world/vision/speech/output + 经济学上界) this.baseEngine.registerUnitsTo(this.station.registry, this.station.economics); this.sessionState = this.createFreshState(); } private createFreshState(): SessionState { const cfg = this.model.cfg; return { state: this.model.newState(), recentChars: [], memory: new Map(), titansMem: new TitansMemory({ dim: cfg.emb, slots: 256 }), floors: new ConfidenceFloors(), l0Probe: new L0Consistency(), ctxWinWindow: new ContextWindow(cfg.ctx * 10), habit: mulberry32(Date.now() % 2147483647), options: { temperature: 0.1, topK: 3 }, ctxWin: [], }; } /** 重置会话状态(用于超时清理) */ reset(): void { this.sessionState = this.createFreshState(); } // ==================== 核心生成逻辑 - 复制 Engine.generate 并适配 per-session ==================== async generate(opts: GenerateOptions): Promise { const { prompt, maxLen, budget } = opts; const ss = this.sessionState; // 重置探针、选项 ss.l0Probe.reset(); ss.options = { temperature: opts.temperature, topK: opts.topK }; if (opts.seed !== undefined) ss.habit = mulberry32(opts.seed); // 重置状态 ss.state = this.model.newState(); // SNN 膜电位重置(如果有) // [P0-1] baseEngine.resetSpike 为私有 → 直接走 multiNeuro.resetSpikeSeq const spike = this.baseEngine.multiNeuro?.spike; if (spike) { this.baseEngine.multiNeuro!.resetSpikeSeq(); this.baseEngine.multiNeuro!.setSpikeInferenceMode(true); } const promptIds = this.tokenizer.encode(prompt); for (const id of promptIds) this.advance(id, ss); ss.ctxWin = promptIds.slice(-this.model.cfg.ctx); const routes: CharRoute[] = []; let text = prompt; for (let i = 0; i < maxLen; i++) { const ctx = ss.ctxWin.slice(); const ctxFp = SessionEngine.ctxFingerprint(ctx, this.model.cfg.vocabSize); const task: Task = { id: `${i}:${ctxFp}`, type: "text", input: ctx, complexity: Math.min(1, 0.4 + (i / Math.max(maxLen, 1)) * 0.4), budget }; // [P0-2] 会话侧召回: 重复缓存 (会话内) + 共享向量库 (只读) — 与 Engine.recallAt 同契约, // 命中写 mem:${ctxFp} → 会话 executor L0 分支真触发 (旧会话 recallAt 只查内存缓存, 向量库召回缺失) const recalled = this.recallAt(ctx, ss); if (recalled) this.station.blackboard.write(`mem:${ctxFp}`, recalled.char); ss.l0Probe.noteTask(); if (recalled) { const deep = this.deepTop1At(ss); ss.l0Probe.record({ ctxPreview: this.tokenizer.decode(ctx).slice(-4), chL0: recalled.char, chDeep: deep.char, deepP: deep.p, source: recalled.source, }); } // L0 记忆直出 / 深链路由 — 会话 executor 真跑 (P0-2 注入单元后) let ch = ""; let memoryHit = false; const res: TaskResult = await this.station.executor.execute(task); memoryHit = res.memoryHit ?? false; if (res.memoryHit && res.depth === 0 && res.output !== undefined && res.output !== "") { ch = String(res.output); const hitId = this.tokenizer.idOf(ch); if (hitId !== undefined) { this.advance(hitId, ss); ss.ctxWin.push(hitId); ss.ctxWin = ss.ctxWin.slice(-this.model.cfg.ctx); ss.ctxWinWindow.add({ type: "char", char: ch, id: hitId }, 1); ss.titansMem.write(Float64Array.from(this.model.embedAvg(ctx)), ch); } } if (ch === "" && res.depth > 0) { const zLogits = this.station.blackboard.read(`z:logits:${task.id}`) as Float64Array | undefined; if (zLogits) { 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(this.sampleToken(this.penalized(probs), ss.options.topK, ss.options.temperature, ss.habit)); } } if (ch === "") { const fallbackLogits = this.peekLogits(4, ss); const probs = this.model.logitsToProbs(fallbackLogits); ch = this.tokenizer.charOf(this.sampleToken(this.penalized(probs), ss.options.topK, ss.options.temperature, ss.habit)); } // 路由深度 → 真实状态推进深度 (MoD 算力与路由结果挂钩, 同 Engine.generate P0-D 纪律) 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 = '。'; } else if (reflection.action === 'reject') { ch = reflection.char; } const id = this.tokenizer.idOf(ch); if (id !== undefined) { // [P0-1] 旧 advance(id, advDepth, ss) 参数顺序错 → advance(chId, ss, depth) this.advance(id, ss, advDepth); ss.ctxWin.push(id); ss.ctxWin = ss.ctxWin.slice(-this.model.cfg.ctx); ss.ctxWinWindow.add({ type: "char", char: ch, id }, 1); ss.titansMem.write(Float64Array.from(this.model.embedAvg(ctx)), ch); } // [P0-1] routes 直用 CharRoute[] (旧自定义类型 memorySource: string 与窄化联合不符) routes.push({ char: ch, depth: res.depth, confidence: res.confidence, units: res.units, memoryHit, memorySource: recalled?.source, }); this.propose(ch, ss); text += ch; if (!memoryHit && id !== undefined) ss.memory.set(ctx.join(","), ch); if (ss.memory.size > 600) { const first = ss.memory.keys().next().value; if (first !== undefined) ss.memory.delete(first); } } // 记住生成内容 - 写入到共享向量库 (带 session 前缀防自举循环; 宪章第10条: 共享库只写本会话命名空间) const sharedStore = this.baseEngine.vectorStore; if (sharedStore) { this.remember(text.substring(prompt.length)); } // SNN 膜电位恢复 (生成结束) if (spike) { this.baseEngine.multiNeuro!.setSpikeInferenceMode(false); } // 写入探针和经济性 (会话各自 30s 写盘节律) this.station.probe.writeToFile(); this.station.economics.report(); return { text, routes, budget }; } // ==================== 辅助方法 ==================== private peekLogits(depth: 1 | 2 | 3 | 4, ss: SessionState): Float64Array { const lastId = ss.ctxWin.length > 0 ? ss.ctxWin[ss.ctxWin.length - 1] : 0; const copy: GRUState = { h1: ss.state.h1.slice(), h2: ss.state.h2.slice(), hist: ss.state.hist.map((h) => h.slice()), ids: [...ss.state.ids], }; return this.model.step(lastId, copy, depth); } private advance(chId: number, ss: SessionState, depth: 1 | 2 | 3 | 4 = 4): void { this.model.step(chId, ss.state, depth); if (this.baseEngine.multiNeuro?.spike) { this.baseEngine.multiNeuro.spikeAdvance(chId); } } private sampleToken(probs: Float64Array, topK: number, temperature: number, rand: () => number): number { 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]; } private penalized(probs: Float64Array): Float64Array { const out = probs.slice(); out[out.length - 1] = 0; const freq = new Map(); for (const ch of this.sessionState.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, ss: SessionState): void { ss.recentChars.push(ch); if (ss.recentChars.length > 16) ss.recentChars.shift(); } private isDegenerate(ch: string): boolean { const n = this.sessionState.recentChars.length; if (n >= 3) { const last3 = this.sessionState.recentChars.slice(-3).join(""); if (last3 === ch.repeat(3)) return true; } if (n >= 16) { const a = this.sessionState.recentChars.slice(-8).join(""); const b = this.sessionState.recentChars.slice(-16, -8).join(""); if (a === b) return true; } return false; } /** * [P0-2] 会话侧召回: 重复缓存 (会话内) + 共享向量库 (只读, MEMORY_TAG 前缀隔离防自举)。 * 命中写点火标记 z:l0:act → 会话 executor L0 分支读 z:l0:act 推导置信度 (与 Engine.recallAt 同契约)。 */ private recallAt(ctxIds: number[], ss: SessionState): { char: string; source: "vector" | "repeat" | "titans" } | null { const cached = ss.memory.get(ctxIds.join(",")); if (cached !== undefined && !this.isDegenerate(cached)) { this.station.blackboard.write("z:l0:act", 0.5); return { char: cached, source: "repeat" }; } // 共享向量库召回 (只读; MEMORY_TAG + session 前缀 → 检索跳过防自举循环) const store = this.baseEngine.vectorStore; if (store) { const q = this.tokenizer.decode(ctxIds); const hits = store.search(this.model.embedAvg(ctxIds), 8); for (const hit of hits) { if (hit.text.startsWith("\uE000")) 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)) { this.station.blackboard.write("z:l0:act", 0.5); return { char: ch, source: "vector" }; } } } } // 会话内 Titans 在线记忆召回 (与 Engine.recallAt 第三路同机制) 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 = ss.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) { const ch = ss.titansMem.chars[best % ss.titansMem.cfg.slots]; if (ch !== undefined && ch !== "" && !this.isDegenerate(ch)) { const l0Act = mem.hitSlots / ss.titansMem.cfg.slots; this.station.blackboard.write("z:l0:act", l0Act); return { char: ch, source: "repeat" }; } } } return null; } private deepTop1At(ss: SessionState): { char: string; p: number } { const logits = this.peekLogits(4, ss); 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] }; } private remember(text: string): void { const store = this.baseEngine.vectorStore; if (!store) return; for (const chunk of chunkText(text, 64, 32)) { if (chunk.length < 8) continue; const ids = this.tokenizer.encode(chunk); // [P0-1] 向量库 add 接受 number[] (vectorstore.ts 签名); multiNeuro 在则用其 embedAvg, 否则空向量占位 const vec: number[] = this.baseEngine.multiNeuro ? this.baseEngine.multiNeuro.embedAvg(ids) : []; store.add(`\uE000${this.sessionId}:${chunk}`, vec); } } // 静态工具方法 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"); } // ==================== 查询方法 (透传共享引擎) ==================== retrieve(q: string, k = 4): Array<{ text: string; score: number }> { return this.baseEngine.retrieve(q, k); } retrieveCN(q: string, k = 4, category?: string): Array<{ text: string; score: number; category?: string }> { return this.baseEngine.retrieveCN(q, k, category); } retrieveMath(q: string, k = 6, category?: string): Array<{ expr: string; description: string; score: number; matchType: string }> { return this.baseEngine.retrieveMath(q, k, category); } reason(q: string) { return this.baseEngine.reason(q); } retrieveCount(): number { return this.baseEngine.retrieveCount(); } totalParamCount(): number { return this.baseEngine.totalParamCount(); } } /** 会话管理器:管理多个 SessionEngine,处理会话生命周期和队列 */ export class SessionManager { private sessions = new Map(); private static readonly SESSION_TIMEOUT = 30 * 60 * 1000; // 30 分钟 private cleanupTimer: NodeJS.Timeout | null = null; constructor(private defaultEngine: Engine) {} getSession(sessId?: string): SessionEngine { if (!sessId) sessId = this.generateSessionId(); let session = this.sessions.get(sessId); if (!session) { session = new SessionEngine(this.defaultEngine, sessId); this.sessions.set(sessId, session); this.startCleanup(); } return session; } endSession(sessId: string): void { const session = this.sessions.get(sessId); if (!session) return; session.reset(); this.sessions.delete(sessId); if (this.sessions.size === 0 && this.cleanupTimer) { clearInterval(this.cleanupTimer); this.cleanupTimer = null; } } /** [P0-1] 旧 private 但 server/index.ts /api/session 直接调用 → 放宽为 public */ generateSessionId(): string { return "session-" + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15); } private startCleanup(): void { if (this.cleanupTimer) return; this.cleanupTimer = setInterval(() => this.cleanupStale(), SessionManager.SESSION_TIMEOUT / 2); } private cleanupStale(): void { for (const id of this.sessions.keys()) { this.endSession(id); } } getStats(): { active: number; totalParams: number } { return { active: this.sessions.size, totalParams: this.defaultEngine.totalParamCount() }; } }