Files
apes-sidebar/src/active-worktree.ts
T
Menghuan1918 3e5576ffbb perf(host): cwd 解析加在飞去重与 TTL 对齐、numstat 加 pathspec 与输出上限
src/active-worktree.ts 是 v0.25 新文件,却插进了 12 条 /sidebar/* 路由共用的
cwdOf():实测每次缓存未命中 9.6–44.7ms、1–4 次真实 git spawn,而 TTL 3s 与客户端
新的 5s 每-tab 轮询错配——每次轮询必然未命中。

- active-worktree:(session, base) 整链与 base 探针都加在飞去重(并发 8 次只 spawn
  一次);ACTIVE_ROOT_TTL_MS 3s→15s(跟随 worktree 的延迟代价写进注释);三个模块级
  缓存加 LRU 上界 512/512/256,负结果仍缓存但可淘汰。
- git.ts:两条 --numstat 加 pathspec(非 ?? 路径 + R/C 的 origin,必须
  --literal-pathspecs;未跟踪路径不参与 ⇒ 未忽略的大目录从两条全仓库 diff 变成零条;
  路径超 8KB 回退整仓库 diff)、runGit 加 8MiB 输出上限、状态行上限改在产出侧生效。
- fs-tree:折叠探测每层封顶 32 且截断层不探测(折叠是纯装饰)。
- index.ts:单层 fs.tree 补 ProbeGate;teardown 主动关闭三个 noServer WSS 的已接受
  连接(ws@8 的 close() 不终止在连 socket,句柄会活到浏览器断开)。
- sidechat-routes:冷线程日志按 (childId, revision, size) memo(16 线程,句柄照旧
  open/close,只有结果被缓存)。
- agent-opens:每会话队列封顶 64,最旧的丢弃,FIFO 与 drainAll 语义不变。

测试:扩展 active-worktree / git-worktree / fs-tree-probe-gate / sidechat-routes /
smoke / agent-opens;14 项「临时回退→变红→还原」突变全部被捕获。
2026-10-15 23:05:32 +08:00

385 lines
17 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Follow the linked git worktree a session is ACTUALLY working in.
*
* The explorer is rooted at the session's stored header cwd, which never
* changes. A session can move into a linked git worktree (`git worktree add`)
* during its life including a worktree of a repository that is not the one
* the header cwd points at and the sidebar should follow it.
*
* The active root is derived from the session's own `tool/call` arguments:
* absolute paths are collected newest-event-first (structured `cwd` / `workdir`
* / `path` / ... fields weigh more than free-form mentions, repeated mentions
* accumulate), each path's git top level is resolved with
* `git rev-parse --show-toplevel` (falling back to its directory for files),
* and only top levels whose `--absolute-git-dir` differs from their
* `--git-common-dir` (i.e. real LINKED worktrees, not the main checkout and
* not submodules) are adopted. The highest-scoring linked worktree of the
* newest event that has one wins; when there is no evidence the session cwd is
* kept (lifted to the repository top level for subdirectory sessions).
*
* Resolutions are cached process-wide ({@link ACTIVE_ROOT_TTL_MS}), concurrent
* callers share one in-flight resolution AND one in-flight base probe, and
* every cache is bounded — so the client's periodic re-ask, the debounced
* fs-watch frames and the twelve route handlers that funnel through
* `cwdOf()` are answered without a process spawn after the first scan.
*/
import { stat } from 'node:fs/promises'
import { dirname, isAbsolute, join, sep } from 'node:path'
import type { Context, SidebarSessionEvent } from './context-types.ts'
import { requireAbsolute } from './fs-tree.ts'
import { runGit } from './git.ts'
import { readPersistedSessionOf } from './session-store.ts'
/**
* How long one computed root is reused before the event log is scanned again.
*
* 15 s, deliberately longer than the client's per-tab 5 s poll: at 3 s every
* poll was a cache miss (one `git rev-parse --show-toplevel` spawn plus up to
* 4 000 events of backlog scanning per ask, measured 9.6–44.7 ms), which is
* the opposite of what this cache is for. The semantic cost of the longer TTL
* is that a session which MOVES into a linked worktree is followed within at
* most 15 s of the move (was 3 s) — every event-driven re-ask (fs-watch
* frames, route calls, editor opens) is served from the cache in the meantime
* and no refresh path is turned off.
*/
const ACTIVE_ROOT_TTL_MS = 15_000
/** Only the newest slice of the event log is considered. */
const ACTIVE_ROOT_MAX_EVENTS = 4_000
/** Upper bound on git probes per scan (cache misses are what cost). */
const ACTIVE_ROOT_MAX_PROBES = 24
/** Bounds of the three process-wide caches below (LRU, oldest evicted first). */
const PATH_TOP_CACHE_MAX = 512
const LINKED_WORKTREE_CACHE_MAX = 512
const ACTIVE_ROOT_CACHE_MAX = 256
/** Explicit override, mainly for tests and manual pinning. */
const ACTIVE_ROOT_OVERRIDE_ENV = 'DSH_SIDEBAR_WORKTREE_ROOT'
/** Keys whose string values describe where a tool actually ran. */
const ACTIVE_ROOT_STRUCTURED_FIELD = /^(cwd|workdir|work_dir|workingdirectory|path|file_path|filepath|target|root)$/i
/** Absolute Windows paths embedded in free-form command text. */
const ACTIVE_ROOT_PATH_PATTERN = /[A-Za-z]:(?:\\|\/)[^\\/\s"'\x60,;|)<>*?]+(?:[\\/][^\\/\s"'\x60,;|)<>*?]+)*/g
/**
* One computed root per session, reused for {@link ACTIVE_ROOT_TTL_MS}. `base`
* is the RAW base the caller passed (not the lifted one), because the entry has
* to be usable BEFORE the git probe below: that probe is a real process spawn
* (measured ~7 ms), and the repeated asks every debounced watch frame, every
* route call, the client's 5 s poll are exactly what the TTL exists to keep
* free.
*
* All three maps are BOUNDED: a long-lived host (and a `keepMounted` right
* sidebar that keeps asking about every session it has ever shown) would
* otherwise grow them forever. `pathTopCache` caches negative answers too
* (`undefined` = "no git top level here"), which is what keeps a non-repo
* session cheap; those entries are evictable like any other.
*/
const activeWorktreeRootCache = new Map<string, { root: string; base: string; expires: number }>()
const pathTopCache = new Map<string, string | undefined>()
const linkedWorktreeCache = new Map<string, boolean>()
/**
* In-flight de-duplication, mirroring `git.ts`'s `repoRootsInFlight`.
*
* `activeRootInFlight` collapses a burst of asks for the SAME (session, base)
* into one resolution (one base spawn, one event scan); `topLevelInFlight`
* collapses the base probe across DIFFERENT sessions that share a base (N tabs
* of one workspace would otherwise spawn N `git rev-parse` processes at once).
* Both delete their entry on the resolved AND the rejected path, so a failure
* never poisons the map.
*/
const activeRootInFlight = new Map<string, Promise<string>>()
const topLevelInFlight = new Map<string, Promise<string | undefined>>()
/**
* Insert or refresh one cache entry, evicting the least recently used entry
* once the cache holds more than `max`. Map iteration order is insertion
* order, so deleting before setting moves the key to the young end — which is
* what makes a HIT refresh recency (this is the repo's bounded-cache style
* from `fs-tree.ts`'s `directoryCache`, with the recency touch added).
*/
function cachePut<K, V>(map: Map<K, V>, key: K, value: V, max: number): void {
map.delete(key)
map.set(key, value)
while (map.size > max) {
const oldest = map.keys().next().value
if (oldest === undefined) break
map.delete(oldest)
}
}
/**
* Normalized IDENTITY of a path (case- and separator-insensitive), for
* comparisons and cache keys only: it lowercases, so it must never be handed
* back to the filesystem. On Linux (case-sensitive) `…/iHpOqm` → `…/ihpoqm`
* is a different, usually non-existent path, and probing it silently drops
* the candidate; macOS and Windows mask that because their filesystems fold
* case. Keep the real path next to its key.
*/
function pathKeyOf(value: string): string {
return value.replace(/[\\/]+/g, '/').replace(/\/+$/, '').toLowerCase()
}
/** Whether `target` is `base` itself or lives under it. */
function pathUnderOf(base: string, target: string): boolean {
const b = pathKeyOf(base)
const t = pathKeyOf(target)
return t === b || t.startsWith(`${b}/`)
}
/** `git rev-parse --show-toplevel` of an existing directory, or undefined. */
async function gitTopLevelOf(cwd: string): Promise<string | undefined> {
if (cwd === '' || !isAbsolute(cwd)) return undefined
try {
if (!(await stat(cwd)).isDirectory()) return undefined
return (await runGit(cwd, ['rev-parse', '--show-toplevel'])).trim()
} catch {
return undefined
}
}
/**
* {@link gitTopLevelOf} with in-flight sharing per path identity: concurrent
* asks for one directory (a route burst, or several sessions rooted in the
* same repository) spawn ONE git process between them.
*/
function topLevelOf(cwd: string): Promise<string | undefined> {
const key = pathKeyOf(cwd)
const pending = topLevelInFlight.get(key)
if (pending !== undefined) return pending
const promise = gitTopLevelOf(cwd).then(
(top) => {
topLevelInFlight.delete(key)
return top
},
(error: unknown) => {
topLevelInFlight.delete(key)
throw error
},
)
topLevelInFlight.set(key, promise)
return promise
}
/** Collapse duplicated separators without destroying a UNC prefix. */
function normalizeActivePath(value: string): string {
const trimmed = value.replace(/[\\/]+$/, '')
if (trimmed.startsWith('\\\\')) return `\\\\${trimmed.slice(2).replace(/[\\/]+/g, '\\')}`
// Fold to the PLATFORM separator, never unconditionally to a backslash: a
// POSIX path folded to `\private\var\…` is no longer absolute for
// `path.isAbsolute`, and `addActivePath` below then drops it the whole
// scan silently found nothing on Linux/macOS. On Windows `sep` IS `\`, so
// the behavior there is unchanged.
return trimmed.replace(/[\\/]+/g, sep)
}
/** One candidate path mentioned by a tool call, with its accumulated score. */
interface ActivePathEntry {
/** The real path (original case), the only form safe to probe. */
path: string
score: number
}
function addActivePath(map: Map<string, ActivePathEntry>, value: unknown, weight: number): void {
if (typeof value !== 'string') return
const normalized = normalizeActivePath(value)
if (normalized === '' || !isAbsolute(normalized)) return
const key = pathKeyOf(normalized)
const entry = map.get(key)
if (entry === undefined) map.set(key, { path: normalized, score: weight })
else entry.score += weight
}
/**
* Collect every absolute path one tool call mentions.
* @param value - JSON-decoded tool arguments (nested values are walked).
* @param map - accumulator keyed by normalized path identity.
* @param structured - whether the current value sits in a cwd/workdir/path field.
*/
function collectActivePaths(value: unknown, map: Map<string, ActivePathEntry>, structured: boolean): void {
if (typeof value === 'string') {
if (isAbsolute(value)) {
addActivePath(map, value, structured ? 8 : 1)
return
}
const head = value.charCodeAt(0)
if (head === 0x7b || head === 0x5b) {
try {
collectActivePaths(JSON.parse(value) as unknown, map, structured)
} catch {
// Not JSON: fall through to the embedded-path scan.
}
}
for (const match of value.match(ACTIVE_ROOT_PATH_PATTERN) ?? []) addActivePath(map, match, 1)
return
}
if (Array.isArray(value)) {
for (const item of value) collectActivePaths(item, map, structured)
return
}
if (value !== null && typeof value === 'object') {
for (const [key, item] of Object.entries(value as Record<string, unknown>)) {
const isStructured = ACTIVE_ROOT_STRUCTURED_FIELD.test(key)
collectActivePaths(item, map, structured || isStructured)
}
}
}
/** git top level for one path (its directory when the path names a file). */
async function topOfActivePath(value: string): Promise<string | undefined> {
const key = pathKeyOf(value)
if (pathTopCache.has(key)) {
const cached = pathTopCache.get(key)
// A cached `undefined` ("not in a repository") is the answer that keeps a
// non-repo session cheap, so it is re-inserted like any other hit — which
// also refreshes its recency in the bounded LRU.
cachePut(pathTopCache, key, cached, PATH_TOP_CACHE_MAX)
return cached
}
let top = await topLevelOf(value)
if (top === undefined) top = await topLevelOf(dirname(value))
const result = top === undefined ? undefined : requireAbsolute(top)
cachePut(pathTopCache, key, result, PATH_TOP_CACHE_MAX)
return result
}
/**
* Whether a git top level is a LINKED worktree: its git dir lives under the
* common dir (`<main>/.git/worktrees/<name>`) instead of being it. Submodules
* report the same dir for both, so they are excluded.
*/
async function isLinkedWorktree(top: string): Promise<boolean> {
const key = pathKeyOf(top)
const cached = linkedWorktreeCache.get(key)
if (cached !== undefined) {
cachePut(linkedWorktreeCache, key, cached, LINKED_WORKTREE_CACHE_MAX)
return cached
}
let linked = false
try {
const out = (await runGit(top, ['rev-parse', '--absolute-git-dir', '--git-common-dir'])).trim().split(/\r?\n/)
const gitDir = out[0]
const commonRaw = out[1] ?? ''
if (gitDir !== undefined && gitDir !== '' && commonRaw !== '') {
const common = isAbsolute(commonRaw) ? commonRaw : join(top, commonRaw)
linked = pathKeyOf(requireAbsolute(gitDir)) !== pathKeyOf(requireAbsolute(common))
}
} catch {
linked = false
}
// `false` is a real answer ("the main checkout", "a submodule", "not a
// repo") and is cached: the negative is what keeps a subdirectory session
// from re-probing the same top level on every scan.
cachePut(linkedWorktreeCache, key, linked, LINKED_WORKTREE_CACHE_MAX)
return linked
}
/** The newest linked worktree the session's tool/call paths resolve to. */
async function activeRootFromEvents(events: readonly SidebarSessionEvent[], base: string): Promise<string | undefined> {
if (events.length === 0) return undefined
const baseKey = pathKeyOf(base)
const start = Math.max(0, events.length - ACTIVE_ROOT_MAX_EVENTS)
let probes = 0
for (let index = events.length - 1; index >= start; index -= 1) {
const event = events[index]
if (event === undefined || event.type !== 'tool/call') continue
const weights = new Map<string, ActivePathEntry>()
collectActivePaths(event.data, weights, false)
if (weights.size === 0) continue
const ordered = [...weights.values()].sort((left, right) => right.score - left.score)
const scores = new Map<string, { top: string; score: number }>()
for (const candidate of ordered) {
if (probes >= ACTIVE_ROOT_MAX_PROBES) break
probes += 1
const top = await topOfActivePath(candidate.path)
if (top === undefined) continue
const topKey = pathKeyOf(top)
if (topKey === baseKey) continue
if (!(await isLinkedWorktree(top))) continue
const entry = scores.get(topKey)
if (entry === undefined) scores.set(topKey, { top, score: candidate.score })
else entry.score += candidate.score
}
if (scores.size > 0) {
let best: string | undefined
let bestScore = -1
for (const entry of scores.values()) {
if (entry.score > bestScore) {
best = entry.top
bestScore = entry.score
}
}
if (best !== undefined) return best
}
if (probes >= ACTIVE_ROOT_MAX_PROBES) return undefined
}
return undefined
}
/** The session's live event log, or the persisted one while it is detached. */
async function sessionEventsOf(ctx: Context, sessionId: string): Promise<readonly SidebarSessionEvent[]> {
const live = ctx.sessions.get(sessionId)
// Session-store fakes in tests (and some embedders) expose only the header;
// a missing snapshot must degrade to the persisted log, never throw.
const events = typeof live?.snapshotEvents === 'function' ? live.snapshotEvents() : undefined
if (events !== undefined) return events
const persisted = await readPersistedSessionOf(ctx, sessionId)
return persisted?.events ?? []
}
/**
* The directory the sidebar should treat as the session's root.
*
* The cache is consulted BEFORE the probe, and concurrent asks for the same
* (session, base) share ONE in-flight resolution: twelve route handlers funnel
* through `cwdOf()`, so a burst of parallel `/sidebar/*` calls used to spawn a
* `git rev-parse` process per call.
* @param ctx - host plugin context.
* @param sessionId - session whose tool calls are inspected.
* @param base - the authoritative session cwd (already resolved by the caller).
* @returns the active linked worktree, the repository top level, or `base`.
*/
export async function activeWorktreeRootOf(ctx: Context, sessionId: string, base: string): Promise<string> {
if (base === '') return base
const override = (process.env[ACTIVE_ROOT_OVERRIDE_ENV] ?? '').trim()
if (override !== '' && isAbsolute(override)) return requireAbsolute(override)
// The comparison is on the raw base, so a session whose caller switched base
// (the `/sidebar/*` routes pass the client's own) never reuses the other
// one's root.
const cached = activeWorktreeRootCache.get(sessionId)
if (cached !== undefined && cached.expires > Date.now() && cached.base === base) {
cachePut(activeWorktreeRootCache, sessionId, cached, ACTIVE_ROOT_CACHE_MAX)
return cached.root
}
const flightKey = `${sessionId}\0${base}`
const pending = activeRootInFlight.get(flightKey)
if (pending !== undefined) return pending
const promise = resolveActiveWorktreeRoot(ctx, sessionId, base).then(
(root) => {
activeRootInFlight.delete(flightKey)
return root
},
(error: unknown) => {
activeRootInFlight.delete(flightKey)
throw error
},
)
activeRootInFlight.set(flightKey, promise)
return promise
}
/** One uncached resolution (see {@link activeWorktreeRootOf}). */
async function resolveActiveWorktreeRoot(ctx: Context, sessionId: string, base: string): Promise<string> {
const top = await topLevelOf(base)
if (top === undefined) {
// "Not inside a repository" is an answer too (the base itself), and caching
// it is what keeps a non-repo session from re-probing on every ask.
cachePut(activeWorktreeRootCache, sessionId, { root: base, base, expires: Date.now() + ACTIVE_ROOT_TTL_MS }, ACTIVE_ROOT_CACHE_MAX)
return base
}
const canonicalTop = requireAbsolute(top)
const effectiveBase = pathUnderOf(base, canonicalTop) ? base : canonicalTop
const active = await activeRootFromEvents(await sessionEventsOf(ctx, sessionId), effectiveBase)
const root = active ?? effectiveBase
cachePut(activeWorktreeRootCache, sessionId, { root, base, expires: Date.now() + ACTIVE_ROOT_TTL_MS }, ACTIVE_ROOT_CACHE_MAX)
return root
}