harness.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 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. + 'keep at most one task in_progress (exactly one while work remains), and mark '
  31. + 'a task completed as soon as it is done.'
  32. /** Options for {@link codingHarness}. */
  33. export interface CodingHarnessOptions {
  34. /**
  35. * Deployment persona for the tree (the system-prompt plugin's `persona`
  36. * config — per-context, not per-agent). Omitted ⇒ no persona section.
  37. */
  38. persona?: string
  39. /** Durable JSONL persistence root (the resume suite needs it; others stay file-free). */
  40. persistenceRoot?: string
  41. /**
  42. * Load {@link BasicCompactService} with this config so the compaction e2e can
  43. * trigger compaction at a small, controlled history size. Omitted ⇒ no
  44. * compaction plugin (the default suites run without it).
  45. */
  46. compact?: BasicCompactConfig
  47. /** Test-only context capacity advertised for `deepseek-v4-flash`. */
  48. modelContextWindow?: number
  49. }
  50. export async function codingHarness(workdir: string, options: CodingHarnessOptions = {}): Promise<Context> {
  51. const ctx = new Context()
  52. await mountAgentLoopTestDependencies(ctx, {
  53. systemPrompt: { persona: options.persona ?? '' },
  54. })
  55. await ctx.plugin(AgentLoop, { agents: [] })
  56. await ctx.plugin(LlmDeepSeek, options.modelContextWindow === undefined ? {} : {
  57. models: [{ id: 'deepseek-v4-flash', contextWindow: options.modelContextWindow }],
  58. })
  59. await ctx.plugin(LocalSubprocessService)
  60. await ctx.plugin(BashEnvPlugin)
  61. await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
  62. await ctx.plugin(ToolBash)
  63. await ctx.plugin(ToolTodo)
  64. // Compaction is opt-in: only the compaction e2e loads the reusable meter and backend.
  65. if (options.compact !== undefined) {
  66. await ctx.plugin(TokenMeterService)
  67. await ctx.plugin(ToolResultPruneService)
  68. await ctx.plugin(BasicCompactService, options.compact)
  69. }
  70. // Durable JSONL persistence is opt-in: only the resume e2e needs it, and the
  71. // other suites stay file-free. Loaded last so a resume's deferred
  72. // `ctx.inject(['sessionPersistence'])` resolves once this is present.
  73. if (options.persistenceRoot !== undefined) {
  74. await ctx.plugin(SessionPersistenceJsonl, { root: options.persistenceRoot })
  75. await ctx.plugin(SessionCheckpointPolicy)
  76. }
  77. return ctx
  78. }
  79. export function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  80. return new Promise((resolve) => {
  81. const dispose = ctx.on('agent/status', (subject, status) => {
  82. if (subject === agent && status === 'idle') {
  83. dispose()
  84. resolve()
  85. }
  86. })
  87. })
  88. }
  89. export function finalText(events: SessionEvent[]): string {
  90. const message = events.findLast(event => event.type === 'assistant/message')
  91. if (message?.type !== 'assistant/message') return ''
  92. return message.data.message.content
  93. .filter(block => block.type === 'text')
  94. .map(block => block.text)
  95. .join('')
  96. }