feat(kbdb): 藏書地圖 M1+M2 — library_map template+/map 三端點(聚合 SQL 住基本盤) #72
Reference in New Issue
Block a user
Delete Branch "feat/library-map-base"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
實作 library-map SDD(
system-dev/docs/3-specs/library-map/,leo 已 confirm 可動工)的 M1+M2。關聯 #39。M1 —
library_maptemplate(D6 零建表零 ALTER)kbdb/migrations/0003_library_map.sql:templates 表 seed 一列(tpl-library-map,與 0001 recipe_stat 同模式,INSERT OR IGNORE 冪等);slots=design §1 的library/narrative/top_entities/relation_profile/bridges/triplet_count/commit_hash/status。ensureLibraryMapTemplate(走既有 createTemplate 路徑),migration 未跑的自架 D1 首次 recompute 自建。triplet)有source_uri、無libraryslot;entries 的metadata.library機制在(portal-auth P1)但既有資料未標記(?library=kb→ 0 筆)。結論=不足,走 SDD 預案:recompute 時冪等在 triplet template 補 optionallibraryslot(ensureTripletLibrarySlot,改 template 不動表);slot 值由 ingest 端補寫(M3)。核實結果已記入 design.md §1。M2 — kbdb base 三端點(design §2 關鍵歸屬裁定:聚合 SQL 只准住基本盤)
POST /map/recompute?library=X:一段聚合 SQL——top_entities(subject+object 兩側 degree 排序 top-N)、relation_profile(predicate 分布)、bridges(同 entity 跨庫 join)、triplet_count(active-only,沿用 status slot 語意)。narrative 本輪收 request body 傳入值(wiki 首段抽取屬 ingest M3,不在本 PR)。寫入順序安全(D1 無跨語句交易):先建新 active map block+record,成功後才把舊 active 標superseded;讀端一律取最新 active 自癒,不會出現地圖真空。source_prefix:M3 backfill 前,無 library 值的舊 triplet 以source_uri LIKE 前綴歸庫(前綴由 caller 提供,base 不寫死 URI 語意)。bridges 的「對面庫」只認 library slot 標記值 → backfill 前偏稀疏(誠實限制,記於 design §1)。GET /map:全館地圖,每庫一行(library+narrative+top 3 entities+triplet_count),給 MCP instructions(數百 token 內)/GUI 共用。GET /map/:library:該庫詳圖(完整 slots+record_id+content)。content=可嵌人話{library}:{narrative}。核心:{top_entities 前 3}(design §5,M6 semantic 路由直接嵌);record_id=該 block entry id,block(人話)與 record(結構化)同一身分。owner_id參數(聚合與讀端都支援 owner filter)。測試
kbdb/tests/library-map.test.ts:聚合 SQL 用 node:sqlite 當真 SQLite 實跑(Node ≥22.5 內建、零新依賴;migrations 0001+0003 原檔 exec)——degree 排序/predicate 統計/跨庫 bridges/source_prefix fallback/owner 隔離/supersede 順序安全全部真 SQL 驗證;route 行為(400/404/回應形狀)走 Hono app.request(與既有測試同款)。npx vitest run45/45 過(含既有 4 檔不動);npx tsc --noEmit乾淨。tests/node-builtins.d.ts 補 node 內建模組最小型別(tsconfig 只掛 workers-types)。merge 後(leo 閘)
POST /map/recompute?library=X(舊 triplet 未帶 library slot 值 → 過渡期帶source_prefix,如 kb 庫={"source_prefix":"gitea:Leo/kb@"})。🤖 Generated with Claude Code
https://claude.ai/code/session_01JUmjwkHLVBHM3ydhT1WSW3