loader-composition.e2e.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { realpathSync } from 'node:fs'
  2. import { readFile, readdir } from 'node:fs/promises'
  3. import { join } from 'node:path'
  4. import { fileURLToPath } from 'node:url'
  5. import { describe, expect, it } from 'vitest'
  6. import { type SessionEvent } from '@deepseek-ai/dsh-session'
  7. import { LOADER_SMOKE_TEST_TIMEOUT_MS, runLoaderSmoke } from '@deepseek-ai/dsh-loader-smoke'
  8. /**
  9. * Keyless REAL-composition coverage for the ACP provider through a test-only
  10. * patch file: parent-session cwd inheritance and model-visible failure detail
  11. * both cross the Loader, subprocess, ACP, tool, and persisted-session paths.
  12. * The with-key tier lives in subagent-acp.e2e.ts.
  13. */
  14. const driver = fileURLToPath(new URL(
  15. './fixtures/loader/driver.ts',
  16. import.meta.url,
  17. ))
  18. const configPath = fileURLToPath(new URL(
  19. './fixtures/loader/acp.patch.yml',
  20. import.meta.url,
  21. ))
  22. const mockServer = fileURLToPath(new URL('./mock-acp-server.ts', import.meta.url))
  23. const repoTsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  24. async function jsonlFiles(dir: string): Promise<string[]> {
  25. const entries = await readdir(dir, { withFileTypes: true })
  26. const paths = await Promise.all(entries.map(async (entry) => {
  27. const path = join(dir, entry.name)
  28. if (entry.isDirectory()) return jsonlFiles(path)
  29. return entry.isFile() && entry.name.endsWith('.jsonl') ? [path] : []
  30. }))
  31. return paths.flat()
  32. }
  33. function toolResultText(events: SessionEvent[]): string {
  34. const results = events.filter(event => event.type === 'tool/result')
  35. expect(results).toHaveLength(1)
  36. return results[0]!.data.message.content[0].content
  37. .filter(block => block.type === 'text')
  38. .map(block => block.text)
  39. .join('')
  40. }
  41. describe('ACP subagent cwd inheritance through the production profile', () => {
  42. it('runs the child in the parent session workspace and announces it as the ACP session cwd', async () => {
  43. let events: SessionEvent[] = []
  44. let workspace = ''
  45. const { stderr } = await runLoaderSmoke({
  46. label: 'acp-subagent cwd composition smoke',
  47. tempDirPrefix: 'acp-subagent-cwd-e2e-',
  48. binScript: driver,
  49. libBinScript: driver,
  50. configPath,
  51. tsconfigPath: repoTsconfig,
  52. env: { DSH_TEST_MOCK_ACP_SERVER: mockServer },
  53. inspect: async (cwd) => {
  54. // The child reports realpaths; canonicalize the temp workspace to match.
  55. workspace = realpathSync(cwd)
  56. const logs = await jsonlFiles(join(cwd, '.sessions'))
  57. expect(logs).toHaveLength(1)
  58. const lines = (await readFile(logs[0] as string, 'utf8')).trimEnd().split('\n')
  59. events = lines.slice(1).map(line => JSON.parse(line) as SessionEvent)
  60. },
  61. })
  62. expect(stderr).not.toContain('UNHANDLED')
  63. // The tool result carries the child's two-line echo: its real process.cwd()
  64. // and the cwd the backend announced in `session/new` — both the parent
  65. // session's workspace, never the harness process's launch directory.
  66. expect(toolResultText(events)).toBe(`${workspace}\n${workspace}`)
  67. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  68. it('presents the ACP remote-limit diagnostic separately from partial output', async () => {
  69. let events: SessionEvent[] = []
  70. const { stderr } = await runLoaderSmoke({
  71. label: 'acp-subagent diagnostic composition smoke',
  72. tempDirPrefix: 'acp-subagent-diagnostic-e2e-',
  73. binScript: driver,
  74. libBinScript: driver,
  75. configPath,
  76. tsconfigPath: repoTsconfig,
  77. env: {
  78. DSH_TEST_MOCK_ACP_SERVER: mockServer,
  79. DSH_TEST_ACP_MODE: 'diagnostic',
  80. },
  81. inspect: async (cwd) => {
  82. const logs = await jsonlFiles(join(cwd, '.sessions'))
  83. expect(logs).toHaveLength(1)
  84. const lines = (await readFile(logs[0] as string, 'utf8')).trimEnd().split('\n')
  85. events = lines.slice(1).map(line => JSON.parse(line) as SessionEvent)
  86. },
  87. })
  88. expect(stderr).not.toContain('UNHANDLED')
  89. expect(toolResultText(events)).toBe(
  90. 'Error: subagent run failed\n'
  91. + 'Diagnostic: Subagent failure (provider: ACP; stage: prompt; category: remote-limit; stop reason: max_turn_requests)\n'
  92. + 'Partial output before the run ended:\npartial loader answer',
  93. )
  94. }, LOADER_SMOKE_TEST_TIMEOUT_MS)
  95. })