harness.ts 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 TokenMeterService from '@deepseek-ai/dsh-token-meter'
  14. import type { TokenMeterConfig } from '@deepseek-ai/dsh-token-meter'
  15. import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  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 coding-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. /** Optional meter profiles loaded before compact-basic. */
  48. tokenMeter?: TokenMeterConfig
  49. }
  50. export async function codingHarness(workdir: string, options: CodingHarnessOptions = {}): Promise<Context> {
  51. const ctx = new Context()
  52. await ctx.plugin(LlmService)
  53. await ctx.plugin(SessionStore)
  54. await ctx.plugin(SystemPrompt, { persona: options.persona ?? '' })
  55. await ctx.plugin(ToolRegistry)
  56. await ctx.plugin(AgentRegistry)
  57. await ctx.plugin(AgentLoop, { agents: [] })
  58. await ctx.plugin(LlmDeepSeek, { models: ['deepseek-v4-flash'] })
  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
  63. // backend, with a lowered profile window so a short real session crosses the threshold.
  64. if (options.compact !== undefined) {
  65. await ctx.plugin(TokenMeterService, options.tokenMeter)
  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) await ctx.plugin(SessionPersistenceJsonl, { root: options.persistenceRoot })
  72. return ctx
  73. }
  74. export function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise<void> {
  75. return new Promise((resolve) => {
  76. const dispose = ctx.on('agent/status', (subject, status) => {
  77. if (subject === agent && status === 'idle') {
  78. dispose()
  79. resolve()
  80. }
  81. })
  82. })
  83. }
  84. export function finalText(events: SessionEvent[]): string {
  85. const message = events.findLast(event => event.type === 'assistant/message')
  86. if (message?.type !== 'assistant/message') return ''
  87. return message.data.content
  88. .filter(block => block.type === 'text')
  89. .map(block => block.text)
  90. .join('')
  91. }