Files
archived-orca/src/relay/git-handler-status-ops.test.ts
T
Jinjing f1d2fe5c65 perf(git-status): duty-cycle, cancel, and cache status polling to cut idle git load (#8922)
* feat(git-status): batch, cancel, and cache git status polling to cut idl

- Add a single duty-cycled refresh scheduler (activity debounce + 60s
  safety timer) replacing multiple overlapping intervals, so status
  polling no longer runs near-continuously on large repos (#7983).
- Let safety refreshes reuse cached numstat line counts instead of
  re-running diff --numstat every cycle, invalidated by head change,
  known mutations, and a bounded TTL.
- Thread AbortSignal/request-token cancellation through IPC, RPC, and
  relay layers so a superseded or backgrounded git:status call is
  killed instead of finishing wastefully.
- Fix automatic upstream/status apply ordering so a slow, older
  refresh can no longer clobber a newer result, and so an earlier
  refresh still applies when a later one fails.

* Fix aborted git status scans being mistaken for completed empty results

- An aborted scan/numstat pass now always rejects instead of silently
  resolving, so a cancelled request can't look like a valid empty status.
- Stop clearing the line-stats cache key on abort, since an aborted pass
  never wrote to it — clearing was evicting a concurrent scan's healthy
  snapshot and forcing a redundant numstat recompute.

* Fix aborted git status scans resolving as completed results

Cancelled scans could still resolve with partial or stale data instead of
rejecting, letting callers treat an aborted refresh as a valid status. Also
stop counting aborted scan duration toward catch-up refresh pacing, which
was stretching the next refresh interval by the full length of a cancelled
(often slow) scan.

* Add cancellable, generation-aware git status polling to cut stale scans

- Route git.status through an abortable subscription per requestToken so
  cancelStatus can actually abort the remote scan instead of being a
  no-op, preventing wasted work and stale responses overwriting fresher
  state.
- Bump the git status polling generation on push-target changes so an
  in-flight refresh against the old remote/branch can't apply stale
  upstream data to the new one.
- Guard the stale-conflict poller against writes after unmount.
- Retire pre-purge line-stat scans in the cache so an older in-flight
  scan can't repopulate a key after a token-scoped purge.
2026-07-15 19:09:14 -07:00

295 lines
11 KiB
TypeScript

import { mkdtempSync } from 'node:fs'
import * as fs from 'node:fs/promises'
import { tmpdir } from 'node:os'
import * as path from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import type { GitExec } from './git-handler-ops'
import { getStatusOp } from './git-handler-status-ops'
import { clearNoEffectiveUpstreamStatusCache } from './git-status-upstream-negative-cache'
import { clearGitStatusLineStatsCache } from '../shared/git-status-line-stats-cache'
const LARGE_STATUS_ENTRY_COUNT = 150_000
function buildLargeStatusOutput(count: number): string {
const lines: string[] = []
for (let index = 0; index < count; index += 1) {
lines.push(`1 A. N... 100644 100644 100644 000000 111111 generated-${index}.txt`)
}
return lines.join('\n')
}
function buildBranchStatusOutput(head: string, branch: string): string {
return [`# branch.oid ${head}`, `# branch.head ${branch}`].join('\n')
}
describe('getStatusOp', () => {
let tmpDir: string
beforeEach(() => {
clearNoEffectiveUpstreamStatusCache()
clearGitStatusLineStatsCache()
tmpDir = mkdtempSync(path.join(tmpdir(), 'relay-git-status-'))
})
afterEach(async () => {
vi.useRealTimers()
clearNoEffectiveUpstreamStatusCache()
await fs.rm(tmpDir, { recursive: true, force: true })
})
it('truncates huge status lists at the limit and flags didHitLimit', async () => {
const statusOutput = buildLargeStatusOutput(LARGE_STATUS_ENTRY_COUNT)
const git = vi.fn<GitExec>(async (args) => {
if (args.includes('status')) {
return { stdout: statusOutput, stderr: '' }
}
if (args.includes('diff')) {
return { stdout: '', stderr: '' }
}
throw new Error(`Unexpected git command: ${args.join(' ')}`)
})
const result = await getStatusOp(git, { worktreePath: tmpDir, limit: 10_000 })
expect(result.didHitLimit).toBe(true)
expect(result.statusLength).toBe(LARGE_STATUS_ENTRY_COUNT)
expect(result.entries).toHaveLength(10_000)
expect(result.entries[0]).toEqual({
path: 'generated-0.txt',
status: 'added',
area: 'staged'
})
// numstat (diff) must be skipped when the limit was hit.
expect(git.mock.calls.some(([args]) => args.includes('diff'))).toBe(false)
})
it('returns the full list and no limit flag when under the limit', async () => {
const statusOutput = buildLargeStatusOutput(5)
const git = vi.fn<GitExec>(async (args) => {
if (args.includes('status')) {
return { stdout: statusOutput, stderr: '' }
}
if (args.includes('diff')) {
return { stdout: '', stderr: '' }
}
throw new Error(`Unexpected git command: ${args.join(' ')}`)
})
const result = await getStatusOp(git, { worktreePath: tmpDir, limit: 10_000 })
expect(result.didHitLimit).toBeUndefined()
expect(result.entries).toHaveLength(5)
})
it('reuses unchanged line stats only for hinted safety reads', async () => {
const statusOutput = `${buildBranchStatusOutput('head-1', '(detached)')}\n1 .M N... 100644 100644 100644 aaaa aaaa src/a.ts`
const git = vi.fn<GitExec>(async (args) => {
if (args.includes('status')) {
return { stdout: statusOutput, stderr: '' }
}
if (args.includes('diff')) {
return { stdout: '3\t2\tsrc/a.ts\n', stderr: '' }
}
throw new Error(`Unexpected git command: ${args.join(' ')}`)
})
await getStatusOp(git, { worktreePath: tmpDir })
const reused = await getStatusOp(git, { worktreePath: tmpDir, reuseLineStats: true })
await getStatusOp(git, { worktreePath: tmpDir })
expect(reused.entries).toContainEqual(
expect.objectContaining({ path: 'src/a.ts', added: 3, removed: 2 })
)
expect(git.mock.calls.filter(([args]) => args.includes('diff'))).toHaveLength(2)
})
it('forwards the request abort signal to status and numstat subprocesses', async () => {
const controller = new AbortController()
const git = vi.fn<GitExec>(async (args) => {
if (args.includes('status')) {
return {
stdout: '1 .M N... 100644 100644 100644 aaaa aaaa src/a.ts',
stderr: ''
}
}
if (args.includes('diff')) {
return { stdout: '1\t0\tsrc/a.ts\n', stderr: '' }
}
throw new Error(`Unexpected git command: ${args.join(' ')}`)
})
await getStatusOp(git, { worktreePath: tmpDir }, { signal: controller.signal })
expect(git.mock.calls).not.toHaveLength(0)
for (const [, , options] of git.mock.calls) {
expect(options?.signal).toBe(controller.signal)
}
})
it('caches no-effective-upstream probes across status polls for the same head', async () => {
const git = vi.fn<GitExec>(async (args) => {
if (args.includes('status')) {
return { stdout: buildBranchStatusOutput('abc123', 'feature'), stderr: '' }
}
if (args[0] === 'symbolic-ref') {
return { stdout: 'feature\n', stderr: '' }
}
if (args[0] === 'rev-parse' && args.includes('HEAD@{u}')) {
throw new Error('fatal: no upstream configured for branch feature')
}
throw new Error(`No upstream fixture for git ${args.join(' ')}`)
})
const first = await getStatusOp(git, { worktreePath: tmpDir })
const firstCallCount = git.mock.calls.length
const second = await getStatusOp(git, { worktreePath: tmpDir })
expect(first.upstreamStatus).toEqual({ hasUpstream: false, ahead: 0, behind: 0 })
expect(second.upstreamStatus).toEqual(first.upstreamStatus)
expect(git.mock.calls).toHaveLength(firstCallCount + 1)
expect(
git.mock.calls.filter(([args]) => args[0] === 'rev-parse' && args.includes('HEAD@{u}'))
).toHaveLength(1)
expect(
git.mock.calls.filter(
([args]) => args[0] === 'rev-parse' && args.includes('refs/remotes/origin/feature')
)
).toHaveLength(1)
})
it('keeps no-effective-upstream probes cached beyond thirty seconds', async () => {
vi.useFakeTimers()
vi.setSystemTime(0)
const git = vi.fn<GitExec>(async (args) => {
if (args.includes('status')) {
return { stdout: buildBranchStatusOutput('abc123', 'feature'), stderr: '' }
}
if (args[0] === 'symbolic-ref') {
return { stdout: 'feature\n', stderr: '' }
}
if (args[0] === 'rev-parse' && args.includes('HEAD@{u}')) {
throw new Error('fatal: no upstream configured for branch feature')
}
if (args[0] === 'rev-parse' && args.includes('refs/remotes/origin/feature')) {
throw new Error('missing remote branch')
}
throw new Error(`No upstream fixture for git ${args.join(' ')}`)
})
await getStatusOp(git, { worktreePath: tmpDir })
vi.setSystemTime(31_000)
await getStatusOp(git, { worktreePath: tmpDir })
expect(
git.mock.calls.filter(([args]) => args[0] === 'rev-parse' && args.includes('HEAD@{u}'))
).toHaveLength(1)
})
it('coalesces concurrent no-effective-upstream probes', async () => {
const git = vi.fn<GitExec>(async (args) => {
if (args.includes('status')) {
return { stdout: buildBranchStatusOutput('abc123', 'feature'), stderr: '' }
}
if (args[0] === 'symbolic-ref') {
return { stdout: 'feature\n', stderr: '' }
}
if (args[0] === 'rev-parse' && args.includes('HEAD@{u}')) {
await Promise.resolve()
throw new Error('fatal: no upstream configured for branch feature')
}
if (args[0] === 'rev-parse' && args.includes('refs/remotes/origin/feature')) {
await Promise.resolve()
throw new Error('missing remote branch')
}
throw new Error(`No upstream fixture for git ${args.join(' ')}`)
})
await Promise.all([
getStatusOp(git, { worktreePath: tmpDir }),
getStatusOp(git, { worktreePath: tmpDir }),
getStatusOp(git, { worktreePath: tmpDir })
])
expect(
git.mock.calls.filter(([args]) => args[0] === 'rev-parse' && args.includes('HEAD@{u}'))
).toHaveLength(1)
expect(
git.mock.calls.filter(
([args]) => args[0] === 'rev-parse' && args.includes('refs/remotes/origin/feature')
)
).toHaveLength(1)
})
it('invalidates cached no-effective-upstream probes when the branch changes', async () => {
let branch = 'feature'
const git = vi.fn<GitExec>(async (args) => {
if (args.includes('status')) {
return { stdout: buildBranchStatusOutput('abc123', branch), stderr: '' }
}
if (args[0] === 'symbolic-ref') {
return { stdout: `${branch}\n`, stderr: '' }
}
if (args[0] === 'rev-parse' && args.includes('HEAD@{u}')) {
throw new Error(`fatal: no upstream configured for branch ${branch}`)
}
if (args[0] === 'rev-parse' && args.some((arg) => arg.startsWith('refs/remotes/origin/'))) {
throw new Error('missing remote branch')
}
throw new Error(`No upstream fixture for git ${args.join(' ')}`)
})
await getStatusOp(git, { worktreePath: tmpDir })
branch = 'other-feature'
await getStatusOp(git, { worktreePath: tmpDir })
expect(
git.mock.calls
.filter(
([args]) =>
args[0] === 'rev-parse' && args.some((arg) => arg.startsWith('refs/remotes/origin/'))
)
.map(([args]) => args.at(-1))
).toEqual(['refs/remotes/origin/feature', 'refs/remotes/origin/other-feature'])
})
it('does not cache a configured push target signal', async () => {
const git = vi.fn<GitExec>(async (args) => {
if (args.includes('status')) {
return { stdout: buildBranchStatusOutput('abc123', 'feature/fix'), stderr: '' }
}
if (args[0] === 'symbolic-ref') {
return { stdout: 'feature/fix\n', stderr: '' }
}
if (args[0] === 'rev-parse' && args.includes('HEAD@{u}')) {
throw new Error('fatal: no upstream configured for branch feature/fix')
}
if (args[0] === 'config' && args.includes('branch.feature/fix.pushRemote')) {
return { stdout: 'fork\n', stderr: '' }
}
if (args[0] === 'config' && args.includes('remote.pushDefault')) {
throw new Error('missing push default')
}
if (args[0] === 'config' && args.includes('branch.feature/fix.remote')) {
return { stdout: 'fork\n', stderr: '' }
}
if (args[0] === 'config' && args.includes('branch.feature/fix.merge')) {
return { stdout: 'refs/heads/feature/fix\n', stderr: '' }
}
if (args[0] === 'config' && args.includes('branch.feature/fix.base')) {
throw new Error('missing branch base')
}
if (args[0] === 'rev-parse' && args.some((arg) => arg.startsWith('refs/remotes/'))) {
throw new Error('missing remote branch')
}
throw new Error(`No upstream fixture for git ${args.join(' ')}`)
})
await getStatusOp(git, { worktreePath: tmpDir })
await getStatusOp(git, { worktreePath: tmpDir })
expect(
git.mock.calls.filter(([args]) => args[0] === 'rev-parse' && args.includes('HEAD@{u}'))
).toHaveLength(2)
})
})