time-context.e2e.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. import { resolveExampleLaunch } from '@deepseek-ai/dsh-loader-smoke'
  9. // Keep the Loader config under examples so both modes exercise the same deployable
  10. // topology: local fixture source plus bare plugins owned by the examples workspace.
  11. const binScript = fileURLToPath(new URL('../../../examples/stdio-demo/src/bin.ts', import.meta.url))
  12. const configPath = fileURLToPath(new URL(
  13. '../../../../examples/echo-agent/tests/fixtures/context/time-context/cordis.yml',
  14. import.meta.url,
  15. ))
  16. const repoTsconfig = fileURLToPath(new URL('../../../../tsconfig.json', import.meta.url))
  17. const PROCESS_TIMEOUT_MS = 30_000
  18. const TEST_TIMEOUT_MS = PROCESS_TIMEOUT_MS + 15_000
  19. const FIRST_REPLY = '[main turn 1] You said: "Time sampled while preparing turn 1, step 1:'
  20. const SECOND_REPLY = '[main turn 2] You said: "Time sampled while preparing turn 2, step 1:'
  21. let child: ChildProcessWithoutNullStreams | undefined
  22. let workdir: string | undefined
  23. afterEach(async () => {
  24. if (child !== undefined && child.exitCode === null) child.kill('SIGKILL')
  25. child = undefined
  26. if (workdir !== undefined) await rm(workdir, { recursive: true, force: true })
  27. workdir = undefined
  28. })
  29. async function jsonlFiles(dir: string): Promise<string[]> {
  30. const entries = await readdir(dir, { withFileTypes: true })
  31. const paths = await Promise.all(entries.map(async (entry) => {
  32. const path = join(dir, entry.name)
  33. if (entry.isDirectory()) return jsonlFiles(path)
  34. return entry.isFile() && entry.name.endsWith('.jsonl') ? [path] : []
  35. }))
  36. return paths.flat()
  37. }
  38. async function runTwoTurns(): Promise<{ stdout: string; stderr: string }> {
  39. workdir = await mkdtemp(join(tmpdir(), 'time-context-e2e-'))
  40. const cwd = workdir
  41. return new Promise((resolve, reject) => {
  42. const launch = resolveExampleLaunch({
  43. srcBin: binScript,
  44. configArgs: [configPath],
  45. tsconfigPath: repoTsconfig,
  46. exposeInternals: true,
  47. env: {
  48. TZ: 'Asia/Shanghai',
  49. DSH_HOME: join(cwd, '.dsh'),
  50. DSH_AGENTS_HOME: join(cwd, '.agents'),
  51. },
  52. })
  53. const proc = spawn(launch.command, launch.args, {
  54. cwd,
  55. env: { ...process.env, ...launch.env },
  56. stdio: ['pipe', 'pipe', 'pipe'],
  57. })
  58. child = proc
  59. let stdout = ''
  60. let stderr = ''
  61. let sentSecond = false
  62. proc.stdout.setEncoding('utf8')
  63. proc.stdout.on('data', (chunk: string) => {
  64. stdout += chunk
  65. if (!sentSecond && stdout.includes(FIRST_REPLY) && stdout.includes('Try "echo <something>" to see a tool call.\n> ')) {
  66. sentSecond = true
  67. proc.stdin.end('second\n')
  68. }
  69. })
  70. proc.stderr.setEncoding('utf8')
  71. proc.stderr.on('data', (chunk: string) => { stderr += chunk })
  72. const timer = setTimeout(() => {
  73. proc.kill('SIGKILL')
  74. reject(new Error(`time-context e2e did not exit within ${PROCESS_TIMEOUT_MS / 1_000}s. stdout:\n${stdout}\nstderr:\n${stderr}`))
  75. }, PROCESS_TIMEOUT_MS)
  76. proc.on('exit', (code) => {
  77. clearTimeout(timer)
  78. if (code === 0) resolve({ stdout, stderr })
  79. else reject(new Error(`time-context e2e exited ${code}. stdout:\n${stdout}\nstderr:\n${stderr}`))
  80. })
  81. proc.on('error', (error) => { clearTimeout(timer); reject(error) })
  82. proc.stdin.write('first\n')
  83. })
  84. }
  85. describe('time-context through a real cordis.yml and stdio process', () => {
  86. it('uses the process zone and persists one ordered context event per request', async () => {
  87. const { stdout, stderr } = await runTwoTurns()
  88. expect(stderr).not.toContain('UNHANDLED')
  89. expect(stdout).toContain('time-context e2e ready.')
  90. expect(stdout).toContain(FIRST_REPLY)
  91. expect(stdout).toContain(SECOND_REPLY)
  92. const logs = await jsonlFiles(join(workdir as string, '.sessions'))
  93. expect(logs).toHaveLength(1)
  94. const lines = (await readFile(logs[0] as string, 'utf8')).trimEnd().split('\n')
  95. const events = lines.slice(1).map(line => JSON.parse(line) as SessionEvent)
  96. expect(events.filter(event => event.type === 'turn/end')).toHaveLength(2)
  97. const contexts = events.filter(event => event.type === 'context/message')
  98. const starts = events.filter(event => event.type === 'step/start')
  99. expect(contexts).toHaveLength(2)
  100. expect(starts).toHaveLength(2)
  101. for (let index = 0; index < contexts.length; index += 1) {
  102. expect(contexts[index]!.seq).toBeLessThan(starts[index]!.seq)
  103. expect(contexts[index]!.surfaceOp).toBe('append')
  104. expect(contexts[index]!.data.source).toEqual({ kind: 'plugin', plugin: 'time-context' })
  105. }
  106. const contextText = contexts.map(event => event.data.content
  107. .filter(block => block.type === 'text')
  108. .map(block => block.text)
  109. .join('\n'))
  110. expect(contextText[0]).toMatch(
  111. /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\]/,
  112. )
  113. expect(contextText[0]).toMatch(
  114. /Elapsed since the preceding model-visible message: (?:\d+d )?(?:\d+h )?(?:\d+m )?\d+s\./,
  115. )
  116. expect(contextText[1]).toMatch(/Time sampled while preparing turn 2, step 1:/)
  117. const headers = events.filter(event => event.type === 'request/header')
  118. expect(JSON.stringify(headers)).not.toContain('Time sampled while preparing')
  119. }, TEST_TIMEOUT_MS)
  120. })