harness.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 ToolResultPruneService from '@deepseek-ai/dsh-compact-tool-result-prune'
  12. import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  13. import * as SessionCheckpointPolicy from '@deepseek-ai/dsh-session-checkpoint-policy'
  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 headless-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. /** Test-only context capacity advertised for `deepseek-v4-flash`. */
  46. modelContextWindow?: number
  47. }
  48. export async function codingHarness(workdir: string, options: CodingHarnessOptions = {}): Promise<Context> {
  49. const ctx = new Context()
  50. await mountAgentLoopTestDependencies(ctx, {
  51. systemPrompt: { persona: options.persona ?? '' },
  52. })
  53. await ctx.plugin(AgentLoop, { agents: [] })
  54. await ctx.plugin(LlmDeepSeek, options.modelContextWindow === undefined ? {} : {
  55. models: [{ id: 'deepseek-v4-flash', contextWindow: options.modelContextWindow }],
  56. })
  57. await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
  58. await ctx.plugin(ToolBash)
  59. await ctx.plugin(ToolTodo)
  60. // Compaction is opt-in: only the compaction e2e loads the reusable meter and backend.
  61. if (options.compact !== undefined) {
  62. await ctx.plugin(TokenMeterService)
  63. await ctx.plugin(ToolResultPruneService)
  64. await ctx.plugin(BasicCompactService, options.compact)
  65. }
  66. // Durable JSONL persistence is opt-in: only the resume e2e needs it, and the
  67. // other suites stay file-free. Loaded last so a resume's deferred
  68. // `ctx.inject(['sessionPersistence'])` resolves once this is present.
  69. if (options.persistenceRoot !== undefined) {
  70. await ctx.plugin(SessionPersistenceJsonl, { root: options.persistenceRoot })
  71. await ctx.plugin(SessionCheckpointPolicy)
  72. }
  73. return ctx
  74. }
  75. export function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  76. return new Promise((resolve) => {
  77. const dispose = ctx.on('agent/status', (subject, status) => {
  78. if (subject === agent && status === 'idle') {
  79. dispose()
  80. resolve()
  81. }
  82. })
  83. })
  84. }
  85. export function finalText(events: SessionEvent[]): string {
  86. const message = events.findLast(event => event.type === 'assistant/message')
  87. if (message?.type !== 'assistant/message') return ''
  88. return message.data.content
  89. .filter(block => block.type === 'text')
  90. .map(block => block.text)
  91. .join('')
  92. }