harness.ts 4.1 KB

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