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 项「临时回退→变红→还原」突变全部被捕获。
291 lines
12 KiB
TypeScript
291 lines
12 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { spawnSync } from 'node:child_process'
|
|
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { activeWorktreeRootOf } from '../src/active-worktree.ts'
|
|
import type { Context } from '../src/context-types.ts'
|
|
|
|
/** The plugin's git probe seam: call order plus an optional canned answer. */
|
|
const seam = vi.hoisted(() => ({
|
|
/** Every cwd handed to the plugin's own git probe, in call order. */
|
|
probed: [] as string[],
|
|
/**
|
|
* Optional canned answer per probe. Set it to exercise the process-wide
|
|
* caches (which are keyed per session) without paying one REAL git spawn per
|
|
* key; `probed` still records every call the module made.
|
|
*/
|
|
responder: null as null | ((cwd: string, args: string[]) => string),
|
|
}))
|
|
|
|
const probed = seam.probed
|
|
|
|
// Records (and delegates to) the real spawn so the case-folding regression is
|
|
// visible on case-insensitive hosts too.
|
|
vi.mock('../src/git.ts', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../src/git.ts')>()
|
|
return {
|
|
...actual,
|
|
runGit: (cwd: string, args: string[], timeoutMs?: number) => {
|
|
probed.push(cwd)
|
|
const canned = seam.responder?.(cwd, args)
|
|
return canned === undefined ? actual.runGit(cwd, args, timeoutMs) : Promise.resolve(canned)
|
|
},
|
|
}
|
|
})
|
|
|
|
const IDENTITY = {
|
|
GIT_AUTHOR_NAME: 'dsh-better-sidebar-test',
|
|
GIT_AUTHOR_EMAIL: 'test@dsh.invalid',
|
|
GIT_COMMITTER_NAME: 'dsh-better-sidebar-test',
|
|
GIT_COMMITTER_EMAIL: 'test@dsh.invalid',
|
|
}
|
|
|
|
function git(cwd: string, args: string[]): string {
|
|
// `core.hooksPath=` keeps a developer's global commit-msg hook out of the scratch repos.
|
|
const result = spawnSync('git', ['-c', 'core.hooksPath=', '-C', cwd, ...args], {
|
|
encoding: 'utf8',
|
|
env: { ...process.env, ...IDENTITY },
|
|
})
|
|
if (result.status !== 0) throw new Error(result.stderr || `git ${args.join(' ')} failed`)
|
|
return result.stdout
|
|
}
|
|
|
|
function initRepo(path: string, file: string): void {
|
|
mkdirSync(path, { recursive: true })
|
|
git(path, ['init', '-q'])
|
|
git(path, ['checkout', '-q', '-b', 'main'])
|
|
writeFileSync(join(path, file), 'base\n')
|
|
git(path, ['add', '-A'])
|
|
git(path, ['commit', '-q', '-m', 'base'])
|
|
}
|
|
|
|
/** A minimal live-session face: header cwd plus the tool/call events to scan. */
|
|
function sessionCtx(cwd: string, calls: Array<Record<string, unknown>>): Context {
|
|
return {
|
|
sessions: {
|
|
get: () => ({
|
|
header: { cwd },
|
|
snapshotEvents: () => calls.map((data, index) => ({ type: 'tool/call', seq: index, time: index, data })),
|
|
}),
|
|
},
|
|
get: () => undefined,
|
|
} as never
|
|
}
|
|
|
|
/**
|
|
* The same live-session face with the session LOOKUP counted. The root cache is
|
|
* consulted before the session is ever read, so `lookups()` is the observable
|
|
* difference between "answered from the TTL cache" and "scanned the event log
|
|
* again" — which is what makes the no-rescan assertion below discriminative.
|
|
*/
|
|
function countingSessionCtx(cwd: string, calls: Array<Record<string, unknown>>): { ctx: Context; lookups: () => number } {
|
|
let lookups = 0
|
|
const ctx = {
|
|
sessions: {
|
|
get: () => {
|
|
lookups += 1
|
|
return {
|
|
header: { cwd },
|
|
snapshotEvents: () => calls.map((data, index) => ({ type: 'tool/call', seq: index, time: index, data })),
|
|
}
|
|
},
|
|
},
|
|
get: () => undefined,
|
|
} as never
|
|
return { ctx: ctx as Context, lookups: () => lookups }
|
|
}
|
|
|
|
describe('active worktree root', () => {
|
|
it('follows a linked worktree of a DIFFERENT repository named by the tool calls', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repoA = join(root, 'repo-a')
|
|
const repoB = join(root, 'repo-b')
|
|
const worktreeB = join(root, 'worktree-b')
|
|
try {
|
|
initRepo(repoA, 'a.txt')
|
|
initRepo(repoB, 'b.txt')
|
|
git(repoB, ['worktree', 'add', '-q', '-b', 'agent', worktreeB])
|
|
// The session's header cwd is repo A, but its newest tool call works in
|
|
// a linked worktree of repo B the sidebar must follow that worktree.
|
|
const ctx = sessionCtx(repoA, [{ cwd: worktreeB, path: join(worktreeB, 'b.txt') }])
|
|
// The roots come back from `git rev-parse --show-toplevel`, i.e. already
|
|
// canonical: compare against the real path (macOS tmpdirs live under the
|
|
// `/var` → `/private/var` symlink; same convention as git-worktree.spec).
|
|
await expect(activeWorktreeRootOf(ctx, 's-cross-repo', repoA)).resolves.toBe(realpathSync.native(worktreeB))
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('keeps the session cwd when no tool call points at a linked worktree', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repo = join(root, 'repo')
|
|
try {
|
|
initRepo(repo, 'a.txt')
|
|
const ctx = sessionCtx(repo, [{ cwd: join(repo, 'src') }])
|
|
await expect(activeWorktreeRootOf(ctx, 's-no-worktree', repo)).resolves.toBe(realpathSync.native(repo))
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('probes the REAL path, never its case-folded identity (Linux is case-sensitive)', async () => {
|
|
// `addActivePath` keys candidates by a lowercased identity; probing that
|
|
// KEY instead of the path silently drops every candidate whose path has an
|
|
// uppercase letter. macOS and Windows fold case and never noticed it —
|
|
// `Repo-A` / `WorkTree-B` below make the difference visible on every host.
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repoA = join(root, 'Repo-A')
|
|
const repoB = join(root, 'Repo-B')
|
|
const worktreeB = join(root, 'WorkTree-B')
|
|
try {
|
|
initRepo(repoA, 'a.txt')
|
|
initRepo(repoB, 'b.txt')
|
|
git(repoB, ['worktree', 'add', '-q', '-b', 'agent', worktreeB])
|
|
probed.length = 0
|
|
const ctx = sessionCtx(repoA, [{ cwd: worktreeB, path: join(worktreeB, 'b.txt') }])
|
|
await expect(activeWorktreeRootOf(ctx, 's-case', repoA)).resolves.toBe(realpathSync.native(worktreeB))
|
|
expect(probed).toContain(worktreeB)
|
|
expect(probed).not.toContain(worktreeB.toLowerCase())
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
/**
|
|
* Steady-state cost: twelve `/sidebar/*` route handlers funnel through
|
|
* `cwdOf()`, so a burst of parallel calls for one session used to spawn one
|
|
* `git rev-parse` per call. The in-flight maps collapse the whole
|
|
* (session, base) resolution into one.
|
|
*/
|
|
it('shares ONE base git spawn across a burst of concurrent asks', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repo = join(root, 'repo')
|
|
try {
|
|
initRepo(repo, 'a.txt')
|
|
const { ctx, lookups } = countingSessionCtx(repo, [])
|
|
probed.length = 0
|
|
const answers = await Promise.all(
|
|
Array.from({ length: 8 }, () => activeWorktreeRootOf(ctx, 's-burst', repo)),
|
|
)
|
|
expect(probed).toEqual([repo])
|
|
// The whole resolution is shared, not just the spawn: the event log is
|
|
// read (and scanned) once for the burst.
|
|
expect(lookups()).toBe(1)
|
|
// Every concurrent caller still gets the resolved answer.
|
|
expect(new Set(answers)).toEqual(new Set([realpathSync.native(repo)]))
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
/**
|
|
* Different sessions rooted in the SAME repository share the base probe:
|
|
* without the per-path in-flight map, N tabs of one workspace spawn N
|
|
* `git rev-parse` processes at once. Each session still resolves on its own
|
|
* (its own event log decides its root).
|
|
*/
|
|
it('shares ONE base probe across different sessions of one repository', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repo = join(root, 'repo')
|
|
try {
|
|
initRepo(repo, 'a.txt')
|
|
const { ctx, lookups } = countingSessionCtx(repo, [])
|
|
probed.length = 0
|
|
await Promise.all(
|
|
Array.from({ length: 6 }, (_unused, index) => activeWorktreeRootOf(ctx, `s-shared-${index}`, repo)),
|
|
)
|
|
expect(probed).toEqual([repo])
|
|
expect(lookups()).toBe(6)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
/** The TTL is what makes the client's 5 s poll free; 3 s made every poll a miss. */
|
|
it('answers a second ask inside the TTL without scanning or spawning', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repo = join(root, 'repo')
|
|
try {
|
|
initRepo(repo, 'a.txt')
|
|
mkdirSync(join(repo, 'src'))
|
|
// A tool call INSIDE the repository: scanning it costs a probe, and the
|
|
// answer is the repository top level.
|
|
const { ctx, lookups } = countingSessionCtx(repo, [{ cwd: join(repo, 'src') }])
|
|
const first = await activeWorktreeRootOf(ctx, 's-ttl', repo)
|
|
expect(first).toBe(realpathSync.native(repo))
|
|
expect(lookups()).toBe(1)
|
|
expect(probed.length).toBeGreaterThan(0)
|
|
|
|
probed.length = 0
|
|
await expect(activeWorktreeRootOf(ctx, 's-ttl', repo)).resolves.toBe(first)
|
|
// No spawn AND no event-log scan (the cache is consulted before the
|
|
// session is read, so the scan never starts).
|
|
expect(probed).toEqual([])
|
|
expect(lookups()).toBe(1)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
/**
|
|
* The client's per-tab poll is every 5 s, so a 3 s TTL made every poll a
|
|
* miss. The cached root must survive past the old window (10 s in, still a
|
|
* hit) — the clock is moved, not the test.
|
|
*/
|
|
it('serves the cached root 10 s later (longer than the old 3 s TTL)', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
const repo = join(root, 'repo')
|
|
try {
|
|
initRepo(repo, 'a.txt')
|
|
const { ctx, lookups } = countingSessionCtx(repo, [])
|
|
const first = await activeWorktreeRootOf(ctx, 's-ttl-span', repo)
|
|
probed.length = 0
|
|
const clock = vi.spyOn(Date, 'now').mockReturnValue(Date.now() + 10_000)
|
|
try {
|
|
await expect(activeWorktreeRootOf(ctx, 's-ttl-span', repo)).resolves.toBe(first)
|
|
} finally {
|
|
clock.mockRestore()
|
|
}
|
|
expect(probed).toEqual([])
|
|
expect(lookups()).toBe(1)
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
/**
|
|
* The three process-wide maps are bounded LRUs. The module exposes no test
|
|
* seam, so the bound is observed the only way it is observable from outside:
|
|
* after `cap` distinct sessions, the OLDEST session's next ask is a miss
|
|
* again (it was evicted) while the youngest ones still hit.
|
|
*/
|
|
it('bounds the root cache: the oldest session is evicted past the cap', async () => {
|
|
const root = mkdtempSync(join(tmpdir(), 'dsh-active-root-'))
|
|
try {
|
|
// The canned answer keeps the 257 probes from being 257 real processes;
|
|
// `probed` still counts every probe the module made.
|
|
seam.responder = (cwd) => `${cwd}\n`
|
|
const ctx = sessionCtx(root, [])
|
|
const cap = 256
|
|
for (let index = 0; index <= cap; index += 1) {
|
|
await activeWorktreeRootOf(ctx, `lru-${index}`, root)
|
|
}
|
|
expect(probed).toHaveLength(cap + 1)
|
|
|
|
probed.length = 0
|
|
// The oldest SURVIVOR and the newest key are still cached: no probe.
|
|
await activeWorktreeRootOf(ctx, 'lru-1', root)
|
|
await activeWorktreeRootOf(ctx, `lru-${cap}`, root)
|
|
expect(probed).toEqual([])
|
|
// Inserted first, so it is the one the cap evicted: re-asked, it probes.
|
|
await activeWorktreeRootOf(ctx, 'lru-0', root)
|
|
expect(probed).toEqual([root])
|
|
} finally {
|
|
seam.responder = null
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|