plugin.spec.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /** The plugin records each top-level turn's file changes from real git snapshots. */
  2. import { mkdir, mkdtemp, realpath, rm, stat, writeFile } from 'node:fs/promises'
  3. import { homedir, tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { afterEach, describe, expect, it, vi } from 'vitest'
  6. import { Context } from '@deepseek-ai/cordis'
  7. import type {} from '@deepseek-ai/dsh-agent'
  8. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  9. import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local'
  10. import * as WorkspaceChanges from '../src/index.ts'
  11. import { changes, endTurn, git, scratchDir, settle, startTurn, toolCall } from './support.ts'
  12. const cleanups: Array<() => Promise<unknown>> = []
  13. afterEach(async () => {
  14. for (const cleanup of cleanups.reverse()) await cleanup()
  15. cleanups.length = 0
  16. vi.restoreAllMocks()
  17. })
  18. async function boot(config: Partial<WorkspaceChanges.Config> = {}) {
  19. const ctx = new Context()
  20. cleanups.push(() => ctx.fiber.dispose())
  21. await ctx.plugin(SessionStore)
  22. await ctx.plugin(LocalSubprocessRuntime)
  23. const dshHome = config.dshHome ?? await scratchDir('dsh-workspace-changes-home-', cleanups)
  24. const fiber = await ctx.plugin(WorkspaceChanges, { ...config, dshHome } as WorkspaceChanges.Config)
  25. return { ctx, fiber, dshHome }
  26. }
  27. async function repository(): Promise<string> {
  28. const cwd = await scratchDir('dsh-workspace-changes-repo-', cleanups)
  29. git(cwd, 'init', '-q', '-b', 'main')
  30. await writeFile(join(cwd, 'a.txt'), 'l1\nl2\nl3\n')
  31. await writeFile(join(cwd, 'b.txt'), 'x\n')
  32. await writeFile(join(cwd, 'same.txt'), 'same\n')
  33. await writeFile(join(cwd, '.gitignore'), '.env*\n')
  34. git(cwd, 'add', '-A')
  35. git(cwd, 'commit', '-q', '-m', 'init')
  36. return cwd
  37. }
  38. describe('workspace-changes in a repository', () => {
  39. it('records the turn’s own changes and excludes the user’s prior uncommitted work', async () => {
  40. const cwd = await repository()
  41. await writeFile(join(cwd, 'b.txt'), 'x user\n')
  42. await writeFile(join(cwd, 'u.txt'), 'user untracked\n')
  43. const { ctx } = await boot()
  44. const session = ctx.sessions.create(SessionId('repo'), { meta: { cwd } })
  45. startTurn(session, 1)
  46. await settle(ctx, session)
  47. await writeFile(join(cwd, 'a.txt'), 'l1\nl2 model\nl3\nl4\n')
  48. toolCall(session, 1, 'edit', { file_path: 'a.txt' }, { meta: { diffs: [{ path: 'a.txt', oldText: 'l2', newText: 'l2 model' }] } })
  49. await mkdir(join(cwd, 'sub', 'dir'), { recursive: true })
  50. await writeFile(join(cwd, 'sub', 'dir', 'c.txt'), 'c\n')
  51. await writeFile(join(cwd, 'new.txt'), 'n1\nn2\n')
  52. await writeFile(join(cwd, 'bin.dat'), Uint8Array.of(0, 1, 2, 255))
  53. toolCall(session, 1, 'bash', { command: 'printf > files' })
  54. await writeFile(join(cwd, '.env'), 'A=1\nB=2\n')
  55. toolCall(session, 1, 'write', { file_path: '.env' }, { meta: { diffs: [{ path: '.env', oldText: null, newText: 'A=1\n' }] } })
  56. toolCall(session, 1, 'edit', { file_path: '.env' }, { meta: { diffs: [{ path: '.env', oldText: 'A=1\n', newText: 'A=1\nB=2\n' }] } })
  57. toolCall(session, 1, 'write', { file_path: join(tmpdir(), 'scratch.txt') }, {
  58. meta: { diffs: [{ path: join(tmpdir(), 'scratch.txt'), oldText: null, newText: 'scratch\n' }] },
  59. })
  60. toolCall(session, 1, 'write', { file_path: 'ignored-error' }, { isError: true, meta: { diffs: [{ path: 'failed.txt', oldText: null, newText: 'x' }] } })
  61. toolCall(session, 1, 'write', { file_path: '.env.gone' }, { meta: { diffs: [{ path: '.env.gone', oldText: null, newText: 'x' }] } })
  62. toolCall(session, 1, 'write', { file_path: 'same.txt' }, { meta: { diffs: [{ path: 'same.txt', oldText: 'same', newText: 'same' }] } })
  63. toolCall(session, 2, 'write', { file_path: 'other-turn' }, { meta: { diffs: [{ path: 'other.txt', oldText: null, newText: 'x' }] } })
  64. endTurn(session, 1)
  65. await settle(ctx, session)
  66. const [recorded, ...rest] = changes(session)
  67. expect(rest).toEqual([])
  68. expect(recorded).toMatchObject({ turn: 1, total: 6 })
  69. expect(recorded!.snapshot.before).toMatch(/^[0-9a-f]{40,64}$/)
  70. expect(recorded!.snapshot.after).toMatch(/^[0-9a-f]{40,64}$/)
  71. expect(recorded!.files).toEqual([
  72. { path: '.env', display: '.env', added: 2, deleted: 0 },
  73. { path: '.env.gone', display: '.env.gone', added: 1, deleted: 0 },
  74. { path: 'a.txt', display: 'a.txt', added: 2, deleted: 1 },
  75. { path: 'bin.dat', display: 'bin.dat', added: 0, deleted: 0, binary: true },
  76. { path: 'new.txt', display: 'new.txt', added: 2, deleted: 0 },
  77. { path: 'sub/dir/c.txt', display: 'sub/dir/c.txt', added: 1, deleted: 0 },
  78. ])
  79. expect(git(cwd, 'status', '--porcelain').split('\n').filter(Boolean).sort()).toEqual([
  80. ' M a.txt', ' M b.txt', '?? bin.dat', '?? new.txt', '?? sub/', '?? u.txt',
  81. ])
  82. })
  83. it('places files above the working directory and outside the repository by their display rule', async () => {
  84. const root = await repository()
  85. const cwd = join(root, 'pkg')
  86. await mkdir(cwd)
  87. const outside = await mkdtemp(join(homedir(), '.dsh-workspace-changes-test-'))
  88. cleanups.push(() => rm(outside, { recursive: true, force: true }))
  89. const { ctx } = await boot()
  90. const session = ctx.sessions.create(SessionId('nested'), { meta: { cwd } })
  91. startTurn(session, 1)
  92. await settle(ctx, session)
  93. await writeFile(join(root, 'a.txt'), 'changed\n')
  94. await writeFile(join(cwd, 'inner.txt'), 'inner\n')
  95. toolCall(session, 1, 'write', { file_path: join(outside, 'note.txt') }, {
  96. meta: { diffs: [{ path: join(outside, 'note.txt'), oldText: null, newText: 'one\ntwo\nthree\n' }] },
  97. })
  98. endTurn(session, 1, 'blocked')
  99. await settle(ctx, session)
  100. const [recorded] = changes(session)
  101. expect(recorded!.files).toEqual([
  102. { path: join(await realpath(root), 'a.txt'), display: '../a.txt', added: 1, deleted: 3 },
  103. { path: 'inner.txt', display: 'inner.txt', added: 1, deleted: 0 },
  104. { path: join(await realpath(outside), 'note.txt'), display: `~/${outside.slice(homedir().length + 1)}/note.txt`, added: 3, deleted: 0 },
  105. ])
  106. })
  107. it('records inside the turn when the agent stops, and again after turn/end only when tools settled later', async () => {
  108. const cwd = await repository()
  109. const { ctx } = await boot()
  110. const session = ctx.sessions.create(SessionId('stopping'), { meta: { cwd } })
  111. const agent = { session } as never
  112. const signal = new AbortController().signal
  113. startTurn(session, 1)
  114. await ctx.serial('agent/turn-stopping', { agent, turn: 1, signal })
  115. expect(changes(session)).toEqual([])
  116. await writeFile(join(cwd, 'one.txt'), '1\n')
  117. toolCall(session, 1, 'bash', { command: 'x' })
  118. await ctx.serial('agent/turn-stopping', { agent, turn: 7, signal })
  119. await ctx.serial('agent/turn-stopping', { agent, turn: 1, signal })
  120. const inTurn = session.snapshotEvents().find(event => event.type === 'workspace/changes')
  121. expect(inTurn?.data).toMatchObject({ turn: 1, total: 1 })
  122. endTurn(session, 1)
  123. await settle(ctx, session)
  124. expect(changes(session)).toHaveLength(1)
  125. expect(session.snapshotEvents().find(event => event.type === 'turn/end')!.seq).toBeGreaterThan(inTurn!.seq)
  126. startTurn(session, 2)
  127. await settle(ctx, session)
  128. await writeFile(join(cwd, 'two.txt'), '2\n')
  129. toolCall(session, 2, 'bash', { command: 'x' })
  130. await ctx.serial('agent/turn-stopping', { agent, turn: 2, signal })
  131. await rm(join(cwd, 'two.txt'))
  132. toolCall(session, 2, 'bash', { command: 'steered' })
  133. endTurn(session, 2)
  134. await settle(ctx, session)
  135. const second = changes(session).filter(data => data.turn === 2)
  136. expect(second.map(data => data.files.map(file => file.path))).toEqual([['two.txt'], []])
  137. expect(second[1]).toMatchObject({ total: 0 })
  138. startTurn(session, 3)
  139. await settle(ctx, session)
  140. toolCall(session, 3, 'read', { file_path: 'a.txt' })
  141. endTurn(session, 3)
  142. await settle(ctx, session)
  143. expect(changes(session).filter(data => data.turn === 3)).toEqual([])
  144. })
  145. it('caps the file list while reporting the complete count', async () => {
  146. const cwd = await repository()
  147. const { ctx } = await boot({ maxFiles: 2 })
  148. const session = ctx.sessions.create(SessionId('capped'), { meta: { cwd } })
  149. startTurn(session, 1)
  150. await settle(ctx, session)
  151. for (const name of ['c.txt', 'd.txt', 'e.txt']) await writeFile(join(cwd, name), `${name}\n`)
  152. toolCall(session, 1, 'bash', { command: 'x' })
  153. toolCall(session, 1, 'edit', { file_path: 'a.txt' }, { meta: { diffs: [{ path: 'a.txt', oldText: 'l1\n', newText: 'l1\n' }] } })
  154. endTurn(session, 1)
  155. await settle(ctx, session)
  156. const [recorded] = changes(session)
  157. expect(recorded!.total).toBe(3)
  158. expect(recorded!.files.map(file => file.display)).toEqual(['c.txt', 'd.txt'])
  159. })
  160. it('warns and skips the turn when its git work fails', async () => {
  161. const cwd = await scratchDir('dsh-workspace-changes-warn-', cleanups)
  162. const blocker = join(cwd, 'blocker')
  163. await writeFile(blocker, 'not a directory')
  164. const { ctx } = await boot({ dshHome: blocker })
  165. const warn = vi.spyOn(ctx.logger, 'warn').mockImplementation(() => undefined)
  166. const session = ctx.sessions.create(SessionId('warn'), { meta: { cwd } })
  167. startTurn(session, 1)
  168. await settle(ctx, session)
  169. await writeFile(join(cwd, 'x.txt'), 'x\n')
  170. toolCall(session, 1, 'bash', { command: 'x' })
  171. endTurn(session, 1)
  172. await settle(ctx, session)
  173. expect(changes(session)).toEqual([])
  174. const own = warn.mock.calls.map(call => String(call[0])).filter(message => message.startsWith('workspace-changes:'))
  175. expect(own).toHaveLength(1)
  176. expect(own[0]).toContain('ENOTDIR')
  177. })
  178. it('rejects non-positive bounds at load', async () => {
  179. const ctx = new Context()
  180. cleanups.push(() => ctx.fiber.dispose())
  181. await ctx.plugin(SessionStore)
  182. await ctx.plugin(LocalSubprocessRuntime)
  183. await expect(ctx.plugin(WorkspaceChanges, { maxFiles: 0 } as WorkspaceChanges.Config)).rejects.toThrow('positive integer maxFiles')
  184. })
  185. })
  186. describe('workspace-changes without a repository', () => {
  187. it('snapshots into a shadow repository under the Harness home and excludes that home', async () => {
  188. const cwd = await scratchDir('dsh-workspace-changes-plain-', cleanups)
  189. const dshHome = join(cwd, '.dsh')
  190. await writeFile(join(cwd, 'existing.txt'), 'before\n')
  191. const { ctx } = await boot({ dshHome })
  192. const session = ctx.sessions.create(SessionId('plain'), { meta: { cwd } })
  193. startTurn(session, 1)
  194. await settle(ctx, session)
  195. await writeFile(join(cwd, 'existing.txt'), 'after\nmore\n')
  196. await mkdir(join(cwd, 'node_modules'), { recursive: true })
  197. await writeFile(join(cwd, 'node_modules', 'dep.js'), 'module\n')
  198. await writeFile(join(dshHome, 'settings.yaml'), 'changed: true\n')
  199. toolCall(session, 1, 'write', { file_path: 'node_modules/dep.js' }, {
  200. meta: { diffs: [{ path: 'node_modules/dep.js', oldText: null, newText: 'module\n' }] },
  201. })
  202. endTurn(session, 1)
  203. await settle(ctx, session)
  204. const [first] = changes(session)
  205. expect(first!.files).toEqual([
  206. { path: 'existing.txt', display: 'existing.txt', added: 2, deleted: 1 },
  207. { path: 'node_modules/dep.js', display: 'node_modules/dep.js', added: 1, deleted: 0 },
  208. ])
  209. const shadows = join(dshHome, 'workspace-changes')
  210. expect((await stat(shadows)).isDirectory()).toBe(true)
  211. await expect(stat(join(cwd, '.git'))).rejects.toThrow()
  212. startTurn(session, 2)
  213. await settle(ctx, session)
  214. await writeFile(join(cwd, 'second.txt'), 's\n')
  215. toolCall(session, 2, 'bash', { command: 'x' })
  216. endTurn(session, 2)
  217. await settle(ctx, session)
  218. expect(changes(session).at(-1)!.files.map(file => file.display)).toEqual(['second.txt'])
  219. })
  220. it('drops a disposed session’s recorder and starts afresh on its next turn', async () => {
  221. const cwd = await scratchDir('dsh-workspace-changes-disposed-', cleanups)
  222. const { ctx, fiber } = await boot()
  223. const session = ctx.sessions.create(SessionId('disposed'), { meta: { cwd } })
  224. startTurn(session, 1)
  225. await settle(ctx, session)
  226. ctx.emit('session/disposed', session)
  227. await writeFile(join(cwd, 'one.txt'), '1\n')
  228. toolCall(session, 1, 'bash', { command: 'x' })
  229. endTurn(session, 1)
  230. await settle(ctx, session)
  231. expect(changes(session)).toEqual([])
  232. startTurn(session, 2)
  233. await settle(ctx, session)
  234. await writeFile(join(cwd, 'two.txt'), '2\n')
  235. toolCall(session, 2, 'bash', { command: 'x' })
  236. endTurn(session, 2)
  237. await settle(ctx, session)
  238. expect(changes(session).map(data => data.files.map(file => file.display))).toEqual([['two.txt']])
  239. startTurn(session, 3)
  240. await settle(ctx, session)
  241. await fiber.dispose()
  242. await writeFile(join(cwd, 'three.txt'), '3\n')
  243. toolCall(session, 3, 'bash', { command: 'x' })
  244. endTurn(session, 3)
  245. await settle(ctx, session)
  246. expect(changes(session).filter(data => data.turn === 3)).toEqual([])
  247. })
  248. it('ignores subagent sessions and sessions without a working directory', async () => {
  249. const cwd = await scratchDir('dsh-workspace-changes-skip-', cleanups)
  250. const { ctx } = await boot()
  251. const sessions = [
  252. ctx.sessions.create(SessionId('child'), { meta: { cwd, delegationDepth: 1 } }),
  253. ctx.sessions.create(SessionId('origin'), { meta: { cwd, origin: 'subagent' } }),
  254. ctx.sessions.create(SessionId('nowhere')),
  255. ]
  256. for (const session of sessions) {
  257. startTurn(session, 1)
  258. await settle(ctx, session)
  259. toolCall(session, 1, 'bash', { command: 'x' })
  260. endTurn(session, 1)
  261. await settle(ctx, session)
  262. expect(changes(session)).toEqual([])
  263. }
  264. await ctx.waterfall('tools/pre-execute', {} as never, () => Promise.resolve(undefined as never))
  265. })
  266. })
  267. describe('workspace-changes without git', () => {
  268. it('records nothing and reports the absence once', async () => {
  269. const cwd = await scratchDir('dsh-workspace-changes-nogit-', cleanups)
  270. const { ctx } = await boot()
  271. vi.spyOn(ctx.subprocess, 'resolveExecutable').mockRejectedValue(new Error('git: not found'))
  272. const info = vi.spyOn(ctx.logger, 'info').mockImplementation(() => undefined)
  273. const session = ctx.sessions.create(SessionId('nogit'), { meta: { cwd } })
  274. for (const turn of [1, 2]) {
  275. startTurn(session, turn)
  276. await settle(ctx, session)
  277. await writeFile(join(cwd, `${turn}.txt`), 'x\n')
  278. toolCall(session, turn, 'bash', { command: 'x' })
  279. endTurn(session, turn)
  280. await settle(ctx, session)
  281. }
  282. expect(changes(session)).toEqual([])
  283. expect(info).toHaveBeenCalledTimes(1)
  284. expect(info.mock.calls[0]![0]).toContain('git is unavailable')
  285. })
  286. it('treats the macOS developer-tools stub as absent until a developer directory is selected', async () => {
  287. const platform = Object.getOwnPropertyDescriptor(process, 'platform')!
  288. Object.defineProperty(process, 'platform', { value: 'darwin', configurable: true })
  289. cleanups.push(async () => { Object.defineProperty(process, 'platform', platform) })
  290. const probes: Array<{ done: Promise<{ exitCode: number | null; signal: null }>; available: boolean; executable?: string }> = [
  291. { done: Promise.reject(new Error('spawn failed')), available: false },
  292. { done: Promise.resolve({ exitCode: 1, signal: null }), available: false },
  293. { done: Promise.resolve({ exitCode: 0, signal: null }), available: true },
  294. { done: Promise.resolve({ exitCode: 1, signal: null }), available: true, executable: '/opt/homebrew/bin/git' },
  295. ]
  296. for (const probe of probes) {
  297. probe.done.catch(() => undefined)
  298. const cwd = await scratchDir('dsh-workspace-changes-stub-', cleanups)
  299. const { ctx } = await boot()
  300. vi.spyOn(ctx.subprocess, 'resolveExecutable').mockResolvedValue(probe.executable ?? '/usr/bin/git')
  301. const real = ctx.subprocess.spawn.bind(ctx.subprocess)
  302. const spawn = vi.spyOn(ctx.subprocess, 'spawn').mockImplementation(spec =>
  303. spec.argv[0] === '/usr/bin/xcode-select' ? { done: probe.done } as never : real(spec))
  304. const info = vi.spyOn(ctx.logger, 'info').mockImplementation(() => undefined)
  305. vi.spyOn(ctx.logger, 'warn').mockImplementation(() => undefined)
  306. const session = ctx.sessions.create(SessionId('stub'), { meta: { cwd } })
  307. startTurn(session, 1)
  308. await settle(ctx, session)
  309. expect(spawn.mock.calls.some(call => call[0].argv[0] === '/usr/bin/xcode-select')).toBe(probe.executable === undefined)
  310. expect(info).toHaveBeenCalledTimes(probe.available ? 0 : 1)
  311. await ctx.fiber.dispose()
  312. }
  313. })
  314. })