harness.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { Context } from 'cordis'
  2. import LlmService from '@deepseek-ai/dsh-llm'
  3. import SessionStore from '@deepseek-ai/dsh-session'
  4. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  5. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  6. import ToolRegistry from '@deepseek-ai/dsh-tools'
  7. import AgentRegistry from '@deepseek-ai/dsh-agent'
  8. import AgentLoop, { ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop'
  9. import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local'
  10. import * as ToolBash from '@deepseek-ai/dsh-tool-bash'
  11. import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
  12. import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  13. import { BasicCompactService } from '@deepseek-ai/dsh-compact-basic'
  14. import type { BasicCompactConfig } from '@deepseek-ai/dsh-compact-basic'
  15. /**
  16. * Shared harness for the coding-agent e2e suites: the full plugin stack
  17. * with the real DeepSeek adapter and the real bash tool. Lives outside the
  18. * *.e2e.ts pattern so importing it never re-registers another file's tests.
  19. */
  20. export const SYSTEM_PROMPT = 'You are a coding agent. Your only tool is bash; '
  21. + 'do file operations with cat/grep/heredocs, check [exit code: N] markers, '
  22. + 'and report results briefly.'
  23. /** Options for {@link codingHarness}. */
  24. export interface CodingHarnessOptions {
  25. /** Durable JSONL persistence root (the resume suite needs it; others stay file-free). */
  26. persistenceRoot?: string
  27. /**
  28. * Load {@link BasicCompactService} with this config so the compaction e2e can
  29. * trigger compaction at a small, controlled history size. Omitted ⇒ no
  30. * compaction plugin (the default suites run without it).
  31. */
  32. compact?: BasicCompactConfig
  33. }
  34. export async function codingHarness(workdir: string, options: CodingHarnessOptions = {}): Promise<Context> {
  35. const ctx = new Context()
  36. await ctx.plugin(LlmService)
  37. await ctx.plugin(SessionStore)
  38. await ctx.plugin(SystemPrompt)
  39. await ctx.plugin(ToolRegistry)
  40. await ctx.plugin(AgentRegistry)
  41. await ctx.plugin(AgentLoop, { agents: [] })
  42. await ctx.plugin(LlmDeepSeek, { models: ['deepseek-v4-flash'] })
  43. await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
  44. await ctx.plugin(ToolBash)
  45. // Compaction is opt-in: only the compaction e2e loads it, with a lowered
  46. // contextWindow/retainTokens so a short real session crosses the threshold.
  47. if (options.compact !== undefined) await ctx.plugin(BasicCompactService, options.compact)
  48. // Durable JSONL persistence is opt-in: only the resume e2e needs it, and the
  49. // other suites stay file-free. Loaded last so a resume's deferred
  50. // `ctx.inject(['sessionPersistence'])` resolves once this is present.
  51. if (options.persistenceRoot !== undefined) await ctx.plugin(SessionPersistenceJsonl, { root: options.persistenceRoot })
  52. return ctx
  53. }
  54. export function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise<void> {
  55. return new Promise((resolve) => {
  56. const dispose = ctx.on('agent/status', (subject, status) => {
  57. if (subject === agent && status === 'idle') {
  58. dispose()
  59. resolve()
  60. }
  61. })
  62. })
  63. }
  64. export function finalText(events: SessionEvent[]): string {
  65. const message = events.findLast(event => event.type === 'assistant/message')
  66. if (message?.type !== 'assistant/message') return ''
  67. return message.data.content
  68. .filter(block => block.type === 'text')
  69. .map(block => block.text)
  70. .join('')
  71. }