time-context.e2e.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process'
  2. import { mkdtemp, readFile, readdir, rm } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { fileURLToPath } from 'node:url'
  6. import { afterEach, describe, expect, it } from 'vitest'
  7. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  8. const binScript = fileURLToPath(new URL('../../../examples/stdio-demo/src/bin.ts', import.meta.url))
  9. const configPath = fileURLToPath(new URL('./fixtures/cordis.yml', import.meta.url))
  10. const repoTsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  11. const tsxLoader = fileURLToPath(import.meta.resolve('tsx'))
  12. const PROCESS_TIMEOUT_MS = 30_000
  13. const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000
  14. const FIRST_REPLY = '[main turn 1] You said: "Time sampled while preparing turn 1, step 1:'
  15. const SECOND_REPLY = '[main turn 2] You said: "Time sampled while preparing turn 2, step 1:'
  16. let child: ChildProcessWithoutNullStreams | undefined
  17. let workdir: string | undefined
  18. afterEach(async () => {
  19. if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
  20. child = undefined
  21. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  22. workdir = undefined
  23. })
  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. async function runTwoTurns(): Promise<{ stdout: string; stderr: string }> {
  34. workdir = await mkdtemp(join(tmpdir(), 'time-context-e2e-'))
  35. const cwd = workdir
  36. return new Promise((resolve, reject) => {
  37. const proc = spawn(
  38. process.execPath,
  39. ['--expose-internals', '--import', tsxLoader, binScript, configPath],
  40. {
  41. cwd,
  42. env: {
  43. ...process.env,
  44. TZ: 'Asia/Shanghai',
  45. TSX_TSCONFIG_PATH: repoTsconfig,
  46. DSH_HOME: join(cwd, '.dsh'),
  47. DSH_AGENTS_HOME: join(cwd, '.agents'),
  48. },
  49. stdio: ['pipe', 'pipe', 'pipe'],
  50. },
  51. )
  52. child = proc
  53. let stdout = ''
  54. let stderr = ''
  55. let sentSecond = false
  56. proc.stdout.setEncoding('utf8')
  57. proc.stdout.on('data', (chunk: string) => {
  58. stdout += chunk
  59. if (!sentSecond && stdout.includes(FIRST_REPLY) && stdout.includes('Try "echo <something>" to see a tool call.\n> ')) {
  60. sentSecond = true
  61. proc.stdin.end('second\n')
  62. }
  63. })
  64. proc.stderr.setEncoding('utf8')
  65. proc.stderr.on('data', (chunk: string) => { stderr += chunk })
  66. const timer = setTimeout(() => {
  67. proc.kill('SIGKILL')
  68. reject(new Error(`time-context e2e did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`))
  69. }, PROCESS_TIMEOUT_MS)
  70. proc.on('exit', (code) => {
  71. clearTimeout(timer)
  72. if (code === 0) resolve({ stdout, stderr })
  73. else reject(new Error(`time-context e2e exited ${code}. stdout:\n${stdout}\nstderr:\n${stderr}`))
  74. })
  75. proc.on('error', (error) => { clearTimeout(timer); reject(error) })
  76. proc.stdin.write('first\n')
  77. })
  78. }
  79. describe('time-context through a real cordis.yml and stdio process', () => {
  80. it('uses the process zone and persists one ordered context event per request', async () => {
  81. const { stdout, stderr } = await runTwoTurns()
  82. expect(stderr).not.toContain('UNHANDLED')
  83. expect(stdout).toContain('time-context e2e ready.')
  84. expect(stdout).toContain(FIRST_REPLY)
  85. expect(stdout).toContain(SECOND_REPLY)
  86. const logs = await jsonlFiles(join(workdir as string, '.sessions'))
  87. expect(logs).toHaveLength(1)
  88. const lines = (await readFile(logs[0] as string, 'utf8')).trimEnd().split('\n')
  89. const events = lines.slice(1).map(line => JSON.parse(line) as SessionEvent)
  90. expect(events.filter(event => event.type === 'turn/end')).toHaveLength(2)
  91. const contexts = events.filter(event => event.type === 'context/message')
  92. const starts = events.filter(event => event.type === 'step/start')
  93. expect(contexts).toHaveLength(2)
  94. expect(starts).toHaveLength(2)
  95. for (let index = 0; index < contexts.length; index += 1) {
  96. expect(contexts[index]!.seq).toBeLessThan(starts[index]!.seq)
  97. expect(contexts[index]!.surfaceOp).toBe('append')
  98. expect(contexts[index]!.data.source).toEqual({ kind: 'plugin', plugin: 'time-context' })
  99. }
  100. const contextText = contexts.map(event => event.data.content
  101. .filter(block => block.type === 'text')
  102. .map(block => block.text)
  103. .join('\n'))
  104. expect(contextText[0]).toMatch(
  105. /Time sampled while preparing turn 1, step 1: \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+08:00\[Asia\/Shanghai\]/,
  106. )
  107. expect(contextText[0]).toMatch(
  108. /Elapsed since the preceding model-visible message: (?:\d+d )?(?:\d+h )?(?:\d+m )?\d+s\./,
  109. )
  110. expect(contextText[1]).toMatch(/Time sampled while preparing turn 2, step 1:/)
  111. const headers = events.filter(event => event.type === 'request/header')
  112. expect(JSON.stringify(headers)).not.toContain('Time sampled while preparing')
  113. }, TEST_TIMEOUT_MS)
  114. })