Files
apes-sidebar/src/search-debug.ts
T
Menghuan1918 c06c633759 feat(search): fs.search 文件名搜索优先用 fd/rg,缺失/失败回退 JS 遍历
issue #203:十万级目录下纯 JS 递归遍历是秒级,10 万条访问预算一顶结果
还被静默截断。现在启动后惰性探测一次本机 fd / rg(每个候选 `--version`
500ms 内验证通过才算数,进程内缓存),探到就用引擎输出,探不到或运行失败
整段回退原来的遍历(导出为 `searchFilesPlain`);坏二进制在进程内禁用,
超时只降级本次(树大 ≠ 二进制坏)。

为什么按 dev 现状重写而不是机械解冲突(PR #303 的落点全部复核过):

- **#801 的必填 `dirs` 是语义级冲突,必须扩引擎契约**。dev 的
  `FsSearchResult` 多了 `dirs`(`TreePanel` 用它把命中行当目录导航,否则
  `fs.read` 报 “is a directory”),而 PR 的引擎侧只产出
  `{paths, truncated}`,`searchFilesPlain` 的空查询也还在返回两字段形状;
  照搬会让 #801 修的 bug 在引擎路径上静默回退。收口:fd 用第二次
  `--type d` 调用取目录子集(fd 自己不做标注),rg 由文件路径倒推匹配的
  目录段(`deriveRgMatches`),dispatch 再把 `dirs` 收敛成 `matches` 的
  子集。残留差异只有**空目录在 rg 下不可见**(没有文件路径能带上它)。
- **rg 的路径级 glob 补成三个**:PR 的 `**/*<q>*/**` 缺一个直接子文件
  (`util/helper.ts` 是最常见形态),只加 `**` 会让这类目录命中在
  rg-only 机器上凭空消失;`**/*<q>/*` + `**/*<q>/**` 两条并用(对真实
  rg 15 实测:单用任一条都丢结果)。
- **`bundledRgCandidates` 的路径推导按真机布局改写**:npm/pnpm 把
  `@vscode/ripgrep-<platform>-<arch>` 提升到 `@deepseek-ai` 的**同级**,
  不存在 `<root>/@deepseek-ai/dsh/node_modules/@vscode/...`;候选根改成
  「从 CLI 入口(argv[1])向上逐级 `node_modules`」+ 各包管理器全局前缀,
  桌面壳那种「自带依赖树 + 外部 node」的布局才探得到捆绑 rg(实测命中)。
- **丢掉 abort 形参**:`src/index.ts` 的生产路径不传 signal(PR 里也只有
  测试在用),单为测试保留一条贯穿三层的通道不划算;引擎子进程由 15s
  内部超时兜底。
- **文档锚点按 dev 重锚**:README 的 v0.12.3 条目已随发布历史迁到
  CHANGELOG(新增 Unreleased 条目),AGENTS §2 的引擎门禁入口挂到现有
  ci-windows 段落,指南 §14 参考实现行补 `src/search-engines.ts`。
- 未新增任何依赖:`@vscode/ripgrep` 只作为**路径**探测,不做模块导入。

Windows 侧由既有 ci-windows lane 承载:`choco install fd ripgrep` 把真
二进制放进 PATH,套件后 `pnpm check:engines`(`scripts/win-engine-check.mjs
--assert`,import `src/search-engines.ts` 取插件自己的 argv)断言
`--path-separator /` 的输出不含 `\`/CR、非 ASCII 文件名命中;引擎缺失则
跳过(fd 在 ubuntu lane 与多数开发机上都没有)。

测试:`tests/search-engines.spec.ts`(探测缓存 / 运行失败禁用 / 超时不禁
用 / argv 对称性与噪声目录排除 / `deriveRgMatches` / 捆绑 rg 候选推导 /
真引擎在场时的端到端——缺席即返回),`tests/fs-search.spec.ts` 拆成
「直接钉遍历」+「注入假引擎的 dispatch 组」(引擎优先、`dirs` 透传与
子集收敛、预算透传、运行失败回退并禁用)。真机核对:本仓库上 rg 路径与
JS 遍历对 `locales` / `util` / `client` / `src` / `e2e` 五个查询的
matches 与 dirs **逐项一致**。

Refs #303
2026-10-05 02:10:45 +08:00

40 lines
1.4 KiB
TypeScript

/**
* Gated debug instrumentation for the fs.search engine pipeline (host half).
* Off by default: with DSH_SEARCH_DEBUG unset nothing is logged, nothing
* touches the disk, and no console output is emitted. Setting
* `DSH_SEARCH_DEBUG=1` in the dsh web environment appends one line per
* probe/search/engine-failure to `<config-dir>/search-debug.log` and mirrors
* it to the host console.
*
* The log file lives under $DSH_HOME (the DSH config dir) when set, exactly
* like pty-deps.ts resolves it; otherwise it falls back to `~/.dsh`. A
* missing/unwritable log dir must never break a search — write errors are
* swallowed.
*
* The flag is read once at module load: toggling it requires restarting the
* host, the same as any other host-side change.
*/
import { appendFileSync } from 'node:fs'
import { join } from 'node:path'
import { homedir } from 'node:os'
const SEARCH_DEBUG = process.env.DSH_SEARCH_DEBUG === '1'
const LOG_FILE = join(
process.env.DSH_HOME !== undefined && process.env.DSH_HOME.trim() !== ''
? process.env.DSH_HOME
: join(homedir(), '.dsh'),
'search-debug.log',
)
/** Append one debug line (gated; no-op unless DSH_SEARCH_DEBUG=1). */
export function debugLog(msg: string): void {
if (!SEARCH_DEBUG) return
try {
appendFileSync(LOG_FILE, `[${new Date().toISOString()}] ${msg}\n`)
} catch {
// A missing/unwritable log path must never break a search.
}
console.log(msg)
}