harness.ts 4.1 KB

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