harness.ts 4.7 KB

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