1
0

headless-driver.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env node
  2. /** Snapshot-only Loader driver: stream one fixture turn as canonical JSONL. */
  3. import type { Context, FiberState } from '@deepseek-ai/cordis'
  4. import { installFailLoud, loadEnv, resolveConfigPath } from '@deepseek-ai/dsh-app-boot'
  5. import { runFixtureTurn } from '@deepseek-ai/dsh-loader-smoke'
  6. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  7. import { bootProductionProfile } from './production-profile.ts'
  8. const NAME = 'headless-test-driver'
  9. const REQUIRED_ENTRY_ENV = 'DSH_LOADER_SMOKE_REQUIRED_ENTRY_ID'
  10. const FIBER_ACTIVE = 2 as FiberState.ACTIVE
  11. const [configPath, ...taskParts] = process.argv.slice(2)
  12. if (configPath === undefined || taskParts.length === 0 || taskParts.every(part => part.trim() === '')) {
  13. throw new Error(`${NAME}: expected <config-path> <task...>`)
  14. }
  15. const uninstallFailLoud = installFailLoud(NAME)
  16. let ctx: Context | undefined
  17. try {
  18. loadEnv(NAME)
  19. ctx = await bootProductionProfile({
  20. binName: NAME,
  21. profile: 'headless',
  22. overlayPaths: [resolveConfigPath(configPath, undefined)],
  23. })
  24. const requiredEntryId = process.env[REQUIRED_ENTRY_ENV]
  25. if (requiredEntryId !== undefined) {
  26. const entry = [...ctx.loader.entries()].find(candidate => candidate.options.id === requiredEntryId)
  27. if (entry === undefined) throw new Error(`${NAME}: required fixture entry not found: ${requiredEntryId}`)
  28. if (entry.fiber === undefined) throw new Error(`${NAME}: required fixture entry failed to import: ${requiredEntryId}`)
  29. await entry.fiber.await()
  30. if (entry.fiber.state !== FIBER_ACTIVE) {
  31. throw new Error(`${NAME}: required fixture entry did not activate: ${requiredEntryId}`)
  32. }
  33. }
  34. const result = await runFixtureTurn(ctx, {
  35. task: taskParts.join(' '),
  36. onEvent: (sessionId: string, event: SessionEvent) => {
  37. process.stdout.write(`${JSON.stringify({ type: 'session_event', sessionId, event })}\n`)
  38. },
  39. })
  40. process.stdout.write(`${JSON.stringify(result)}\n`)
  41. } catch (error: unknown) {
  42. process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`)
  43. process.exitCode = 1
  44. } finally {
  45. await ctx?.fiber.dispose()
  46. uninstallFailLoud()
  47. }