Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/architecture/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Search uses `vault-search` and canonical Turso reads. `Vault` owns one indexer and a session registry. App Lock retains the Vault; closing it cancels jobs, drops every reader and joins the indexer before closing the store. The index lives under `derived/search-index/`, with the archive's encryption mode and a derived search key.

The indexer reads ordered outbox prefixes, loads current page documents and profile memberships, commits the highest sequence with `SearchIndex::apply`, then removes acknowledged rows with a Batch write. Reopen resumes from the durable index sequence. Reapplying a page replaces it. Missing, incompatible and corrupt generations rebuild from canonical page IDs. Healthy rebuilds publish a new generation only after its source scan and outbox tail are complete. Existing sessions retain their old reader until released. Initial builds publish increasing prefixes; every response identifies incomplete coverage.
The indexer reads ordered outbox prefixes, loads current page documents and profile memberships, commits the highest sequence with `SearchIndex::apply`, then removes acknowledged rows with a Batch write. It prunes after each applied batch, and once at its first poll after opening, for rows a crash left between publishing and pruning; a poll that finds nothing new writes nothing. Until 2026-11-13 every poll deleted from the outbox, ten times a second, and even a delete of no rows makes the archive writer checkpoint, hold readers back and empty their page caches. Reopen resumes from the durable index sequence. Reapplying a page replaces it. Missing, incompatible and corrupt generations rebuild from canonical page IDs. Healthy rebuilds publish a new generation only after its source scan and outbox tail are complete. Existing sessions retain their old reader until released. Initial builds publish increasing prefixes; every response identifies incomplete coverage.

Normalization remains in vault-core: ICU NFKC, accent folding, OpenCC, aliases, compact text and CJK segmentation. Visible titles from different profiles retain phrase boundaries. Date/profile/browser predicates apply to the same canonical visit. Selective dates use the visible time indexes; other searches resolve posting batches through page→URL→visible-visit indexes. Hydration verifies current visibility and, once the archive has moved past the session's index snapshot, rechecks each page's current text against the query, so revoked visits and text never reach a result. Writes during a session never restart it: an earlier version restarted membership and counting on every canonical change, and a steady stream of writes (a backup, link previews, page-text fetching) then kept a search from ever finishing.

Expand Down
11 changes: 11 additions & 0 deletions docs/architecture/semantic-index-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ lose the job's counts. Unchanged chunks still advance through the normal ledger
rebuild retains its captured archive boundary; an incremental resume against another lineage
starts at the beginning of that lineage's needs, including changed low-id visits.

A fresh build counts what is left to index before its first request, for the progress
denominator. The count reads visit ids 10,000 at a time, one short read each with a stop check in
between, and names `idx_ai_embeddings_lineage_history`. Left to itself Turso searches
`ai_embeddings` by provider, model and lineage, which every row of a lineage shares, so it reads
the whole lineage once per visit. The count was one unhinted statement, and from the lineage
column (2026-11-08) until 2026-11-13 it ran that way: 0.35 s for 2,000 synthetic visits already
indexed, 22.7 s for 16,000, hours for a real archive, and nothing could stop it. The same
8,000 visits took 6 ms on the schema before the lineage column. Any statement that joins
`ai_embeddings` on `history_id` needs the hint; `ai/tests_native/indexing/start.rs` checks the
plans of a build over an existing index.

The lease/closure audit covers status, query/staleness reads, related pages, topics, similar
searches, grouping `Prepared._generation`, grouping event/opened-page reads, snapshot signatures
and snapshot publication. `Prepared` stays on its caller, including the equal-digest warm branch;
Expand Down
6 changes: 5 additions & 1 deletion docs/architecture/turso-migration-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

- 每條連線只屬於一個執行緒。
- **寫入執行緒:** 每個資料庫一條,處理寫入佇列。佇列有兩個優先級:「互動」(星號、筆記、標籤、設定、預覽狀態)和「批次」(備份、匯入、重建)。每個批次交易結束後,先處理互動寫入,再開下一批。
- **讀取執行緒池:** 大小由記憶體量測決定。每條讀取只開短交易,以免卡住 checkpoint。
- **讀取執行緒池:** 大小由記憶體量測決定。每條讀取只開短交易,以免卡住 checkpoint。要數或走遍整個存檔的讀取分頁做:每頁一個交易,頁與頁之間檢查取消。
- 對外是同步的 request/reply 介面,接上 Tauri 的 `spawn_blocking`。不把 async 往上傳。

**取消:** 長操作(匯出、搜尋 collector、regex 掃描、批次寫入)每批之間檢查取消旗標。Turso 沒有公開的中斷介面。
Expand All @@ -80,6 +80,8 @@

- 每個批次之後做 PASSIVE。
- 寫入佇列空下來,而且讀取都結束時,做 TRUNCATE。
- 為了等到「讀取都結束」,writer 先擋住新的讀取,再等進行中的讀取離開。這一等最多 50 ms:短讀取在這之內都會結束。有一條讀取跑得久而等不到時,writer 放行新的讀取,改在最後一條讀取離開時再做。那次等的時候在裡面的讀取,只要還有一條沒走,writer 就不再擋第二次:不管是之後又有寫入,還是它聽說有別的讀取走了(那個通知可能是它忙的時候留下的)。這條規則記在閘門裡(`vault-store/src/checkpoint.rs` 的 `Gate`),writer 的迴圈只問閘門。`Metrics::truncates_put_off` 數 writer 放棄了幾次,測試靠它判斷,不量時間。
- 所以跑得久的讀取只會延後 TRUNCATE(它的 snapshot 本來就讓 WAL 截不掉),不會讓別的讀取或後面的寫入等它。延後的期間 WAL 會隨寫入長大:Turso 的 PASSIVE 不會讓 WAL 從頭寫起(量過:100 批都沒截時,WAL 就是整個匯入的大小),要等那條讀取結束才截掉。2026-11-13 以前這一等沒有上限:一條跑了幾小時的語句,加上任何一次寫入,讓 Archive 與 Intelligence 的所有讀取跟著等了幾小時(`vault-store/tests/admission.rs`)。
- 不依賴自動 checkpoint 的設定,0.8.1 會忽略它。

**Migration:**
Expand All @@ -89,6 +91,8 @@

**引擎日誌:** Turso 在 B-tree 和 VDBE 的每一步都開 `tracing` span。行程裡沒有 `tracing` dispatcher 時,`tracing` 會把每個 span 轉給 `log`,app 的 logger 開在 Debug,於是每一步都要問一次 logger 的逐模組過濾。2026-10-07 量到,同一份兩萬多筆訪問的資料,Insights 重建在測試裡 7 秒,在 app 裡超過一分鐘。所以 app 啟動時用 `vault_store::route_engine_diagnostics_to_log()` 設一個只要 warning 和 error 的 dispatcher:span 在呼叫點就被關掉,引擎的警告照樣進 app 的日誌。

**跑得久的語句:** 一條語句跑超過 30 秒,`vault-store` 在 log 裡點名它:哪個資料庫的哪條連線、跑了多久、語句的開頭(引號裡的字換成 `?`、註解拿掉;綁定的值本來就不在語句裡)。之後每翻一倍(60 秒、120 秒…)再記一次,結束時也記一行。它停不了那條語句(SDK 沒有中斷介面),只讓它不再無聲:讀取不再互相等之後,一條跑不完的語句唯一的跡象是佔住一條連線與一個核心、WAL 一直長大。2026-11-13 那條跑了幾小時,log 裡一個字也沒有。做法:每條連線記下自己正在跑的語句,`store-watch` 執行緒每 5 秒看一次(`vault-store/src/running.rs`)。工作跑語句的每一條路都經過 `Session::watch`:`Session` 的 `execute`、`query`、`for_each`,先 `prepare` 再 `execute` 或逐列讀的,還有 `integrity_check`。逐列讀的語句讀到最後一列就算結束,不等游標被丟掉。`vault-store` 自己跑的不看:BEGIN 與 COMMIT、checkpoint、migration 的版本紀錄、查詢計劃、複製資料庫。成本是每條語句多一次鎖和複製語句開頭的 300 bytes:最便宜的那種語句(prepare 好的單列 INSERT,約 620 ns)多約 20 ns,在各次量測的起伏之內。

**錯誤:** 引擎的錯誤轉成 PathKeep 的分類:鎖定中、忙碌、損毀、金鑰錯誤、取消。前端遇到忙碌會自己恢復。

## 5. 寫入模型
Expand Down
36 changes: 35 additions & 1 deletion docs/plan/BACKLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@

> 2026-10-25:清掉已完成或前提已不存在的 block(v0.3 Paper 前端、已移除的 coverage / mutation gate、已修好的 flaky 測試等,清單見 CHANGELOG 同日)。2026-04 到 2026-06 的收尾註記也一併移除,要看去 git 歷史或 CHANGELOG。

- [ ] **WORK-SLOW-READ-ALARM** — 跑得久的語句現在會進 log,但 E2E 不會因此失敗、畫面上看不到、也停不了它(2026-11-13 `WORK-LONG-READ-OUTAGE` 做了 log 的部分)
- 讀先:`docs/architecture/turso-migration-design.md` §4(跑得久的語句)、`vault-store/src/running.rs`、`scripts/run-playwright.mjs`、`docs/design/ux-principles.md`。
- 現況:一條語句跑超過 30 秒,`rust.log` 會點名它,之後每翻倍再記一次。要有人去看 log 才知道。
- 要做:
- E2E 跑完檢查 app 沒有記這種行。測資只有兩萬多筆造訪,任何一句跑到 30 秒都是計劃選錯:這次的 COUNT 在測資大小上要 40 秒左右,E2E 的 120 秒等待沒擋下來。Rust 的警告會轉到瀏覽器的 console 再印到 runner 的輸出,但 `run-playwright.mjs` 現在是 `stdio: 'inherit'`,沒有在看;做的時候要先證明這條路真的把 `vault_store` 的行帶出來。
- 畫面:後台任務頁說出「有一個讀取已經跑了多久」,要先過 `ux-principles`。
- 停下它:`turso_core` 0.8.3 有 `Connection::interrupt`,SDK(`turso`)沒有露出來。查清楚能不能用,能的話暫停與取消就可以停掉跑到一半的語句,而不是等它自己結束。

- [ ] **WORK-INDEX-UPDATE-WALKS-ARCHIVE** — 每次備份後的索引更新都把整個存檔走一遍,只有幾十筆新造訪也一樣(2026-11-13 `WORK-LONG-READ-OUTAGE` 驗收時量到)
- 讀先:`docs/architecture/semantic-index-control.md`、`vault-core/src/ai/indexing/backfill.rs`(`run_embedding_backfill` 從游標 0 開始)、`candidates.rs`、`store_rows.rs` 的 `cleanup_stale_embeddings`。
- 現象:增量建置每次從第一筆造訪讀到最後一筆(每 64 筆一塊:讀造訪、比內容雜湊、讀 enrichment),再把整個 lineage 的列走一遍找已撤銷的造訪,開跑前還要數一次剩多少。幾十萬筆造訪的存檔上,一次更新光是走完就要幾十秒到一兩分鐘,每小時一次;14.4M 是它的三十倍。都是分頁的短讀取,不會擋別人,只是白做。
- 方向(先設計、先量):備份知道自己寫了哪些造訪、改了哪些頁面;讓更新只讀那些,整個走一遍留給「重新檢查」這種明確的操作。要處理:enrichment 之後才抓到內容的頁面、撤銷與還原、進度的分母。

- [ ] **WORK-UNBOUNDED-READS** — 還有一批「一句讀完整張表」的語句(2026-11-13 `WORK-LONG-READ-OUTAGE` 掃程式碼列出,多數沒跑 `EXPLAIN`)
- 讀先:`docs/architecture/turso-migration-design.md` §4、`vault-core/tests/intelligence_read_plans.rs`(計劃測試怎麼寫)、下面的 `WORK-TURSO-MINMAX-WRAPPER-AUDIT` 與 `WORK-SEMANTIC-VISIT-SIDECAR`(已經各自列管的不重複)。
- 現象:這些各佔一條讀取連線(每個資料庫只有兩條)直到跑完,中途不能取消;`WORK-LONG-READ-OUTAGE` 之後不再讓別的讀取等。照「多常跑」排:
- Insights 建置開頭(每次有變動的備份之後):`intelligence/build_pipeline.rs:22-34` 一個讀取裡每個 profile 一句 `COUNT(*) FROM search_terms`。
- 後台任務頁開著時每 2 秒:`intelligence_runtime/snapshot.rs:94-100`(`page_content_enrichments` 上沒有索引的 `COUNT`)、`enrichment/content_fetch_api.rs:172-178`。
- 清單與卡片:`intelligence/search_queries.rs:70-85`(視窗函數掃範圍內所有關鍵字搜尋)、`ai/similar_searches.rs:216-238`、`archive/history/og_images.rs:84-90`(幾乎整張 `urls` 的 `COUNT(DISTINCT)`)、`ai/working_set.rs:190-194`。
- Doctor 與修復:`archive/doctor.rs:76-115` 一個讀取跑二十幾句整表語句(其中 `DAMAGED_PAGES` 每頁一個沒有提示的相關子查詢,計劃沒驗過)、`doctor_derived.rs:59-110`、`intelligence_schema.rs:52-108`;`doctor.rs:176-184` 與 `:282-287` 是整表的寫入工作,會佔住唯一的 writer。
- 沒有前端呼叫、計劃沒驗過的 IPC 命令:`intelligence_navigation.rs:84-95`、`phase_four.rs:284-303`、`intelligence_explain.rs:138-146`。
- 方向:每一條先 `EXPLAIN QUERY PLAN`(在 14.4M 的種子資料上量),再決定分頁、加索引提示或加索引;常跑的先做。凡是拿 join 的鍵去查、表上又有「以常數欄開頭」的索引的語句都要看:Turso 沒有統計資料時會挑常數那個索引(這次的 COUNT 就是)。

- [ ] **WORK-STALE-WHEN-BACKEND-SILENT** — 後端不回的時候,畫面照樣顯示舊資料,按鈕沒有反應也沒有說明;IPC 與排程都沒有逾時(2026-11-13 `WORK-LONG-READ-OUTAGE` 查的時候確認)
- 讀先:`docs/design/ux-principles.md`、`docs/architecture/owner-process.md`(§請求不設期限的理由)、`src/features/settings/background/`、`src/lib/ipc/bridge.ts`、`vault-owner-ipc/src/client.rs`。
- 現象:那天所有讀取都不回。後台任務頁停在點「繼續」之前的那一列(已暫停、舊的錯誤訊息,還給 ▶ 與 ✕),標題卻是「1 項運行中」:列與標題是兩個查詢,一個先回了;之後的輪詢都掛在同一個沒回的請求上,沒有轉圈也沒有錯誤。暫停、取消、開關按下去就一直停在 pending。排程備份的 worker 送出請求後無限等,launchd 的工作也沒有 `TimeOut`,於是之後每小時的備份都不會啟動。
- 方向:先分清楚「慢」和「不回」。畫面:資料超過一段時間沒更新要看得出來(不是把舊資料當新的);一個動作送出後沒回,要說明並讓人能重試。排程:worker 等 owner 要有上限,逾時記成一次失敗的嘗試再退出,下一個小時才會再跑。設計要先過 `ux-principles`;`owner-process.md` 寫了請求不設期限的理由,要改先寫 trade-off。

- [ ] **WORK-EMPTY-WRITE-CHECKPOINT** — 什麼都沒改的寫入工作也讓 writer 做一次 checkpoint(2026-11-13 `WORK-LONG-READ-OUTAGE` 看到;搜尋索引器那個每 100 ms 一次的已經拿掉)
- 讀先:`vault-store/src/worker.rs`(writer 迴圈的 `dirty`)、`checkpoint.rs`。
- 現象:writer 做完任何一個工作就當自己有東西要截,於是擋住新讀取(最多 50 ms)再做 TRUNCATE。改了 0 列的工作也一樣:`og_images.rs:255-257`、`enrichment/search_text.rs:15`、`stars`/`annotations` 沒變動的儲存。
- 方向:WAL 裡沒有 frame 就不用截。先確認 Turso 的 `wal_checkpoint(PASSIVE)` 在空的 WAL 上回報的 frame 數可以拿來判斷,再改。

- [ ] **WORK-BACKUP-INTEGRITY-SCALE** — 原生備份的新身分與讀法在 14.4M 和目標機上的量測(2026-11-01 PR #18 只量了 100 萬筆、M5 Max)
- 要量:14.4M 的 v1 存檔更新(migration 2–21,含開啟者 backfill 與重建列號索引)的時間與峰值記憶體;更新後第一次備份(整個重讀、給每筆時間);之後每小時與每週的備份。目標機 4 核 / 8 GB,冷快取。
- 另外量一份 Sync 從新到舊寫的大檔(開啟者大多在子造訪之後):讀完才找到的開啟者分批寫,量它的時間。
Expand Down Expand Up @@ -107,10 +140,11 @@
- 根因:Turso 0.8.1 `translate/optimizer/mod.rs` 的 `detect_simple_aggregate` 只在結果欄「正好是」聚合式時走 MIN/MAX 捷徑(沿索引或 rowid 直接走到極值);外面包 `COALESCE`、`+1` 都不算,就退回掃描。Intelligence v2 遷移因此每批掃一次越長越大的新表,1M / 4M 量到 26.5 s / 129 s;改成不包、在 Rust 裡處理 NULL 之後 16.4 s / 66.9 s(`semantic-index-control.md`)。
- 要查(14.4M 時每次都掃一遍的優先):`intelligence/build_pipeline.rs:24`(每次 intelligence 建置掃 `visits`)、`ai/indexing/backfill.rs:413` 與 `:539`、`ai/generations/build.rs:145`、`ai/similar_searches.rs:198`、`ai/indexing/store_rows.rs:91`、`archive/doctor_derived.rs:76`,以及 `intelligence_runtime/enqueue.rs`、`job_control.rs` 的 `MAX(list_order)+1`(小表,可能不用改)。
- 做法:先用 `EXPLAIN` 確認每一條真的掃表,再改成不包的 `MAX(...)` 並在 Rust 裡處理 NULL;有 WHERE 的要確認存取路徑能直接走到極值。結果記進 `turso-review-ledger.md`。
- 2026-11-13(`WORK-LONG-READ-OUTAGE`):增量建置開頭的三條在真實存檔的本機副本上確認是掃描(`SCAN ai_embeddings`、`SCAN visits`、`SCAN visits USING INDEX idx_visits_visible_time_id`),幾十萬筆造訪時合計約 2.5 秒,其中 `store_rows.rs` 那條掃整張 `ai_embeddings` 就佔 2 秒。`backfill.rs` 的 `max_candidate_history_id` 的註解說它「cheap」,不對。

- [ ] **WORK-SEMANTIC-VISIT-SIDECAR** — 語意搜尋每次都讀一遍範圍內的所有造訪:100 萬次 385 ms,14.4M 估 5.5 秒(M5 Max),目標機更慢(2026-10-17 做完 `WORK-SEMANTIC-PIPELINE` 量到)
- 現況:一次語意搜尋 100 萬次造訪 500 ms,其中 385 ms 是一條 SQL 串流範圍內每次造訪(算頁面成員、最新時間與每頁最高分)。向量掃描只要 6 ms。
- 串流是一次讀取,整段佔住 archive 兩條讀取連線中的一條;writer 做 WAL truncate checkpoint 前要等進行中的讀取結束(`vault-store/src/checkpoint.rs` 的 `wait_for_readers`,`worker.rs` 呼叫),這段時間進來的寫入也跟著等。14.4M 時一次語意搜尋可能讓匯入、備份的寫入延後數秒。以前的逐批回查是很多次短讀取,不會這樣;日期篩選的全文搜尋(`eligibility::load`)本來就是同一種長讀取。
- 串流是一次讀取,整段佔住 archive 兩條讀取連線中的一條;writer 做 WAL truncate checkpoint 前要等進行中的讀取結束(`vault-store/src/checkpoint.rs` 的 `wait_for_readers`,`worker.rs` 呼叫)。2026-11-13 以前這一等沒有上限,這段時間進來的讀取與寫入都跟著等;現在最多等 50 ms(`WORK-LONG-READ-OUTAGE`),長讀取只佔一條讀取連線、延後 WAL 的截斷。以前的逐批回查是很多次短讀取,不會這樣;日期篩選的全文搜尋(`eligibility::load`)本來就是同一種長讀取。
- 方向(先設計、先量):建索引時把每次造訪的頁面與時間投影到索引旁(跟 `.pkpos` 同一份或並排),加上撤銷造訪的集合;沒有篩選或只有日期篩選時,頁面成員與每頁分數在記憶體裡算完,用到網站、標籤、筆記、星號、profile 等篩選才走 SQL 串流。要處理:索引之後才進來的造訪(成員與最新時間仍要算到它們)、撤銷與還原、App Lock 時放掉記憶體、`semantic_stage_bench` 量 1M / 4M。

- [!] **WORK-VECTOR-MEMORY-AT-SCALE** — 大語料時完整 int8 排序放不進 8 GB(等用戶在研究文件「What this means」的 A / B / C 中選)
Expand Down
Loading
Loading