harness.ts 4.4 KB

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