harness.ts 4.0 KB

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