核查用户列出的性能断言后,两处「真实存在」的放大被收敛(其余项按用户选择不动, 逐条结论与不做的理由见 docs/plans/2026-10-13-file-tree-perf-fold-preload-and-probe-budget.md)。 - 客户端:compactLoadTargets 改为从渲染根(cwd + expanded)沿 compactChain 下行, 上限天然继承 MAX_COMPACT_DEPTH。此前它扫描所有已加载层:渲染在 32 停手,预加载 却继续请求第 33、34… 节,每节一次串行 POST 加一次全树 render。顺带订正 MAX_COMPACT_DEPTH 上方「健康缓存永不触及」的不准确注释(合法深链会触及; 循环守卫是 compactChain 的 visited)。 - 宿主:新增 PROBE_BUDGET / ProbeGate / createProbeGate,listDirectory 增加可选 gate 参数(只在真实 fs 探测外层取槽,缓存命中不花),fs.trees 每请求建一个 gate 传给它的所有层。此前 64 路径 × 每层 32 路可同开约 2048 个 opendir;单层与 fs.tree 单路径行为逐字不变。 测试:新增 tests/fs-tree-probe-gate.spec.ts(gate 在飞上界 / 排队任务抛错后释放且 FIFO / limit<1 夹紧;listDirectory 每个目录探测恰花一槽、缓存命中不花;三层共享 一个 gate 计 9 次);file-tree-compact(-fold) 新增「到上限即停」用例(把实现回退 即变红,判别力已实测);smoke 增加「一次 fs.trees 只建一个 gate」的路由级断言。 不动虚拟化、watch 提示与全局句柄、DirRow chain identity。
139 lines
5.2 KiB
TypeScript
139 lines
5.2 KiB
TypeScript
/**
|
||
* The request-wide probe budget behind `fs.trees`.
|
||
*
|
||
* One batch lists up to `FS_TREES_MAX_PATHS` levels concurrently, and each
|
||
* level runs its own bounded probe pool (a `stat` per symlink row, an
|
||
* `opendir` per directory row for the singleton fold mark). Without a shared
|
||
* budget those bounds multiply — a cold mount of a wide tree could hold
|
||
* levels × pool probes open at once. The bound itself is proved on the gate;
|
||
* these tests prove the WIRING: every probe of every level spends the one
|
||
* gate the request was given (and a cache hit spends nothing at all). The
|
||
* route's per-request gate SHARING is asserted in tests/smoke.spec.ts.
|
||
*/
|
||
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
|
||
import { tmpdir } from 'node:os'
|
||
import { join } from 'node:path'
|
||
import { describe, expect, it } from 'vitest'
|
||
import { createProbeGate, listDirectory, type ProbeGate } from '../src/fs-tree.ts'
|
||
|
||
/** A gate that only COUNTS acquisitions: it bounds nothing of its own, so the
|
||
* count it reports is exactly the probes the caller routed through it. */
|
||
function countingGate(): { gate: ProbeGate; calls: () => number } {
|
||
const state = { calls: 0 }
|
||
return {
|
||
gate: {
|
||
async run<T>(task: () => Promise<T>): Promise<T> {
|
||
state.calls += 1
|
||
return await task()
|
||
},
|
||
},
|
||
calls: () => state.calls,
|
||
}
|
||
}
|
||
|
||
describe('createProbeGate', () => {
|
||
it('keeps at most `limit` tasks in flight', async () => {
|
||
const gate = createProbeGate(2)
|
||
let live = 0
|
||
let peak = 0
|
||
await Promise.all(Array.from({ length: 8 }, () => gate.run(async () => {
|
||
live += 1
|
||
peak = Math.max(peak, live)
|
||
await new Promise(resolve => setTimeout(resolve, 1))
|
||
live -= 1
|
||
})))
|
||
expect(peak).toBe(2)
|
||
expect(live).toBe(0)
|
||
})
|
||
|
||
it('releases the slot when a queued task throws, in FIFO order', async () => {
|
||
const gate = createProbeGate(1)
|
||
const order: string[] = []
|
||
const first = gate.run(async () => {
|
||
await new Promise(resolve => setTimeout(resolve, 1))
|
||
order.push('first')
|
||
})
|
||
const failing = gate.run(async () => {
|
||
order.push('failing')
|
||
throw new Error('probe failed')
|
||
})
|
||
const last = gate.run(async () => {
|
||
order.push('last')
|
||
return 'ok'
|
||
})
|
||
await first
|
||
await expect(failing).rejects.toThrow('probe failed')
|
||
// A thrown probe must not strand the waiter behind it.
|
||
await expect(last).resolves.toBe('ok')
|
||
expect(order).toEqual(['first', 'failing', 'last'])
|
||
})
|
||
|
||
it('clamps a limit below one so the gate still runs', async () => {
|
||
const gate = createProbeGate(0)
|
||
const settled = await Promise.race([
|
||
gate.run(async () => 'ok'),
|
||
new Promise<string>(resolve => { setTimeout(() => { resolve('timed out') }, 100) }),
|
||
])
|
||
expect(settled).toBe('ok')
|
||
})
|
||
})
|
||
|
||
describe('listDirectory probe budget', () => {
|
||
it('spends one gate slot per directory probe, and none on a cache hit', async () => {
|
||
const dir = mkdtempSync(join(tmpdir(), 'dsh-sidebar-probe-gate-'))
|
||
try {
|
||
// Four singleton dirs (one child each) plus one ordinary dir: every
|
||
// directory row costs exactly one compact probe.
|
||
for (let index = 0; index < 4; index += 1) {
|
||
mkdirSync(join(dir, `only-${index}`))
|
||
writeFileSync(join(dir, `only-${index}`, 'child.txt'), 'x')
|
||
}
|
||
mkdirSync(join(dir, 'plain'))
|
||
writeFileSync(join(dir, 'plain', 'a.txt'), 'x')
|
||
writeFileSync(join(dir, 'plain', 'b.txt'), 'x')
|
||
|
||
const counted = countingGate()
|
||
const listing = await listDirectory(dir, 1000, undefined, counted.gate)
|
||
expect(counted.calls()).toBe(5)
|
||
expect(listing.entries.filter(entry => entry.compact === true).map(entry => entry.name).sort())
|
||
.toEqual(['only-0', 'only-1', 'only-2', 'only-3'])
|
||
|
||
// A fresh call is answered from the level cache, which runs no probe at
|
||
// all — so it must not spend budget either.
|
||
const spent = counted.calls()
|
||
const cached = await listDirectory(dir, 1000, undefined, counted.gate)
|
||
expect(counted.calls()).toBe(spent)
|
||
expect(cached).toBe(listing)
|
||
} finally {
|
||
rmSync(dir, { recursive: true, force: true })
|
||
}
|
||
})
|
||
|
||
it('routes a whole batch of levels through ONE shared gate', async () => {
|
||
const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-probe-batch-'))
|
||
try {
|
||
const levels: string[] = []
|
||
for (let index = 0; index < 3; index += 1) {
|
||
const level = join(root, `l${index}`)
|
||
mkdirSync(level)
|
||
for (let child = 0; child < 3; child += 1) {
|
||
mkdirSync(join(level, `s${child}`))
|
||
writeFileSync(join(level, `s${child}`, 'child.txt'), 'x')
|
||
}
|
||
levels.push(level)
|
||
}
|
||
|
||
const counted = countingGate()
|
||
// Exactly the shape the `fs.trees` route builds: one gate, many levels.
|
||
const listings = await Promise.all(levels.map(level => listDirectory(level, 1000, undefined, counted.gate)))
|
||
// 3 levels × 3 directory rows: every probe went through the shared gate.
|
||
expect(counted.calls()).toBe(9)
|
||
for (const listing of listings) {
|
||
expect(listing.entries.filter(entry => entry.compact === true)).toHaveLength(3)
|
||
}
|
||
} finally {
|
||
rmSync(root, { recursive: true, force: true })
|
||
}
|
||
})
|
||
})
|