2026-06-13 00:47:32 +08:00
|
|
|
import { Context } from 'cordis'
|
|
|
|
|
import type { SessionEvent } from '@deepseek-ai/dsh-session'
|
2026-07-18 12:33:49 +08:00
|
|
|
import type { Agent } from '@deepseek-ai/dsh-agent'
|
2026-07-14 02:32:35 +08:00
|
|
|
import AgentLoop from '@deepseek-ai/dsh-agent-loop'
|
2026-07-16 17:39:53 +08:00
|
|
|
import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit'
|
2026-06-13 00:47:32 +08:00
|
|
|
import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local'
|
|
|
|
|
import * as ToolBash from '@deepseek-ai/dsh-tool-bash'
|
2026-06-29 10:56:55 +08:00
|
|
|
import * as ToolTodo from '@deepseek-ai/dsh-tool-todo'
|
2026-06-13 00:47:32 +08:00
|
|
|
import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
|
2026-07-15 14:47:29 +08:00
|
|
|
import TokenMeterService from '@deepseek-ai/dsh-token-meter'
|
2026-07-16 18:51:26 +08:00
|
|
|
import ToolResultPruneService from '@deepseek-ai/dsh-compact-tool-result-prune'
|
2026-06-16 22:28:01 +08:00
|
|
|
import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
|
2026-07-21 14:50:06 +08:00
|
|
|
import * as SessionCheckpointPolicy from '@deepseek-ai/dsh-session-checkpoint-policy'
|
2026-06-26 08:59:33 +08:00
|
|
|
import { BasicCompactService } from '@deepseek-ai/dsh-compact-basic'
|
|
|
|
|
import type { BasicCompactConfig } from '@deepseek-ai/dsh-compact-basic'
|
2026-06-13 00:47:32 +08:00
|
|
|
|
|
|
|
|
/**
|
2026-07-20 19:26:04 +08:00
|
|
|
* Shared harness for the headless-agent e2e suites: the full plugin stack
|
2026-06-29 10:56:55 +08:00
|
|
|
* with the real DeepSeek adapter and the real bash + todo_write tools. Lives
|
|
|
|
|
* outside the *.e2e.ts pattern so importing it never re-registers another
|
|
|
|
|
* file's tests.
|
2026-06-13 00:47:32 +08:00
|
|
|
*/
|
|
|
|
|
|
2026-06-29 19:44:38 +08:00
|
|
|
export const SYSTEM_PROMPT = 'You are a coding agent. Use bash for file operations '
|
|
|
|
|
+ 'with cat/grep/heredocs; check [exit code: N] markers, '
|
2026-06-13 00:47:32 +08:00
|
|
|
+ 'and report results briefly.'
|
|
|
|
|
|
2026-06-29 10:56:55 +08:00
|
|
|
/** System prompt for the todo_write e2e: nudges the model to plan with the tool. */
|
|
|
|
|
export const TODO_SYSTEM_PROMPT = 'You are a coding agent. For multi-step work, '
|
|
|
|
|
+ 'use the todo_write tool to track a task list: send the WHOLE list each call, '
|
2026-06-29 19:44:38 +08:00
|
|
|
+ 'keep at most one task in_progress (exactly one while work remains), and mark '
|
|
|
|
|
+ 'a task completed as soon as it is done.'
|
2026-06-29 10:56:55 +08:00
|
|
|
|
2026-06-26 08:59:33 +08:00
|
|
|
/** Options for {@link codingHarness}. */
|
|
|
|
|
export interface CodingHarnessOptions {
|
2026-07-05 23:23:46 +08:00
|
|
|
/**
|
|
|
|
|
* Deployment persona for the tree (the system-prompt plugin's `persona`
|
|
|
|
|
* config — per-context, not per-agent). Omitted ⇒ no persona section.
|
|
|
|
|
*/
|
|
|
|
|
persona?: string
|
2026-06-26 08:59:33 +08:00
|
|
|
/** Durable JSONL persistence root (the resume suite needs it; others stay file-free). */
|
|
|
|
|
persistenceRoot?: string
|
|
|
|
|
/**
|
|
|
|
|
* Load {@link BasicCompactService} with this config so the compaction e2e can
|
|
|
|
|
* trigger compaction at a small, controlled history size. Omitted ⇒ no
|
|
|
|
|
* compaction plugin (the default suites run without it).
|
|
|
|
|
*/
|
|
|
|
|
compact?: BasicCompactConfig
|
2026-07-20 15:34:00 +08:00
|
|
|
/** Test-only context capacity advertised for `deepseek-v4-flash`. */
|
|
|
|
|
modelContextWindow?: number
|
2026-06-26 08:59:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function codingHarness(workdir: string, options: CodingHarnessOptions = {}): Promise<Context> {
|
2026-06-13 00:47:32 +08:00
|
|
|
const ctx = new Context()
|
2026-07-16 17:39:53 +08:00
|
|
|
await mountAgentLoopTestDependencies(ctx, {
|
|
|
|
|
systemPrompt: { persona: options.persona ?? '' },
|
|
|
|
|
})
|
2026-06-13 00:47:32 +08:00
|
|
|
await ctx.plugin(AgentLoop, { agents: [] })
|
2026-07-20 15:34:00 +08:00
|
|
|
await ctx.plugin(LlmDeepSeek, options.modelContextWindow === undefined ? {} : {
|
|
|
|
|
models: [{ id: 'deepseek-v4-flash', contextWindow: options.modelContextWindow }],
|
|
|
|
|
})
|
2026-06-13 00:47:32 +08:00
|
|
|
await ctx.plugin(LocalBashExecutor, { cwd: workdir, timeoutMs: 30_000 })
|
|
|
|
|
await ctx.plugin(ToolBash)
|
2026-06-29 10:56:55 +08:00
|
|
|
await ctx.plugin(ToolTodo)
|
2026-07-20 15:34:00 +08:00
|
|
|
// Compaction is opt-in: only the compaction e2e loads the reusable meter and backend.
|
2026-07-15 14:47:29 +08:00
|
|
|
if (options.compact !== undefined) {
|
2026-07-20 15:34:00 +08:00
|
|
|
await ctx.plugin(TokenMeterService)
|
2026-07-16 18:02:15 +08:00
|
|
|
await ctx.plugin(ToolResultPruneService)
|
2026-07-15 14:47:29 +08:00
|
|
|
await ctx.plugin(BasicCompactService, options.compact)
|
|
|
|
|
}
|
2026-06-16 22:28:01 +08:00
|
|
|
// Durable JSONL persistence is opt-in: only the resume e2e needs it, and the
|
|
|
|
|
// other suites stay file-free. Loaded last so a resume's deferred
|
|
|
|
|
// `ctx.inject(['sessionPersistence'])` resolves once this is present.
|
2026-07-21 14:50:06 +08:00
|
|
|
if (options.persistenceRoot !== undefined) {
|
|
|
|
|
await ctx.plugin(SessionPersistenceJsonl, { root: options.persistenceRoot })
|
|
|
|
|
await ctx.plugin(SessionCheckpointPolicy)
|
|
|
|
|
}
|
2026-06-13 00:47:32 +08:00
|
|
|
return ctx
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-14 02:32:35 +08:00
|
|
|
export function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
|
2026-06-13 00:47:32 +08:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const dispose = ctx.on('agent/status', (subject, status) => {
|
|
|
|
|
if (subject === agent && status === 'idle') {
|
|
|
|
|
dispose()
|
|
|
|
|
resolve()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function finalText(events: SessionEvent[]): string {
|
|
|
|
|
const message = events.findLast(event => event.type === 'assistant/message')
|
|
|
|
|
if (message?.type !== 'assistant/message') return ''
|
|
|
|
|
return message.data.content
|
|
|
|
|
.filter(block => block.type === 'text')
|
|
|
|
|
.map(block => block.text)
|
|
|
|
|
.join('')
|
|
|
|
|
}
|