git.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /** Git command bounds, snapshot recovery, and diff failure reporting. */
  2. import { chmod, mkdir, readdir, readFile, realpath, writeFile } from 'node:fs/promises'
  3. import { join } from 'node:path'
  4. import { afterEach, describe, expect, it } from 'vitest'
  5. import { Context } from '@deepseek-ai/cordis'
  6. import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local'
  7. import { GitRunner, blobText, diffTrees, ignoredPaths, locateGitWorkspace, snapshotTree, treeBlob } from '../src/git.ts'
  8. import { TurnRecorder } from '../src/recorder.ts'
  9. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  10. import { git, scratchDir, startTurn, toolCall } from './support.ts'
  11. /** An object directory factory under a scratch root. */
  12. const objectsIn = (root: string) => () => Promise.resolve(join(root, 'objects'))
  13. const cleanups: Array<() => Promise<unknown>> = []
  14. afterEach(async () => {
  15. for (const cleanup of cleanups.reverse()) await cleanup()
  16. cleanups.length = 0
  17. })
  18. async function runner(limits = { timeoutMs: 30_000, outputMaxBytes: 1024 * 1024 }, executable = 'git') {
  19. const ctx = new Context()
  20. cleanups.push(() => ctx.fiber.dispose())
  21. await ctx.plugin(SessionStore)
  22. await ctx.plugin(LocalSubprocessRuntime)
  23. return { ctx, git: new GitRunner(ctx.subprocess, await ctx.subprocess.resolveExecutable(executable).catch(() => executable), limits) }
  24. }
  25. const signal = new AbortController().signal
  26. describe('GitRunner', () => {
  27. it('reports timeouts and external aborts as failures', async () => {
  28. const cwd = await scratchDir('dsh-git-runner-', cleanups)
  29. const { git: slow } = await runner({ timeoutMs: 1, outputMaxBytes: 1024 })
  30. await expect(slow.run(['--version'], { cwd, signal })).rejects.toThrow('timed out after 1ms')
  31. const { git: quick } = await runner()
  32. const aborted = new AbortController()
  33. setTimeout(() => { aborted.abort() }, 0)
  34. await expect(quick.run(['--version'], { cwd, signal: aborted.signal })).rejects.toThrow('git --version was aborted')
  35. const ok = await quick.run(['--version'], { cwd, signal })
  36. expect(ok.exitCode).toBe(0)
  37. expect(ok.stdout).toContain('git version')
  38. })
  39. })
  40. describe('snapshots and diffs', () => {
  41. it('snapshots a repository whose index holds unmerged entries without touching that index', async () => {
  42. const cwd = await scratchDir('dsh-git-conflict-', cleanups)
  43. git(cwd, 'init', '-q', '-b', 'main')
  44. await writeFile(join(cwd, 'f.txt'), 'base\n')
  45. git(cwd, 'add', '-A'); git(cwd, 'commit', '-q', '-m', 'base')
  46. git(cwd, 'checkout', '-q', '-b', 'side')
  47. await writeFile(join(cwd, 'f.txt'), 'side\n')
  48. git(cwd, 'commit', '-q', '-am', 'side')
  49. git(cwd, 'checkout', '-q', 'main')
  50. await writeFile(join(cwd, 'f.txt'), 'main\n')
  51. git(cwd, 'commit', '-q', '-am', 'main')
  52. expect(() => git(cwd, 'merge', 'side')).toThrow()
  53. expect(git(cwd, 'status', '--porcelain')).toContain('UU f.txt')
  54. const { git: runnerGit } = await runner()
  55. const workspace = await locateGitWorkspace(runnerGit, cwd, objectsIn(await scratchDir('dsh-git-store-', cleanups)), signal)
  56. expect(workspace?.root).toBe(await realpath(cwd))
  57. const tree = await snapshotTree(runnerGit, workspace!, signal)
  58. expect(tree).toMatch(/^[0-9a-f]{40,64}$/)
  59. expect(git(cwd, 'status', '--porcelain')).toContain('UU f.txt')
  60. })
  61. it('fails loudly when the addressed repository cannot be written or diffed', async () => {
  62. const cwd = await scratchDir('dsh-git-broken-', cleanups)
  63. const { git: runnerGit } = await runner()
  64. const store = objectsIn(await scratchDir('dsh-git-store-', cleanups))
  65. expect(await locateGitWorkspace(runnerGit, cwd, store, signal)).toBeNull()
  66. const broken = { root: cwd, gitDir: join(cwd, 'missing'), scratch: cwd, env: {}, excludes: [] }
  67. await expect(snapshotTree(runnerGit, broken, signal)).rejects.toThrow('git add in')
  68. await expect(ignoredPaths(runnerGit, broken, ['x'], signal)).rejects.toThrow('git check-ignore failed')
  69. expect(await ignoredPaths(runnerGit, broken, [], signal)).toEqual(new Set())
  70. git(cwd, 'init', '-q')
  71. const workspace = (await locateGitWorkspace(runnerGit, cwd, store, signal))!
  72. await expect(diffTrees(runnerGit, workspace, 'a'.repeat(40), 'b'.repeat(40), signal)).rejects.toThrow('git diff-tree failed')
  73. await writeFile(join(cwd, 'many.txt'), Array.from({ length: 50 }, (_, index) => `line ${index}`).join('\n'))
  74. const before = await snapshotTree(runnerGit, workspace, signal)
  75. await writeFile(join(cwd, 'many.txt'), 'gone')
  76. for (let index = 0; index < 20; index += 1) await writeFile(join(cwd, `file-${index}.txt`), 'x\n')
  77. const after = await snapshotTree(runnerGit, workspace, signal)
  78. const { git: tiny } = await runner({ timeoutMs: 30_000, outputMaxBytes: 16 })
  79. await expect(diffTrees(tiny, workspace, before, after, signal)).rejects.toThrow('exceeded')
  80. expect((await diffTrees(runnerGit, workspace, before, after, signal)).length).toBe(21)
  81. })
  82. })
  83. describe('repository edge cases', () => {
  84. it.skipIf(process.platform === 'win32')('snapshots past an unreadable file', async () => {
  85. const cwd = await scratchDir('dsh-git-unreadable-', cleanups)
  86. git(cwd, 'init', '-q', '-b', 'main')
  87. await writeFile(join(cwd, 'ok.txt'), 'ok\n')
  88. await writeFile(join(cwd, 'locked.txt'), 'locked\n')
  89. await chmod(join(cwd, 'locked.txt'), 0o000)
  90. cleanups.push(() => chmod(join(cwd, 'locked.txt'), 0o644))
  91. const { git: runnerGit } = await runner()
  92. const store = objectsIn(await scratchDir('dsh-git-store-', cleanups))
  93. const workspace = (await locateGitWorkspace(runnerGit, cwd, store, signal))!
  94. expect(await snapshotTree(runnerGit, workspace, signal)).toMatch(/^[0-9a-f]{40,64}$/)
  95. })
  96. it('refuses an index that exists but cannot be copied instead of starting from an empty one', async () => {
  97. const cwd = await scratchDir('dsh-git-bad-index-', cleanups)
  98. git(cwd, 'init', '-q', '-b', 'main')
  99. await mkdir(join(cwd, '.git', 'index'))
  100. const { git: runnerGit } = await runner()
  101. const store = objectsIn(await scratchDir('dsh-git-store-', cleanups))
  102. const workspace = (await locateGitWorkspace(runnerGit, cwd, store, signal))!
  103. await expect(snapshotTree(runnerGit, workspace, signal)).rejects.toThrow()
  104. })
  105. it('reports a repository git cannot read instead of treating it as absent', async () => {
  106. const cwd = await scratchDir('dsh-git-unsupported-', cleanups)
  107. git(cwd, 'init', '-q')
  108. const config = join(cwd, '.git', 'config')
  109. const original = await readFile(config, 'utf8')
  110. await writeFile(config, original.replace(/repositoryformatversion = \d+/, 'repositoryformatversion = 99'))
  111. const { git: runnerGit } = await runner()
  112. const store = objectsIn(await scratchDir('dsh-git-store-', cleanups))
  113. await expect(locateGitWorkspace(runnerGit, cwd, store, signal)).rejects.toThrow('git rev-parse failed')
  114. await writeFile(config, original)
  115. const blocked = join(cwd, 'store-file')
  116. await writeFile(blocked, 'not a directory')
  117. await expect(locateGitWorkspace(runnerGit, cwd, objectsIn(blocked), signal)).rejects.toThrow()
  118. })
  119. })
  120. describe('treeBlob and blobText', () => {
  121. it('locates a blob by its literal path, sizes it, reads it, and reports trees and missing paths as null', async () => {
  122. const cwd = await scratchDir('dsh-git-blob-', cleanups)
  123. git(cwd, 'init', '-q', '-b', 'main')
  124. await mkdir(join(cwd, 'dir'))
  125. await writeFile(join(cwd, 'dir', 'inner.txt'), 'inner\n')
  126. await writeFile(join(cwd, 'a[1].txt'), 'bracket\n')
  127. await writeFile(join(cwd, 'a1.txt'), 'plain\n')
  128. const { git: runnerGit } = await runner()
  129. const workspace = await locateGitWorkspace(runnerGit, cwd, objectsIn(await scratchDir('dsh-git-store-', cleanups)), signal)
  130. if (workspace === null) throw new Error('repository not located')
  131. const tree = await snapshotTree(runnerGit, workspace, signal)
  132. const bracket = await treeBlob(runnerGit, workspace, tree, 'a[1].txt', signal)
  133. expect(bracket).toEqual({ oid: git(cwd, 'hash-object', 'a[1].txt').trim(), size: 8 })
  134. expect(await blobText(runnerGit, workspace, bracket!.oid, 64, signal)).toBe('bracket\n')
  135. expect(await treeBlob(runnerGit, workspace, tree, 'dir', signal)).toBeNull()
  136. expect(await treeBlob(runnerGit, workspace, tree, 'missing.txt', signal)).toBeNull()
  137. expect(await treeBlob(runnerGit, workspace, tree, 'dir/inner.txt', signal)).toMatchObject({ size: 6 })
  138. })
  139. })
  140. describe('TurnRecorder', () => {
  141. it('stays silent when disposed while git work is pending, and warns on failures otherwise', async () => {
  142. const cwd = await scratchDir('dsh-recorder-', cleanups)
  143. const { ctx, git: runnerGit } = await runner()
  144. const session = ctx.sessions.create(SessionId('recorder'), { meta: { cwd } })
  145. const warnings: string[] = []
  146. let release!: (runner: GitRunner | null) => void
  147. const gate = new Promise<GitRunner | null>((resolve) => { release = resolve })
  148. const tempRoot = await scratchDir('dsh-git-store-', cleanups)
  149. const env = { git: gate, tempRoot, maxFiles: 10, maxFileBytes: 1024, diffTimeoutMs: 100, warn: (m: string) => { warnings.push(m) } }
  150. const disposed = new TurnRecorder(session, cwd, env)
  151. disposed.start(1)
  152. await new Promise(resolve => setTimeout(resolve, 5))
  153. const disposal = disposed.dispose()
  154. release(runnerGit)
  155. await disposal
  156. disposed.start(2)
  157. await disposed.settled()
  158. expect(warnings).toEqual([])
  159. expect(disposed.summary(1)).toBeUndefined()
  160. const { git: missing } = await runner(undefined, '/nonexistent/git-binary')
  161. const failing = new TurnRecorder(session, cwd, { ...env, git: Promise.resolve(missing) })
  162. failing.start(1)
  163. await failing.settled()
  164. expect(warnings).toHaveLength(1)
  165. expect(warnings[0]).toContain('workspace-changes:')
  166. })
  167. it('removes its snapshot objects on disposal and never creates them outside a repository', async () => {
  168. const tempRoot = await scratchDir('dsh-git-store-', cleanups)
  169. const { ctx, git: runnerGit } = await runner()
  170. const env = {
  171. git: Promise.resolve(runnerGit), tempRoot, maxFiles: 10, maxFileBytes: 1024, diffTimeoutMs: 100,
  172. warn: (m: string) => { throw new Error(m) },
  173. }
  174. const plain = new TurnRecorder(ctx.sessions.create(SessionId('plain'), { meta: { cwd: tempRoot } }), tempRoot, env)
  175. plain.start(1)
  176. await plain.settled()
  177. await plain.dispose()
  178. const cwd = await scratchDir('dsh-recorder-repo-', cleanups)
  179. git(cwd, 'init', '-q', '-b', 'main')
  180. const repo = new TurnRecorder(ctx.sessions.create(SessionId('repo'), { meta: { cwd } }), cwd, env)
  181. repo.start(1)
  182. await repo.settled()
  183. const [objects, ...others] = (await readdir(tempRoot)).filter(entry => entry.startsWith('dsh-workspace-changes-'))
  184. expect(others).toEqual([])
  185. expect(objects).toBeDefined()
  186. await repo.dispose()
  187. expect(await readdir(tempRoot)).toEqual([])
  188. })
  189. it('keeps its own directory out of the snapshots when the temporary root lies inside the work tree', async () => {
  190. const cwd = await scratchDir('dsh-recorder-tmp-in-tree-', cleanups)
  191. git(cwd, 'init', '-q', '-b', 'main')
  192. await writeFile(join(cwd, 'tracked.txt'), 'one\n')
  193. git(cwd, 'add', '-A'); git(cwd, 'commit', '-q', '-m', 'init')
  194. const tempRoot = join(cwd, 'tmp')
  195. await mkdir(tempRoot)
  196. const { ctx, git: runnerGit } = await runner()
  197. const session = ctx.sessions.create(SessionId('tmp-in-tree'), { meta: { cwd } })
  198. const env = {
  199. git: Promise.resolve(runnerGit), tempRoot, maxFiles: 10, maxFileBytes: 1024, diffTimeoutMs: 100,
  200. warn: (m: string) => { throw new Error(m) },
  201. }
  202. const recorder = new TurnRecorder(session, cwd, env)
  203. startTurn(session, 1)
  204. recorder.start(1)
  205. await recorder.settled()
  206. await writeFile(join(cwd, 'tracked.txt'), 'one\ntwo\n')
  207. recorder.observe(toolCall(session, 1, 'bash', { command: 'x' }))
  208. await recorder.stopping(1)
  209. const announced = session.snapshotEvents().find(event => event.type === 'workspace/changes')
  210. expect(recorder.summary(announced!.seq)?.files.map(file => file.display)).toEqual(['tracked.txt'])
  211. await recorder.dispose()
  212. })
  213. })