3 Commits

Author SHA1 Message Date
Leo d028a66625 sync: collector/ 同步自 inkstone/arcrun-rag@45b74a4(桌面小幫手 0.18.36) 2026-08-24 18:26:55 +08:00
Leo f1658fd2ad sync: collector/ 同步自 inkstone/arcrun-rag@a39cea2(桌面小幫手 0.18.34) 2026-08-20 11:26:28 +08:00
Leo 969c3f2bcf revert: 還原被第一次同步蓋掉的三個檔(D95,inkstone/InkStoneCo#40)
2026-08-18 出貨線第一次真的把 arcrun-rag 的 collector/ 同步過來,
盲目鏡射蓋掉/刪掉了三個**本來就該只屬於這個 repo** 的檔:

  .gitignore           21 行的「不要再長回來」閘 → 被換成 arcrun-rag 那份的一行
                       🔴 最嚴重:那份清單正是擋 43 MB 建置產物的東西,
                          它被蓋掉之後,下一趟同步就會把產物搬進來
  README.md            介紹這個獨立 repo 的 → 被換成 arcrun-rag 子目錄的 README
  cmd/collector/main.go 正常源碼,被誤判成建置產物刪掉
                       (.gitignore 的 `collector` 一行是指根目錄那顆執行檔,
                         但無斜線樣式在任何層級都命中 ⇒ 連 cmd/collector/ 也中)

出貨線那側已修(installer/scripts/line-source-repo.mjs):
  · destOwned:目的 repo 的門面檔不覆蓋、不刪除,且同步前檢查它們還在不在
  · 已被目的 repo 追蹤的檔案不算「被忽略」(git 自己的語意)⇒ main.go 不再被誤刪

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-18 21:33:34 +08:00
27 changed files with 3905 additions and 41 deletions
+21
View File
@@ -1 +1,22 @@
node_modules/
# ── 編譯產物不進版控(D95,2026-08-18)────────────────────────────────────
# 這個 repo 從 arcrun-rag 的 collector/ 抽出來時,歷史裡有 46 個 >1MB 的編譯產物
# arcrun-tray 的 .app、各版 dmg、下載回來的 WebView2 安裝檔…),總計 626 MB。
# 抽取時已把它們從歷史裡剔除 ⇒ 這個 repo 全部歷史只剩 8 MB。
#
# 🔴 這一份清單是「不要再長回來」的那道閘:出貨線每次把 arcrun-rag 的 collector/
# 同步過來時,`git add -A` 會照這份跳過它們(見 installer/scripts/line-source-repo.mjs)。
# ⚠️ arcrun-rag 那邊**現在還追蹤著這四個檔**——不在這一輪拿掉,是因為
# `daemon-version.py --source-state` 的指紋把它們算在內,改動會讓 daemon-freshness
# 那道閘擋下出貨。拆它要跟戳版帳本一起處理,另立一輪(票:inkstone/InkStoneCo#40)。
collector
cmd/arcrun-app/arcrun-app
cmd/arcrun-app/dist/
cmd/arcrun-app/dist-msix/
cmd/arcrun-app/build/bin/
cmd/arcrun-app/build/windows/installer/tmp/
cmd/arcrun-app/frontend/dist/
cmd/arcrun-app/frontend/node_modules/
cmd/arcrun-app/frontend/wailsjs/
cmd/**/*.app
+23
View File
@@ -47,6 +47,29 @@
---
## 下一版(未發佈)
- 🔴 **移除資料夾時,可以順便把我們放進去的隱藏檔案一起清掉**:小幫手整理你的資料夾時,會在**每一層**放一個隱藏的整理稿目錄(`.wiki``.arcrun-rag`)。以前你把資料夾從清單移除之後,這些東西**全部留在原地**——隱藏的、散在幾十層裡,你自己幾乎刪不乾淨。現在移除的對話框多了一個勾選框「順便把 Arcrun RAG 放在這個資料夾裡的檔案清掉」,勾起來會**先把確切要刪哪些列給你看**,你看過再按確定。
- **只刪我們自己建的,你的檔案一個都不會碰**:每一筆都要有我們自己寫下的依據(我們放的說明檔、我們的整理帳本、我們的檔名前綴)才會刪;只要有一個東西認不出來,那整個目錄就原封不動留著,並且在清單上告訴你留了什麼、為什麼留。
- **這幾種情況我們刻意不刪**:你那個資料夾裡如果還有別的資料夾也在同步清單裡,它底下的東西不碰;以前從你資料夾搬進暫存區的檔案不碰(那些是你的);捷徑(symlink)不碰。
- 不勾這個框的話,行為跟以前一模一樣:硬碟上一個檔都不會少。
## 0.18.352026-08-24
- 🔴 **桌面小幫手多了「App 界面」:一打開就是九宮格,點一下就用得到**:以前小幫手只做一件事——把資料夾整理進知識庫;要用知識庫上的 App(例如寫一則筆記),得另外開瀏覽器登入雲端。現在小幫手最上面多了一頁「App 界面」,裡面是**你連的那個知識庫實際裝了的 App**,一個 icon 一個 App,點下去就在小幫手裡打開,不用再開瀏覽器。
- 這批 App **跟你在知識庫網頁上看到的是同一批**:清單直接跟知識庫要,桌面上不另存一份,所以你在任何一台電腦看到的都一樣;在雲端裝了新 App,回到這裡按「重新整理」就會出現。
- 沒有自己畫面的 App 也點得開:會列出它能做的事,一條一顆「現在執行」。
- 連了不只一個知識庫的話,「App 界面」上方可以切換要看哪一個。
- 還沒連上知識庫、或這個知識庫一個 App 都沒裝,畫面會直接說是哪一種情況,不會只給你一片空白。
- **原本的功能一項都沒少**:同步狀態與「立刻同步」搬到了每一頁都看得到的最上方,首頁、各知識庫、AI 設定、版本與更新都在側邊欄原來的位置。
## 0.18.342026-08-20
- 🔴 **雲端現在看得到你資料夾的完整層次,不再只有第一層**:以前不管你的資料夾疊了幾層,雲端只認得最外面那一層——`專案/教育部標案/會議紀錄` 這種放在深處的資料夾,在知識庫裡完全不存在,只會以檔案路徑的一段文字出現。現在**每一層資料夾都有自己的一格**,而且**每一格都連著它的上一層**,所以你可以像在檔案總管裡一樣,從最外層一路點進最深的那個資料夾。每一格還會告訴你這一層直接放了幾份文件、連同底下的子資料夾一共幾份。
- 這些索引**不經 AI 萃取**(本機掃一遍就有答案),所以不吃你的每日額度,額度用完的時候照樣會更新。
- 空的資料夾目前還不會出現(要等掃描開始記錄資料夾本身);已經同步過的內容不受影響。
- 🔑 **同一份檔同步到兩台電腦,問答的出處不再只看得到一台**:上一版已經讓每份原稿記住它來自哪一台機器,但 AI 回答時還是會把同名的檔案當成同一份而只列出其中一台。現在內容還是只留一份(不會重複),但出處那一格會把它存在的每一台都列出來。
## 0.18.332026-08-18
- 🔴 **現在看得出每份原稿在哪一台電腦上**:以前知識庫只記得檔案的相對路徑,所以你在兩台電腦、或電腦加 NAS 上放了同名的檔,系統分不出誰是誰——問答的出處只會顯示其中一台,另一台就這樣消失了。現在每份原稿都會記住它來自哪一台機器,預設用「你的帳號@電腦名稱」,你可以改成自己看得懂的稱呼(例如「教育部 Leo 的 Mac」)。既有的資料不受影響,那一格會誠實地空著,不會憑空編一個機器名給你。
+24 -9
View File
@@ -1,15 +1,30 @@
# collector
# arcrun-collector — 桌面小幫手(Arcrun RAG Desktop
> 📦 **桌面小幫手(`0.18.x`)的版本發佈住在 [`inkstone/arcrun-collector`](https://git.uncle6.me/inkstone/arcrun-collector/releases),不在本 repo 的版本發佈頁**D952026-08-18`inkstone/InkStoneCo#40`)。
> **這個 repo 是「桌面小幫手」的家:原始碼、版本說明、每一版的成品,全部在這裡。**
>
> 本 repo`arcrun-rag`)的版本發佈頁只放**雲端引擎 `1.4.x`**。兩條線是兩個產品,
> 疊在同一條歷史上就答不出「最新版是哪一個」——leo:「**把一個差很多的東西塞進去別人的歷史裡**」
> 出貨線由 `installer/ship.targets.json` 的 `releaseRecord.lineRepos` 決定每條線發到哪,
> **宣告缺一條就不准開跑**`installer/scripts/line-source-repo.mjs`)。
> - **要下載桌面小幫手** → 右邊的「版本發布 / Releases」,最新一版就是最上面那筆。
> 每一筆都掛著當版的三個成品:macOS 的 `.dmg`、Windows 的 `.exe` 與 `.msix`
> - **這一版改了什麼** → 同一筆 release 的內文,原稿是 [`CHANGELOG.md`](CHANGELOG.md)。
> - **版本號長什麼樣** → `0.18.x`[`DAEMON_LINE`](DAEMON_LINE) 宣告的那條線)。
> 雲端引擎是 `1.4.x`,**是另一個產品、另一個 repo**(見下)。
>
> ⚠️ 源碼此刻**兩邊都有**:這個目錄仍是編輯的地方,出貨時同步到 `arcrun-collector`。
> 把本目錄整個下架是另一輪的事(要動 20 幾支讀 `collector/` 的腳本)。
> 舊的 `0.18.30``0.18.33` 兩筆還留在本 repo 的版本發佈頁,等新位置站穩後再由總管決定何時拆。
> ## 跟 arcrun-rag 的關係
>
> [`inkstone/arcrun-rag`](https://git.uncle6.me/inkstone/arcrun-rag) 是**雲端引擎**`1.4.x`)——
> 跑在使用者自己 Cloudflare 帳號上的那些 worker。桌面小幫手會連上它,但兩者是兩個產品、
> 兩條版本線、兩份版本說明。
>
> 🔴 **為什麼分開**D95leo 2026-08-18):在此之前兩個產品的版本發布疊在 arcrun-rag
> 同一條歷史上(`0.18.33`、`0.18.30` 跟 `1.4.49`、`1.4.48` 並排),而且出貨線每跑一次就多疊一筆。
> leo 原話:「**我強調了不要扭曲,這就是扭曲,把一個差很多的東西塞進去別人的歷史裡。**」
> 兩個產品共用一條版本歷史,「最新版是哪一個」就沒有答案。
>
> 📌 過渡狀態(誠實記著):原始碼目前仍同時存在於 `arcrun-rag` 的 `collector/`
> 由出貨線每次出貨同步過來。**把 `collector/` 從 arcrun-rag 拆掉**要動 20 幾支讀那個路徑的
> 腳本,另立一輪處理(票:`inkstone/InkStoneCo#40`)。
> 在那之前,**發佈的落點以本 repo 為準**。
---
> ✂️ **legacy Node 版(Gitea push 鏈)已於 2026-07-19 刪除**SDD `ingest-hash-trigger` task 4
> 「Gitea webhook 接收端程式碼與路由已刪除(git 記錄可查)」)。`index.js`/`transform.js`/
+543
View File
@@ -0,0 +1,543 @@
// cleanup.go — 斷連時把 daemon 自己寫進使用者資料夾的東西收回來
// arcrun-rag#138leo 2026-08-24:「碎型會在每個資料夾安裝隱藏資料夾,人工刪除不容易,
// 所以當它斷連,應該要可以幫它把 Arcrun RAG 建立的資料夾刪掉」)。
//
// 🔴 這是本 repo 唯一一支會**刪使用者資料夾裡的檔**的程式碼。tidy.go 的紅線是
// 「一行 os.Remove 都沒有」——那條紅線在這裡不成立(使用者要的就是刪掉),
// 所以改用另一組更嚴的規矩:
//
// ① **只刪帳面上認得出來的東西。** 不靠「路徑看起來像我們的」「副檔名是 .md」
// 這類推測——每一筆都要說得出一句**我們自己寫下的證據**(見 CleanupItem.Evidence)。
// ② **一個認不出來的鄰居,就讓整個目錄留下。** `.wiki/` 裡只要有一個檔不在帳本上、
// 也沒有標記,就從「刪整個目錄」降級成「只刪認得出來的那幾個檔」,目錄本身留著。
// ⇒ 誤留一點殘渣是可接受的;誤刪使用者的知識不是。
// ③ **先看清單再動手。** PlanCleanup 只讀不寫,ApplyCleanup 動手前**重算一次**計畫
// (不吃呼叫端傳回來的舊清單)——畫面上看到的是快照,刪的當下才是真相。
//
// 認人的三種證據(全部是 daemon 自己寫下去的字,不是猜的):
//
// E1 `.arcrun-rag/` 工作區:目錄裡的 `.gitignore` 內容**逐字**等於 workspaceIgnoreBody
// repoguard.go 寫的那份,自己寫著「整個資料夾可以安全刪除」)。
// E2 `.wiki/` 卡片目錄:目錄裡的 `.gitignore` 內容逐字等於 wikiIgnoreBody
// wikishape.go 寫的,自己寫著「不是你的檔案」),或目錄裡有解析得開的
// `manifest.json`(我們的 wiki 帳本)。
// E3 `system-dev/wiki/cards/` 底下帶 MachineMark`arcrun-`)前綴的檔
// ——machinemark.go 的規約:daemon 在監看根底下產生的每個檔名都帶這個前綴。
//
// 🔴 三個**刻意不刪**的東西(每一個都是實查出來的,不是保守而已):
//
// ① `.arcrun-rag/legacy-template/`:那是 tidy.go 的**收容處**,裡面裝的是從使用者
// 資料夾**搬進來的檔**。tidy 自己說得很清楚:「TemplateOwns 是路徑身分判準,
// 分不出『daemon 鋪的』還是『repo 本來就有的』」⇒ 那些檔的所有權我們判不了。
// 而 `.arcrun-rag/.gitignore` 上「整個資料夾可以安全刪除」那句話是 #105 寫的,
// 當時工作區裡還沒有收容處。**不把那句話套到收容處上**(#138 紅線第三條問的正是這題)。
// ② 另一個**還在監看清單裡**的資料夾底下的東西:實務上真的會巢狀
// 2026-08-24 現場:`pms` 與 `pms/pms_v1_legacy` 同時在看守)。移除外層時把內層的
// 工作區刪掉=把一個還在跑的同步弄壞。
// ③ 任何 symlink:不跟隨、不刪。刪一條指出去的連結會刪到監看根以外的東西。
package collector
import (
"encoding/json"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
)
// 產物分類(給畫面分組用,也是 Apply 的處理方式)。
const (
CleanupKindWorkspace = "workspace" // `.arcrun-rag/` 工作區(整個目錄)
CleanupKindWorkFile = "work-file" // 工作區裡的單一項目(收容處存在時的降級路徑)
CleanupKindWikiDir = "wiki-dir" // `.wiki/` 卡片目錄(整個目錄)
CleanupKindWikiFile = "wiki-file" // `.wiki/` 裡的單一產物(有陌生鄰居時的降級路徑)
CleanupKindMarked = "marked-card" // 帶 arcrun- 標記的卡片(非隱藏的卡片產物區)
)
// CleanupItem=一筆「打算刪掉」的東西。Evidence 是白話的認人依據,會直接顯示給使用者看
// ——沒有 Evidence 的東西不准進這個清單。
type CleanupItem struct {
Rel string `json:"rel"` // 相對監看根,斜線分隔
IsDir bool `json:"is_dir"`
Kind string `json:"kind"`
Evidence string `json:"evidence"`
Files int `json:"files"` // 這一筆底下有幾個檔(目錄才 > 1)
Bytes int64 `json:"bytes"`
}
// CleanupKeep=一筆「認得出來但刻意不刪」或「認不出來所以不敢刪」的東西。
// 這份清單和 Remove 一樣要給使用者看——沉默地留下殘渣,跟沉默地刪掉一樣糟。
type CleanupKeep struct {
Rel string `json:"rel"`
Reason string `json:"reason"`
}
// CleanupPlan=一次清理的完整帳目。PlanCleanup 只讀不寫,這份東西就是「動手前的清單」。
type CleanupPlan struct {
Root string `json:"root"`
Remove []CleanupItem `json:"remove"`
Keep []CleanupKeep `json:"keep"`
Files int `json:"files"` // Remove 涵蓋的檔案總數
Bytes int64 `json:"bytes"`
}
// CleanupFailure=刪不掉的那一筆(權限、檔案正被開著…)。
type CleanupFailure struct {
Rel string `json:"rel"`
Error string `json:"error"`
}
// CleanupResult=實際刪掉了什麼。
type CleanupResult struct {
Removed []string `json:"removed"`
Failed []CleanupFailure `json:"failed"`
Files int `json:"files"`
Bytes int64 `json:"bytes"`
}
var cleanupBakRe = regexp.MustCompile(`^(.*)\.bak-\d+$`)
// WorkspaceIgnoreBodyForTestWikiIgnoreBodyForTest 讓**別的 package 的迴歸網**
// cmd/arcrun-app)造得出「與 daemon 真正寫下去一字不差」的宣告檔。
//
// 為什麼不讓測試自己抄一份字串:抄的那份會漂。認人的判準是「逐字相等」,
// 判準與樣本一旦不同源,這兩份 .gitignore 改一個字就會有一整層測試靜默失效。
// 名字帶 ForTest 是說給讀 code 的人聽的——**production 路徑不准依賴它們**。
func WorkspaceIgnoreBodyForTest() string { return workspaceIgnoreBody }
func WikiIgnoreBodyForTest() string { return wikiIgnoreBody }
// PlanCleanup 列出「把 root 從監看清單移除時,可以連帶收掉的 daemon 產物」。
//
// **這支函式不寫任何檔案。** otherRoots=其他仍在監看清單裡的資料夾(絕對路徑);
// 落在它們底下的東西一律不碰(見檔頭不刪清單②)。
func PlanCleanup(root string, otherRoots []string) (*CleanupPlan, error) {
absRoot, err := filepath.Abs(root)
if err != nil {
return nil, err
}
absRoot = filepath.Clean(absRoot)
if st, serr := os.Lstat(absRoot); serr != nil {
return nil, serr
} else if !st.IsDir() {
return nil, &fs.PathError{Op: "cleanup", Path: absRoot, Err: fs.ErrInvalid}
}
// 只留「真的在 absRoot 底下」的其他根——不在底下的本來就走不到,留著只會拖慢比對。
var others []string
for _, o := range otherRoots {
a, oerr := filepath.Abs(o)
if oerr != nil {
continue
}
a = filepath.Clean(a)
if a != absRoot && pathWithin(absRoot, a) {
others = append(others, a)
}
}
p := &cleanupPlanner{
root: absRoot,
others: others,
cards: collectWikiCardIndex(absRoot, others),
plan: &CleanupPlan{Root: absRoot},
}
p.walk(absRoot)
sort.Slice(p.plan.Remove, func(i, j int) bool { return p.plan.Remove[i].Rel < p.plan.Remove[j].Rel })
sort.Slice(p.plan.Keep, func(i, j int) bool { return p.plan.Keep[i].Rel < p.plan.Keep[j].Rel })
return p.plan, nil
}
// ApplyCleanup 真的動手刪。
//
// 🔴 它**重算一次計畫**,不接受呼叫端傳進來的舊清單——使用者看清單、按確定之間可能過了
// 幾秒或幾分鐘,那段時間裡他可能剛好把一個自己的檔存進 `.wiki/`。畫面上那份是快照,
// 刪的當下重算的這份才是判準。回傳的 plan 就是實際執行的那一份(呼叫端要顯示就用它)。
func ApplyCleanup(root string, otherRoots []string) (*CleanupPlan, *CleanupResult, error) {
plan, err := PlanCleanup(root, otherRoots)
if err != nil {
return nil, nil, err
}
res := &CleanupResult{}
// 由深到淺刪:先刪目錄裡的東西,再刪目錄自己(降級路徑會同時出現父目錄底下的多筆)。
items := append([]CleanupItem(nil), plan.Remove...)
sort.Slice(items, func(i, j int) bool { return len(items[i].Rel) > len(items[j].Rel) })
for _, it := range items {
abs := filepath.Join(plan.Root, filepath.FromSlash(it.Rel))
// 最後一道機械閘:不管計畫怎麼算的,目標一定要在監看根底下,而且不是 symlink。
if !pathWithin(plan.Root, abs) {
res.Failed = append(res.Failed, CleanupFailure{Rel: it.Rel, Error: "路徑不在這個資料夾底下,沒有刪"})
continue
}
st, serr := os.Lstat(abs)
if serr != nil {
if os.IsNotExist(serr) {
continue // 已經不在了(重複按、或上一輪刪過)=不是失敗
}
res.Failed = append(res.Failed, CleanupFailure{Rel: it.Rel, Error: serr.Error()})
continue
}
if st.Mode()&os.ModeSymlink != 0 {
res.Failed = append(res.Failed, CleanupFailure{Rel: it.Rel, Error: "這是一條捷徑(symlink),沒有刪"})
continue
}
var derr error
if it.IsDir {
derr = os.RemoveAll(abs)
} else {
derr = os.Remove(abs)
}
if derr != nil {
res.Failed = append(res.Failed, CleanupFailure{Rel: it.Rel, Error: derr.Error()})
continue
}
res.Removed = append(res.Removed, it.Rel)
res.Files += it.Files
res.Bytes += it.Bytes
}
sort.Strings(res.Removed)
return plan, res, nil
}
// ── 以下是實作細節 ────────────────────────────────────────────────
type cleanupPlanner struct {
root string
others []string
cards map[string]bool // 帳本上每張卡的絕對路徑
plan *CleanupPlan
}
func (p *cleanupPlanner) rel(abs string) string {
r, err := filepath.Rel(p.root, abs)
if err != nil {
return filepath.ToSlash(abs)
}
return filepath.ToSlash(r)
}
func (p *cleanupPlanner) remove(abs, kind, evidence string, isDir bool) {
files, bytes := countTree(abs)
p.plan.Remove = append(p.plan.Remove, CleanupItem{
Rel: p.rel(abs), IsDir: isDir, Kind: kind, Evidence: evidence, Files: files, Bytes: bytes,
})
p.plan.Files += files
p.plan.Bytes += bytes
}
func (p *cleanupPlanner) keep(abs, reason string) {
p.plan.Keep = append(p.plan.Keep, CleanupKeep{Rel: p.rel(abs), Reason: reason})
}
// walk 走整棵樹。刻意**不用 filepath.WalkDir**:我們要在「進不進去某個目錄」這件事上
// 完全自己說了算(其他隱藏目錄整棵跳過、別的監看根整棵跳過、symlink 不跟隨)。
func (p *cleanupPlanner) walk(dir string) {
entries, err := os.ReadDir(dir)
if err != nil {
return
}
for _, e := range entries {
abs := filepath.Join(dir, e.Name())
if e.Type()&os.ModeSymlink != 0 {
continue // 捷徑一律不碰(可能指到監看根以外)
}
if !e.IsDir() {
continue // 目錄以外的檔,只有在下面那幾個已認定的目錄裡才會被處理
}
if p.underOtherRoot(abs) {
p.keep(abs, "這個資料夾也還在同步清單裡,它底下的東西留給它自己管")
continue
}
switch e.Name() {
case workspaceRelDir:
p.planWorkspace(abs)
continue
case wikiRelDir:
p.planWikiDir(abs)
continue
}
if strings.HasPrefix(e.Name(), ".") {
continue // 其他隱藏目錄整棵跳過——daemon 的 Scan 也不進去,那裡不會有我們的東西
}
if p.rel(abs) == cardsRelDir {
p.planMarkedCards(abs)
continue
}
p.walk(abs)
}
}
func (p *cleanupPlanner) underOtherRoot(abs string) bool {
for _, o := range p.others {
if abs == o || pathWithin(o, abs) {
return true
}
}
return false
}
// planWorkspace 處理 `.arcrun-rag/`。
func (p *cleanupPlanner) planWorkspace(abs string) {
if !fileHasExactBody(filepath.Join(abs, ".gitignore"), workspaceIgnoreBody) {
p.keep(abs, "這個 .arcrun-rag 目錄裡沒有我們寫的 .gitignore 宣告,認不出是不是我們建的,所以不動它")
return
}
const ev = "目錄裡的 .gitignore 逐字等於我們寫的那份(自己寫著「整個資料夾可以安全刪除」)"
quarantine := filepath.Join(abs, "legacy-template")
if st, err := os.Lstat(quarantine); err == nil && st.IsDir() && !isEmptyDir(quarantine) {
// 收容處裡是「從你的資料夾搬進來的檔」⇒ 整個目錄不能一次刪,改成逐項刪、留下收容處。
p.keep(quarantine, "這裡面是以前從你的資料夾搬進來的檔案(不是我們產生的),所有權判不了,一律留著讓你自己處理")
entries, err := os.ReadDir(abs)
if err != nil {
return
}
for _, e := range entries {
if e.Name() == "legacy-template" {
continue
}
child := filepath.Join(abs, e.Name())
if e.Type()&os.ModeSymlink != 0 {
p.keep(child, "這是一條捷徑(symlink),不碰")
continue
}
p.remove(child, CleanupKindWorkFile, ev, e.IsDir())
}
return
}
p.remove(abs, CleanupKindWorkspace, ev, true)
}
// planWikiDir 處理一個 `.wiki/`。
//
// 兩步:① 這個目錄是不是我們的(要有我們寫下的宣告)② 裡面**每一個**東西是不是都認得。
// 第二步是這支函式的重點——認得全部才刪整個目錄,有一個認不得就降級成逐檔刪。
func (p *cleanupPlanner) planWikiDir(abs string) {
evidence := ""
if fileHasExactBody(filepath.Join(abs, ".gitignore"), wikiIgnoreBody) {
evidence = "目錄裡的 .gitignore 逐字等於我們寫的那份(自己寫著「這個資料夾是 Arcrun RAG 產生的 wiki,不是你的檔案」)"
} else if isOurWikiManifest(filepath.Join(abs, "manifest.json")) {
evidence = "目錄裡有我們寫的 manifest.json(wiki 帳本,記著每張卡對應哪份原稿)"
} else {
p.keep(abs, "這個 .wiki 目錄裡既沒有我們寫的 .gitignore、也沒有我們的 manifest.json,認不出是不是我們建的,所以不動它")
return
}
entries, err := os.ReadDir(abs)
if err != nil {
p.keep(abs, "讀不到這個目錄的內容("+err.Error()+"),所以不動它")
return
}
type known struct {
name string
isDir bool
}
var ours []known
var strangers []string
accounted := map[string]bool{}
for _, e := range entries {
if p.wikiEntryIsOurs(abs, e) {
accounted[e.Name()] = true
}
}
for _, e := range entries {
if accounted[e.Name()] {
ours = append(ours, known{e.Name(), e.IsDir()})
continue
}
// `.bak-<unixnano>` 是 safeWriteCard 的備份:本體認得,它就認得。
if m := cleanupBakRe.FindStringSubmatch(e.Name()); m != nil && accounted[m[1]] {
ours = append(ours, known{e.Name(), e.IsDir()})
continue
}
strangers = append(strangers, e.Name())
}
if len(strangers) == 0 {
p.remove(abs, CleanupKindWikiDir, evidence, true)
return
}
// 降級:只刪認得出來的那幾個,目錄與陌生鄰居原封不動。
for _, k := range ours {
p.remove(filepath.Join(abs, k.name), CleanupKindWikiFile, evidence, k.isDir)
}
for _, s := range strangers {
p.keep(filepath.Join(abs, s), "這個檔不在我們的帳本上、也沒有 arcrun- 標記——可能是你自己放進來的,留著")
}
}
// wikiEntryIsOurs 回答「`.wiki/` 裡的這一項是不是我們寫的」。四條路,全部要拿得出證據。
func (p *cleanupPlanner) wikiEntryIsOurs(dir string, e os.DirEntry) bool {
if e.Type()&os.ModeSymlink != 0 {
return false // 捷徑一律不算我們的
}
abs := filepath.Join(dir, e.Name())
if e.IsDir() {
return false // 我們從來不在 .wiki/ 底下開子目錄
}
switch {
case e.Name() == ".gitignore":
return fileHasExactBody(abs, wikiIgnoreBody)
case e.Name() == "manifest.json":
return isOurWikiManifest(abs)
case e.Name() == "00-INDEX.md":
return fileHasPrefix(abs, "# 00-INDEX")
case p.cards[abs]:
return true // 帳本上有這張卡
case IsMarked(e.Name()):
return true // machinemark.go 的規約前綴
}
return false
}
// planMarkedCards 處理 `system-dev/wiki/cards/`——那是**看得見**的卡片產物區
// (監看根不在筆記庫也不在版控時的落點)。這裡不刪目錄,只刪帶標記的檔:
// 那個目錄是 system-dev-template 的規約路徑,使用者自己的卡也住在那裡(#105 的教訓)。
func (p *cleanupPlanner) planMarkedCards(abs string) {
_ = filepath.WalkDir(abs, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() || d.Type()&os.ModeSymlink != 0 {
return nil
}
if IsMarked(d.Name()) {
p.remove(path, CleanupKindMarked, "檔名帶 arcrun- 標記——daemon 產生的每個檔都帶這個前綴(machinemark.go 規約)", false)
}
return nil
})
}
// collectWikiCardIndex 把樹裡每一份 `.wiki/manifest.json` 讀出來,攤成「這些絕對路徑是我們的卡」。
//
// 為什麼要掃整棵樹而不是只讀監看根那一份:實務上樹裡會有**別的監看根**留下的帳本
// `pms/pms_v1_legacy/.wiki/manifest.json`)。帳本裡的卡路徑是相對**它自己的根**
// (=那個 `.wiki` 的上層目錄),所以每份帳本各自解析。
func collectWikiCardIndex(absRoot string, others []string) map[string]bool {
out := map[string]bool{}
var walk func(dir string)
walk = func(dir string) {
entries, err := os.ReadDir(dir)
if err != nil {
return
}
for _, e := range entries {
if !e.IsDir() || e.Type()&os.ModeSymlink != 0 {
continue
}
abs := filepath.Join(dir, e.Name())
if e.Name() == wikiRelDir {
addManifestCards(out, abs)
continue
}
if strings.HasPrefix(e.Name(), ".") {
continue
}
walk(abs)
}
}
walk(absRoot)
// 別的監看根底下那份帳本也要讀——它記的卡可能落在**我們這一側**(節點在共同祖先下)。
// 讀帳本是純讀取,不代表會刪那邊的東西(刪不刪由 underOtherRoot 決定)。
for _, o := range others {
addManifestCards(out, filepath.Join(o, wikiRelDir))
}
return out
}
func addManifestCards(out map[string]bool, wikiDir string) {
path := filepath.Join(wikiDir, "manifest.json")
m, ok := readOurWikiManifest(path)
if !ok {
return
}
owner := filepath.Dir(wikiDir) // 帳本裡的路徑相對這一層
for _, d := range m.Docs {
for _, c := range d.Cards {
c = strings.TrimSpace(c)
if c == "" || strings.Contains(c, "..") || filepath.IsAbs(filepath.FromSlash(c)) {
continue // 帳本被改壞時不讓它把我們指到樹外面
}
out[filepath.Join(owner, filepath.FromSlash(c))] = true
}
}
}
func readOurWikiManifest(path string) (*wikiManifest, bool) {
data, err := os.ReadFile(path)
if err != nil {
return nil, false
}
var m wikiManifest
if json.Unmarshal(data, &m) != nil {
return nil, false
}
// 認的是形狀,不是「檔名叫 manifest.json」:要有版本號,而且每筆 doc 都要有我們的鍵。
if m.Version <= 0 {
return nil, false
}
for _, d := range m.Docs {
if d.Node == "" || d.DocID == "" {
return nil, false
}
}
return &m, true
}
func isOurWikiManifest(path string) bool {
_, ok := readOurWikiManifest(path)
return ok
}
// fileHasExactBody:檔案內容**逐字**等於 want。逐字比對是刻意的——「開頭像」不夠,
// 有人可能在我們的宣告後面接自己的規則,那份 .gitignore 就不只是我們的了。
func fileHasExactBody(path, want string) bool {
data, err := os.ReadFile(path)
return err == nil && string(data) == want
}
func fileHasPrefix(path, want string) bool {
f, err := os.Open(path)
if err != nil {
return false
}
defer f.Close()
buf := make([]byte, len(want))
n, _ := f.Read(buf)
return n == len(want) && string(buf) == want
}
// pathWithin 回答 child 是不是 parent 底下(不含 parent 自己)。純字串比對,
// 呼叫端傳進來的都已經是 Abs+Clean 過的路徑。
func pathWithin(parent, child string) bool {
rel, err := filepath.Rel(parent, child)
if err != nil {
return false
}
return rel != "." && !strings.HasPrefix(rel, "..") && !filepath.IsAbs(rel)
}
func isEmptyDir(dir string) bool {
entries, err := os.ReadDir(dir)
return err == nil && len(entries) == 0
}
// countTree 算一筆刪除目標涵蓋幾個檔、多少位元組(給清單顯示「會刪掉 N 個檔」)。
func countTree(abs string) (files int, bytes int64) {
st, err := os.Lstat(abs)
if err != nil {
return 0, 0
}
if !st.IsDir() {
return 1, st.Size()
}
_ = filepath.WalkDir(abs, func(_ string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return nil
}
if info, ierr := d.Info(); ierr == nil {
files++
bytes += info.Size()
}
return nil
})
return files, bytes
}
+95
View File
@@ -0,0 +1,95 @@
// cleanup_cli.go — `collector cleanup` 子命令(arcrun-rag#138)。
//
// 為什麼 App 之外還要有一支 CLI:#138 的驗收條件是「使用者要能在動手前看到將要刪掉
// 哪些東西」。畫面上那份清單與這支印出來的是**同一個 PlanCleanup**——
// 一份判斷邏輯兩個出口,不會出現「畫面說會刪 A、實際刪了 B」。
// 而且出事時(畫面按鈕壞掉、使用者不信任 GUI)他仍然有一條看得見全部細節的路。
package collector
import (
"encoding/json"
"fmt"
"os"
"strings"
)
// multiFlag 讓 --keep 可以重複給(其他還在看守的資料夾)。
type multiFlag []string
func (m *multiFlag) String() string { return strings.Join(*m, ",") }
func (m *multiFlag) Set(v string) error { *m = append(*m, v); return nil }
func runCleanup(args []string) int {
fs2 := newFlagSet()
folder := fs2.String("folder", "", "要斷連的資料夾(必填)")
apply := fs2.Bool("apply", false, "真的刪(不加=只列清單,什麼都不動)")
asJSON := fs2.Bool("json", false, "輸出 JSON(給程式讀)")
var keep multiFlag
fs2.Var(&keep, "keep", "其他還在看守的資料夾(可重複給)——它們底下的東西一律不碰")
if err := fs2.Parse(args); err != nil {
return 2
}
if *folder == "" {
fmt.Fprintln(os.Stderr, "錯誤:--folder 為必填")
return 2
}
var plan *CleanupPlan
var res *CleanupResult
var err error
if *apply {
plan, res, err = ApplyCleanup(*folder, keep)
} else {
plan, err = PlanCleanup(*folder, keep)
}
if err != nil {
fmt.Fprintln(os.Stderr, "collector cleanup:", err)
return 1
}
if *asJSON {
out := struct {
Plan *CleanupPlan `json:"plan"`
Result *CleanupResult `json:"result,omitempty"`
}{plan, res}
data, _ := json.MarshalIndent(out, "", " ")
fmt.Println(string(data))
if res != nil && len(res.Failed) > 0 {
return 1
}
return 0
}
fmt.Println(plan.Root)
if len(plan.Remove) == 0 {
fmt.Println("沒有找到任何 Arcrun RAG 建立的東西——這個資料夾不需要清理。")
} else {
if !*apply {
fmt.Println("※ 這只是清單,什麼都還沒刪。確認沒問題後,同一行指令加上 --apply 才會真的刪。")
}
fmt.Printf("會刪掉 %d 項(共 %d 個檔):\n", len(plan.Remove), plan.Files)
for _, it := range plan.Remove {
mark := "檔"
if it.IsDir {
mark = "目錄"
}
fmt.Printf(" 刪 %s %s%d 個檔)\n 依據:%s\n", mark, it.Rel, it.Files, it.Evidence)
}
}
if len(plan.Keep) > 0 {
fmt.Printf("留著不動 %d 項:\n", len(plan.Keep))
for _, k := range plan.Keep {
fmt.Printf(" 留 %s\n 原因:%s\n", k.Rel, k.Reason)
}
}
if res != nil {
fmt.Printf("實際刪掉 %d 項 / %d 個檔。\n", len(res.Removed), res.Files)
for _, f := range res.Failed {
fmt.Printf(" ✗ 刪不掉 %s%s\n", f.Rel, f.Error)
}
if len(res.Failed) > 0 {
return 1
}
}
return 0
}
+429
View File
@@ -0,0 +1,429 @@
// cleanup_test.go — 斷連清理的迴歸網(arcrun-rag#138)。
//
// 這支測的重點**不是「有沒有刪掉」**,是「**有沒有刪到不該刪的**」。
// 每一個 case 都對應一種「差一點就誤刪使用者知識」的擺法。
package collector
import (
"encoding/json"
"os"
"path/filepath"
"sort"
"strings"
"testing"
)
func cuWriteFile(t *testing.T, path, body string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
// makeWiki 造一個「長得跟 daemon 產的一模一樣」的 .wiki 目錄。
func makeWiki(t *testing.T, dir string, cards ...string) {
t.Helper()
cuWriteFile(t, filepath.Join(dir, ".gitignore"), wikiIgnoreBody)
cuWriteFile(t, filepath.Join(dir, "00-INDEX.md"), "# 00-INDEX\n\n## 文件\n")
for _, c := range cards {
cuWriteFile(t, filepath.Join(dir, c), "# "+strings.TrimSuffix(c, ".md")+"\n")
}
}
// makeRootManifest 寫出監看根那一份 `.wiki/manifest.json`(帳本)。
// nodeCards=節點 →「相對監看根」的卡片路徑,跟 daemon 實際寫的形狀一致
// 2026-08-24 對照 `pms` 現場的真檔驗過:帳本涵蓋每一張卡,只有 00-INDEX.md 不在裡面)。
func makeRootManifest(t *testing.T, root string, nodeCards map[string][]string) {
t.Helper()
m := &wikiManifest{Version: 1}
for node, cards := range nodeCards {
m.Docs = append(m.Docs, wikiDoc{
Node: nodeKeyOf(node), Path: "來源.md", DocID: "doc-" + node + "-1",
Status: "extracted", Cards: cards,
})
}
data, err := json.MarshalIndent(m, "", " ")
if err != nil {
t.Fatal(err)
}
cuWriteFile(t, filepath.Join(root, wikiRelDir, "manifest.json"), string(data)+"\n")
}
func makeWorkspace(t *testing.T, root string) {
t.Helper()
cuWriteFile(t, filepath.Join(root, workspaceRelDir, ".gitignore"), workspaceIgnoreBody)
cuWriteFile(t, filepath.Join(root, workspaceRelDir, "wiki", "cards", "arcrun-資料夾總覽-x.md"), "# x\n")
}
func relsOf(items []CleanupItem) []string {
out := make([]string, 0, len(items))
for _, it := range items {
out = append(out, it.Rel)
}
sort.Strings(out)
return out
}
func keepRels(keeps []CleanupKeep) []string {
out := make([]string, 0, len(keeps))
for _, k := range keeps {
out = append(out, k.Rel)
}
sort.Strings(out)
return out
}
// snapshotTree 記下整棵樹每個檔的相對路徑與內容雜湊——用來證明「使用者的檔一個都沒動」。
func cuSnapshotTree(t *testing.T, root string) map[string]string {
t.Helper()
out := map[string]string{}
err := filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return nil
}
rel, _ := filepath.Rel(root, p)
data, rerr := os.ReadFile(p)
if rerr != nil {
return nil
}
out[filepath.ToSlash(rel)] = string(data)
return nil
})
if err != nil {
t.Fatal(err)
}
return out
}
// ── ① 基本盤:多層巢狀的 .wiki 與工作區全部收掉,使用者的原稿一個不少 ──
func TestCleanupRemovesNestedProductsKeepsUserFiles(t *testing.T) {
root := t.TempDir()
// 使用者的東西
cuWriteFile(t, filepath.Join(root, "README.md"), "我的說明\n")
cuWriteFile(t, filepath.Join(root, "docs", "設計.md"), "我的設計\n")
cuWriteFile(t, filepath.Join(root, "docs", "deep", "更深的.md"), "我的深層檔\n")
// daemon 的東西(三層)
makeWorkspace(t, root)
makeWiki(t, filepath.Join(root, wikiRelDir), "根卡.md")
makeWiki(t, filepath.Join(root, "docs", wikiRelDir), "設計卡.md")
makeWiki(t, filepath.Join(root, "docs", "deep", wikiRelDir), "深卡.md")
makeRootManifest(t, root, map[string][]string{
"": {".wiki/根卡.md"},
"docs": {"docs/.wiki/設計卡.md"},
"docs/deep": {"docs/deep/.wiki/深卡.md"},
})
before := cuSnapshotTree(t, root)
plan, res, err := ApplyCleanup(root, nil)
if err != nil {
t.Fatal(err)
}
if len(res.Failed) != 0 {
t.Fatalf("不該有刪不掉的:%+v", res.Failed)
}
want := []string{".arcrun-rag", ".wiki", "docs/.wiki", "docs/deep/.wiki"}
if got := relsOf(plan.Remove); !equalStrings(got, want) {
t.Fatalf("要刪的清單不對\n got=%v\nwant=%v", got, want)
}
after := cuSnapshotTree(t, root)
// 使用者的檔:逐一比對「內容一字不差」。
for _, rel := range []string{"README.md", "docs/設計.md", "docs/deep/更深的.md"} {
if after[rel] != before[rel] {
t.Fatalf("使用者的檔被動到了:%sbefore=%q after=%q", rel, before[rel], after[rel])
}
}
// 剩下的檔**只能是**使用者那三個。
if len(after) != 3 {
t.Fatalf("清完之後應該只剩使用者的 3 個檔,實際剩 %d:%v", len(after), after)
}
for _, hidden := range []string{".wiki", ".arcrun-rag", "docs/.wiki", "docs/deep/.wiki"} {
if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(hidden))); !os.IsNotExist(err) {
t.Fatalf("%s 沒被刪掉", hidden)
}
}
}
// ── ② 陌生鄰居:.wiki 裡有一個不是我們的檔 ⇒ 整個目錄留下,只刪認得的 ──
func TestCleanupKeepsWikiDirWithStrangerFile(t *testing.T) {
root := t.TempDir()
wiki := filepath.Join(root, wikiRelDir)
makeWiki(t, wiki, "卡.md")
makeRootManifest(t, root, map[string][]string{"": {".wiki/卡.md"}})
cuWriteFile(t, filepath.Join(wiki, "我自己寫的.md"), "這是我的\n")
plan, res, err := ApplyCleanup(root, nil)
if err != nil {
t.Fatal(err)
}
_ = res
for _, it := range plan.Remove {
if it.Kind == CleanupKindWikiDir {
t.Fatalf("有陌生鄰居時不准整個目錄刪掉:%+v", it)
}
}
if _, err := os.Stat(filepath.Join(wiki, "我自己寫的.md")); err != nil {
t.Fatalf("使用者自己的檔被刪了:%v", err)
}
if _, err := os.Stat(wiki); err != nil {
t.Fatalf(".wiki 目錄不該被刪:%v", err)
}
// 帳本以外的檔(00-INDEX 與 .gitignore 是我們的)該刪掉。
if _, err := os.Stat(filepath.Join(wiki, "00-INDEX.md")); !os.IsNotExist(err) {
t.Fatalf("00-INDEX.md 應該被刪掉")
}
if !contains(keepRels(plan.Keep), ".wiki/我自己寫的.md") {
t.Fatalf("留下的東西要出現在 Keep 清單上讓使用者看到:%v", keepRels(plan.Keep))
}
}
// ── ③ 沒有我們宣告的 .wiki(使用者自己的同名目錄)⇒ 一個檔都不碰 ──
func TestCleanupIgnoresForeignWikiDir(t *testing.T) {
root := t.TempDir()
wiki := filepath.Join(root, wikiRelDir)
cuWriteFile(t, filepath.Join(wiki, "我的筆記.md"), "我的\n")
cuWriteFile(t, filepath.Join(wiki, "00-INDEX.md"), "# 00-INDEX\n") // 連名字都一樣也不行
before := cuSnapshotTree(t, root)
plan, _, err := ApplyCleanup(root, nil)
if err != nil {
t.Fatal(err)
}
if len(plan.Remove) != 0 {
t.Fatalf("沒有我們的宣告就一個都不准刪:%v", relsOf(plan.Remove))
}
if got := cuSnapshotTree(t, root); len(got) != len(before) {
t.Fatalf("樹被動到了:before=%v after=%v", before, got)
}
}
// ── ④ .gitignore 被使用者改過(後面加了自己的規則)⇒ 不算我們的宣告 ──
func TestCleanupRequiresExactIgnoreBody(t *testing.T) {
root := t.TempDir()
wiki := filepath.Join(root, wikiRelDir)
makeWiki(t, wiki, "卡.md")
cuWriteFile(t, filepath.Join(wiki, ".gitignore"), wikiIgnoreBody+"\n!我要收版控的.md\n")
plan, err := PlanCleanup(root, nil)
if err != nil {
t.Fatal(err)
}
if len(plan.Remove) != 0 {
t.Fatalf(".gitignore 被改過就不該認:%v", relsOf(plan.Remove))
}
}
// ── ⑤ 工作區裡有 legacy-template 收容處 ⇒ 收容處留著,其餘刪 ──
func TestCleanupKeepsLegacyTemplateQuarantine(t *testing.T) {
root := t.TempDir()
makeWorkspace(t, root)
quar := filepath.Join(root, legacyTemplateRelDir, "system-dev", "wiki", "status.md")
cuWriteFile(t, quar, "使用者的舊 status\n")
plan, res, err := ApplyCleanup(root, nil)
if err != nil {
t.Fatal(err)
}
if len(res.Failed) != 0 {
t.Fatalf("不該有失敗:%+v", res.Failed)
}
if _, err := os.Stat(quar); err != nil {
t.Fatalf("收容處裡的檔被刪了——那是從使用者資料夾搬進來的:%v", err)
}
if _, err := os.Stat(filepath.Join(root, workspaceRelDir, "wiki")); !os.IsNotExist(err) {
t.Fatalf("工作區裡我們自己的東西該刪掉")
}
if !contains(keepRels(plan.Keep), ".arcrun-rag/legacy-template") {
t.Fatalf("收容處要出現在 Keep 清單:%v", keepRels(plan.Keep))
}
}
// ── ⑥ 另一個還在看守的巢狀根 ⇒ 它底下的東西一個都不碰(2026-08-24 pms 現場擺法)──
func TestCleanupSkipsOtherWatchedRoot(t *testing.T) {
root := t.TempDir()
inner := filepath.Join(root, "legacy")
makeWorkspace(t, root)
makeWiki(t, filepath.Join(root, wikiRelDir), "外卡.md")
makeRootManifest(t, root, map[string][]string{"": {".wiki/外卡.md"}})
makeWorkspace(t, inner)
makeWiki(t, filepath.Join(inner, wikiRelDir), "內卡.md")
makeWiki(t, filepath.Join(inner, "sub", wikiRelDir), "更內卡.md")
makeRootManifest(t, inner, map[string][]string{
"": {".wiki/內卡.md"},
"sub": {"sub/.wiki/更內卡.md"},
})
plan, res, err := ApplyCleanup(root, []string{inner})
if err != nil {
t.Fatal(err)
}
if len(res.Failed) != 0 {
t.Fatalf("不該有失敗:%+v", res.Failed)
}
for _, rel := range relsOf(plan.Remove) {
if strings.HasPrefix(rel, "legacy/") {
t.Fatalf("動到另一個還在看守的根:%s", rel)
}
}
for _, p := range []string{
filepath.Join(inner, workspaceRelDir, ".gitignore"),
filepath.Join(inner, wikiRelDir, "內卡.md"),
filepath.Join(inner, "sub", wikiRelDir, "更內卡.md"),
} {
if _, err := os.Stat(p); err != nil {
t.Fatalf("還在看守的資料夾底下的東西被刪了:%s(%v)", p, err)
}
}
if _, err := os.Stat(filepath.Join(root, wikiRelDir)); !os.IsNotExist(err) {
t.Fatalf("外層自己的 .wiki 該刪掉")
}
}
// ── ⑦ 看得見的卡片產物區:只刪帶 arcrun- 標記的檔,目錄與別人的卡不動 ──
func TestCleanupMarkedCardsOnly(t *testing.T) {
root := t.TempDir()
cards := filepath.Join(root, filepath.FromSlash(cardsRelDir))
cuWriteFile(t, filepath.Join(cards, "arcrun-機器卡.md"), "# 機器卡\n")
cuWriteFile(t, filepath.Join(cards, "leo自己的卡.md"), "# 我的\n")
cuWriteFile(t, filepath.Join(root, "system-dev", "wiki", "status.md"), "我的 status\n")
_, res, err := ApplyCleanup(root, nil)
if err != nil {
t.Fatal(err)
}
if len(res.Failed) != 0 {
t.Fatalf("不該有失敗:%+v", res.Failed)
}
if _, err := os.Stat(filepath.Join(cards, "arcrun-機器卡.md")); !os.IsNotExist(err) {
t.Fatalf("帶標記的卡該刪掉")
}
for _, p := range []string{
filepath.Join(cards, "leo自己的卡.md"),
filepath.Join(root, "system-dev", "wiki", "status.md"),
cards,
} {
if _, err := os.Stat(p); err != nil {
t.Fatalf("不該動的東西被動了:%s%v", p, err)
}
}
}
// ── ⑧ symlink 不跟隨、不刪(指出去的連結會刪到監看根以外)──
func TestCleanupNeverFollowsSymlink(t *testing.T) {
root := t.TempDir()
outside := t.TempDir()
cuWriteFile(t, filepath.Join(outside, "外面的重要檔.md"), "別碰\n")
makeWiki(t, filepath.Join(root, wikiRelDir), "卡.md")
if err := os.Symlink(outside, filepath.Join(root, "捷徑")); err != nil {
t.Skipf("這台建不了 symlink%v", err)
}
// 連 .wiki 自己是條捷徑的情況也擋
link2 := filepath.Join(root, "sub", wikiRelDir)
if err := os.MkdirAll(filepath.Join(root, "sub"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(outside, link2); err != nil {
t.Fatal(err)
}
plan, res, err := ApplyCleanup(root, nil)
if err != nil {
t.Fatal(err)
}
if len(res.Failed) != 0 {
t.Fatalf("不該有失敗:%+v", res.Failed)
}
for _, rel := range relsOf(plan.Remove) {
if strings.HasPrefix(rel, "捷徑") || strings.HasPrefix(rel, "sub/") {
t.Fatalf("動到 symlink%s", rel)
}
}
if _, err := os.Stat(filepath.Join(outside, "外面的重要檔.md")); err != nil {
t.Fatalf("監看根以外的檔被刪了:%v", err)
}
}
// ── ⑨ PlanCleanup 保證不寫檔(「先看清單」的前提就是看清單這件事本身無副作用)──
func TestPlanCleanupWritesNothing(t *testing.T) {
root := t.TempDir()
cuWriteFile(t, filepath.Join(root, "我的.md"), "x\n")
makeWorkspace(t, root)
makeWiki(t, filepath.Join(root, wikiRelDir), "卡.md")
makeWiki(t, filepath.Join(root, "docs", wikiRelDir), "卡2.md")
before := cuSnapshotTree(t, root)
plan, err := PlanCleanup(root, nil)
if err != nil {
t.Fatal(err)
}
if len(plan.Remove) == 0 {
t.Fatal("這份擺法應該找得到東西")
}
after := cuSnapshotTree(t, root)
if len(before) != len(after) {
t.Fatalf("PlanCleanup 動了檔案:before=%d after=%d", len(before), len(after))
}
for k, v := range before {
if after[k] != v {
t.Fatalf("PlanCleanup 改了 %s", k)
}
}
// 每一筆都要說得出依據——沒依據的東西不准進清單。
for _, it := range plan.Remove {
if strings.TrimSpace(it.Evidence) == "" {
t.Fatalf("這一筆沒有依據:%+v", it)
}
}
}
// ── ⑩ 帳本認卡:卡名不帶標記、也不是 00-INDEX,靠 manifest.json 認出來 ──
func TestCleanupAccountsCardsViaManifest(t *testing.T) {
root := t.TempDir()
wiki := filepath.Join(root, wikiRelDir)
cuWriteFile(t, filepath.Join(wiki, "手動卡.md"), "# 手動卡\n")
cuWriteFile(t, filepath.Join(wiki, "manifest.json"), `{"version":1,"docs":[
{"node":"(根)","path":"a.md","doc_id":"abc","status":"extracted","cards":[".wiki/手動卡.md"]}]}`)
// 故意不放 .gitignore ⇒ 只能靠帳本認人
plan, err := PlanCleanup(root, nil)
if err != nil {
t.Fatal(err)
}
if got := relsOf(plan.Remove); !equalStrings(got, []string{".wiki"}) {
t.Fatalf("帳本上的卡+帳本自己=全部認得,應該整個目錄刪掉,實際 %v(keep=%v", got, keepRels(plan.Keep))
}
}
func equalStrings(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func contains(ss []string, want string) bool {
for _, s := range ss {
if s == want {
return true
}
}
return false
}
+18 -11
View File
@@ -1,20 +1,22 @@
{
"_algo": 4,
"version": "0.18.33",
"fingerprint": "7ba0f6dd2d080925",
"version": "0.18.35",
"fingerprint": "3bd7f3fb0124bda4",
"files": {
".gitignore": "4d56952b0fb13bf8f9b6c13a6d4c34a075bac3af447636a1df4335d7576e2f97",
"CHANGELOG.md": "5f2cd870b97a25fee01f5d2b4e7ac7c9afdd091c95ca5725ff7401823b651967",
"CHANGELOG.md": "1c740bc8fa983be721c80c95b7941504d3d5e42c6e8d663a59d9c294d72e5877",
"DAEMON_LINE": "d5019abbdc8a5f2919e9e3510391891cd7fbdf0765bf16ec83caa779f370116d",
"README.md": "3329fbc8f4ca5e966f26c5745117bf2ddc65603b4ed78060835270dc39132bfc",
"README.md": "9d92cac236b20a0b183eea3e7f5e39ad492f05192c4ea602eb11c3d09967327f",
"check-standalone.sh": "65fbce096326791c2f103a51e76d0ad79e9e510e2a7700b49ac2d2d81f73c853",
"cloud_latest.go": "c2ad7f87bbd55247540eb924ac3be5deadbc4488b504770a5127a01c748f0e0f",
"cloud_latest_test.go": "703963b65da91ae0c4638b9ad676ca7f12a474c635e903a5d988e4980b3888aa",
"cloud_version.go": "eebc2552e09e199928e82607bebf90daef36b5721ec9b8a098304c10dbec46f9",
"cloud_version_test.go": "66c004de1f0a3958851f22a22367065f13b3381ac7d936ae54d812c26a0b9fb6",
"cmd/arcrun-app/.gitignore": "7502ba597654ebb3463535bdfb51c271abad666a67c7ac8ab36deca1a72615f8",
"cmd/arcrun-app/README.md": "9ea849308b5e578d9a43b73fc3d72637d21428eca9c96a448d31078e8627bcb4",
"cmd/arcrun-app/app.go": "4c65f895c68f82a84d633e6e6d05fbabb86e51632f9ae3d693e39a67ca2b981c",
"cmd/arcrun-app/README.md": "23e53fdf53fcd96b6d8d7a121eb91d3242d11b69d655770473b745c49bb387b9",
"cmd/arcrun-app/app.go": "3d61ca98be11ff8049808331eee07b6001cc24b44d23440c694b267a0b3cd942",
"cmd/arcrun-app/apps.go": "0df6da9d55adbdec500fbb01005a305287d1dbf796b7e463f2b2f222626b4c96",
"cmd/arcrun-app/apps_test.go": "0e2f137f8b49f421e869ab3192ca12c8bb756029d8730487ccc44733aaf7f96c",
"cmd/arcrun-app/arcrun-app": "e2bdf54a15d69860df05820274a80d7497b7d62a4e8ba219f72f30b543456b79",
"cmd/arcrun-app/assets/store/README.md": "a9028a727947c5038ca938c664b18e4851ae3bf0e6d8838c82aedea43d02ded2",
"cmd/arcrun-app/assets/store/Square150x150Logo.png": "27942d943096ba23f036b030726e905f6df6123428cd7d8ec687ce5b11e42db1",
@@ -48,9 +50,10 @@
"cmd/arcrun-app/build/windows/wails.exe.manifest": "c63cb75ce48fb1354c95264f6fc43a7686a9b85a82a71efa9722ae4ce6561dc0",
"cmd/arcrun-app/changelog-section.sh": "8b39dfdfbf8670fc850102e43b056b98dda52b2a7bd2a49f8d961c171c7abe23",
"cmd/arcrun-app/check-cis.sh": "0627a2187160fd1f8c9f618588f6398c4b087a91394e8d387f9a9710f79dd8a5",
"cmd/arcrun-app/check-launcher.mjs": "0c9b4ebd142554b69c1eedf46e5618078b60a050b9f7e5afbf6d3c34d509337d",
"cmd/arcrun-app/check-render.sh": "8cd0fb4fe975111f74afb3bc841bc34fe9b160aeddcf67559a504c5187b8ae02",
"cmd/arcrun-app/check-tray.sh": "7ff5c5e4d3dba6a356d9b556fc93cefe1c9e93827362ddd553fdb02717701cf5",
"cmd/arcrun-app/connect.go": "18fc927ab55858596c1c6b9e913694fd60cc6dd0c12f4f1640144dcf7f12b1d9",
"cmd/arcrun-app/connect.go": "0da69f6b781c3283851304511b1bd2ebd6f3b9663ae28b3605bab13072d39f9d",
"cmd/arcrun-app/daemon-notes.mjs": "452434bf3d744366717219968ee94ba7bb773dbbcd62d69a46937cbb967811b9",
"cmd/arcrun-app/daemon-version.py": "786a0212800dd5c5d3a9ff9faf5c5930be1428f033bc20a9de9399a30b18a824",
"cmd/arcrun-app/daemon-version.test.mjs": "ee052f19806ba0fa3970442199a57dc76d69829c5b72f9e3879dc50ce0f1abfc",
@@ -79,8 +82,8 @@
"cmd/arcrun-app/frontend/src/assets/fonts/OFL.txt": "8656c2a27357b3140047a39b27e3cd928256544ad8a004ef77087f761358208c",
"cmd/arcrun-app/frontend/src/assets/fonts/nunito-v16-latin-regular.woff2": "06f3af3fe52542d40ad9bc14ec03e04deaabd09ec369221cc8f536db1c72bf55",
"cmd/arcrun-app/frontend/src/assets/images/logo-universal.png": "157a874acd3363634747287c0b01034e9f1eec838a4cf39827dfea789e730494",
"cmd/arcrun-app/frontend/src/main.js": "75bec60d9af627c990b2f2007ffd70e28e33a4a728522099777e028cd670e385",
"cmd/arcrun-app/frontend/src/style.css": "08e890f3f0d7f603249175d5415f64c43370d08bbd30b66c7ccb39a58e399169",
"cmd/arcrun-app/frontend/src/main.js": "9c448240ae49491081cb3dec467801fe7d02893f15be4a074f41542786173564",
"cmd/arcrun-app/frontend/src/style.css": "24b3797c731720b60ce8a2a19582b4a76e8e37a9d2eacce8dbe205ba4008849b",
"cmd/arcrun-app/gen-icons.py": "df5b3b33e500fc564badfc8bd2a9ca6a43f7d7e3b31111c41e3b322137c4c96c",
"cmd/arcrun-app/go.mod": "8b9e57bcfd44886d1ec06480e033af706ff3fed470786c57074900dd48ecf86f",
"cmd/arcrun-app/go.sum": "bc4453654c203e5af251c5a3061616cb0ceeb1b6a78a02ace2ca85524d217256",
@@ -95,6 +98,7 @@
"cmd/arcrun-app/supervise.go": "f7eb6c74524b5d8d0abe520b831a98b85d19d18c00d1a585493ababcbd01c81f",
"cmd/arcrun-app/tray_darwin.go": "ff3cb22fc2663b08b9f3cd14887ab4411ed338ec8920b58ad0fac029f09edae0",
"cmd/arcrun-app/tray_darwin.m": "b4ddf0bb420adf00d580d65c8cfbbe130a78e803dd1e92a1617ddaf6ef172fac",
"cmd/arcrun-app/tray_other.go": "0c3df95b5f7999f6319d0bf5e24fdbf821bf97a79db04077d9e7eaf358299880",
"cmd/arcrun-app/tray_windows.go": "adb4f390d3523ac3676f16c27f2b5ea4b9cee850113bc767a45ab9ce503d4963",
"cmd/arcrun-app/update_api.go": "1e94052085cea79d406a06772fd171944090a31c30af0456f57b214b065d4d87",
"cmd/arcrun-app/upgrade_repair_test.go": "65413d0b07b6c011de2af2783247d0f602a680a199229b52a77e176626c8ef26",
@@ -111,7 +115,7 @@
"convert_table_test.go": "d0371b7566ef3152f9dd42f9f990e0dffa1c50a0c8e874a28415fa2e4c188394",
"convert_test.go": "04f3fa30d1be5f910c0e0be3308ced2963191ab030a9eaccd986ef581fcd4e18",
"convert_wiring_test.go": "09e97bf32ace245b55acc7d65ee5c0bb7fd84f8fb1aab61f603f9e9d18b22f13",
"direct.go": "27fed72987d3f5984cae939f0b09de069a9f13f97e24e91361b60399e1bdb5d8",
"direct.go": "98d51b233e31320de20f4c605ce420433fb4720136c9eb188fbd69e1a136fb69",
"direct_extract_test.go": "d3465c1b5953858ae780d192a2cf515d452f48eee3ff18a6345ce64320debbbb",
"direct_multi_test.go": "d0aa211d90aaaa9d7dbe2b04f1aaa5989e91e91b037144cc0f4e0fd068f43c2b",
"direct_pacing.go": "2690b2fff858250028886bc3cd7793be54b4d4a03ddca41485476cb04b79f7c3",
@@ -126,6 +130,9 @@
"extract_test.go": "63560ecca929c6507d93b3f52988bb7ce2cd6137140eb373c520eac5ee052c76",
"extract_workersai.go": "ea77c25d2a14d905e3c7f3a7caaa3dff2fa5ceb71c98c772a258ef0e770106f3",
"extract_workersai_test.go": "188822f4c44ee5bf3b1400e1475af314ad831296538a68c5e1006341ba63765e",
"folderindex.go": "f3dd186fd7e882ff293b3f8aa6b2582b018290ee5f14bbdc02a7ac131834fd69",
"folderindex_helpers_test.go": "2cf17b70c396225f728e8e160cfc75d7bdc76ac1ce8e574d1450b020050c0734",
"folderindex_test.go": "a5319eb1e92473cb5c32d3beddb7889997556227824de300dd39953b43beab70",
"foldershape.go": "b1b237f5674674efdc9bdea849b63c571a014e64bec33af42fb7c4769fa7582b",
"go.mod": "05dacc224af2a6797db86689435f83537efb433ffba521467b95d1099f5e12db",
"go.sum": "a9c92957ca368462aab32a82a6f51c9586ea07478a7967012986d757e9969ab3",
@@ -144,7 +151,7 @@
"machineid_test.go": "5b54ac62ee1fb28b8f0a873888aeddb844af0aa74472bfb849573cfc6691a290",
"machinemark.go": "b93464c2ee89a73c0e0da38bcce5dc1ba6c1deba2fc483f28c6021c3ffdee4a0",
"main.go": "e02e7b32dd230f33e35c7ebf62d81b7599e2b86a0ef7cadc0fd34b672f42381d",
"manifest.go": "ce3733ed13c6caedad477392e14af7714a39616819477182ab8bd14eb5d8eae6",
"manifest.go": "768049a34c06920711a980bb19fb41e7f1feff648f22265ba0c3d1c1ad85020f",
"manifest_extractedby_test.go": "8019fe35675a1e801b7bda829ee1d647de32db03c029b1c651f71573e644c69e",
"manifest_retry_test.go": "e6b4a321ccddfeed8cdbfabe6d031d5a9c59a33b47baf0b6d21150fa8406748a",
"multiaccount_connected_test.go": "ba1817b73a5dd0dd5d300750e54d5c53bd82a75ef6e43caeddfcb2303d64ff66",
+3 -1
View File
@@ -3,5 +3,7 @@
"v0.18.30": "396f3c3b68cdd0b3",
"0.18.31": "d6b2b6083258b853",
"0.18.32": "4753db807169fe3d",
"0.18.33": "7ba0f6dd2d080925"
"0.18.33": "7ba0f6dd2d080925",
"0.18.34": "d8c7490b9d32e62e",
"0.18.35": "3bd7f3fb0124bda4"
}
+77 -12
View File
@@ -1,19 +1,84 @@
# README
# Arcrun 桌面小幫手(Wails)— 地端 build 說明
## About
> 這份取代了原本 `wails init` 留下的英文樣板 README(那份只寫「用 `wails build`」,
> 而這裡真正的打包線是旁邊那四支腳本,不是裸的 `wails build`)。
>
> 🔴 **為什麼會有這份**leo 2026-08-24`inkstone/arcrun-rag#137`):
> 「daemon 部分要傳給地端做,**或是你做好給它 build**」——採後者。
> 雲端只交**編得起來的 code**,安裝檔(DMG/exemsix)由地端打,
> 因為那需要 macOS/Windows 環境與簽章,雲端沒有。
This is the official Wails Vanilla template.
---
You can configure the project by editing `wails.json`. More information about the project settings can be found
here: https://wails.io/docs/reference/project-config
## 一、一次性前置(每台機器裝一次)
## Live Development
| 需要 | 怎麼裝 | 用來做什麼 |
|---|---|---|
| Go **1.25+** | `brew install go``go.mod``go 1.25.0`;較舊的 Go 會自己抓 toolchain | 編 App 與 collector |
| Node 18+ | `brew install node` | 編前端(Vite |
| Wails CLI v2 | `go install github.com/wailsapp/wails/v2/cmd/wails@v2.13.0` | 打包 |
| mingw-w64 | `brew install mingw-w64` | **只有要打 Windows 版才需要**WebView2 要 CGo 交叉編譯) |
| cmake、icu4c | `brew install cmake icu4c` `bash build-msix.sh --setup` | **只有要送 Microsoft Store 才需要** |
To run in live development mode, run `wails dev` in the project directory. This will run a Vite development
server that will provide very fast hot reload of your frontend changes. If you want to develop in a browser
and have access to your Go methods, there is also a dev server that runs on http://localhost:34115. Connect
to this in your browser, and you can call your Go code from devtools.
> `wails` 裝完記得 `export PATH="$PATH:$(go env GOPATH)/bin"`(四支 build 腳本自己也會補這行)。
## Building
## 二、打包:跑既有的腳本,不要自己下 `wails build`
To build a redistributable, production mode package, use `wails build`.
```sh
cd collector/cmd/arcrun-app
./build-mac.sh # → build/bin/Arcrun.app
./build-dmg.sh # → dist/Arcrun-<版本>.dmg (自己會先跑 build-mac.sh
./build-win.sh # → dist/Arcrun-<版本>.exe (在 Mac 上交叉編譯,需 mingw-w64
./build-msix.sh # → dist/Arcrun-<版本>.msix (送 Store 用;不必自購憑證)
```
🔴 **版本號不要手打。** `daemon-version.py --stamp` 會從 `collector/CHANGELOG.md`
最上面那個「下一版(未發佈)」段落戳出版號並補上日期——四支腳本都已經呼叫它。
本次(`#137`)的 changelog 段落已經寫好,所以**這一版打出來會是 `0.18.35`**
`./daemon-version.py` 唯讀查詢可以先確認)。
🔴 打包前若改過任何 `collector/` 底下的檔,指紋會變 ⇒ 同一個版號不准對應兩份原始碼,
腳本會擋下來並告訴你怎麼辦。那是刻意的閘,不要繞過它。
## 三、交貨前自己先驗(這三支都在本目錄,跑得很快)
```sh
go test ./... # 後端(含本次新增的 App 啟動器 12 條)
bash check-cis.sh # CIS 合規(色票/lockup/選中態)
npm --prefix frontend run build && node check-launcher.mjs # 畫面與互動(需 playwright
```
`check-launcher.mjs` 會真的把 `frontend/dist` 渲染出來、真的點下去,
截圖丟在 `/tmp/arcrun-launcher-shots/`。沒裝 playwright 它會自己跳過
`npm i -D playwright && npx playwright install chromium`)。
macOS 上另外還有 `check-render.sh`(量 lockup 像素與深色模式),需要本機的 Chrome。
## 四、只有在真機上才驗得到的(雲端做不到,地端請補)
雲端這台沒有 GUI、也沒有 WebView,所以下面這些**沒有被驗過**,
打包完請在真機上走一次:
1. `wails dev`(或直接開打好的 `Arcrun.app`)能不能起來、視窗尺寸對不對。
2. 系統匣:Mac 是原生 `NSStatusItem``tray_darwin.m`)、Windows 是 `energye/systray`
點 icon 開窗、右鍵只有「結束 Arcrun」。
3. **App 啟動器**(本次新增):打開就落在「App 界面」,九宮格列出的是你連的那個
知識庫實際裝了的 App;點一個進去、按一下它的動作,看結果回不回得來。
4. WindowsWebView2 有沒有裝、SmartScreen 會不會攔、Defender 會不會誤判。
## 五、平台檔案對照(改東西前先看這張)
| 檔 | build tag | 說明 |
|---|---|---|
| `tray_darwin.go` / `tray_darwin.m` | `darwin` | macOS 原生 NSStatusItem |
| `tray_windows.go` | `windows` | Windows systray |
| `tray_other.go` | `!darwin && !windows` | **no-op**。讓 `go build``go test` 在 Linux/CI 上跑得起來,對出貨零影響(Linux 不是出貨平台) |
| `dock_darwin.go` / `dock_other.go` | 同上 | Dock icon 隱藏 |
## 六、開發時的即時預覽
```sh
wails dev
```
Vite 熱重載;`http://localhost:34115` 可以用瀏覽器連進去,Go 方法一樣叫得到。
+69 -1
View File
@@ -68,6 +68,15 @@ type accountCfg struct {
RetiringFolders []string `json:"retiring_folders,omitempty"`
Extractor string `json:"extractor,omitempty"`
GeminiAPIKey string `json:"gemini_api_key,omitempty"`
// PortalSessionPortalSessionExp=這台電腦對這個知識庫的 portal 登入憑證
// arcrun-rag#137 App 啟動器用;App 詳情與動作只有 portal session 那條路有,
// 見 apps.go 檔頭)。**這是 session token 不是密碼**——密碼仍然零落地。
//
// 🔴 這兩欄是 App 端獨有的(collector 的 AccountConfig 沒有):collector 讀 config
// 時會忽略不認得的欄位,所以加在這裡是安全的;反過來(collector 有而這裡沒有)
// 才是上面那條註解講的、會靜默掉欄位的方向。
PortalSession string `json:"portal_session,omitempty"`
PortalSessionExp int64 `json:"portal_session_exp,omitempty"`
}
type directConfig struct {
@@ -719,7 +728,18 @@ func (a *App) AddFolder(accIdx int, path string) error {
// vs 我不要這份資料了),而**猜錯任何一邊都是不可逆的**——猜「保留」則產品承諾的
// 「資料所有權完全屬於使用者」是假的;猜「收回」則整理好的知識被誤刪。
// ⇒ 在動作的當下把兩個後果講清楚、讓他自己挑(見前端 confirmRemove 的文案)。
func (a *App) RemoveFolder(accIdx int, path string, takedown bool) error {
//
// cleanupLocal=第三個獨立選擇(arcrun-rag#138leo 2026-08-24:「碎型會在每個資料夾
// 安裝隱藏資料夾,人工刪除不容易,所以當它斷連,應該要可以幫它把 Arcrun RAG 建立的
// 資料夾刪掉」):連同硬碟上那些隱藏資料夾一起收掉。
//
// 與 takedown **正交,不合成一個開關**takedown 管**雲端**的知識(搜尋還找不找得到),
// cleanupLocal 管**他硬碟上**的產物(資料夾乾不乾淨)。兩個後果不一樣,合起來就是替他決定
// ——這正是 #46 那次修掉的病,別再犯一次。
//
// 🔴 順序不可顛倒:**先把資料夾從 WatchFolders 拿掉並重啟看守,再刪本地產物。**
// 反過來的話 collector 還看著那個根,剛刪掉的 `.wiki/` 下一輪就長回來。
func (a *App) RemoveFolder(accIdx int, path string, takedown, cleanupLocal bool) error {
cfg, err := loadCfg()
if err != nil {
return err
@@ -753,6 +773,17 @@ func (a *App) RemoveFolder(accIdx int, path string, takedown bool) error {
return err
}
restartWatch()
if cleanupLocal && found {
// 看守已經停掉了(restartWatch 讀的是剛存下去的設定),現在刪才不會被重建。
// 刪不掉的那幾筆不讓整個移除失敗——資料夾已經從清單拿掉了,那是使用者要的主要結果;
// 清理是附帶的善後,回一個講得出「哪幾筆沒刪掉、為什麼」的錯誤就好。
if _, res, cerr := collector.ApplyCleanup(path, cfg.otherWatchedRoots(accIdx, path)); cerr != nil {
return fmt.Errorf("資料夾已經移除,但清理 Arcrun RAG 建立的檔案時出錯:%w", cerr)
} else if len(res.Failed) > 0 {
return fmt.Errorf("資料夾已經移除,但有 %d 個項目刪不掉(第一個:%s——%s)",
len(res.Failed), res.Failed[0].Rel, res.Failed[0].Error)
}
}
if takedown {
// 不必等下一輪輪詢——使用者剛按下按鈕,他期待「現在就開始」。
// 沿用既有的 sync-now 訊號檔,不新發明一套 IPC。
@@ -761,6 +792,43 @@ func (a *App) RemoveFolder(accIdx int, path string, takedown bool) error {
return nil
}
// PlanFolderCleanup 回答「按下『連同這個資料夾裡的檔案一起清掉』會刪到哪些東西」。
//
// 🔴 #138 的驗收條件之一:「使用者要能在動手前看到將要刪掉哪些東西」——不是按下去就無聲刪光。
// 這支**只讀不寫**collector.PlanCleanup 保證),前端在確認對話框裡先叫它、把清單攤出來。
// 真的動手時 ApplyCleanup 會**重算一次**,所以這裡拿到的清單過期也不會刪錯東西。
func (a *App) PlanFolderCleanup(accIdx int, path string) (*collector.CleanupPlan, error) {
cfg, err := loadCfg()
if err != nil {
return nil, err
}
if accIdx < 0 || accIdx >= len(cfg.Accounts) {
return nil, fmt.Errorf("找不到這個知識庫帳號")
}
return collector.PlanCleanup(path, cfg.otherWatchedRoots(accIdx, path))
}
// otherWatchedRoots=除了 path 以外、**所有帳號**還在看守(或還在收回中)的資料夾。
//
// 🔴 為什麼要跨帳號收集:巢狀擺法是真的(2026-08-24 現場,`pms` 與 `pms/pms_v1_legacy`
// 同時在同一個帳號的看守清單裡)。移除外層時把內層的工作區刪掉=把一個還在跑的同步弄壞,
// 而且不同帳號可以看守同一棵樹底下的不同層——判準是「這條路徑還有沒有人在用」,
// 跟它掛在哪個帳號無關。
func (c *directConfig) otherWatchedRoots(accIdx int, path string) []string {
var out []string
for i := range c.Accounts {
for _, f := range c.Accounts[i].WatchFolders {
if i == accIdx && f == path {
continue
}
out = append(out, f)
}
// 還在收回中的資料夾同樣還沒結束——它的帳本正在被逐筆撤除,別去動它的檔。
out = append(out, c.Accounts[i].RetiringFolders...)
}
return out
}
// pruneFinishedRetirements 把「collector 已回報收乾淨」的資料夾從設定裡清掉。
//
// 為什麼由 App 清而不是 collector 自己清:config.json 的寫入者只有 App 一個,
+495
View File
@@ -0,0 +1,495 @@
package main
// apps.go — 桌面小幫手的 App 啟動器後端(inkstone/arcrun-rag#137
//
// ─────────────────────────────────────────────────────────────────────────────
// 這個檔案在解什麼
// ─────────────────────────────────────────────────────────────────────────────
// leo 2026-08-24:「所有的 App 需要有一個類似 Android/iOS 的九宮格啟動界面,
// 每個 App 有一個 icon**這會運行在 portal 及 daemon**。」
//
// 桌面這半沒有自己的 KBDB,也**不准**在本機另存一份 App 清單
// (上游 `inkstone/Arcrun#82` 已定「安裝態只有一份真相源」=實例的 `{tenant}:app:{id}`)。
// ⇒ 本檔做的事只有一件:**去使用者連的那個實例問**,把答案原樣交給畫面。
// 沒有任何預設清單、沒有快取到磁碟、沒有第二份 schema。
//
// ─────────────────────────────────────────────────────────────────────────────
// 為什麼是「兩條路」而不是一條(這是本票的規格判斷,理由寫在這裡不寫在票上)
// ─────────────────────────────────────────────────────────────────────────────
// 上游同一份安裝態有兩個對外面:
//
// ① `/portal/data/apps*` 認 **portal session**Bearer)——Portal 那半走這條,
// 清單/詳情(含畫面 HTML)/動作三支齊全。
// ② `/apps` 認 **X-Arcrun-API-Key**(=實例 namespace)——CLI/操盤 AI 走這條,
// **只有清單**,沒有詳情、沒有動作。
//
// 小幫手手上本來就有 ②的鑰匙(`config.json` 的 `api_key`,連線時實例自己下發的),
// 但**沒有**①的 session(密碼刻意零落地,見 connect.go)。
//
// 所以:
// - **清單一律先走①、拿不到就退回②**。兩支回的都是 `summarizeApp` 的同一個形狀
// id/name/icon/has_ui/version),所以九宮格**在完全沒登入的情況下也是真資料**。
// - **詳情與動作只有①有**,所以那兩件需要一次 portal 登入。連線精靈當下順手換一張
// session`Connect` 已經有密碼在手,見 connect.go 的呼叫點),使用者通常不會再被問。
// session 過期(實例預設 7 天)才會在 App 頁上出現一次登入框。
//
// 🔴 密碼仍然零落地:存的是 session token,不是密碼(與 connect.go 同一條紅線)。
//
// ─────────────────────────────────────────────────────────────────────────────
// 不做輪詢
// ─────────────────────────────────────────────────────────────────────────────
// 畫面每秒打一次 `GetState()`,但**本檔的任何函式都不掛在那條線上**——
// App 清單只在「打開啟動器」與「按重新整理」時才去問實例一次。
// 把網路請求塞進每秒的 tick 等於自己造一個輪詢器,那是明令禁止的形狀。
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// appHTTPTimeoutApp 動作背後是一整條工作流(可能打 LLM),比單純讀清單久得多。
// 清單/詳情用短的,動作用長的——不讓「讀清單」被一條慢工作流的預算拖著。
const (
appReadTimeout = 20 * time.Second
appActionTimeout = 120 * time.Second
)
// ── 前端要的形狀 ───────────────────────────────────────────────────────────
// UIApp=九宮格上的一格。逐欄對應上游 `summarizeApp`,不多不少——
// 多一欄就是在桌面端發明一個實例不認得的概念。
type UIApp struct {
ID string `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
HasUI bool `json:"hasUi"`
Version string `json:"version"`
}
// UIAppList=一個知識庫的啟動器內容。
//
// 🔴 Error 與「空清單」是兩件事,不准混成同一句話(arcrun-rag#10「寧可明顯失敗」):
// - `Error != ""` ⇒ 問不到(連不上/實例太舊沒有這支端點)——畫面要說「問不到」
// - `Error == "" && len(Apps) == 0` ⇒ 真的一個都沒裝——畫面要說「還沒有 App」
//
// 兩者長得像,但使用者該做的事完全相反。
type UIAppList struct {
AccIdx int `json:"accIdx"`
Account string `json:"account"` // 知識庫顯示名(畫面上的切換器用)
Host string `json:"host"`
Apps []UIApp `json:"apps"`
Error string `json:"error"`
// Source=這份清單是用哪把鑰匙問到的("session""apikey")。給診斷用,
// 畫面不依它分支——兩條路的內容是同一份安裝態。
Source string `json:"source"`
}
// UIAppWorkflowApp 的一條工作流(沒有自帶畫面的 App 就靠這個列表當預設畫面,
// 與 Portal 的 renderAppView 同一套呈現,不另立第二種)。
type UIAppWorkflow struct {
Name string `json:"name"`
Description string `json:"description"`
}
// UIAppDetail=點進一個 App 之後要的全部東西。
type UIAppDetail struct {
ID string `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
Version string `json:"version"`
HasUI bool `json:"hasUi"`
UIHtml string `json:"uiHtml"`
Workflows []UIAppWorkflow `json:"workflows"`
Actions []string `json:"actions"`
// NeedsLogin=這台機器沒有(或已過期)這個知識庫的 portal session。
// 不是錯誤,是「還差一步」——畫面要長出登入框,不是紅字。
NeedsLogin bool `json:"needsLogin"`
Email string `json:"email"` // 登入框預填(帳號本來就存在 config 裡)
Error string `json:"error"`
}
// ── 內部:HTTP 小工具 ─────────────────────────────────────────────────────
// apiBaseOf 回傳這個帳號的 API basecypher origin)。
// 連線精靈存進來的 CypherURL 已經是 API 位址(normalizePortalURL 換算過),
// 這裡只做去尾斜線,**不重新發明第二套換算**。
func apiBaseOf(a accountCfg) string {
return strings.TrimRight(strings.TrimSpace(a.CypherURL), "/")
}
// accountAt 取第 idx 個帳號;越界回錯(前端傳來的索引可能落後於剛改過的設定)。
func accountAt(cfg *directConfig, idx int) (accountCfg, error) {
if cfg == nil || idx < 0 || idx >= len(cfg.Accounts) {
return accountCfg{}, errors.New("找不到這個知識庫")
}
return cfg.Accounts[idx], nil
}
// appDo 送一個請求並讀回 body。回傳 (status, body, err)err 只代表「連不上」,
// HTTP 4xx/5xx 一律當成「連得上但被拒絕」由呼叫端判讀——
// 把兩者混成一個 error 就分不出「網路斷了」與「密碼過期了」。
func appDo(req *http.Request, timeout time.Duration) (int, []byte, error) {
res, err := (&http.Client{Timeout: timeout}).Do(req)
if err != nil {
return 0, nil, errors.New("連不上這個知識庫——請確認網路正常")
}
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 4<<20)) // App 自帶畫面可能不小,但給上限
return res.StatusCode, b, nil
}
// appErrMessage 從實例回的 JSON 撈 error 欄位;撈不到就用狀態碼講人話。
func appErrMessage(status int, body []byte) string {
var e struct {
Error string `json:"error"`
}
if json.Unmarshal(body, &e) == nil && strings.TrimSpace(e.Error) != "" {
return e.Error
}
switch status {
case http.StatusNotFound:
return "這個知識庫還沒有 App 功能(實例版本較舊,更新後就會出現)"
case http.StatusUnauthorized, http.StatusForbidden:
return "這個知識庫的登入已過期"
}
return fmt.Sprintf("知識庫回了 HTTP %d", status)
}
// ── portal session(詳情與動作唯一的鑰匙)────────────────────────────────
// sessionValid:留 60 秒安全邊際,免得剛好在請求路上過期。
func sessionValid(a accountCfg) bool {
return strings.TrimSpace(a.PortalSession) != "" &&
(a.PortalSessionExp == 0 || time.Now().Unix() < a.PortalSessionExp-60)
}
// portalLogin 用帳密換一張 portal session token。
//
// 🔴 這支是**唯一**碰密碼的地方,而且密碼只活在參數裡:換完就丟,不寫檔、不 log。
func portalLogin(base, email, password string) (token string, expiresIn int64, err error) {
body, _ := json.Marshal(map[string]string{
"email": strings.ToLower(strings.TrimSpace(email)),
"password": password,
})
req, err := http.NewRequest(http.MethodPost, base+"/portal/login", bytes.NewReader(body))
if err != nil {
return "", 0, err
}
req.Header.Set("Content-Type", "application/json")
status, raw, err := appDo(req, appReadTimeout)
if err != nil {
return "", 0, err
}
var out struct {
Success bool `json:"success"`
SessionToken string `json:"session_token"`
SessionExpires int64 `json:"session_expires_in"`
Error string `json:"error"`
}
_ = json.Unmarshal(raw, &out)
if status != http.StatusOK || !out.Success || out.SessionToken == "" {
if strings.TrimSpace(out.Error) != "" {
return "", 0, errors.New(out.Error)
}
return "", 0, errors.New(appErrMessage(status, raw))
}
return out.SessionToken, out.SessionExpires, nil
}
// storeSession 把換到的 session 寫回設定(只存 token 與到期時間,不存密碼)。
func storeSession(idx int, token string, expiresIn int64) error {
cfg, err := loadCfg()
if err != nil {
return err
}
if idx < 0 || idx >= len(cfg.Accounts) {
return errors.New("找不到這個知識庫")
}
cfg.Accounts[idx].PortalSession = token
if expiresIn > 0 {
cfg.Accounts[idx].PortalSessionExp = time.Now().Unix() + expiresIn
} else {
cfg.Accounts[idx].PortalSessionExp = 0
}
return saveCfg(cfg)
}
// clearSession 在實例說「這張 session 不算數」時把它丟掉。
// 留著一張已知無效的 token 只會讓下一次再撞一次 401,然後畫面又閃一下錯誤。
func clearSession(idx int) {
cfg, err := loadCfg()
if err != nil || idx < 0 || idx >= len(cfg.Accounts) {
return
}
if cfg.Accounts[idx].PortalSession == "" && cfg.Accounts[idx].PortalSessionExp == 0 {
return
}
cfg.Accounts[idx].PortalSession = ""
cfg.Accounts[idx].PortalSessionExp = 0
if err := saveCfg(cfg); err != nil {
appLog("清掉過期的知識庫登入失敗:%v", err)
}
}
// tryStoreSessionFromLogin 在連線精靈成功之後順手換一張 session(connect.go 呼叫)。
// **失敗不算連線失敗**——沒有 session 只是「App 詳情要再登入一次」,
// 而資料夾同步這條主線根本用不到它。
func tryStoreSessionFromLogin(cfg *directConfig, idx int, email, password string) {
if idx < 0 || idx >= len(cfg.Accounts) {
return
}
token, exp, err := portalLogin(apiBaseOf(cfg.Accounts[idx]), email, password)
if err != nil {
appLog("連線成功但沒換到知識庫的 App 登入(不影響同步):%v", err)
return
}
cfg.Accounts[idx].PortalSession = token
if exp > 0 {
cfg.Accounts[idx].PortalSessionExp = time.Now().Unix() + exp
}
}
// ── 前端呼叫的四支 ─────────────────────────────────────────────────────────
// ListApps 回一個知識庫上「實際裝了的」App。
//
// 兩條路的順序與理由見檔頭。**沒有任何寫死的清單**——問不到就誠實說問不到。
func (a *App) ListApps(accIdx int) UIAppList {
out := UIAppList{AccIdx: accIdx}
cfg, _ := loadCfg()
acc, err := accountAt(cfg, accIdx)
if err != nil {
out.Error = err.Error()
return out
}
out.Account = accountName(acc)
out.Host = shortHost(acc.CypherURL)
base := apiBaseOf(acc)
if base == "" {
out.Error = "這個知識庫沒有網址,請重新連線一次"
return out
}
// ① portal session(與 Portal 那半同一支端點、同一份權限)
if sessionValid(acc) {
req, _ := http.NewRequest(http.MethodGet, base+"/portal/data/apps", nil)
req.Header.Set("Authorization", "Bearer "+acc.PortalSession)
status, raw, derr := appDo(req, appReadTimeout)
switch {
case derr != nil:
out.Error = derr.Error()
return out
case status == http.StatusOK:
out.Apps = parseAppList(raw)
out.Source = "session"
return out
case status == http.StatusUnauthorized || status == http.StatusForbidden:
clearSession(accIdx) // 過期了 ⇒ 掉到 ② 去,清單照樣看得到
default:
// 其他錯(例如 5xx)**不要**掉到 ②——那會把「實例出問題」偽裝成正常。
out.Error = appErrMessage(status, raw)
return out
}
}
// ② 實例 API key(小幫手本來就有的那把;只有清單,沒有詳情/動作)
if strings.TrimSpace(acc.APIKey) == "" {
out.Error = "這台電腦還沒有這個知識庫的鑰匙,請重新連線一次"
return out
}
req, _ := http.NewRequest(http.MethodGet, base+"/apps", nil)
req.Header.Set("X-Arcrun-API-Key", acc.APIKey)
status, raw, derr := appDo(req, appReadTimeout)
if derr != nil {
out.Error = derr.Error()
return out
}
if status != http.StatusOK {
out.Error = appErrMessage(status, raw)
return out
}
out.Apps = parseAppList(raw)
out.Source = "apikey"
return out
}
// parseAppList 讀 `{apps:[…]}`。兩支端點回的都是 summarizeApp 的形狀,所以只有一份解析。
// 純函式(單測用)。
func parseAppList(raw []byte) []UIApp {
var body struct {
Apps []struct {
ID string `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
HasUI bool `json:"has_ui"`
Version string `json:"version"`
} `json:"apps"`
}
if json.Unmarshal(raw, &body) != nil {
return nil
}
apps := make([]UIApp, 0, len(body.Apps))
for _, x := range body.Apps {
name := x.Name
if strings.TrimSpace(name) == "" {
name = x.ID
}
apps = append(apps, UIApp{ID: x.ID, Name: name, Icon: x.Icon, HasUI: x.HasUI, Version: x.Version})
}
return apps
}
// GetApp 取一個 App 的詳情(自帶畫面/工作流清單/動作白名單)。
//
// 🔴 只有 portal session 這一條路——上游的 API key 面**沒有**詳情端點
//
// `/apps` 只有列表;見 `cypher-executor/src/routes/apps.ts`)。
// 所以沒有 session 時回 NeedsLogin,讓畫面長一次登入框,而不是回一句錯誤。
func (a *App) GetApp(accIdx int, id string) UIAppDetail {
out := UIAppDetail{ID: id}
cfg, _ := loadCfg()
acc, err := accountAt(cfg, accIdx)
if err != nil {
out.Error = err.Error()
return out
}
out.Email = acc.Email
if !sessionValid(acc) {
out.NeedsLogin = true
return out
}
base := apiBaseOf(acc)
req, _ := http.NewRequest(http.MethodGet, base+"/portal/data/apps/"+urlPathEscape(id), nil)
req.Header.Set("Authorization", "Bearer "+acc.PortalSession)
status, raw, derr := appDo(req, appReadTimeout)
if derr != nil {
out.Error = derr.Error()
return out
}
if status == http.StatusUnauthorized || status == http.StatusForbidden {
clearSession(accIdx)
out.NeedsLogin = true
return out
}
if status != http.StatusOK {
out.Error = appErrMessage(status, raw)
return out
}
var d struct {
ID string `json:"id"`
Name string `json:"name"`
Icon string `json:"icon"`
Version string `json:"version"`
HasUI bool `json:"has_ui"`
UIHtml string `json:"ui_html"`
Actions []string `json:"actions"`
Workflows []struct {
Name string `json:"name"`
Description string `json:"description"`
} `json:"workflows"`
}
if json.Unmarshal(raw, &d) != nil {
out.Error = "這個知識庫回的內容看不懂(版本可能不相容)"
return out
}
out.ID, out.Name, out.Icon, out.Version = d.ID, d.Name, d.Icon, d.Version
out.HasUI, out.UIHtml, out.Actions = d.HasUI, d.UIHtml, d.Actions
for _, w := range d.Workflows {
out.Workflows = append(out.Workflows, UIAppWorkflow{Name: w.Name, Description: w.Description})
}
if strings.TrimSpace(out.Name) == "" {
out.Name = out.ID
}
return out
}
// RunAppAction 觸發 App 的一個白名單動作,回傳實例給的結果 JSON(原樣字串)。
//
// 🔴 白名單由**實例**裁決,不在這裡複製一份判斷(K6:前端/桌面端都不是裁決點)。
//
// 這裡不認得任何 action 名稱,只負責把使用者按的那顆按鈕送出去。
//
// payloadJSON 是前端組好的 JSON 物件字串;空字串=沒有 payload。
func (a *App) RunAppAction(accIdx int, id, action, payloadJSON string) (string, error) {
cfg, _ := loadCfg()
acc, err := accountAt(cfg, accIdx)
if err != nil {
return "", err
}
if strings.TrimSpace(action) == "" {
return "", errors.New("沒有指定要做什麼")
}
if !sessionValid(acc) {
return "", errors.New("這個知識庫的登入已過期,請在畫面上重新登入一次")
}
var payload any = map[string]any{}
if s := strings.TrimSpace(payloadJSON); s != "" {
if err := json.Unmarshal([]byte(s), &payload); err != nil {
return "", errors.New("送出的內容格式不對")
}
}
body, _ := json.Marshal(map[string]any{"action": action, "payload": payload})
req, err := http.NewRequest(http.MethodPost,
apiBaseOf(acc)+"/portal/data/apps/"+urlPathEscape(id)+"/action", bytes.NewReader(body))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+acc.PortalSession)
status, raw, derr := appDo(req, appActionTimeout)
if derr != nil {
return "", derr
}
if status == http.StatusUnauthorized || status == http.StatusForbidden {
// 403 也可能是「這個動作不在白名單」——那句話由實例自己講,這裡照抄。
if status == http.StatusUnauthorized {
clearSession(accIdx)
}
return "", errors.New(appErrMessage(status, raw))
}
if status != http.StatusOK {
return "", errors.New(appErrMessage(status, raw))
}
return string(raw), nil
}
// PortalLogin 讓使用者在 App 頁上補一次登入(session 過期時)。
// email 用 config 裡存的那個(就是他連線時用的帳號),只問密碼。
func (a *App) PortalLogin(accIdx int, password string) error {
cfg, _ := loadCfg()
acc, err := accountAt(cfg, accIdx)
if err != nil {
return err
}
if strings.TrimSpace(acc.Email) == "" {
return errors.New("這個知識庫沒有記錄帳號,請用「新增知識庫帳號」重新連一次")
}
token, exp, err := portalLogin(apiBaseOf(acc), acc.Email, password)
if err != nil {
return err
}
return storeSession(accIdx, token, exp)
}
// urlPathEscapeApp id 的字元集上游已限死 `^[a-z][a-z0-9_-]{0,63}$`
// 但我們是**收方**不是**發方**——照樣 escape,不假設對面永遠守規矩。
func urlPathEscape(s string) string {
var b strings.Builder
for _, r := range s {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9',
r == '-', r == '_', r == '.', r == '~':
b.WriteRune(r)
default:
for _, c := range []byte(string(r)) {
fmt.Fprintf(&b, "%%%02X", c)
}
}
}
return b.String()
}
+384
View File
@@ -0,0 +1,384 @@
package main
// apps_test.go — App 啟動器後端的驗收(inkstone/arcrun-rag#137
//
// 🔴 這裡的假實例**不是憑印象寫的**:每一支端點的路徑、認證標頭、回應欄位名,
//
// 都是照 `inkstone/Arcrun` 分支 `feat/app-system-v0` 的原始碼抄下來的
// `cypher-executor/src/routes/apps.ts`、`routes/portal-data.ts`、
// `lib/app-system.ts` 的 summarizeAppdetailApp、`routes/portal.ts` 的 /portal/login)。
// 抄錯了測試就會綠得很假——所以下面每一段都標了它對應上游哪一支。
//
// 這組測試回答的是驗收條件 1 與 2 裡「機器答得出來」的那幾格:
// - 清單真的來自實例(兩條路都試過),桌面端沒有任何寫死清單
// - 「問不到」與「一個都沒裝」分得出來
// - session 過期會被清掉、且清單仍然看得到(退回 API key)
// - 動作真的打到 /portal/data/apps/:id/action,白名單由實例裁決
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
// writeCfgWithAccount 造一份最小可用的 config.json(一個知識庫)。
func writeCfgWithAccount(t *testing.T, base string, acc accountCfg) {
t.Helper()
acc.CypherURL = base
if acc.Namespace == "" {
acc.Namespace = "ns-test"
}
cfg := map[string]any{
"manifest": filepath.Join(appDir(), "manifest.json"),
"accounts": []accountCfg{acc},
}
b, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(appDir(), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(configPath(), b, 0o600); err != nil {
t.Fatal(err)
}
}
func tempHome(t *testing.T) {
t.Helper()
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
}
// fakeInstance 是「一台裝了 App 系統的 Arcrun 實例」的最小替身。
type fakeInstance struct {
// sessionToken=目前有效的 session;空字串=任何 Bearer 都算過期。
sessionToken string
password string
apiKey string
// 收到的動作(給測試斷言用)
gotAction string
gotPayload map[string]any
// 讓測試模擬「實例壞掉」
listStatus int
}
func (f *fakeInstance) server(t *testing.T) *httptest.Server {
t.Helper()
// 上游 summarizeApp 的形狀(lib/app-system.ts
summary := []map[string]any{
{"id": "note", "name": "筆記", "icon": "🗒️", "has_ui": true, "version": "0.1.0"},
{"id": "weekly", "name": "週報", "icon": "📊", "has_ui": false, "version": "0.2.0"},
}
mux := http.NewServeMux()
// POST /portal/loginroutes/portal.ts
mux.HandleFunc("/portal/login", func(w http.ResponseWriter, r *http.Request) {
var in struct{ Email, Password string }
_ = json.NewDecoder(r.Body).Decode(&in)
if in.Password != f.password {
w.WriteHeader(401)
_ = json.NewEncoder(w).Encode(map[string]any{"error": "email 或密碼錯誤"})
return
}
f.sessionToken = "sess-" + in.Email
_ = json.NewEncoder(w).Encode(map[string]any{
"success": true, "session_token": f.sessionToken, "session_expires_in": 604800,
})
})
// GET /appsroutes/apps.tsX-Arcrun-API-Key
mux.HandleFunc("/apps", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Arcrun-API-Key") != f.apiKey {
w.WriteHeader(401)
_ = json.NewEncoder(w).Encode(map[string]any{"error": "缺少 X-Arcrun-API-Key header"})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"apps": summary, "count": len(summary)})
})
// /portal/data/apps*routes/portal-data.tsBearer portal session
mux.HandleFunc("/portal/data/apps", func(w http.ResponseWriter, r *http.Request) {
if !f.authed(r) {
w.WriteHeader(401)
_ = json.NewEncoder(w).Encode(map[string]any{"error": "session 無效或已過期"})
return
}
if f.listStatus != 0 {
w.WriteHeader(f.listStatus)
_ = json.NewEncoder(w).Encode(map[string]any{"error": "實例內部錯誤"})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"apps": summary, "count": len(summary)})
})
mux.HandleFunc("/portal/data/apps/", func(w http.ResponseWriter, r *http.Request) {
if !f.authed(r) {
w.WriteHeader(401)
_ = json.NewEncoder(w).Encode(map[string]any{"error": "session 無效或已過期"})
return
}
rest := strings.TrimPrefix(r.URL.Path, "/portal/data/apps/")
if strings.HasSuffix(rest, "/action") {
id := strings.TrimSuffix(rest, "/action")
var in struct {
Action string `json:"action"`
Payload map[string]any `json:"payload"`
}
_ = json.NewDecoder(r.Body).Decode(&in)
// 上游 runAppAction:白名單不中 → 403(K6,裁決點在實例不在呼叫端)
if id != "note" || in.Action != "notes-create" {
w.WriteHeader(403)
_ = json.NewEncoder(w).Encode(map[string]any{"error": "這個動作不在這個 App 的白名單內"})
return
}
f.gotAction, f.gotPayload = in.Action, in.Payload
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "result": map[string]any{"id": "blk_1"}})
return
}
if rest != "note" {
w.WriteHeader(404)
_ = json.NewEncoder(w).Encode(map[string]any{"error": "找不到這個 App"})
return
}
// 上游 detailApp 的形狀
_ = json.NewEncoder(w).Encode(map[string]any{
"id": "note", "name": "筆記", "icon": "🗒️", "version": "0.1.0",
"has_ui": true, "ui_html": "<div id=x>hi</div>",
"workflows": []map[string]string{{"name": "notes-create", "description": "寫入一則筆記"}},
"actions": []string{"notes-create"},
})
})
return httptest.NewServer(mux)
}
func (f *fakeInstance) authed(r *http.Request) bool {
tok := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
return f.sessionToken != "" && tok == f.sessionToken
}
// ── 驗收條件 1:清單來自實例,兩條路都通 ────────────────────────────────
func TestListApps_UsesPortalSessionWhenAvailable(t *testing.T) {
tempHome(t)
fi := &fakeInstance{sessionToken: "sess-a", password: "pw", apiKey: "ns-test"}
srv := fi.server(t)
defer srv.Close()
writeCfgWithAccount(t, srv.URL, accountCfg{
InstanceName: "我的庫", Email: "a@b.c", APIKey: "ns-test",
PortalSession: "sess-a", PortalSessionExp: 0,
})
got := (&App{}).ListApps(0)
if got.Error != "" {
t.Fatalf("不該有錯:%s", got.Error)
}
if got.Source != "session" {
t.Fatalf("該走 portal session,實際走 %q", got.Source)
}
if len(got.Apps) != 2 || got.Apps[0].ID != "note" || got.Apps[0].Icon != "🗒️" || !got.Apps[0].HasUI {
t.Fatalf("清單沒有原樣接住實例的回應:%+v", got.Apps)
}
if got.Account != "我的庫" {
t.Fatalf("知識庫名字不對:%q", got.Account)
}
}
func TestListApps_FallsBackToApiKeyWithoutSession(t *testing.T) {
tempHome(t)
fi := &fakeInstance{sessionToken: "", password: "pw", apiKey: "ns-test"}
srv := fi.server(t)
defer srv.Close()
// 完全沒有 session(=剛升級上來、或從沒登入過)——九宮格照樣要看得到真資料
writeCfgWithAccount(t, srv.URL, accountCfg{Email: "a@b.c", APIKey: "ns-test"})
got := (&App{}).ListApps(0)
if got.Error != "" || got.Source != "apikey" || len(got.Apps) != 2 {
t.Fatalf("沒有 session 時該退回 API key 並照樣列出來:%+v", got)
}
}
func TestListApps_ExpiredSessionIsClearedAndListStillWorks(t *testing.T) {
tempHome(t)
// 實例已經不認這張 sessionf.sessionToken 與 config 裡那張不同)
fi := &fakeInstance{sessionToken: "sess-new", password: "pw", apiKey: "ns-test"}
srv := fi.server(t)
defer srv.Close()
writeCfgWithAccount(t, srv.URL, accountCfg{
Email: "a@b.c", APIKey: "ns-test", PortalSession: "sess-old",
})
got := (&App{}).ListApps(0)
if got.Error != "" || got.Source != "apikey" || len(got.Apps) != 2 {
t.Fatalf("session 過期時清單仍該看得到(退回 API key):%+v", got)
}
// 已知無效的 token 要被丟掉,否則下一次還會再撞一次 401
cfg, _ := loadCfg()
if cfg.Accounts[0].PortalSession != "" {
t.Fatalf("過期的 session 沒有被清掉:%q", cfg.Accounts[0].PortalSession)
}
}
// 🔴「問不到」不准偽裝成「一個都沒裝」——實例 5xx 時不准悄悄退回 API key 假裝正常。
func TestListApps_InstanceErrorIsNotDisguisedAsEmpty(t *testing.T) {
tempHome(t)
fi := &fakeInstance{sessionToken: "sess-a", password: "pw", apiKey: "ns-test", listStatus: 500}
srv := fi.server(t)
defer srv.Close()
writeCfgWithAccount(t, srv.URL, accountCfg{Email: "a@b.c", APIKey: "ns-test", PortalSession: "sess-a"})
got := (&App{}).ListApps(0)
if got.Error == "" {
t.Fatalf("實例回 500 卻沒有回報錯誤:%+v", got)
}
if len(got.Apps) != 0 {
t.Fatalf("出錯時不該給半份清單:%+v", got.Apps)
}
}
func TestListApps_UnknownAccountSaysSo(t *testing.T) {
tempHome(t)
writeCfgWithAccount(t, "https://example.invalid", accountCfg{Email: "a@b.c", APIKey: "k"})
if got := (&App{}).ListApps(7); got.Error == "" {
t.Fatalf("越界的索引該回錯,不是空清單")
}
}
// ── 驗收條件 2:點進去能用 ───────────────────────────────────────────────
func TestGetApp_ReturnsDetailWithUI(t *testing.T) {
tempHome(t)
fi := &fakeInstance{sessionToken: "sess-a", password: "pw", apiKey: "ns-test"}
srv := fi.server(t)
defer srv.Close()
writeCfgWithAccount(t, srv.URL, accountCfg{Email: "a@b.c", APIKey: "ns-test", PortalSession: "sess-a"})
d := (&App{}).GetApp(0, "note")
if d.Error != "" || d.NeedsLogin {
t.Fatalf("不該有錯也不該要登入:%+v", d)
}
if !d.HasUI || d.UIHtml != "<div id=x>hi</div>" {
t.Fatalf("自帶畫面沒接住:%+v", d)
}
if len(d.Workflows) != 1 || d.Workflows[0].Name != "notes-create" {
t.Fatalf("工作流清單沒接住:%+v", d.Workflows)
}
if len(d.Actions) != 1 || d.Actions[0] != "notes-create" {
t.Fatalf("動作白名單沒接住:%+v", d.Actions)
}
}
// 沒有 session ⇒ 回 NeedsLogin(要長登入框),不是回一句錯誤。
// 這是刻意的:上游的 API key 面**沒有**詳情端點(routes/apps.ts 只有 install/list/delete),
// 所以「看詳情」在協定上就只有 portal session 那一條路。
func TestGetApp_WithoutSessionAsksForLoginNotError(t *testing.T) {
tempHome(t)
fi := &fakeInstance{sessionToken: "", password: "pw", apiKey: "ns-test"}
srv := fi.server(t)
defer srv.Close()
writeCfgWithAccount(t, srv.URL, accountCfg{Email: "a@b.c", APIKey: "ns-test"})
d := (&App{}).GetApp(0, "note")
if !d.NeedsLogin {
t.Fatalf("沒有 session 時該回 NeedsLogin%+v", d)
}
if d.Error != "" {
t.Fatalf("這不是錯誤,是還差一步:%q", d.Error)
}
if d.Email != "a@b.c" {
t.Fatalf("登入框要能預填帳號:%q", d.Email)
}
}
func TestPortalLogin_StoresSessionNotPassword(t *testing.T) {
tempHome(t)
fi := &fakeInstance{sessionToken: "", password: "pw", apiKey: "ns-test"}
srv := fi.server(t)
defer srv.Close()
writeCfgWithAccount(t, srv.URL, accountCfg{Email: "a@b.c", APIKey: "ns-test"})
if err := (&App{}).PortalLogin(0, "wrong"); err == nil {
t.Fatalf("密碼錯了應該要失敗")
}
if err := (&App{}).PortalLogin(0, "pw"); err != nil {
t.Fatalf("密碼對了卻失敗:%v", err)
}
raw, err := os.ReadFile(configPath())
if err != nil {
t.Fatal(err)
}
// 🔴 密碼零落地:整份 config 裡不准出現密碼字串
if strings.Contains(string(raw), "pw\"") || strings.Contains(string(raw), "\"password\"") {
t.Fatalf("密碼被寫進 config 了:%s", raw)
}
cfg, _ := loadCfg()
if cfg.Accounts[0].PortalSession == "" || cfg.Accounts[0].PortalSessionExp == 0 {
t.Fatalf("session 沒有存起來:%+v", cfg.Accounts[0])
}
// 存了 session 之後,詳情就拿得到了
if d := (&App{}).GetApp(0, "note"); d.NeedsLogin || d.Error != "" {
t.Fatalf("登入之後詳情還是拿不到:%+v", d)
}
}
func TestRunAppAction_HitsInstanceAndCarriesPayload(t *testing.T) {
tempHome(t)
fi := &fakeInstance{sessionToken: "sess-a", password: "pw", apiKey: "ns-test"}
srv := fi.server(t)
defer srv.Close()
writeCfgWithAccount(t, srv.URL, accountCfg{Email: "a@b.c", APIKey: "ns-test", PortalSession: "sess-a"})
out, err := (&App{}).RunAppAction(0, "note", "notes-create", `{"text":"嗨"}`)
if err != nil {
t.Fatalf("動作失敗:%v", err)
}
if !strings.Contains(out, "blk_1") {
t.Fatalf("沒有把實例的結果原樣帶回來:%s", out)
}
if fi.gotAction != "notes-create" || fi.gotPayload["text"] != "嗨" {
t.Fatalf("payload 沒送到:%q %+v", fi.gotAction, fi.gotPayload)
}
}
// 白名單是**實例**裁決的(K6)——桌面端不複製一份判斷,只負責把實例的話原樣說出來。
func TestRunAppAction_WhitelistIsDecidedByInstance(t *testing.T) {
tempHome(t)
fi := &fakeInstance{sessionToken: "sess-a", password: "pw", apiKey: "ns-test"}
srv := fi.server(t)
defer srv.Close()
writeCfgWithAccount(t, srv.URL, accountCfg{Email: "a@b.c", APIKey: "ns-test", PortalSession: "sess-a"})
_, err := (&App{}).RunAppAction(0, "note", "rm-rf", "")
if err == nil {
t.Fatalf("不在白名單的動作該失敗")
}
if !strings.Contains(err.Error(), "白名單") {
t.Fatalf("該把實例的原話帶回來,實際:%v", err)
}
}
// ── 純函式 ──────────────────────────────────────────────────────────────
func TestParseAppList_FallsBackToIdWhenNameMissing(t *testing.T) {
apps := parseAppList([]byte(`{"apps":[{"id":"x","name":"","icon":"","has_ui":false,"version":"1"}]}`))
if len(apps) != 1 || apps[0].Name != "x" {
t.Fatalf("沒有名字時該退回 id%+v", apps)
}
}
func TestUrlPathEscape(t *testing.T) {
for in, want := range map[string]string{
"note": "note",
"a/b": "a%2Fb",
"a b": "a%20b",
"../etc": "..%2Fetc",
"筆記": "%E7%AD%86%E8%A8%98",
} {
if got := urlPathEscape(in); got != want {
t.Fatalf("urlPathEscape(%q)=%q,想要 %q", in, got, want)
}
}
}
+284
View File
@@ -0,0 +1,284 @@
// check-launcher.mjs — App 啟動器的**看得見的驗收**inkstone/arcrun-rag#137
//
// 🔴 leo 2026-08-04:「既然是 Web,你做的時候**無法檢視**?」
// ⇒ 這支就是那句話對 App 啟動器的兌現:真的把 frontend/dist 渲染出來、
// 真的按下去、真的量畫面上有什麼,而不是「看程式碼覺得應該會動」。
//
// 與旁邊 check-render.sh 的分工:
// · check-render.sh 量 CIS 的像素(lockup 接合/深色模式)——只跑得動 macOS 的 Chrome
// · 本檔 驗 App 啟動器的**行為**(九宮格/點進去/sandbox 橋/既有頁沒壞)
// 用 playwright 的 chromiummacOSLinuxCI 都跑得動
//
// 跑法:`node check-launcher.mjs`(沒裝 playwright 就自己跳過,不擋別人的流程)
// 產出:截圖在 /tmp/arcrun-launcher-shots/,每一條檢查印一行 ✅/❌
//
// ⚠️ 這支驗的是**前端這一半**(畫面、互動、sandbox 邊界)。後端那一半
// (真的去問實例、session/API key 兩條路、白名單由實例裁決)在 apps_test.go
// 那裡打的是照上游原始碼寫的假實例。**兩邊都綠才算前後端一體。**
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const here = path.dirname(fileURLToPath(import.meta.url));
const dist = path.join(here, 'frontend', 'dist');
const shots = '/tmp/arcrun-launcher-shots';
// playwright 從哪裡來:本地 node_modules 優先,其次全域安裝(CI/雲端常見)。
// CommonJS 包從 ESM import 進來時,具名匯出可能只在 default 上,兩種都接。
async function loadChromium() {
const tries = ['playwright', '/opt/node22/lib/node_modules/playwright/index.js'];
for (const spec of tries) {
try {
const m = await import(spec);
const c = m.chromium || (m.default && m.default.chromium);
if (c) return c;
} catch { /* 下一個 */ }
}
return null;
}
const chromium = await loadChromium();
if (!chromium) {
console.log('⚠️ 沒有 playwright,跳過畫面驗收(npm i -D playwright && npx playwright install chromium');
process.exit(0);
}
if (!fs.existsSync(path.join(dist, 'index.html'))) {
console.error('❌ 沒有 frontend/dist——先 npm --prefix frontend run build');
process.exit(1);
}
// ── 靜態伺服器(dist 原樣,不改任何一個 byte)──
const MIME = { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.png': 'image/png' };
const srv = http.createServer((req, res) => {
const rel = decodeURIComponent(req.url.split('?')[0]);
const f = path.join(dist, rel === '/' ? 'index.html' : rel);
if (!f.startsWith(dist) || !fs.existsSync(f) || fs.statSync(f).isDirectory()) { res.writeHead(404).end(); return; }
res.writeHead(200, { 'Content-Type': MIME[path.extname(f)] || 'application/octet-stream' });
fs.createReadStream(f).pipe(res);
});
await new Promise((r) => srv.listen(0, '127.0.0.1', r));
const origin = `http://127.0.0.1:${srv.address().port}`;
// ── 假後端:形狀逐欄對齊 apps.go 的 UIAppListUIAppDetail ──
//
// 🔴 這裡**不是**在假裝 Go 那半會怎麼運作(那由 apps_test.go 打真 HTTP 驗)。
// 這裡只回「Go 已經回過來的東西長什麼樣」,讓畫面有東西可畫。
const mock = (scenario) => `
const scenario = ${JSON.stringify(scenario)};
window.__scenario = scenario;
window.__calls = [];
const APPS = [
{ id: 'note', name: '筆記', icon: '🗒️', hasUi: true, version: '0.1.0' },
{ id: 'weekly', name: '週報', icon: '📊', hasUi: false, version: '0.2.0' },
];
const DETAIL = {
note: { id:'note', name:'筆記', icon:'🗒️', version:'0.1.0', hasUi:true,
uiHtml: '<h3 id="t">河道</h3><button id="go">寫一則</button><pre id="out"></pre>' +
'<scr'+'ipt>document.getElementById("go").onclick=async()=>{' +
'const r=await window.arcrunApp.action("notes-create",{text:"嗨"});' +
'document.getElementById("out").textContent=JSON.stringify(r);};' +
'document.title="app-ui-loaded";' +
'window.__peek=(()=>{try{return typeof parent.go}catch(e){return "BLOCKED:"+e.name}})();' +
'</scr'+'ipt>',
workflows:[{name:'notes-create',description:'寫入一則筆記'}], actions:['notes-create'] },
weekly: { id:'weekly', name:'週報', icon:'📊', version:'0.2.0', hasUi:false, uiHtml:'',
workflows:[{name:'weekly-report',description:'每週一產週報'}], actions:['weekly-report'] },
};
const ACCOUNTS = [{
name:'我的知識庫', host:'arcrun-cypher-executor.demo.workers.dev', folders:[{path:'/Users/leo/Notes',accIdx:0}],
cloudVerKnown:true, cloudVerStale:false, cloudVerMine:'1.4.42', email:'leo@example.com',
}];
window.go = { main: { App: {
GetState: async () => ({
version:'v0.18.34', statusBig:'看守中 · 資料夾有變動就會自動整理', statusSub:'上次檢查 10:21',
syncing:false, engine:'workers-ai', geminiKey:'',
accounts: scenario === 'noaccount' ? [] : ACCOUNTS,
steps:[{title:'看守資料夾',meta:'有變動就自動開始',state:'done'},
{title:'發現變化',meta:'上次 10:20',state:'done'},
{title:'用 AI 整理成知識卡',state:'done'},
{title:'上傳到你的知識庫',meta:'上次 3 份',state:'done'}],
skipped:null, engineTrouble:false, logFolder:'/Users/leo/.arcrun-rag',
progress:{total:120,done:117,pending:3,cantSync:0,groups:[]}, quota:null,
}),
ListApps: async (i) => {
window.__calls.push(['ListApps', i]);
if (scenario === 'listerror') return { accIdx:i, account:'我的知識庫', apps:[], error:'連不上這個知識庫——請確認網路正常' };
if (scenario === 'noapps') return { accIdx:i, account:'我的知識庫', apps:[], error:'', source:'session' };
return { accIdx:i, account:'我的知識庫', host:'demo', apps:APPS, error:'', source:'session' };
},
GetApp: async (i, id) => {
window.__calls.push(['GetApp', i, id]);
if (scenario === 'needlogin') return { id, name:'筆記', icon:'🗒️', needsLogin:true, email:'leo@example.com', workflows:[], actions:[] };
return DETAIL[id];
},
RunAppAction: async (i, id, action, payload) => {
window.__calls.push(['RunAppAction', i, id, action, payload]);
if (action !== 'notes-create' && action !== 'weekly-report') throw new Error('這個動作不在這個 App 的白名單內');
return JSON.stringify({ ok:true, result:{ id:'blk_1', echo: JSON.parse(payload||'{}') } });
},
PortalLogin: async (i, pw) => { window.__calls.push(['PortalLogin', i]); if (pw !== 'pw') throw new Error('email 或密碼錯誤'); },
SyncNow: async () => {}, PickFolder: async () => '', AddFolder: async () => {}, RemoveFolder: async () => {},
SetAI: async () => {}, OpenURL: () => { window.__calls.push(['OpenURL']); }, OpenLogFolder: async () => {},
Connect: async () => {}, CheckUpdate: async () => ({ latest:'v0.18.34' }),
DownloadUpdate: async () => ({}), ApplyUpdate: async () => {}, ExportDiagnostics: async () => '',
}}};
`;
let fail = 0;
const ok = (m) => console.log(' ✅ ' + m);
const ng = (m) => { console.log(' ❌ ' + m); fail = 1; };
const check = (cond, m) => (cond ? ok(m) : ng(m));
fs.mkdirSync(shots, { recursive: true });
const browser = await chromium.launch();
async function open(scenario, theme = 'light') {
const ctx = await browser.newContext({ viewport: { width: 1100, height: 760 } });
const page = await ctx.newPage();
await page.addInitScript(`try{localStorage.setItem('arcrun_app_theme','${theme}')}catch(e){}`);
await page.addInitScript(mock(scenario));
const errs = [];
page.on('pageerror', (e) => errs.push(String(e)));
await page.goto(origin);
await page.waitForTimeout(400);
return { ctx, page, errs };
}
console.log('━━━ App 啟動器畫面驗收 ━━━');
// ① 打開就是九宮格,磁磚是真資料(不是寫死的)
{
const { ctx, page, errs } = await open('ok');
await page.waitForSelector('.appgrid', { timeout: 5000 }).catch(() => {});
const tiles = await page.$$eval('.appcell .nm', (n) => n.map((e) => e.textContent.trim()));
check(errs.length === 0, `啟動器沒有 JS 例外${errs.length ? '' + errs[0] + '' : ''}`);
check(await page.$('.appgrid') !== null, '打開就看到九宮格');
check(tiles.join('|') === '筆記|週報|加裝 App', `磁磚=實例回的那兩個 App + 加裝(實際:${tiles.join('|')}`);
check((await page.$eval('.apphead .s', (e) => e.textContent)).includes('2 個 App 已安裝'), '頁首說得出裝了幾個');
const calls = await page.evaluate(() => window.__calls.filter((c) => c[0] === 'ListApps').length);
check(calls === 1, `只問了實例一次,不是每秒輪詢(實際 ${calls} 次)`);
await page.waitForTimeout(2500);
const calls2 = await page.evaluate(() => window.__calls.filter((c) => c[0] === 'ListApps').length);
check(calls2 === 1, `2.5 秒後仍然只問過一次(實際 ${calls2} 次)— 沒有掛在每秒的 tick 上`);
await page.screenshot({ path: `${shots}/01-launcher-light.png` });
await ctx.close();
}
// ①b 深色模式也要對
{
const { ctx, page } = await open('ok', 'dark');
await page.waitForSelector('.appgrid');
await page.screenshot({ path: `${shots}/02-launcher-dark.png` });
check(await page.getAttribute('html', 'data-theme') === 'dark', '深色模式渲染得出來');
await ctx.close();
}
// ② 點一個「有自帶畫面」的 App → 進 sandbox iframe,且橋真的通
{
const { ctx, page, errs } = await open('ok');
await page.waitForSelector('.appgrid');
await page.click('[data-appopen="note"]');
await page.waitForSelector('#appFrame', { timeout: 5000 });
const frame = await (await page.$('#appFrame')).contentFrame();
await frame.waitForSelector('#go', { timeout: 5000 });
check(true, '點「筆記」進得去,App 自帶畫面渲染出來');
// 🔴 sandbox 邊界:iframe 裡的 script 不准碰得到 parent 的 window.go
// (那上面掛著 RemoveFolder(takedown)SetAIConnect
const peek = await frame.evaluate(() => window.__peek);
check(typeof peek === 'string' && peek.startsWith('BLOCKED'), `iframe 碰不到 parent 的 window.go(實際:${peek}`);
// 橋:iframe 按鈕 → postMessage → go.RunAppAction → 回到 iframe
await frame.click('#go');
await frame.waitForFunction(() => document.getElementById('out').textContent.length > 0, null, { timeout: 5000 });
const out = await frame.$eval('#out', (e) => e.textContent);
check(out.includes('blk_1'), `sandbox 橋走得通,結果回得到 App 畫面(實際:${out.slice(0, 80)}`);
const call = await page.evaluate(() => window.__calls.find((c) => c[0] === 'RunAppAction'));
check(call && call[3] === 'notes-create' && JSON.parse(call[4]).text === '嗨', 'payload 原樣送到後端');
check(errs.length === 0, `App 頁沒有 JS 例外${errs.length ? '' + errs[0] + '' : ''}`);
await page.screenshot({ path: `${shots}/03-app-ui.png` });
await ctx.close();
}
// ②b 沒有自帶畫面的 App → 工作流清單 +「現在執行」
{
const { ctx, page } = await open('ok');
await page.waitForSelector('.appgrid');
await page.click('[data-appopen="weekly"]');
await page.waitForSelector('.wfitem', { timeout: 5000 });
check(await page.$eval('.wfitem .nm', (e) => e.textContent) === 'weekly-report', '沒有畫面的 App 列出它的工作流');
await page.click('[data-apprun="weekly-report"]');
await page.waitForFunction(() => document.querySelector('.wfitem .out').textContent.includes('完成'), null, { timeout: 5000 });
check(true, '「現在執行」按得動,結果顯示在那一列底下');
await page.screenshot({ path: `${shots}/04-app-workflows.png` });
await ctx.close();
}
// ③ 三種「不是正常」的狀態都要說人話,且分得出來
{
const { ctx, page } = await open('noapps');
await page.waitForSelector('.appgrid');
const txt = await page.textContent('#page');
check(txt.includes('這個知識庫還沒有 App'), '一個都沒裝 → 說「還沒有 App」並教怎麼裝');
check(!txt.includes('連不上'), '一個都沒裝時不准講成連線失敗');
await page.screenshot({ path: `${shots}/05-empty.png` });
await ctx.close();
}
{
const { ctx, page } = await open('listerror');
await page.waitForTimeout(300);
const txt = await page.textContent('#page');
check(txt.includes('暫時看不到') && txt.includes('連不上'), '問不到 → 說「問不到」並帶實例的原話');
check(!txt.includes('還沒有 App'), '問不到時不准講成「一個都沒裝」');
await page.screenshot({ path: `${shots}/06-error.png` });
await ctx.close();
}
{
const { ctx, page } = await open('noaccount');
await page.waitForTimeout(300);
const txt = await page.textContent('#page');
check(txt.includes('歡迎使用 Arcrun') || txt.includes('還沒有連上知識庫'), '沒連任何實例 → 說人話(連線精靈),不是空白');
await page.screenshot({ path: `${shots}/07-noaccount.png` });
await ctx.close();
}
{
const { ctx, page } = await open('needlogin');
await page.waitForSelector('.appgrid');
await page.click('[data-appopen="note"]');
await page.waitForSelector('#apPw', { timeout: 5000 });
check(await page.inputValue('#apEmail') === 'leo@example.com', 'session 過期 → 長登入框並預填帳號');
await page.screenshot({ path: `${shots}/08-needlogin.png` });
await ctx.close();
}
// ④ 驗收條件 3:現有功能一項都沒少
{
const { ctx, page, errs } = await open('ok');
await page.waitForSelector('.appgrid');
const navs = await page.$$eval('#side .nav .nm', (n) => n.map((e) => e.textContent.trim()));
for (const want of ['App 界面', '首頁', '我的知識庫', 'AI 設定', '版本與更新']) {
check(navs.includes(want), `側欄仍有「${want}`);
}
const pages = [
['home', '現在的狀態'],
['lib:0', '開啟知識庫網頁'],
['ai', '用哪個 AI 幫你整理文件'],
['update', '版本與更新'],
];
for (const [p, marker] of pages) {
await page.click(`#side .nav[data-p="${p}"]`);
await page.waitForTimeout(200);
const t = await page.textContent('#page');
check(t.includes(marker), `${p}」頁還在(找得到「${marker}」)`);
}
// 全站頁首的同步狀態與「立刻同步」在每一頁都看得到(首頁換走了它們也沒消失)
check((await page.textContent('#statusBig')).includes('看守中'), '同步狀態在全站頁首,換頁不會不見');
check(await page.$('#btnSync') !== null, '「立刻同步」按鈕還在');
check(errs.length === 0, `逛完全部頁面沒有 JS 例外${errs.length ? '' + errs[0] + '' : ''}`);
await page.screenshot({ path: `${shots}/09-existing-update.png` });
await ctx.close();
}
await browser.close();
srv.close();
console.log(`\n截圖:${shots}`);
console.log(fail ? '\n❌ 畫面驗收未過' : '\n✅ 畫面驗收全過');
process.exit(fail);
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env bash
# check-remove-cleanup.sh — 驗**使用者真的會走的那條路**:
# 從各庫頁按「移除」→ 對話框 → 勾「順便清掉」→ 看到清單 → 按確定(arcrun-rag#138)。
#
# 🔴 為什麼要這一支:Go 那邊的迴歸網只證明「函式做對事」,證明不了
# 「按鈕存在、清單真的攤出來、第四個參數真的傳下去」。#138 的驗收條件裡
# 「使用者要能在動手前看到將要刪掉哪些東西」**只有前端驗得到**。
# (同 check-render.sh 的理由:改完 UI 不准拿沒看過的畫面交件。)
#
# 做法沿用 check-render.sh:把 dist 複製到暫存、插一支 mock.js 假扮 window.go
# 再用 headless Chrome 真的載入、真的點下去,最後 --dump-dom 讀結果。
set -uo pipefail
cd "$(dirname "$0")"
CHROME="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
[ -x "$CHROME" ] || { echo "❌ 找不到 Chrome,跳過"; exit 0; }
[ -d frontend/dist ] || { echo "❌ 沒有 frontend/dist——先 (cd frontend && npm run build)"; exit 1; }
W=$(mktemp -d)
SRV_PID=""
cleanup() { rm -rf "$W"; [ -n "$SRV_PID" ] && kill "$SRV_PID" 2>/dev/null; }
trap cleanup EXIT
lsof -ti tcp:8798 2>/dev/null | xargs -r kill 2>/dev/null; sleep 0.3
cp -R frontend/dist/* "$W/"
cat > "$W/mock.js" <<'JS'
// 假的 window.go:一個知識庫、一個看守中的資料夾,PlanFolderCleanup 回一份
// 形狀與 collector.CleanupPlan 完全一致的清單(含「留著不動」那一半)。
window.__calls = [];
window.go = { main: { App: {
GetState: async () => ({
version: 'v0', statusBig: '看守中', statusSub: '', syncing: false,
engine: 'workers-ai', geminiKey: '', extractedOK: 0,
accounts: [{ name: '測試庫', host: 'arcrun-cypher-executor.example.workers.dev',
folders: [{ path: '/kb/pms', accIdx: 0, retiring: false }] }],
}),
PlanFolderCleanup: async (acc, path) => {
window.__calls.push(['PlanFolderCleanup', acc, path]);
return {
root: path, files: 52, bytes: 1234,
remove: [
{ rel: '.arcrun-rag', is_dir: true, kind: 'workspace', evidence: '依據甲', files: 1, bytes: 10 },
{ rel: '.wiki', is_dir: true, kind: 'wiki-dir', evidence: '依據乙', files: 8, bytes: 100 },
{ rel: 'docs/.wiki', is_dir: true, kind: 'wiki-dir', evidence: '依據乙', files: 43, bytes: 900 },
],
keep: [{ rel: 'pms_v1_legacy', reason: '這個資料夾也還在同步清單裡' }],
};
},
RemoveFolder: async (acc, path, takedown, cleanupLocal) => {
window.__calls.push(['RemoveFolder', acc, path, takedown, cleanupLocal]);
},
SyncNow: async () => {}, PickFolder: async () => '', AddFolder: async () => {},
SetAI: async () => {}, OpenURL: () => {}, Connect: async () => {},
CheckUpdate: async () => ({}), OpenLogFolder: () => {}, LogFolderPath: () => '',
ListApps: async () => ({ apps: [] }),
} } };
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const say = (k, v) => {
const d = document.createElement('div');
d.setAttribute('data-check', k); d.textContent = String(v);
document.body.appendChild(d);
};
addEventListener('DOMContentLoaded', async () => {
await sleep(600); // 等 main.js 拿到 state、畫完側欄
// 走到各庫頁(使用者點側邊欄那個庫)
const nav = [...document.querySelectorAll('.nav')].find((b) => (b.dataset.p || '').startsWith('lib:'));
if (nav) nav.click(); else location.hash = '';
await sleep(300);
const rm = document.querySelector('[data-rm]');
say('有移除按鈕', !!rm);
if (!rm) { say('done', 1); return; }
rm.click();
await sleep(200);
const box = document.getElementById('rmClean');
say('有清理勾選框', !!box);
say('預設不勾', box ? box.checked === false : false);
if (!box) { say('done', 1); return; }
box.checked = true; box.dispatchEvent(new Event('change'));
await sleep(400);
const planTxt = (document.getElementById('rmPlan') || {}).textContent || '';
say('清單有列出要刪的路徑', planTxt.includes('.arcrun-rag') && planTxt.includes('docs/.wiki'));
say('清單有列出留著不動的', planTxt.includes('pms_v1_legacy'));
say('清單有講幾個檔', planTxt.includes('52'));
document.getElementById('c2').click();
await sleep(400);
const call = window.__calls.find((c) => c[0] === 'RemoveFolder');
say('有呼叫 RemoveFolder', !!call);
say('第四個參數帶了 true', call ? call[4] === true : false);
say('路徑正確', call ? call[2] === '/kb/pms' : false);
say('done', 1);
});
JS
python3 - "$W" <<'PY'
import sys
p = sys.argv[1] + '/index.html'
s = open(p, encoding='utf-8').read()
s = s.replace('src="/assets/', 'src="./assets/').replace('href="/assets/', 'href="./assets/')
s = s.replace('<script type="module"', '<script src="./mock.js"></script>\n<script type="module"', 1)
open(p, 'w', encoding='utf-8').write(s)
PY
(cd "$W" && python3 -m http.server 8798 >/dev/null 2>&1) &
SRV_PID=$!
sleep 2
"$CHROME" --headless --disable-gpu --virtual-time-budget=8000 --dump-dom \
"http://localhost:8798/index.html" 2>/dev/null > "$W/dom.html"
python3 - "$W/dom.html" <<'PY'
import re, sys
dom = open(sys.argv[1], encoding='utf-8').read()
got = dict(re.findall(r'<div data-check="([^"]+)">([^<]*)</div>', dom))
if not got:
print(" ❌ 頁面沒跑到底(一個檢查點都沒寫出來)——看 dom.html"); sys.exit(1)
bad = 0
for k, v in got.items():
if k == 'done':
continue
ok = v == 'true'
print(f" {'✅' if ok else '❌'} {k}{v}")
bad += 0 if ok else 1
if 'done' not in got:
print(" ❌ 驅動腳本沒跑完"); sys.exit(1)
sys.exit(1 if bad else 0)
PY
+6
View File
@@ -104,6 +104,10 @@ func (a *App) Connect(portalURL, email, password string) error {
cfg.Accounts[i].APIKey = r.Config.Namespace
cfg.Accounts[i].Email = r.Config.Email
cfg.Accounts[i].InstanceName = r.Config.InstanceName
// arcrun-rag#137:密碼**此刻**還在手上,順手換一張 portal session
// 之後 App 啟動器要看詳情/按動作就不必再問一次密碼。
// 換不到不算連線失敗(同步這條主線用不到它),見 apps.go。
tryStoreSessionFromLogin(cfg, i, r.Config.Email, password)
return saveCfg(cfg)
}
}
@@ -117,6 +121,8 @@ func (a *App) Connect(portalURL, email, password string) error {
Namespace: r.Config.Namespace,
APIKey: r.Config.Namespace,
})
// arcrun-rag#137:同上,順手換一張 portal session 給 App 啟動器用。
tryStoreSessionFromLogin(cfg, len(cfg.Accounts)-1, r.Config.Email, password)
if wasFirstEver {
// leo 2026-08-08:全新安裝自動裝一個「可刪除的預設庫」,讓使用者不必
// 自己選資料夾也能立刻看到「丟檔 → 知識卡 → 搜得到」整條路(見 default_library.go)。
+435 -5
View File
@@ -27,10 +27,28 @@ $('themeBtn').onclick = () =>
applyTheme(document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark');
let state = null;
let page = 'home'; // 'home' | 'ai' | 'update' | 'lib:<idx>'
// page'apps'App 啟動器)| 'app:<accIdx>:<id>' | 'home' | 'ai' | 'update' | 'lib:<idx>'
//
// 🔴 arcrun-rag#137:預設落在 App 啟動器,不是首頁——leo 2026-08-24 的原話是
// 「**打開桌面小幫手就看到**跟 Portal 同一套的 App 啟動器」。
// 同步狀態沒有因此消失:它在**全站頁首**statusBig/statusSub+「立刻同步」),
// 每一頁都看得到;首頁那些卡片仍在側欄的「首頁」裡,一鍵可達。
// 還沒連任何知識庫時 render() 會把它改回 'home'(那裡才是連線精靈)。
let page = 'apps';
let updateInfo = null;
let obStep = 1; // 首次啟動精靈目前在第幾步(issue #23,見 onboarding()
// ── App 啟動器的暫存(arcrun-rag#137)──
//
// 🔴 這是**畫面暫存,不是本機清單**:只活在這個視窗的記憶體裡,關掉就沒了,
// 永遠不寫檔。上游 inkstone/Arcrun#82 已定「安裝態只有一份真相源」=實例上那一份,
// 桌面端不准另存一份(本票紅線)。存在這裡只是為了不要每次換頁都重打一次網路。
let appsAccIdx = 0; // 啟動器現在在看哪一個知識庫
let appsCache = {}; // accIdx -> ListApps() 的回傳(undefined=還沒問,null=正在問)
let appDetail = null; // 目前打開的那個 App 的詳情(GetApp() 的回傳)
let appDetailKey = ''; // 'accIdx:id',避免慢回應蓋掉已經換過去的另一個 App
let appFrameBridge = null; // App 自帶畫面那個 iframe 的 postMessage 監聽器(換頁時要拆掉)
// ── 覆蓋層(只給必須打斷的確認)──
function openSheet(html, wire) { $('sheet').innerHTML = html; $('overlay').classList.add('on'); if (wire) wire(); }
function closeSheet() { $('overlay').classList.remove('on'); }
@@ -50,7 +68,21 @@ document.addEventListener('keydown', (e) => { if (e.key === 'Escape') closeSheet
// 之後若那個提案 confirm,是在這層之上疊總覽 pill,不是重做這裡。
function renderNav() {
const accs = (state && state.accounts) || [];
// arcrun-rag#137:「App」這一段在最上面,且**只有連了知識庫才出現**——
// 沒有實例就沒有 App,把一個必定空的入口擺在第一項只會讓人以為壞了。
// 目前打開的那個 App 以子項的形式掛在「App 界面」下面(同 Portal 的做法:
// 已安裝的 App 是側欄的一格),這樣使用者知道自己在哪、也回得去。
const appNav = !accs.length ? '' : `
<div class="sec">App</div>
<div class="nav" data-p="apps"><span class="ic"></span><span class="nm">App </span></div>
${page.startsWith('app:') && appDetail && !appDetail.error ? `
<div class="nav" data-p="${esc(page)}" style="padding-left:44px">
<span class="ic">${esc(appDetail.icon || '▢')}</span>
<span class="nm">${esc(appDetail.name || appDetail.id)}</span>
</div>` : ''}
<div class="sec">小幫手</div>`;
$('nav').innerHTML = `
${appNav}
<div class="nav" data-p="home"><span class="ic"></span><span class="nm"></span></div>
${accs.length ? `<div class="sec">知識庫</div>` : ''}
${accs.map((a, i) => `
@@ -64,7 +96,17 @@ function renderNav() {
<div class="nav" data-p="update"><span class="ic"></span><span class="nm"></span></div>`;
$('nav').querySelectorAll('.nav').forEach((el) => {
el.classList.toggle('on', el.dataset.p === page);
el.onclick = () => { page = el.dataset.p; renderNav(); renderPage(); };
el.onclick = () => {
const p = el.dataset.p;
if (p === page) return;
// 離開 App 頁時把 iframe 的橋拆掉(見 goToApp 同一段理由)。
if (page.startsWith('app:') && appFrameBridge) {
window.removeEventListener('message', appFrameBridge); appFrameBridge = null;
}
page = p;
renderNav(); renderPage();
if (p === 'apps') loadApps(appsAccIdx);
};
});
}
@@ -411,10 +453,260 @@ function onboarding() {
</div>`;
}
// ═══════════════════════════════════════════════════════════════════════════
// App 啟動器(arcrun-rag#137
// ═══════════════════════════════════════════════════════════════════════════
//
// leo 2026-08-24:「所有的 App 需要有一個類似 Android/iOS 的九宮格啟動界面,
// 每個 App 有一個 icon**這會運行在 portal 及 daemon**。」
//
// 🔴 清單一律問實例,桌面端沒有任何寫死或存檔的 App 名單(本票紅線;
// 上游 inkstone/Arcrun#82「安裝態只有一份真相源」)。後端兩條取得路徑
// 與為什麼是兩條,全寫在 collector/cmd/arcrun-app/apps.go 的檔頭。
//
// 🔴 **不掛在每秒的 tick 上**:只有「打開啟動器」「按重新整理」「切知識庫」
// 這三個使用者動作會真的去問實例一次。把它塞進 tick 等於自造輪詢器。
// loadApps 去問某個知識庫裝了哪些 App;問完只在「使用者還停在啟動器」時重畫。
async function loadApps(accIdx, force) {
if (!force && appsCache[accIdx] !== undefined) return;
appsCache[accIdx] = null; // null=問中(畫面顯示「查詢中」)
if (page === 'apps') renderPage();
let res;
try {
res = await go.ListApps(accIdx);
} catch (ex) {
res = { accIdx, apps: [], error: String(ex) };
}
appsCache[accIdx] = res;
if (page === 'apps') renderPage();
}
function pageApps(s) {
const accs = s.accounts || [];
if (!accs.length) {
// 「沒連任何實例時要說人話」(驗收條件 1)——不是空白,也不是壞掉的樣子。
return `<div class="empty">
<div class="t">還沒有連上知識庫</div>
<div class="d">App 住在你的知識庫上連上之後這裡就會列出它裝了哪些 App</div>
<button class="primary" id="apConnect">連上知識庫</button>
</div>`;
}
if (appsAccIdx >= accs.length) appsAccIdx = 0;
const acc = accs[appsAccIdx];
const r = appsCache[appsAccIdx];
// 知識庫切換器:只有一個庫時不畫(一顆永遠只能按自己的按鈕是純噪音)。
const switcher = accs.length > 1 ? `
<div class="appswitch">
${accs.map((a, i) => `<span class="chip ${i === appsAccIdx ? 'on' : ''}" data-appacc="${i}">${esc(a.name)}</span>`).join('')}
</div>` : '';
let sub = '查詢中…';
let body = `<div class="card"><div class="d">正在問「${esc(acc.name)}」裝了哪些 App…</div></div>`;
if (r) {
if (r.error) {
// 🔴 「問不到」與「一個都沒裝」是兩件事,畫面上必須分得出來
// (使用者該做的事完全相反:一個是修連線,一個是去裝 App)。
sub = '這次沒問到';
body = `<div class="card">
<h3>暫時看不到這個知識庫的 App</h3>
<div class="d">${esc(r.error)}</div>
<div class="acts"><button id="apRetry">再試一次</button></div>
</div>`;
} else {
const apps = r.apps || [];
sub = `${apps.length} 個 App 已安裝`;
body = `<div class="appgrid">
${apps.map((a) => `
<div class="appcell">
<div class="apptile" data-appopen="${esc(a.id)}" title="${esc(a.name)}${a.version ? ' · v' + esc(a.version) : ''}">${esc(a.icon || '▢')}</div>
<div class="nm" title="${esc(a.name)}">${esc(a.name)}</div>
</div>`).join('')}
<div class="appcell">
<div class="apptile add" id="apAdd" title="怎麼加裝 App"></div>
<div class="nm dim">加裝 App</div>
</div>
</div>
${apps.length ? '' : `<div class="card" style="margin-top:26px">
<h3>這個知識庫還沒有 App</h3>
<div class="d">App 是裝在知識庫上的跟你的 AI 說一句幫我裝一個 X
或用 <b>acr</b> App </div>
</div>`}`;
}
}
return `
<div class="apphead">
<div class="g">
<div class="t">App 界面</div>
<div class="s">${esc(acc.name)} · ${esc(sub)}</div>
</div>
<button id="apRefresh">重新整理</button>
</div>
${switcher}
${body}`;
}
// ── 單一 App 的頁 ─────────────────────────────────────────────────────────
async function loadAppDetail(accIdx, id) {
const key = accIdx + ':' + id;
appDetailKey = key;
appDetail = null;
renderPage();
let d;
try {
d = await go.GetApp(accIdx, id);
} catch (ex) {
d = { id, error: String(ex) };
}
if (appDetailKey !== key) return; // 使用者已經換去別的 App 了,這份回應作廢
appDetail = d;
renderNav(); // 側欄的 App 子項要顯示名字與 icon
renderPage();
}
function pageApp(accIdx, id) {
const d = appDetail;
const head = (ico, nm, ver) => `
<div class="head">
<span class="ico">${esc(ico || '▢')}</span>
<span class="nm">${esc(nm || id)}</span>
${ver ? `<span class="vr">v${esc(ver)}</span>` : ''}
<span class="sp"></span>
<button data-appback="1"> App 界面</button>
</div>`;
if (!d) return `<div class="appview">${head('', id, '')}<div class="card"><div class="d">載入中…</div></div></div>`;
if (d.needsLogin) {
// session 過期/這台機器還沒換過 session。不是錯誤,是「還差一步」。
return `<div class="appview">${head(d.icon, d.name, d.version)}
<div class="card">
<h3>請先登入這個知識庫</h3>
<div class="d">要打開 App 的畫面或執行它的動作需要你在這個知識庫的帳號登入一次
之後這台電腦會記住一段時間同步不受影響</div>
<div class="field"><div class="lb">帳號</div>
<input type="text" id="apEmail" value="${esc(d.email || '')}" disabled/></div>
<div class="field"><div class="lb">密碼</div><input type="password" id="apPw"/></div>
<div class="err" id="apErr" style="display:none"></div>
<div class="acts"><button class="primary" id="apLogin">登入</button></div>
</div></div>`;
}
if (d.error) {
return `<div class="appview">${head(d.icon, d.name, d.version)}
<div class="card">
<h3>打不開這個 App</h3>
<div class="d">${esc(d.error)}</div>
<div class="acts"><button id="apReload">再試一次</button></div>
</div></div>`;
}
if (d.hasUi && d.uiHtml) {
// 自帶畫面 ⇒ 掛進 sandbox iframemountAppUI 會在 wire() 之後填內容)。
return `<div class="appview">${head(d.icon, d.name, d.version)}
<iframe class="appframe" id="appFrame" sandbox="allow-scripts"></iframe></div>`;
}
// 沒有自帶畫面 ⇒ 列出工作流,一條一顆「現在執行」
//(與 Portal 的系統預設畫面同一套,不另立第二種呈現)。
const wfs = d.workflows || [];
if (!wfs.length) {
return `<div class="appview">${head(d.icon, d.name, d.version)}
<div class="card"><h3>這個 App 沒有可以按的東西</h3>
<div class="d">它既沒有自己的畫面也沒有登記任何工作流</div></div></div>`;
}
return `<div class="appview">${head(d.icon, d.name, d.version)}
${wfs.map((w) => `
<div class="wfitem" data-wf="${esc(w.name)}">
<div class="top">
<span class="nm">${esc(w.name)}</span>
<button data-apprun="${esc(w.name)}">現在執行</button>
</div>
${w.description ? `<div class="d">${esc(w.description)}</div>` : ''}
<div class="out"></div>
</div>`).join('')}`;
}
// mountAppUI 把 App 自帶的 HTML 放進 **sandbox iframe**。
//
// 🔴 為什麼一定要 iframe(這是桌面端與 Portal 的關鍵差異,不是潔癖):
// Portal 是網頁,把 App 的 HTML 直接 innerHTML 進去,那段 script 最多拿到
// 同一頁的 fetch 與 session token。**桌面這半不一樣**——這裡的 window 上掛著
// `window.go.main.App``Connect``SetAI``RemoveFolder(…, takedown=true, cleanupLocal=true)`
// 全都在上面。直接 innerHTML = 任何一個 App 的作者都能刪掉使用者雲端的知識
// ——#138 之後**連他硬碟上的整理稿也刪得掉**,這道窄門只會越來越重要。
// ⇒ `sandbox="allow-scripts"`**不給** allow-same-origin ⇒ 不同源,
// 碰不到 parent 的任何東西),只留一條 postMessage 的窄門。
//
// 🔴 窄門的形狀刻意與 Portal 一致:App 作者一樣只認得
// `window.arcrunApp.action(action, payload)`,回一個 `{ok,status,d}`——
// 這樣同一個 App 的畫面在 Portal 與桌面上都跑得起來,作者不必寫兩份。
function mountAppUI(accIdx, d) {
const f = $('appFrame');
if (!f) return;
const bridge = `
<script>
(function () {
var seq = 0, pending = {};
window.addEventListener('message', function (e) {
var m = e.data;
if (!m || m.__arcrun !== 'result') return;
var p = pending[m.id]; if (!p) return; delete pending[m.id];
p(m.payload);
});
window.arcrunApp = {
action: function (action, payload) {
return new Promise(function (resolve) {
var id = ++seq; pending[id] = resolve;
parent.postMessage({ __arcrun: 'action', id: id, action: action, payload: payload || {} }, '*');
});
}
};
})();
<\/script>`;
// 讓 App 的畫面跟本體同一套底色/字體(它是 Arcrun 的一部分,不是外站)。
const skin = `<style>
:root{color-scheme:${document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'light'}}
html,body{margin:0;padding:16px;background:transparent;
color:${getComputedStyle(document.documentElement).getPropertyValue('--ink').trim() || '#17181A'};
font-family:-apple-system,"IBM Plex Sans","PingFang TC","Noto Sans TC","Microsoft JhengHei",system-ui,sans-serif;
font-size:15px}
</style>`;
f.srcdoc = '<!doctype html><meta charset="utf-8">' + skin + bridge + d.uiHtml;
f.onload = () => {
if (appFrameBridge) window.removeEventListener('message', appFrameBridge);
appFrameBridge = async (ev) => {
if (!f.contentWindow || ev.source !== f.contentWindow) return; // 只認自己這個 iframe
const m = ev.data;
if (!m || m.__arcrun !== 'action') return;
const reply = (payload) =>
f.contentWindow && f.contentWindow.postMessage({ __arcrun: 'result', id: m.id, payload }, '*');
try {
const raw = await go.RunAppAction(accIdx, d.id, String(m.action || ''), JSON.stringify(m.payload || {}));
let parsed = null;
try { parsed = JSON.parse(raw); } catch (e) { parsed = { ok: true, result: raw }; }
reply({ ok: true, status: 200, d: parsed });
} catch (ex) {
reply({ ok: false, status: 0, d: { error: String(ex) } });
}
};
window.addEventListener('message', appFrameBridge);
};
}
function renderPage() {
if (!state) return;
let html;
if (page.startsWith('lib:')) html = pageLib(state, Number(page.slice(4)));
if (page === 'apps') html = pageApps(state);
else if (page.startsWith('app:')) {
const p = page.split(':');
html = pageApp(Number(p[1]), p.slice(2).join(':'));
}
else if (page.startsWith('lib:')) html = pageLib(state, Number(page.slice(4)));
else if (page === 'ai') html = pageAI(state);
else if (page === 'update') html = pageUpdate(state);
else html = pageHome(state);
@@ -447,12 +739,105 @@ function wire() {
document.querySelectorAll('[data-rm]').forEach((b) => {
b.onclick = () => confirmRemove(Number(b.dataset.acc), b.dataset.rm);
});
// ── App 啟動器(arcrun-rag#137)──
on('apConnect', showConnect);
on('apRefresh', () => loadApps(appsAccIdx, true));
on('apRetry', () => loadApps(appsAccIdx, true));
on('apAdd', showHowToInstallApp);
on('apReload', () => { const p = page.split(':'); loadAppDetail(Number(p[1]), p.slice(2).join(':')); });
on('apLogin', appLogin);
document.querySelectorAll('[data-appacc]').forEach((b) => {
b.onclick = () => { appsAccIdx = Number(b.dataset.appacc); renderPage(); loadApps(appsAccIdx); };
});
document.querySelectorAll('[data-appopen]').forEach((b) => {
b.onclick = () => goToApp(appsAccIdx, b.dataset.appopen);
});
document.querySelectorAll('[data-appback]').forEach((b) => {
b.onclick = () => { page = 'apps'; appDetail = null; appDetailKey = ''; renderNav(); renderPage(); };
});
document.querySelectorAll('[data-apprun]').forEach((b) => { b.onclick = () => runAppAction(b); });
// App 自帶畫面:DOM 換好之後才掛 iframe(srcdoc 要等元素真的在文件裡)
if (page.startsWith('app:') && appDetail && appDetail.hasUi && appDetail.uiHtml) {
mountAppUI(Number(page.split(':')[1]), appDetail);
}
}
// goToApp 換到某個 App 的頁。換頁前先把上一個 App 的 postMessage 監聽器拆掉——
// 不拆的話每開一次 App 就多留一個死監聽器(而且它還綁著舊的 accIdx/appId)。
function goToApp(accIdx, id) {
if (appFrameBridge) { window.removeEventListener('message', appFrameBridge); appFrameBridge = null; }
page = 'app:' + accIdx + ':' + id;
renderNav();
loadAppDetail(accIdx, id);
}
// 「加裝 App」:桌面端**不假裝自己能安裝**——安裝是實例上的動作
// (跟 AI 說一句話,或 acr 推一份宣告)。這裡只把「東西從哪來」講清楚,
// 順便給一個開知識庫網頁的出口。
function showHowToInstallApp() {
const acc = (state.accounts || [])[appsAccIdx];
const portal = acc ? 'https://' + acc.host.replace('arcrun-cypher-executor.', 'arcrun-rag-ui.') + '/portal/' : '';
openSheet(`
<h2>怎麼加裝 App</h2>
<p>App 是裝在<b>知識庫</b>
或在知識庫網頁上看到的都是同一批 App</p>
<p>兩種裝法跟你的 AI 幫我裝一個 或用 <b>acr</b> App
裝好之後回到這裡按重新整理就會出現</p>
<div class="acts">
<button id="c1">知道了</button>
${portal ? `<button class="primary" id="c2">開啟知識庫網頁</button>` : ''}
</div>`,
() => {
$('c1').onclick = closeSheet;
if ($('c2')) $('c2').onclick = () => { go.OpenURL(portal); closeSheet(); };
});
}
async function appLogin() {
const accIdx = Number(page.split(':')[1]);
const id = page.split(':').slice(2).join(':');
const err = $('apErr');
const btn = $('apLogin');
if (btn) btn.disabled = true;
try {
await go.PortalLogin(accIdx, $('apPw').value);
if (btn) btn.disabled = false;
loadAppDetail(accIdx, id);
} catch (ex) {
if (btn) btn.disabled = false;
if (err) { err.textContent = String(ex); err.style.display = 'block'; }
}
}
// runAppAction:沒有自帶畫面的 App,那顆「現在執行」。
// 🔴 白名單是**實例**裁決的(K6)——這裡不認得任何動作名稱,只負責把按鈕送出去、
// 把實例回的話原樣顯示。失敗就說失敗,不改寫成「可能成功」。
async function runAppAction(btn) {
const accIdx = Number(page.split(':')[1]);
const id = page.split(':').slice(2).join(':');
const item = btn.closest('.wfitem');
const out = item && item.querySelector('.out');
btn.disabled = true;
if (out) { out.className = 'out'; out.textContent = '執行中…'; }
try {
const raw = await go.RunAppAction(accIdx, id, btn.dataset.apprun, '{}');
if (out) out.textContent = '完成:' + String(raw).slice(0, 600);
} catch (ex) {
if (out) { out.className = 'out bad'; out.textContent = '失敗:' + String(ex); }
}
btn.disabled = false;
}
function render(s) {
const first = !state;
const navChanged = state && JSON.stringify((state.accounts||[]).map(a=>[a.name,(a.folders||[]).length]))
!== JSON.stringify((s.accounts||[]).map(a=>[a.name,(a.folders||[]).length]));
// arcrun-rag#137:還沒連上任何知識庫時,第一眼要落在連線精靈(首頁),
// 不是一個註定空的 App 啟動器。連上之後(accounts 從 0 變成 1)也不要硬把
// 使用者拉走——他當下正在看剛連好的東西。
if (first && page === 'apps' && !(s.accounts || []).length) page = 'home';
state = s;
$('ver').textContent = s.version || '';
$('statusBig').textContent = s.statusBig;
@@ -460,6 +845,9 @@ function render(s) {
$('statusSub').textContent = s.statusSub;
if (first || navChanged) { renderNav(); renderPage(); }
else if (page === 'home') renderPage(); // 首頁的狀態時間軸要跟著跳
// 🔴 這是**唯一**一次自動去問實例:第一次拿到 state(=知道有哪些知識庫)之後。
// 之後只有使用者按重新整理/切知識庫才會再問一次——**不掛在每秒的 tick 上**。
if (first && page === 'apps' && (s.accounts || []).length) loadApps(appsAccIdx);
}
async function tick() { try { render(await go.GetState()); } catch (e) {} }
@@ -481,28 +869,70 @@ async function addFolder(accIdx) {
// 那句話**在技術上是對的**,但它預設使用者要的是「只停止同步」,
// 而他要的是「我不要這份資料了」。⇒ 病不在少一句說明,在**替他決定了**。
// 現在兩個選擇都擺出來、後果各寫一行,由他挑。
// 🔴 arcrun-rag#138leo 2026-08-24):「碎型會在每個資料夾安裝隱藏資料夾,人工刪除不容易,
// 所以當它斷連,應該要可以幫它把 Arcrun RAG 建立的資料夾刪掉」
// ⇒ 多一個**獨立的勾選框**,不是第三顆單選:雲端怎麼處理、硬碟怎麼處理是兩件事,
// 合成一個選項就又是替他決定(#46 修掉的正是那個病)。
// 🔴 預設不勾——刪檔不可逆,預設值往「什麼都不動」倒。
// 🔴 勾了才去問清單,並且把**每一筆路徑攤出來**:#138 的驗收條件白紙黑字寫著
// 「使用者要能在動手前看到將要刪掉哪些東西」,按下去就無聲刪光不算做完。
function confirmRemove(accIdx, path) {
openSheet(`
<h2>移除這個資料夾</h2>
<p>${esc(path)}要怎麼處理<b>電腦裡的原始檔案不會被動到</b></p>
<p>${esc(path)}要怎麼處理<b>自己的檔案不會被動到</b></p>
<label class="radio"><input type="radio" name="rmMode" value="takedown" checked/>
<span><b>連同雲端的知識一起收回</b><br/>
<span class="d">這個資料夾整理出來的知識會從知識庫刪除之後搜尋找不到AI 也不會再拿它回答<b>刪掉就要不回來</b></span></span></label>
<label class="radio"><input type="radio" name="rmMode" value="unwatch"/>
<span><b>只停止同步雲端的知識保留</b><br/>
<span class="d">以後這個資料夾有變動不會再上傳但之前整理好的知識留在知識庫裡搜尋和 AI 照樣找得到</span></span></label>
<label class="radio"><input type="checkbox" id="rmClean"/>
<span><b>順便把 Arcrun RAG 放在這個資料夾裡的檔案清掉</b><br/>
<span class="d">我們會在每一層資料夾放一個隱藏的整理稿目錄<code>.wiki</code><code>.arcrun-rag</code>
散在各層你自己很難刪乾淨勾起來會先列出<b>確切要刪哪些</b></span></span></label>
<div id="rmPlan" class="d" style="display:none;margin:8px 0 4px"></div>
<div class="acts"><button id="c1">取消</button><button class="primary" id="c2"></button></div>`,
() => {
$('c1').onclick = closeSheet;
const box = $('rmClean'), out = $('rmPlan');
box.onchange = async () => {
if (!box.checked) { out.style.display = 'none'; out.innerHTML = ''; return; }
out.style.display = ''; out.textContent = '正在看這個資料夾裡有哪些是我們建的…';
try {
out.innerHTML = renderCleanupPlan(await go.PlanFolderCleanup(accIdx, path));
} catch (e) {
out.textContent = '看不到清單(' + e + ')——沒把握就先別勾這一項。';
}
};
$('c2').onclick = async () => {
const mode = document.querySelector('input[name="rmMode"]:checked');
const takedown = !mode || mode.value === 'takedown';
await go.RemoveFolder(accIdx, path, takedown); closeSheet();
await go.RemoveFolder(accIdx, path, takedown, box.checked); closeSheet();
state = await go.GetState(); renderNav(); renderPage();
};
});
}
// renderCleanupPlan 把「將要刪掉什麼/刻意留下什麼」攤成使用者看得懂的清單。
// 🔴 留下的那一半一樣要顯示:沉默地留下殘渣,跟沉默地刪掉一樣糟——他要的是
// 「這個資料夾乾淨了」,那就得讓他看得到還有什麼沒清、為什麼沒清。
function renderCleanupPlan(plan) {
const rm = (plan && plan.remove) || [], keep = (plan && plan.keep) || [];
if (!rm.length && !keep.length) return '這個資料夾裡沒有找到任何 Arcrun RAG 建立的檔案,不需要清理。';
let h = '';
if (rm.length) {
h += `<b>會刪掉這 ${rm.length} 項(共 ${plan.files} 個檔):</b><ul style="margin:4px 0 0 16px">`;
for (const it of rm) h += `<li>${esc(it.rel)}${it.is_dir ? '' : ''}${it.files} 個檔)</li>`;
h += '</ul>';
}
if (keep.length) {
h += `<b style="display:block;margin-top:8px">這 ${keep.length} 項我不會動:</b><ul style="margin:4px 0 0 16px">`;
for (const k of keep) h += `<li>${esc(k.rel)}${esc(k.reason)}</li>`;
h += '</ul>';
}
return h;
}
function showConnect() {
openSheet(`
<h2>連上你的知識庫</h2>
+108
View File
@@ -304,3 +304,111 @@ input[type=text], input[type=password] {
width: 7px; height: 7px; border-radius: 50%; flex: none;
background: var(--err); margin-left: 4px;
}
/*
App 啟動器arcrun-rag#137
leo 2026-08-24所有的 App 需要有一個類似 Android/iOS **九宮格啟動界面**
每個 App 有一個 icon這會運行在 portal daemon
🔴 尺寸圓角間距逐項照 leo 已核准的設計稿Launcher-Desktop.dc.html
磁磚 88x88圓角 22格線 gap 32/24hover 上浮 2px標籤 14px 置中
🔴 顏色**一個 hex 都不准出現**全部走 arcrun-cis.css 的變數本檔開頭那條規矩
*/
/* 頁首:知識庫名 + 幾個 App + 重新整理(同 .libhead 的結構,不另發明一種頁首) */
.apphead {
display: flex; align-items: center; gap: 14px;
padding-bottom: 18px; margin-bottom: 26px;
border-bottom: 2px solid rgba(var(--amber-rgb), .35); /* 設計稿的 accentSoft 分隔線 */
}
.apphead .g { flex: 1; min-width: 0; }
.apphead .t { font-size: 22px; font-weight: 600; letter-spacing: .04em; }
.apphead .s { margin-top: 5px; font-size: 13.5px; color: rgba(var(--ink-rgb), .55); }
/* 多個知識庫時才出現的切換器(只有一個庫時畫面上不該有這排噪音) */
.appswitch { display: flex; gap: 8px; margin-bottom: 20px; flex-wrap: wrap; }
/* 圓角用 10px button刻意**不用**膠囊check-cis.sh 明文擋膠囊選中態
leo 08-04 否決過而且這排切換器與按鈕本來就該是同一種形狀語彙 */
.appswitch .chip {
padding: 6px 13px; font-size: 13px; border-radius: 10px;
border: 1px solid rgba(var(--ink-rgb), .18);
color: rgba(var(--ink-rgb), .6); cursor: pointer; user-select: none;
}
.appswitch .chip.on {
border-color: var(--amber); color: var(--amber);
background: rgba(var(--amber-rgb), .08); font-weight: 600;
}
/* 九宮格本體auto-fill 而不是固定 4 桌面視窗是可以被拉窄的min 820px
固定欄數會在窄視窗把磁磚擠變形 */
.appgrid {
display: grid; grid-template-columns: repeat(auto-fill, minmax(116px, 1fr));
gap: 32px 24px;
}
.appcell { display: flex; flex-direction: column; align-items: center; gap: 11px; }
.apptile {
width: 88px; height: 88px; border-radius: 22px;
display: flex; align-items: center; justify-content: center;
font-size: 38px; line-height: 1;
background: var(--well); border: 1px solid rgba(var(--ink-rgb), .1);
cursor: pointer; user-select: none;
transition: border-color .12s, background .12s, transform .12s;
}
.apptile:hover {
transform: translateY(-2px);
border-color: var(--amber);
background: rgba(var(--amber-rgb), .08);
}
/* 「加裝 App」——設計稿是虛線框+加號,語意是「這一格還沒有東西」 */
.apptile.add {
background: none; border: 1.5px dashed rgba(var(--ink-rgb), .22);
font-size: 28px; color: rgba(var(--ink-rgb), .45);
}
.appcell .nm {
font-size: 14px; text-align: center; line-height: 1.4;
max-width: 116px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.appcell .nm.dim { color: rgba(var(--ink-rgb), .55); }
/* App 頁 */
.appview { display: flex; flex-direction: column; }
.appview .head {
display: flex; align-items: center; gap: 12px;
padding-bottom: 14px; margin-bottom: 16px;
border-bottom: 1px solid rgba(var(--ink-rgb), .1);
}
.appview .head .ico { font-size: 24px; line-height: 1; }
.appview .head .nm { font-size: 17px; font-weight: 600; }
.appview .head .vr {
font-size: 12px; color: rgba(var(--ink-rgb), .4);
font-family: ui-monospace, Menlo, monospace;
}
.appview .head .sp { flex: 1; }
/* App 自帶畫面**關在 sandbox iframe ** main.js mountAppUI 的理由
高度吃滿剩下的版面 App 自己決定要怎麼排 */
/* 高度用 calc 而不是 flex:1height:100%#page flex item 自帶 padding
百分比高度在那裡會多算出一段 padding 而長出一條假捲軸
250px頁首( 90) + main padding(48) + App 頁首( 60) + 呼吸 */
.appframe {
width: 100%; height: calc(100vh - 250px); min-height: 420px;
border: 1px solid rgba(var(--ink-rgb), .1); border-radius: 12px;
background: var(--paper-a);
}
/* 沒有自帶畫面的 App列出它的工作流一條一顆現在執行
Portal 的系統預設畫面同一套呈現不另立第二種 */
.wfitem {
background: var(--well); border-radius: 12px;
padding: 15px 18px; margin-bottom: 10px;
}
.wfitem .top { display: flex; align-items: baseline; gap: 12px; }
.wfitem .nm { font-size: 15px; font-weight: 600; flex: 1; min-width: 0; }
.wfitem .d { margin-top: 7px; font-size: 13.5px; color: rgba(var(--ink-rgb), .55); line-height: 1.7; }
.wfitem .out {
margin-top: 8px; font-size: 12.5px; line-height: 1.6;
font-family: ui-monospace, Menlo, monospace;
color: rgba(var(--ink-rgb), .55);
word-break: break-all; white-space: pre-wrap;
}
.wfitem .out.bad { color: var(--err); }
@@ -0,0 +1,168 @@
// remove_folder_cleanup_test.go — arcrun-rag#138:移除資料夾時可以連同硬碟上的
// 隱藏資料夾一起收掉,而且**只收我們自己建的**。
//
// 本檔釘 App 這一半的四件事(認人的判準本身由 collector/cleanup_test.go 釘):
// 1. 沒勾清理 ⇒ 硬碟上一個檔都不准少(預設不刪,跟 #46 同一條紅線)
// 2. 勾了清理 ⇒ 我們的隱藏資料夾(含巢狀層)沒了,使用者的檔案一個不少
// 3. 另一個還在看守(或還在收回中)的資料夾底下 ⇒ 不碰
// 4. PlanFolderCleanup 只讀不寫,且拿得到「將要刪掉哪些」的清單
package main
import (
"os"
"path/filepath"
"sort"
"testing"
collector "arcrun-rag/collector"
)
func mkfile(t *testing.T, path, body string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
// makeFakeWatched 造一個「daemon 跑過」的資料夾:使用者的原稿+我們的隱藏產物(兩層)。
func makeFakeWatched(t *testing.T, root string) {
t.Helper()
mkfile(t, filepath.Join(root, "我的原稿.md"), "使用者的知識\n")
mkfile(t, filepath.Join(root, "docs", "設計.md"), "使用者的設計\n")
// 工作區與各層卡片目錄的內容逐字取自 collector 真正寫下去的那兩份宣告
mkfile(t, filepath.Join(root, ".arcrun-rag", ".gitignore"), collector.WorkspaceIgnoreBodyForTest())
for _, node := range []string{"", "docs"} {
dir := filepath.Join(root, node, ".wiki")
mkfile(t, filepath.Join(dir, ".gitignore"), collector.WikiIgnoreBodyForTest())
mkfile(t, filepath.Join(dir, "00-INDEX.md"), "# 00-INDEX\n\n## 文件\n")
mkfile(t, filepath.Join(dir, "arcrun-一張卡.md"), "# 卡\n")
}
}
func listFiles(t *testing.T, root string) []string {
t.Helper()
var out []string
_ = filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return nil
}
rel, _ := filepath.Rel(root, p)
out = append(out, filepath.ToSlash(rel))
return nil
})
sort.Strings(out)
return out
}
// ① 沒勾清理=行為與從前一字不差:硬碟上一個檔都不准少。
func TestRemoveFolderWithoutCleanupTouchesNothing(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
root := filepath.Join(t.TempDir(), "kb")
makeFakeWatched(t, root)
newTestCfgWithFolder(t, root)
before := listFiles(t, root)
if err := (&App{}).RemoveFolder(0, root, false, false); err != nil {
t.Fatalf("移除失敗:%v", err)
}
after := listFiles(t, root)
if len(before) != len(after) {
t.Fatalf("沒勾清理就不准動硬碟:before=%v after=%v", before, after)
}
}
// ② 勾了清理:我們的東西沒了,使用者的東西一個不少。
func TestRemoveFolderWithCleanupRemovesOnlyOurs(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
root := filepath.Join(t.TempDir(), "kb")
makeFakeWatched(t, root)
newTestCfgWithFolder(t, root)
if err := (&App{}).RemoveFolder(0, root, false, true); err != nil {
t.Fatalf("移除失敗:%v", err)
}
got := listFiles(t, root)
want := []string{"docs/設計.md", "我的原稿.md"}
sort.Strings(want)
if len(got) != len(want) {
t.Fatalf("清完之後應該只剩使用者的檔\n got=%v\nwant=%v", got, want)
}
for i := range got {
if got[i] != want[i] {
t.Fatalf("清完之後應該只剩使用者的檔\n got=%v\nwant=%v", got, want)
}
}
}
// ③ 巢狀在裡面、但**還在收回中**的資料夾 ⇒ 不碰它底下的東西。
func TestRemoveFolderCleanupSkipsRetiringNestedRoot(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
root := filepath.Join(t.TempDir(), "kb")
inner := filepath.Join(root, "legacy")
makeFakeWatched(t, root)
makeFakeWatched(t, inner)
cfg := &directConfig{
Accounts: []accountCfg{{
CypherURL: "https://example.workers.dev", Namespace: "abc", APIKey: "abc",
WatchFolders: []string{root},
RetiringFolders: []string{inner}, // 還在把雲端的資料收回來,別動它的檔
}},
Extractor: "workers-ai",
}
if err := saveCfg(cfg); err != nil {
t.Fatal(err)
}
if err := (&App{}).RemoveFolder(0, root, false, true); err != nil {
t.Fatalf("移除失敗:%v", err)
}
for _, p := range []string{
filepath.Join(inner, ".arcrun-rag", ".gitignore"),
filepath.Join(inner, ".wiki", "arcrun-一張卡.md"),
filepath.Join(inner, "docs", ".wiki", "arcrun-一張卡.md"),
} {
if _, err := os.Stat(p); err != nil {
t.Fatalf("還在收回中的資料夾底下的東西被刪了:%s(%v)", p, err)
}
}
if _, err := os.Stat(filepath.Join(root, ".wiki")); !os.IsNotExist(err) {
t.Fatalf("外層自己的 .wiki 該被清掉")
}
}
// ④ 先看清單:拿得到內容,而且看完之後硬碟一個檔都沒變。
func TestPlanFolderCleanupIsReadOnly(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
root := filepath.Join(t.TempDir(), "kb")
makeFakeWatched(t, root)
newTestCfgWithFolder(t, root)
before := listFiles(t, root)
plan, err := (&App{}).PlanFolderCleanup(0, root)
if err != nil {
t.Fatalf("拿不到清單:%v", err)
}
if len(plan.Remove) == 0 {
t.Fatal("這份擺法應該列得出東西")
}
for _, it := range plan.Remove {
if it.Evidence == "" {
t.Fatalf("清單上每一筆都要說得出依據:%+v", it)
}
}
if after := listFiles(t, root); len(after) != len(before) {
t.Fatalf("看清單這個動作動了硬碟:before=%v after=%v", before, after)
}
}
@@ -38,7 +38,7 @@ func TestRemoveFolderWithTakedownQueuesRetirement(t *testing.T) {
t.Setenv("USERPROFILE", home)
newTestCfgWithFolder(t, "/kb/logseq-plugin")
if err := (&App{}).RemoveFolder(0, "/kb/logseq-plugin", true); err != nil {
if err := (&App{}).RemoveFolder(0, "/kb/logseq-plugin", true, false); err != nil {
t.Fatalf("移除失敗:%v", err)
}
cfg, err := loadCfg()
@@ -90,7 +90,7 @@ func TestRemoveFolderWithoutTakedownKeepsCloudData(t *testing.T) {
t.Setenv("USERPROFILE", home)
newTestCfgWithFolder(t, "/kb/keep-cloud")
if err := (&App{}).RemoveFolder(0, "/kb/keep-cloud", false); err != nil {
if err := (&App{}).RemoveFolder(0, "/kb/keep-cloud", false, false); err != nil {
t.Fatalf("移除失敗:%v", err)
}
cfg, _ := loadCfg()
+19
View File
@@ -0,0 +1,19 @@
//go:build !darwin && !windows
package main
// tray_other.go — 非出貨平台(Linux 等)的系統匣 no-oparcrun-rag#137)。
//
// 為什麼加這一個檔:`setupTray` 只在 `tray_darwin.go``tray_windows.go` 有實作,
// 所以整包在 Linux **連編都編不起來**`undefined: setupTray`)——
// 連帶 `go build ./...``go test ./...` 這兩件最基本的機械驗證在雲端/CI 上做不了,
// 而這台桌面 App 的出貨機是 macOS 與 Windows,本來就沒人會在 Linux 跑它。
//
// ⇒ 補一個 no-op,讓「編得過/測得過」這條線在任何機器上都成立。
// **對出貨零影響**darwin 與 windows 兩個 tag 都排除了這個檔,
// 兩邊拿到的仍然是各自那份真正的托盤實作。
//
// 🔴 這不是「Linux 版桌面 App」:Linux 上沒有托盤、也沒有人測過 WebView,
//
// 它存在的唯一理由是讓編譯器與測試跑得起來(同 dock_other.go 的先例)。
func setupTray(_ *App) {}
+14
View File
@@ -0,0 +1,14 @@
// Command collector 是 collector 的獨立命令列執行檔。
//
// 真正的邏輯全在 arcrun-rag/collector 套件裡(Run)。這支只是薄薄的外殼,
// 讓「單獨跑 collector」這條路(開發、除錯、伺服器端批次)繼續可用;
// 桌面 App 則直接呼叫同一個 Run——**兩者共用同一份程式碼,不是兩份**。
package main
import (
"os"
collector "arcrun-rag/collector"
)
func main() { os.Exit(collector.Run(os.Args[1:])) }
+11
View File
@@ -1323,6 +1323,17 @@ func runDirectOnceRoot(cfg *DirectConfig, root string, dryRun bool, qs *quotaSta
}
}
// 碎形目錄索引(`inkstone/Arcrun#146`leo 2026-08-19「每個巢狀資料夾都要有 index,
// 且每一層指向子層次目錄」):接在總覽卡後面,同一個理由放在萃取迴圈之前——
// 目錄結構是本機掃一遍就有的答案,不該跟 LLM 排同一條隊、不該被額度牆擋住。
// 一卡一關聯(見 folderindex.go 檔頭)⇒ 單一請求恆定 1 條 rel,不受資料夾數量影響。
if fcRes := syncFolderCards(cfg, absRoot, m, len(payload.Events) > 0, dryRun, runNow); len(fcRes) > 0 {
results = append(results, fcRes...)
if fcRes[0].Status != "planned" {
saveManifest() // 同上:folder_card_hashes 當下就落盤,斷點續傳才接得上
}
}
// 承上:把本輪偵測到的 removed 路徑暫時放回 m.Entries,直到迴圈裡真的處理到它、
// POST 成功才由「removed」分支明確刪除。失敗或本輪還沒輪到(單輪上限)都維持放回的狀態,
// 下一輪自然重新偵測、重新嘗試下架——不會因為別的事件先存檔而被誤永久跳過。
+364
View File
@@ -0,0 +1,364 @@
// folderindex.go — 讓目錄索引變成**碎形**:每一層資料夾各有自己的一格,父子接得起來
// `inkstone/Arcrun#146`leo 2026-08-19)。
//
// leo 的原話(兩句,就是本檔的全部規格):
//
// 「daemon 可以**碎形**的在每個巢狀資料夾中產生 index & wiki」
// 「**每一層目錄指向子層次目錄**」
//
// 🔴 為什麼不是把 inventory.go 的上限調大:
//
// `inventory.go` 的資料模型只有**兩層**——`inventoryCardPath(library)` 讓一個監看根
// 只能有一張卡,`invTopDirOf(rel)` 只取相對路徑第一段。實測(14 檔/4 層巢狀)產出
// 1 張卡、3 條「第一層 >> part_of >> 總覽頁」,第二層以下一個節點都沒有。
// 深度是**寫死在模型裡的**,不是參數。
//
// 🔴 為什麼是「一個資料夾一張卡」而不是「一張卡帶全部關聯」:
//
// `rag_ingest_card` 對每條 rel 各發一個 POSTforeach)+upsert 前置的逐筆 DELETE。
// 把 N 個目錄的關聯塞進同一張卡 ⇒ 單一請求 N 條 rel ⇒ 撞 Workers 免費層
// **50 subrequests/請求**的天花板(`inventory.go` 的 `maxInventoryRels = 10`
// 就是為了這面牆才壓的)。拆成一卡一關聯後,**每個請求恆定 1 條 rel**,
// 天花板與資料夾數量脫鉤;變多的是請求「次數」,那個由既有的 `pace()` 節流吸收。
//
// 誠實界線(不要讓讀的人以為它做了沒做的事):
// - **空目錄不會有卡**。目錄集合是從 manifest 的檔案路徑反推的,而 `scan.go` 從來
// 沒有記錄過目錄本身(`d.IsDir()` 只用來決定要不要整棵跳過)。
// 「空資料夾也要看得見」是 `inkstone/arcrun-rag#106`,要先讓掃描記得目錄才做得到。
// - 卡片內容是**機械統計**(檔數、子資料夾、最近改了什麼),不經 LLM ——
// 與 `inventory.go` 同一個理由:這種問題本機掃一遍就有答案,不該跟萃取排同一條隊。
package collector
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"path"
"sort"
"strings"
"time"
)
const (
// maxFolderCards=單一監看根最多產幾張資料夾卡。
//
// 🔴 這是一道**會被講出來**的上限,不是安靜截斷(principles「no silent caps」):
// 超過時 `syncFolderCards` 會在結果訊息裡指名少送了幾個、從哪一層以下開始少。
// 200 的由來:一次同步 200 個請求已經是既有 `MaxEventsPerRun`(預設同量級)的規模,
// 再多就該讓使用者知道「你的資料夾深到要分批」,而不是我們自己吞掉。
maxFolderCards = 200
// maxFolderChildrenListed=卡片上「子資料夾」段最多列幾個。純顯示上限,
// 不影響關聯(關聯永遠由子卡自己宣告,見檔頭)。
maxFolderChildrenListed = 50
// maxFolderFilesListed=卡片上「本層檔案」最多列幾個。
maxFolderFilesListed = 100
)
// FolderCard=一個資料夾在雲端的那一格。
type FolderCard struct {
// Rel=相對監看根的路徑(`""`=根本身,根不由本檔產卡,見 BuildFolderCards)。
Rel string
// Page=雲端頁名(也是三元組裡的節點名)。
Page string
// Path=合成的 source_uri 鍵(不落地本機,同 inventoryCardPath 的慣例)。
Path string
// Parent=父資料夾的頁名。這就是「每一層指向子層次目錄」的那條邊的另一端。
Parent string
// Content=卡片全文(markdown)。
Content string
}
// folderCardPageName 回傳某個相對路徑的卡片頁名。
// 根用 inventoryPageName(沿用既有那張總覽卡,不另開一張打對台);
// 子層用 `資料夾:<根名>/<相對路徑>`——帶根名是因為不同監看根可能有同名子資料夾。
func folderCardPageName(absRoot, rel string) string {
if rel == "" {
return inventoryPageName(absRoot)
}
base := path.Base(strings.ReplaceAll(strings.TrimRight(absRoot, "/\\"), "\\", "/"))
if base == "" || base == "." || base == "/" {
base = "知識資料夾"
}
return "資料夾:" + base + "/" + rel
}
// folderCardPath 回傳子資料夾卡的合成路徑。
// 帶 library(每個監看根唯一)+相對路徑的 slug ⇒ 同帳號多資料夾不互撞。
// slug 為空(例如純中文路徑)時退成路徑雜湊,理由同 librarySlug 的 t89。
func folderCardPath(library, rel string) string {
slug := pathSlug(rel)
return ".arcrun-rag/wiki/cards/arcrun-資料夾-" + library + "-" + slug + ".md"
}
// pathSlug 把相對路徑轉成檔名安全的鍵。非 ASCII 一律轉底線分段(同 librarySlug 的規則),
// 全轉光就退成 sha256 前 8 hex——**路徑穩定所以鍵穩定,兩個不同路徑必然不同鍵**。
func pathSlug(rel string) string {
var b strings.Builder
lastUnderscore := false
for _, r := range rel {
switch {
case (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-' || r == '_':
b.WriteRune(r)
lastUnderscore = false
case r >= 'A' && r <= 'Z':
b.WriteRune(r + 32)
lastUnderscore = false
default:
if !lastUnderscore && b.Len() > 0 {
b.WriteRune('_')
lastUnderscore = true
}
}
}
s := strings.Trim(b.String(), "_")
sum := sha256.Sum256([]byte(rel))
short := hex.EncodeToString(sum[:])[:8]
if s == "" {
return "d_" + short
}
// 一律接上雜湊尾碼:slug 會把「A/B」與「A_B」壓成同一個字串,
// 只靠 slug 會讓兩個不同資料夾共用一張卡(靜默混層,同 t89 的形狀)。
return s + "-" + short
}
// dirsFromEntries 從 manifest 的檔案路徑反推出**所有祖先目錄**(含中間層)。
// 回傳排序過的相對路徑(不含根的 "")。
func dirsFromEntries(entries map[string]*ManifestEntry) []string {
set := map[string]bool{}
for p, e := range entries {
if e == nil {
continue
}
rel := strings.ReplaceAll(p, "\\", "/")
seg := strings.Split(rel, "/")
if len(seg) < 2 {
continue // 根層檔案,沒有祖先目錄
}
for i := 1; i < len(seg); i++ {
set[strings.Join(seg[:i], "/")] = true
}
}
out := make([]string, 0, len(set))
for d := range set {
out = append(out, d)
}
sort.Strings(out)
return out
}
// parentRel 回傳某個相對目錄的父目錄(頂層目錄的父是根,回 "")。
func parentRel(rel string) string {
if i := strings.LastIndexByte(rel, '/'); i > 0 {
return rel[:i]
}
return ""
}
// BuildFolderCards 為每一個(含有檔案的)子資料夾各做一張卡。純函式、零 IO、輸出確定。
//
// 根本身不在回傳值裡——根那張是 `inventory.go` 的總覽卡,兩者分工不重疊:
// 總覽卡回答「這個監看根整體有什麼」,資料夾卡回答「這一層有什麼、它掛在誰底下」。
func BuildFolderCards(absRoot string, entries map[string]*ManifestEntry, library string) []FolderCard {
dirs := dirsFromEntries(entries)
// 每層直屬檔案 / 每層子目錄 / 每層含子孫的總檔數
direct := map[string][]invFile{}
subtotal := map[string]int{}
children := map[string][]string{}
for p, e := range entries {
if e == nil {
continue
}
rel := strings.ReplaceAll(p, "\\", "/")
d := path.Dir(rel)
if d == "." {
d = ""
}
direct[d] = append(direct[d], invFile{path: path.Base(rel), mtime: e.Mtime})
for cur := d; ; cur = parentRel(cur) {
subtotal[cur]++
if cur == "" {
break
}
}
}
for _, d := range dirs {
p := parentRel(d)
children[p] = append(children[p], d)
}
for k := range children {
sort.Strings(children[k])
}
cards := make([]FolderCard, 0, len(dirs))
for _, rel := range dirs {
page := folderCardPageName(absRoot, rel)
var b strings.Builder
sep := " " + strings.Repeat(">", 2) + " "
b.WriteString("# " + page + "\n\n")
b.WriteString("## 一句話定義\n")
b.WriteString(fmt.Sprintf("這是「%s」底下的資料夾 `%s`:本層直接放了 %d 份文件,連同子資料夾共 %d 份。本頁由掃描自動整理,隨檔案變動即時更新,不經 AI 萃取。\n\n",
path.Base(strings.ReplaceAll(strings.TrimRight(absRoot, "/\\"), "\\", "/")),
rel, len(direct[rel]), subtotal[rel]))
// 子資料夾:這一段就是「每一層目錄指向子層次目錄」給人看的那一面
// (給機器看的那一面是子卡自己的「## 關聯」)。
b.WriteString("## 子資料夾\n")
kids := children[rel]
if len(kids) == 0 {
b.WriteString("(沒有子資料夾)\n")
}
for i, c := range kids {
if i >= maxFolderChildrenListed {
b.WriteString(fmt.Sprintf("- (其餘 %d 個子資料夾未列出)\n", len(kids)-i))
break
}
b.WriteString(fmt.Sprintf("- %s%d 份)\n", path.Base(c), subtotal[c]))
}
b.WriteString("\n")
b.WriteString("## 本層檔案\n")
files := direct[rel]
sort.Slice(files, func(i, j int) bool { return files[i].path < files[j].path })
if len(files) == 0 {
b.WriteString("(本層沒有直接放文件,內容都在子資料夾裡)\n")
}
for i, f := range files {
if i >= maxFolderFilesListed {
b.WriteString(fmt.Sprintf("- (其餘 %d 份未列出)\n", len(files)-i))
break
}
b.WriteString("- " + f.path + "" + invDateOf(f.mtime) + "\n")
}
b.WriteString("\n")
// ── 關聯:**恰好一條**,指向父層。天花板與資料夾數量脫鉤的關鍵就在這裡。──
parent := folderCardPageName(absRoot, parentRel(rel))
b.WriteString("## 關聯\n")
b.WriteString("- " + page + sep + "part_of" + sep + parent + "\n")
cards = append(cards, FolderCard{
Rel: rel, Page: page, Path: folderCardPath(library, rel),
Parent: parent, Content: b.String(),
})
}
return cards
}
// syncFolderCards 在總覽卡之後送資料夾卡。回 nil=這輪不必送。
//
// 冪等:每張卡的內容雜湊記在 `manifest.folder_card_hashes[rel]`,沒變不送。
// 失敗不擋主流程(同 syncInventory);額度訊息換成人話。
// 上限:`maxFolderCards`,超過的**指名講出來**,不安靜吞掉。
func syncFolderCards(cfg *DirectConfig, absRoot string, m *Manifest, hasEvents, dryRun bool, now time.Time) []DirectResult {
if !hasEvents && len(m.FolderCardHashes) > 0 {
return nil // 靜止輪不重送(同 syncInventory 的③)
}
lib := cfg.libraryFor(absRoot)
cards := BuildFolderCards(absRoot, m.Entries, lib)
if len(cards) == 0 {
return nil
}
if m.FolderCardHashes == nil {
m.FolderCardHashes = map[string]string{}
}
// 先刪掉「這輪已經不存在的資料夾」的記帳,避免雜湊表無限長大。
live := map[string]bool{}
for _, c := range cards {
live[c.Rel] = true
}
for rel := range m.FolderCardHashes {
if !live[rel] {
delete(m.FolderCardHashes, rel)
}
}
// 🔴 先挑「這輪真的要送的」,**再**套上限——順序反過來就會變成永遠只重試前 200 個,
// 後面的資料夾一輩子送不上去(而且畫面上看起來一切正常)。
type pending struct {
card FolderCard
hash string
}
var todo []pending
for _, c := range cards {
sum := sha256.Sum256([]byte(c.Content))
h := "sha256:" + hex.EncodeToString(sum[:])
if m.FolderCardHashes[c.Rel] == h {
continue // 冪等:內容沒變
}
todo = append(todo, pending{card: c, hash: h})
}
if len(todo) == 0 {
return nil
}
var dropped []string
if len(todo) > maxFolderCards {
for _, t := range todo[maxFolderCards:] {
dropped = append(dropped, t.card.Rel)
}
todo = todo[:maxFolderCards]
}
var out []DirectResult
wf := cfg.CardIngestWF
if wf == "" {
wf = "rag_ingest_card"
}
mach := cfg.machineIdentity()
for _, t := range todo {
c := t.card
res := DirectResult{Type: "folder", Path: c.Path}
if dryRun {
res.Status = "planned"
out = append(out, res)
continue
}
pace()
status, _, err := cfg.postJSON(cfg.triggerURL(wf), map[string]any{
"page_name": c.Page,
"path": c.Path,
"card_content": c.Content,
"library": lib,
"machine": mach.ID,
"machine_label": mach.Label,
})
res.HTTPStatus = status
if err != nil {
res.Status = "failed"
if isQuotaExhausted(err.Error()) {
res.Error = "雲端今天的額度用完了,資料夾索引會稍後自動補送(不影響檔案同步)"
} else {
res.Error = "資料夾索引上傳失敗(不影響檔案同步):" + err.Error()
}
out = append(out, res)
// 這一張失敗就停手:後面幾百張多半會撞同一面牆,繼續撞只是把
// 「一個壞掉的雲端」變成「幾百筆一樣的錯誤」(t195 的 1387 輪教訓)。
break
}
res.Status = "ingested"
m.FolderCardHashes[c.Rel] = t.hash
out = append(out, res)
}
if len(dropped) > 0 {
out = append(out, DirectResult{
Type: "folder",
Status: "skipped",
Error: fmt.Sprintf("資料夾超過單輪上限 %d 個,這輪少送 %d 個(例:%s)——下一輪會接著送",
maxFolderCards, len(dropped), strings.Join(firstN(dropped, 3), "、")),
})
}
return out
}
// firstN 取前 n 個(不足就全給)。只給訊息用。
func firstN(ss []string, n int) []string {
if len(ss) <= n {
return ss
}
return ss[:n]
}
+10
View File
@@ -0,0 +1,10 @@
package collector
import (
"fmt"
"time"
)
func pad(i int) string { return fmt.Sprintf("%03d", i) }
func nowFixed() time.Time { return time.Unix(1787100000, 0) }
+164
View File
@@ -0,0 +1,164 @@
package collector
import (
"strings"
"testing"
)
// ent 造一份最小 manifest entries(只有 path 有意義)。
func ent(paths ...string) map[string]*ManifestEntry {
m := map[string]*ManifestEntry{}
for i, p := range paths {
m[p] = &ManifestEntry{ContentHash: "sha256:x", Mtime: int64(1787000000 + i)}
}
return m
}
// 這份就是 leo 08-19 要 demo 的形狀:4 層巢狀。
var nested = ent(
"README.md",
"01-公司制度/請假規則.md",
"01-公司制度/資訊安全/密碼原則.md",
"02-專案/教育部標案/RFP摘要.md",
"02-專案/教育部標案/會議紀錄/20260801-啟動會議.md",
"02-專案/教育部標案/會議紀錄/20260812-期中檢討.md",
"03-產品/價目表.csv",
)
func TestDirsIncludeIntermediateLevels(t *testing.T) {
got := dirsFromEntries(nested)
want := []string{
"01-公司制度", "01-公司制度/資訊安全",
"02-專案", "02-專案/教育部標案", "02-專案/教育部標案/會議紀錄",
"03-產品",
}
if len(got) != len(want) {
t.Fatalf("目錄數 %d,期望 %d%v", len(got), len(want), got)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("第 %d 個是 %q,期望 %q", i, got[i], want[i])
}
}
}
// 🔴 本檔的核心宣稱:每個巢狀資料夾各一張卡,而且父子接得起來。
func TestFractalOneCardPerFolderAndChainReachesRoot(t *testing.T) {
cards := BuildFolderCards("/x/demo-test1", nested, "test1")
if len(cards) != 6 {
t.Fatalf("卡數 %d,期望 6(每個子資料夾一張)", len(cards))
}
byPage := map[string]FolderCard{}
for _, c := range cards {
byPage[c.Page] = c
// 每張卡的「## 關聯」恰好一條——這是「不撞 50 subrequests」的結構保證
rel := c.Content[strings.Index(c.Content, "## 關聯"):]
if n := strings.Count(rel, "\n- "); n != 1 {
t.Fatalf("%s 的關聯有 %d 條,必須恰好 1 條", c.Page, n)
}
if !strings.Contains(rel, c.Page+" >> part_of >> "+c.Parent) {
t.Fatalf("%s 的關聯不是指向父層 %s%q", c.Page, c.Parent, rel)
}
}
// 從最深的一格一路往上走,必須走得到根(=總覽卡頁名),且步數正確。
deepest := folderCardPageName("/x/demo-test1", "02-專案/教育部標案/會議紀錄")
cur, hops := byPage[deepest], 0
if cur.Page == "" {
t.Fatalf("找不到最深的那張卡 %q", deepest)
}
for {
hops++
parent, ok := byPage[cur.Parent]
if !ok {
if cur.Parent != inventoryPageName("/x/demo-test1") {
t.Fatalf("鏈斷在 %q,它的父 %q 既不是卡也不是根", cur.Page, cur.Parent)
}
break
}
cur = parent
if hops > 10 {
t.Fatal("往上走超過 10 步,疑似有環")
}
}
if hops != 3 {
t.Fatalf("從第三層走到根走了 %d 步,期望 3", hops)
}
}
// 卡片要講得出「這一層有幾份、含子層幾份」與子資料夾清單——那是給人看的「指向子層」。
func TestCardCountsAndChildrenListing(t *testing.T) {
cards := BuildFolderCards("/x/demo-test1", nested, "test1")
var top FolderCard
for _, c := range cards {
if c.Rel == "02-專案" {
top = c
}
}
if top.Page == "" {
t.Fatal("找不到 02-專案 的卡")
}
if !strings.Contains(top.Content, "本層直接放了 0 份文件,連同子資料夾共 3 份") {
t.Fatalf("份數講錯了:\n%s", top.Content)
}
if !strings.Contains(top.Content, "- 教育部標案(3 份)") {
t.Fatalf("子資料夾清單不對:\n%s", top.Content)
}
}
// t89 同款:slug 會把 "A/B" 與 "A_B" 壓成同一個字串,只靠 slug 會讓兩個資料夾共用一張卡。
func TestPathSlugDoesNotCollapseDifferentPaths(t *testing.T) {
if pathSlug("a/b") == pathSlug("a_b") {
t.Fatal("a/b 與 a_b 的鍵相同 ⇒ 兩個資料夾會共用一張卡(靜默混層)")
}
// 純中文路徑 slug 後為空,必須退成雜湊而不是空字串
z := pathSlug("專案/會議")
if z == "" || strings.HasPrefix(z, "-") {
t.Fatalf("純中文路徑的鍵不合法:%q", z)
}
if pathSlug("專案/會議") == pathSlug("專案/紀錄") {
t.Fatal("兩個不同的純中文路徑得到同一個鍵")
}
}
// 🔴 迴歸守門:上限必須套在「要送的」上,不是套在「全部」上。
// 反過來寫的話,第 201 個之後的資料夾永遠送不上去,而畫面上一切正常。
func TestCapAppliesToPendingNotAll(t *testing.T) {
paths := make([]string, 0, maxFolderCards+20)
for i := 0; i < maxFolderCards+20; i++ {
paths = append(paths, "d"+pad(i)+"/f.md")
}
entries := ent(paths...)
cards := BuildFolderCards("/x/root", entries, "lib")
if len(cards) != maxFolderCards+20 {
t.Fatalf("卡數 %d,期望 %d", len(cards), maxFolderCards+20)
}
m := &Manifest{Entries: entries, FolderCardHashes: map[string]string{}}
// 假裝前 maxFolderCards 個都已經送成功(雜湊相同)
for _, c := range cards[:maxFolderCards] {
m.FolderCardHashes[c.Rel] = hashOf(c.Content)
}
cfg := &DirectConfig{}
res := syncFolderCards(cfg, "/x/root", m, true, true /*dryRun*/, nowFixed())
if len(res) != 20 {
t.Fatalf("這輪排到 %d 張,期望剩下的 20 張——上限若套在全部上,這裡會是 0", len(res))
}
}
func TestIdempotentWhenNothingChanged(t *testing.T) {
m := &Manifest{Entries: nested, FolderCardHashes: map[string]string{}}
cfg := &DirectConfig{}
first := syncFolderCards(cfg, "/x/demo-test1", m, true, true, nowFixed())
if len(first) != 6 {
t.Fatalf("第一輪 %d 張,期望 6", len(first))
}
// dryRun 不寫雜湊,改用真的算一次填進去模擬送成功
for _, c := range BuildFolderCards("/x/demo-test1", nested, "") {
m.FolderCardHashes[c.Rel] = hashOf(c.Content)
}
if second := syncFolderCards(cfg, "/x/demo-test1", m, true, true, nowFixed()); second != nil {
t.Fatalf("內容沒變卻還要送 %d 張", len(second))
}
}
+4
View File
@@ -89,6 +89,8 @@ func Run(args []string) int {
return runTemplateInstall(args[1:])
case "tidy":
return runTidy(args[1:])
case "cleanup":
return runCleanup(args[1:])
default:
usage()
return 2
@@ -107,7 +109,9 @@ func usage() {
collector sync --root <dir> --manifest <file> [--max-removed-ratio 0.4] [--dry-run]
collector direct --config <config.json> [--once] [--dry-run]
collector tidy --folder <dir> [--apply] [--json]
collector cleanup --folder <dir> [--keep <還在看守的別的資料夾>] [--apply] [--json]
tidy: 找出以前寫進資料夾還沒帶 arcrun- 標記的舊產物並改名/搬走預設只列出不動手 --apply 才執行從不刪檔
cleanup: 斷連時把 daemon 建立的 .arcrun-rag/ 與各層 .wiki/ 收掉預設只列清單 --apply 才真的刪認不出來的一律留著
upload 需環境變數: CF_ACCOUNT_ID / CF_API_TOKEN / R2_BUCKET
sync 另需: ARCRUN_TRIGGER_URLnamed-webhook 觸發完整 URL
direct R2: 監看資料夾 讀檔內容直送實例 rag_ingest_direct workflow設定走 --config JSON`)
+5
View File
@@ -70,6 +70,11 @@ const MaxFailBeforeSkip = 8
// Manifest 對應一個被勾選的資料夾。
type Manifest struct {
FolderID string `json:"folder_id"`
// FolderCardHashes=每個子資料夾索引卡的內容雜湊(鍵=相對監看根的目錄路徑)。
// 冪等用:內容沒變就不重送(同 InventoryHash 的角色,只是一層變多層)。
// 資料夾消失時由 syncFolderCards 清掉對應的鍵,不讓這張表無限長大。
// `inkstone/Arcrun#146` 碎形目錄索引)
FolderCardHashes map[string]string `json:"folder_card_hashes,omitempty"`
Root string `json:"root"`
Entries map[string]*ManifestEntry `json:"entries"`
// InventoryHash=最後一次**成功送達雲端**的資料夾總覽卡內容雜湊(結構先行,