harness.ts 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  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 coding-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. /** Durable JSONL persistence root (the resume suite needs it; others stay file-free). */
  33. persistenceRoot?: string
  34. /**
  35. * Load {@link BasicCompactService} with this config so the compaction e2e can
  36. * trigger compaction at a small, controlled history size. Omitted ⇒ no
  37. * compaction plugin (the default suites run without it).
  38. */
  39. compact?: BasicCompactConfig
  40. }
  41. export async function codingHarness(workdir: string, options: CodingHarnessOptions = {}): Promise<Context> {
  42. const ctx = new Context()
  43. await ctx.plugin(LlmService)
  44. await ctx.plugin(SessionStore)
  45. await ctx.plugin(SystemPrompt)
  46. await ctx.plugin(ToolRegistry)
  47. await ctx.plugin(AgentRegistry)
  48. await ctx.plugin(AgentLoop, { agents: [] })
  49. await ctx.plugin(LlmDeepSeek, { models: ['deepseek-v4-flash'] })
  50. await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
  51. await ctx.plugin(ToolBash)
  52. await ctx.plugin(ToolTodo)
  53. // Compaction is opt-in: only the compaction e2e loads it, with a lowered
  54. // contextWindow/retainTokens so a short real session crosses the threshold.
  55. if (options.compact !== undefined) await ctx.plugin(BasicCompactService, options.compact)
  56. // Durable JSONL persistence is opt-in: only the resume e2e needs it, and the
  57. // other suites stay file-free. Loaded last so a resume's deferred
  58. // `ctx.inject(['sessionPersistence'])` resolves once this is present.
  59. if (options.persistenceRoot !== undefined) await ctx.plugin(SessionPersistenceJsonl, { root: options.persistenceRoot })
  60. return ctx
  61. }
  62. export function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise<void> {
  63. return new Promise((resolve) => {
  64. const dispose = ctx.on('agent/status', (subject, status) => {
  65. if (subject === agent && status === 'idle') {
  66. dispose()
  67. resolve()
  68. }
  69. })
  70. })
  71. }
  72. export function finalText(events: SessionEvent[]): string {
  73. const message = events.findLast(event => event.type === 'assistant/message')
  74. if (message?.type !== 'assistant/message') return ''
  75. return message.data.content
  76. .filter(block => block.type === 'text')
  77. .map(block => block.text)
  78. .join('')
  79. }