harness.ts 3.8 KB

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