@@ -148,12 +148,36 @@ export function queryStreak(db: Database.Database): Record<string, unknown> {
148148 } ;
149149}
150150
151- /** learning_memory.discoveries — 深海发现(当前存于客户端 IndexedDB,sqlite 未接线) */
152- export function queryDiscoveries ( ) : Record < string , unknown > {
151+ /** 读取渲染进程同步过来的世界快照(useWorldSnapshotSync 写入,无则 null) */
152+ function readWorldSnapshot ( db : Database . Database ) : {
153+ corals ?: { total : number ; healthy : number ; bleached : number ; totalDepth : number } ;
154+ discoveries ?: { count : number } ;
155+ capturedAt ?: string ;
156+ } | null {
157+ try {
158+ const row = db . prepare ( "SELECT payload FROM world_snapshots WHERE id = 'latest'" ) . get ( ) as
159+ { payload : string } | undefined ;
160+ if ( ! row ) return null ;
161+ return JSON . parse ( row . payload ) ;
162+ } catch {
163+ return null ;
164+ }
165+ }
166+
167+ /** learning_memory.discoveries — 深海发现(优先读世界快照,无则占位) */
168+ export function queryDiscoveries ( db : Database . Database ) : Record < string , unknown > {
169+ const snap = readWorldSnapshot ( db ) ;
170+ if ( ! snap ?. discoveries ) {
171+ return {
172+ count : null ,
173+ items : [ ] ,
174+ note : '尚未获取到发现图鉴快照(应用启动并产生发现后自动同步)' ,
175+ } ;
176+ }
153177 return {
154- count : null ,
155- items : [ ] ,
156- note : '发现图鉴暂存于客户端本地存储,跨进程接线将在后续批次完成 ' ,
178+ count : snap . discoveries . count ,
179+ capturedAt : snap . capturedAt ?? null ,
180+ note : '图鉴明细存于客户端本地,此处为累计数量快照 ' ,
157181 } ;
158182}
159183
@@ -185,11 +209,12 @@ export function queryRecentSessions(db: Database.Database, limitRaw: unknown): R
185209}
186210
187211/**
188- * learning_memory.world_state — 世界状态快照(sqlite 派生版)
189- * @ai -context 珊瑚/发现实体存于渲染进程 IndexedDB,此处以 sqlite 过程数据
190- * 派生近似信号并显式标注 provenance,避免误导消费方 。
212+ * learning_memory.world_state — 世界状态快照
213+ * @ai -context 优先消费渲染进程同步的精确快照(珊瑚健康度→雾与活力);
214+ * 无快照时回退 sqlite 过程数据派生值并显式标注 provenance。
191215 */
192216export function queryWorldState ( db : Database . Database ) : Record < string , unknown > {
217+ const snap = readWorldSnapshot ( db ) ;
193218 const focus = db . prepare ( `
194219 SELECT COALESCE(SUM(actual_duration), 0) AS totalSeconds FROM pomodoro_sessions WHERE interrupted = 0
195220 ` ) . get ( ) as { totalSeconds : number } ;
@@ -198,11 +223,31 @@ export function queryWorldState(db: Database.Database): Record<string, unknown>
198223 ) . get ( ) as { n : number } ) . n ;
199224 const totalCards = ( db . prepare ( 'SELECT COUNT(*) AS n FROM flashcards' ) . get ( ) as { n : number } ) . n ;
200225
201- return {
226+ const base = {
202227 depthMeters : Math . round ( focus . totalSeconds / 60 ) ,
203228 depthNote : '以专注分钟折算的潜航深度(1 分钟 ≈ 1 米)' ,
204229 hazyShare : totalCards > 0 ? Math . round ( ( due / totalCards ) * 100 ) / 100 : 0 ,
205230 hazyNote : '朦胧知识占比(0-1)——雾的浓度,复习即拨开' ,
206- provenance : '由主库过程数据派生;珊瑚/发现等世界实体的精确状态以后续批次为准' ,
231+ } ;
232+
233+ if ( snap ?. corals ) {
234+ const { total, healthy, bleached, totalDepth } = snap . corals ;
235+ return {
236+ ...base ,
237+ ecosystem : {
238+ coralTotal : total ,
239+ coralHealthy : healthy ,
240+ coralBleached : bleached ,
241+ vitality : total > 0 ? Math . round ( ( healthy / total ) * 100 ) / 100 : null ,
242+ accumulatedDepthMeters : totalDepth ,
243+ } ,
244+ capturedAt : snap . capturedAt ?? null ,
245+ provenance : '珊瑚生态来自渲染进程同步快照(精确态);潜航深度与朦胧占比由主库派生' ,
246+ } ;
247+ }
248+
249+ return {
250+ ...base ,
251+ provenance : '由主库过程数据派生;珊瑚精确态待应用启动后同步' ,
207252 } ;
208253}
0 commit comments