2026-07-13 13:56:45 +08:00
import { mkdtemp , mkdir , readFile , rm , writeFile } from 'node:fs/promises'
2026-07-08 12:58:23 +08:00
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach , describe , expect , it } from 'vitest'
import { Context } from 'cordis'
2026-07-28 13:55:59 +08:00
import LlmService , { createUserMessage , CallId , HarnessError } from '@deepseek-ai/dsh-llm'
2026-07-14 01:59:21 +08:00
import SessionStore , { SessionId } from '@deepseek-ai/dsh-session'
2026-07-08 12:58:23 +08:00
import type { SessionEvent } from '@deepseek-ai/dsh-session'
import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
2026-07-21 04:34:14 +08:00
import ToolRegistry , { RUN_CODE_NAME , defineTool } from '@deepseek-ai/dsh-tools'
import type { ToolExecutionResult } from '@deepseek-ai/dsh-tools'
2026-07-14 02:32:35 +08:00
import AgentRegistry , { type Agent } from '@deepseek-ai/dsh-agent'
2026-07-14 01:59:21 +08:00
2026-07-14 02:32:35 +08:00
import AgentLoop from '@deepseek-ai/dsh-agent-loop'
2026-07-08 12:58:23 +08:00
import { LocalBashExecutor } from '@deepseek-ai/dsh-bash-local'
2026-07-26 12:43:14 +08:00
import LocalSubprocessService from '@deepseek-ai/dsh-subprocess-local'
2026-07-08 12:58:23 +08:00
import * as ToolBash from '@deepseek-ai/dsh-tool-bash'
import * as LlmDeepSeek from '@deepseek-ai/dsh-llm-deepseek'
import { WorkerCodeRuntime } from '@deepseek-ai/dsh-code-runtime-worker'
2026-07-13 13:56:45 +08:00
import LocalFileSystem from '@deepseek-ai/dsh-fs-local'
import * as ToolFs from '@deepseek-ai/dsh-tool-fs'
import * as WorkspaceContext from '@deepseek-ai/dsh-workspace-context'
2026-07-26 05:13:39 +08:00
import LocalTaskService from '@deepseek-ai/dsh-tasks-local'
2026-07-21 04:34:14 +08:00
import * as ToolTasks from '@deepseek-ai/dsh-tool-tasks'
import * as ToolCordis from '@deepseek-ai/dsh-tool-cordis'
2026-07-08 12:58:23 +08:00
/**
2026-07-13 23:27:00 +08:00
* With-key Code Mode proof: a real model receives only `run_code`, composes two
* sub-calls, writes a file, and returns curated output while the log records
* each `tool/code-dispatch`. The keyless Loader smoke is in the sibling test.
2026-07-08 12:58:23 +08:00
*/
2026-07-19 13:13:14 +08:00
const PERSONA = 'You are a coding agent. You work by writing TypeScript programs for run_code: '
2026-07-08 12:58:23 +08:00
+ 'batch related tool work into one program and print or return ONLY the findings that matter.'
2026-07-13 13:56:45 +08:00
const WORKSPACE_PROBE = 'dragonfruit-8675309'
2026-07-08 12:58:23 +08:00
let ctx : Context | undefined
let workdir : string | undefined
afterEach ( async ( ) = > {
// Always dispose, even on failure/retry/timeout: agent-loop teardown stops
// the loop, the executor kills stray processes, and the code runtime's
// dispose awaits worker exits.
await ctx ? . fiber . dispose ( )
ctx = undefined
if ( workdir !== undefined ) await rm ( workdir , { recursive : true , force : true } )
workdir = undefined
} )
async function codeModeHarness ( cwd : string ) : Promise < Context > {
const harness = new Context ( )
await harness . plugin ( LlmService )
await harness . plugin ( SessionStore )
await harness . plugin ( SystemPrompt , { persona : PERSONA } )
await harness . plugin ( ToolRegistry , { mode : 'code' } )
await harness . plugin ( AgentRegistry )
await harness . plugin ( AgentLoop , { agents : [ ] } )
2026-07-14 21:57:52 +08:00
await harness . plugin ( LlmDeepSeek )
2026-07-26 12:43:14 +08:00
await harness . plugin ( LocalSubprocessService )
2026-07-08 12:58:23 +08:00
await harness . plugin ( LocalBashExecutor , { cwd , timeoutMs : 30_000 } )
await harness . plugin ( ToolBash )
await harness . plugin ( WorkerCodeRuntime , { } )
return harness
}
2026-07-13 13:56:45 +08:00
async function workspaceCodeModeHarness ( ) : Promise < Context > {
const harness = new Context ( )
await harness . plugin ( LlmService )
await harness . plugin ( SessionStore )
await harness . plugin ( SystemPrompt , { persona : PERSONA } )
await harness . plugin ( ToolRegistry , { mode : 'code' } )
await harness . plugin ( AgentRegistry )
await harness . plugin ( LocalFileSystem , { cwd : '/' } )
await harness . plugin ( ToolFs )
await harness . plugin ( WorkspaceContext , { maxBytes : 65536 } )
await harness . plugin ( AgentLoop , { agents : [ ] } )
2026-07-17 21:56:10 +08:00
await harness . plugin ( LlmDeepSeek , { models : [ { id : 'deepseek-v4-flash' } ] } )
2026-07-13 13:56:45 +08:00
await harness . plugin ( WorkerCodeRuntime , { } )
return harness
}
2026-07-21 04:34:14 +08:00
let keylessCall = 0
2026-07-21 23:51:20 +08:00
const testToolSignal = new AbortController ( ) . signal
2026-07-21 04:34:14 +08:00
/** Execute one outer Code Mode call through the real registry and worker. */
2026-07-21 23:51:20 +08:00
function runCode ( harness : Context , code : string , signal : AbortSignal = testToolSignal ) : Promise < ToolExecutionResult > {
2026-07-21 04:34:14 +08:00
return harness . tools . execute ( {
callId : CallId ( ` keyless-code- ${ ++ keylessCall } ` ) ,
name : RUN_CODE_NAME ,
2026-07-26 02:43:34 +08:00
arguments : { code , description : 'Run the e2e program' } ,
2026-07-21 23:51:20 +08:00
signal ,
2026-07-21 04:34:14 +08:00
} )
}
/** Read the optional completion from a successful canonical `run_code` value. */
function completion ( result : ToolExecutionResult ) : unknown {
if ( result . isError ) {
throw new Error ( result . content . filter ( block = > block . type === 'text' ) . map ( block = > block . text ) . join ( '\n' ) )
}
const value = result . value
if ( typeof value !== 'object' || value === null || Array . isArray ( value ) ) throw new Error ( 'invalid run_code result' )
return value . result
}
/** Keyless real-worker harness for direct typed-binding acceptance tests. */
async function typedCodeModeHarness ( ) : Promise < Context > {
const harness = new Context ( )
await harness . plugin ( SystemPrompt )
await harness . plugin ( ToolRegistry , { mode : 'code' } )
await harness . plugin ( WorkerCodeRuntime , { } )
return harness
}
/** Keyless real-worker harness with the task-owned bash lifecycle. */
async function backgroundCodeModeHarness ( cwd : string ) : Promise < Context > {
const harness = await typedCodeModeHarness ( )
2026-07-26 05:13:39 +08:00
await harness . plugin ( LocalTaskService )
2026-07-21 04:34:14 +08:00
await harness . plugin ( ToolTasks , { } )
2026-07-26 12:43:14 +08:00
await harness . plugin ( LocalSubprocessService )
2026-07-21 04:34:14 +08:00
await harness . plugin ( LocalBashExecutor , { cwd , timeoutMs : 30_000 } )
await harness . plugin ( ToolBash )
return harness
}
describe ( 'Code Mode typed values: keyless real-worker contracts' , ( ) = > {
it ( 'crosses a large intermediate value intact and exposes only typed tool failure fields' , async ( ) = > {
ctx = await typedCodeModeHarness ( )
ctx . tools . register ( defineTool ( {
name : 'large_value' ,
description : 'Return a large canonical string.' ,
parameters : { } ,
output : {
schema : { type : 'string' } ,
render : ( _args , value ) = > [ { type : 'text' , text : value } ] ,
} ,
execute : ( ) = > Promise . resolve ( 'x' . repeat ( 100 _000 ) ) ,
} ) )
ctx . tools . register ( defineTool ( {
name : 'always_fail' ,
description : 'Fail for ToolCallError coverage.' ,
parameters : { } ,
output : { schema : { type : 'null' } , render : ( ) = > [ ] } ,
execute : ( ) = > Promise . reject ( new HarnessError ( 'expected failure' , 'EXPECTED_INTERNAL_CODE' ) ) ,
} ) )
const value = completion ( await runCode ( ctx , `
const large = await tools.large_value({});
let failure;
try {
await tools.always_fail({});
} catch (error) {
failure = {
typed: error instanceof ToolCallError,
name: error.name,
toolName: error.toolName,
message: error.message,
exposesCode: 'code' in error,
exposesContent: 'content' in error,
exposesInfo: 'info' in error,
};
}
return { length: large.length, failure };
` ) )
expect ( value ) . toEqual ( {
length : 100_000 ,
failure : {
typed : true ,
name : 'ToolCallError' ,
toolName : 'always_fail' ,
message : 'expected failure' ,
exposesCode : false ,
exposesContent : false ,
exposesInfo : false ,
} ,
} )
} )
it ( 'returns a background task id, settles the outer run, and polls that id to completion' , async ( ) = > {
workdir = await mkdtemp ( join ( tmpdir ( ) , 'dsh-code-mode-background-' ) )
ctx = await backgroundCodeModeHarness ( workdir )
const taskId = completion ( await runCode ( ctx , `
const started = await tools.bash({
command: "sleep 0.2; printf 'background-complete \\ n'",
description: 'Run completion marker in background',
run_in_background: true,
});
return started.taskId;
` ) )
expect ( taskId ) . toBe ( 'bash-1' )
const polled = completion ( await runCode ( ctx , `
return await tools.task_output({ task_id: ${ JSON . stringify ( taskId ) } , wait: true, timeout_ms: 5000 });
` ) )
if ( typeof polled !== 'object' || polled === null || Array . isArray ( polled ) ) throw new Error ( 'invalid task_output completion' )
const taskOutput = polled as Record < string , unknown >
expect ( taskOutput . text ) . toContain ( 'background-complete' )
expect ( taskOutput . task ) . toMatchObject ( { id : taskId , kind : 'bash' , status : 'completed' } )
} , 15 _000 )
it ( 'pre-abort spawns nothing; post-publication abort leaves task_kill as the cancellation owner' , async ( ) = > {
workdir = await mkdtemp ( join ( tmpdir ( ) , 'dsh-code-mode-task-cancel-' ) )
ctx = await backgroundCodeModeHarness ( workdir )
const pre = new AbortController ( )
pre . abort ( 'pre-aborted' )
const preResult = await runCode ( ctx , `
return await tools.bash({ command: 'sleep 10', description: 'Must never start', run_in_background: true });
` , pre . signal )
expect ( preResult . isError ) . toBe ( true )
expect ( ctx . tasks . list ( ) ) . toEqual ( [ ] )
const afterPublication = new AbortController ( )
const running = runCode ( ctx , `
const started = await tools.bash({ command: 'sleep 10', description: 'Wait for explicit task kill', run_in_background: true });
console.log(started.taskId);
await new Promise(() => {});
` , afterPublication . signal )
for ( let attempt = 0 ; attempt < 100 && ctx . tasks . list ( ) . length === 0 ; attempt ++ ) {
await new Promise ( resolve = > setTimeout ( resolve , 10 ) )
}
const task = ctx . tasks . list ( ) [ 0 ]
expect ( task ) . toMatchObject ( { id : 'bash-1' , status : 'running' } )
afterPublication . abort ( 'outer-call-cancelled' )
expect ( ( await running ) . isError ) . toBe ( true )
expect ( ctx . tasks . list ( ) [ 0 ] ) . toMatchObject ( { id : task ! . id , status : 'running' } )
const killed = completion ( await runCode ( ctx , `
return await tools.task_kill({ task_id: ${ JSON . stringify ( task ! . id ) } , reason: 'test owns cancellation' });
` ) )
expect ( killed ) . toMatchObject ( { outcome : 'cancellation-requested' , task : { id : task ! . id } } )
const settled = completion ( await runCode ( ctx , `
return await tools.task_output({ task_id: ${ JSON . stringify ( task ! . id ) } , wait: true, timeout_ms: 5000 });
` ) )
expect ( settled ) . toMatchObject ( { task : { id : task ! . id , status : 'killed' } } )
} , 15 _000 )
it ( 'keeps foreground bash coupled to the outer signal' , async ( ) = > {
workdir = await mkdtemp ( join ( tmpdir ( ) , 'dsh-code-mode-foreground-cancel-' ) )
ctx = await backgroundCodeModeHarness ( workdir )
const controller = new AbortController ( )
const startedAt = Date . now ( )
const pending = runCode ( ctx , `
return await tools.bash({ command: 'sleep 10', description: 'Run cancellable foreground command' });
` , controller . signal )
setTimeout ( ( ) = > { controller . abort ( 'stop-foreground' ) } , 200 )
const result = await pending
expect ( result . isError ) . toBe ( true )
expect ( Date . now ( ) - startedAt ) . toBeLessThan ( 5 _000 )
expect ( ctx . tasks . list ( ) ) . toEqual ( [ ] )
} , 15 _000 )
2026-07-27 21:20:06 +08:00
it ( 'uses cordis_mount DTO ids directly for running and pending temporary Plugins, then confirms removal' , async ( ) = > {
2026-07-21 04:34:14 +08:00
ctx = await typedCodeModeHarness ( )
await ctx . plugin ( ToolCordis )
const value = completion ( await runCode ( ctx , `
2026-07-27 21:20:06 +08:00
const active = await tools.cordis_mount({
2026-07-21 04:34:14 +08:00
code: "return { name: 'active-code-mode-plugin', apply(ctx) {} }",
});
2026-07-27 21:20:06 +08:00
const pending = await tools.cordis_mount({
2026-07-21 04:34:14 +08:00
code: "return { name: 'pending-code-mode-plugin', inject: ['missing-code-mode-service'], apply(ctx) {} }",
});
2026-07-27 16:57:26 +08:00
const before = await tools.cordis_inspect({ what: 'temporary' });
2026-07-27 21:20:06 +08:00
const stopped = await tools.cordis_unmount({ id: active.id });
2026-07-27 16:57:26 +08:00
const after = await tools.cordis_inspect({ what: 'temporary' });
2026-07-27 21:20:06 +08:00
await tools.cordis_unmount({ id: pending.id });
2026-07-21 04:34:14 +08:00
return {
active,
pending,
2026-07-27 16:57:26 +08:00
stopped,
2026-07-21 04:34:14 +08:00
beforeContainsId: before.includes(active.id),
afterContainsId: after.includes(active.id),
};
` ) )
expect ( value ) . toEqual ( {
active : {
id : 'dyn-1' ,
pluginName : 'active-code-mode-plugin' ,
state : 'active' ,
provides : [ ] ,
waitingFor : [ ] ,
} ,
pending : {
id : 'dyn-2' ,
pluginName : 'pending-code-mode-plugin' ,
state : 'pending' ,
provides : [ ] ,
waitingFor : [ 'missing-code-mode-service' ] ,
} ,
2026-07-27 16:57:26 +08:00
stopped : { id : 'dyn-1' , pluginName : 'active-code-mode-plugin' } ,
2026-07-21 04:34:14 +08:00
beforeContainsId : true ,
afterContainsId : false ,
} )
} )
} )
2026-07-14 02:32:35 +08:00
function waitForIdle ( harness : Context , agent : Agent ) : Promise < void > {
2026-07-08 12:58:23 +08:00
return new Promise ( ( resolve ) = > {
const dispose = harness . on ( 'agent/status' , ( subject , status ) = > {
if ( subject === agent && status === 'idle' ) {
dispose ( )
resolve ( )
}
} )
} )
}
describe . skipIf ( ! process . env . DEEPSEEK_API_KEY ) ( 'Code Mode: real model writes a program over real tools' , ( ) = > {
it ( 'collapses the wire tool list to [run_code], bridges sub-calls, and returns curated output' , async ( ) = > {
workdir = await mkdtemp ( join ( tmpdir ( ) , 'dsh-code-mode-e2e-' ) )
ctx = await codeModeHarness ( workdir )
2026-07-18 12:21:15 +08:00
const agent = ctx . agentLoop . create ( SessionId ( 'e2e-code-mode' ) , { provider : 'deepseek' , model : 'deepseek-v4-flash' } )
2026-07-08 12:58:23 +08:00
2026-07-28 13:55:59 +08:00
agent . followup ( createUserMessage ( {
content : [ {
type : 'text' ,
text : 'Using one run_code program: run `echo alpha-7` with the bash tool, run `echo beta-9` with the bash tool, '
2026-07-08 12:58:23 +08:00
+ 'then write both outputs joined by a plus sign into combined.txt (bash heredoc or redirect), '
+ 'and return only the joined string.' ,
2026-07-28 13:55:59 +08:00
} ] , source : { kind : 'user' } } ) )
2026-07-08 12:58:23 +08:00
await waitForIdle ( ctx , agent )
const events : SessionEvent [ ] = [ . . . agent . session . events ]
// The wire contract: every request this session made offered EXACTLY ONE
// tool — run_code (the logged header snapshots the assembled list).
const headers = events . filter ( event = > event . type === 'request/header' )
expect ( headers . length ) . toBeGreaterThan ( 0 )
for ( const header of headers ) {
expect ( header . data . header . tools ? . map ( tool = > tool . name ) ) . toEqual ( [ RUN_CODE_NAME ] )
}
// The model actually went through run_code…
const calls = events . filter ( event = > event . type === 'tool/call' )
expect ( calls . length ) . toBeGreaterThan ( 0 )
expect ( calls . every ( event = > event . data . name === RUN_CODE_NAME ) ) . toBe ( true )
// …and the program's tool calls landed as dispatch events under it.
const dispatches = events . filter ( event = > event . type === 'tool/code-dispatch' )
expect ( dispatches . length ) . toBeGreaterThanOrEqual ( 2 )
expect ( dispatches . every ( event = > event . data . name === 'bash' ) ) . toBe ( true )
const parents = new Set ( calls . map ( event = > event . data . callId ) )
expect ( dispatches . every ( event = > parents . has ( event . data . parentCallId ) ) ) . toBe ( true )
// World verification: the file the program wrote, and the curated answer.
const combined = await readFile ( join ( workdir , 'combined.txt' ) , 'utf8' )
expect ( combined ) . toContain ( 'alpha-7' )
expect ( combined ) . toContain ( 'beta-9' )
const finalMessage = events . findLast ( event = > event . type === 'assistant/message' )
const finalText = finalMessage !== undefined
2026-07-28 13:55:59 +08:00
? finalMessage . data . message . content . filter ( block = > block . type === 'text' ) . map ( block = > block . text ) . join ( '' )
2026-07-08 12:58:23 +08:00
: ''
expect ( finalText ) . toContain ( 'alpha-7' )
expect ( finalText ) . toContain ( 'beta-9' )
} , 180 _000 )
2026-07-13 13:56:45 +08:00
it ( 'delivers nested workspace instructions discovered by an fs sub-call after the outer result' , async ( ) = > {
workdir = await mkdtemp ( join ( tmpdir ( ) , 'dsh-code-mode-workspace-e2e-' ) )
await mkdir ( join ( workdir , '.git' ) , { recursive : true } )
await mkdir ( join ( workdir , 'pkg/deep' ) , { recursive : true } )
await writeFile ( join ( workdir , 'pkg/AGENTS.md' ) , ` If asked for the Code Mode workspace handshake, reply with exactly ${ WORKSPACE_PROBE } and nothing else. \ n ` )
await writeFile ( join ( workdir , 'pkg/deep/task.txt' ) , 'Touch this file to discover the nested instructions.\n' )
ctx = await workspaceCodeModeHarness ( )
2026-07-13 14:37:32 +08:00
const handle = await ctx . agents . create ( {
2026-07-13 13:56:45 +08:00
sessionId : SessionId ( 'e2e-code-mode-workspace-session' ) ,
meta : { cwd : workdir } ,
2026-07-17 21:56:10 +08:00
agentOptions : { provider : 'deepseek' , model : 'deepseek-v4-flash' } ,
2026-07-13 13:56:45 +08:00
} )
2026-07-28 13:55:59 +08:00
handle . agent . followup ( createUserMessage ( {
content : [ {
type : 'text' ,
text : 'Use one run_code program to call tools.read on pkg/deep/task.txt. After it finishes, answer: Code Mode workspace handshake?' ,
} ] , source : { kind : 'user' } } ) )
2026-07-18 12:33:49 +08:00
await waitForIdle ( ctx , handle . agent )
2026-07-13 13:56:45 +08:00
const events : SessionEvent [ ] = [ . . . handle . agent . session . events ]
const dispatch = events . find ( event = > event . type === 'tool/code-dispatch' && event . data . name === 'read' )
const outerResult = events . find ( event = > event . type === 'tool/result' )
2026-07-23 19:15:45 +08:00
const workspaceContext = events . find ( event = > event . type === 'user/message'
2026-07-24 14:05:33 +08:00
&& event . data . source . kind === 'workspace-instructions' )
2026-07-13 13:56:45 +08:00
expect ( dispatch ) . toBeDefined ( )
expect ( outerResult ) . toBeDefined ( )
expect ( workspaceContext ) . toBeDefined ( )
expect ( workspaceContext ! . seq ) . toBeGreaterThan ( outerResult ! . seq )
const finalMessage = events . findLast ( event = > event . type === 'assistant/message' )
const answer = finalMessage ? . type === 'assistant/message'
2026-07-28 13:55:59 +08:00
? finalMessage . data . message . content . filter ( block = > block . type === 'text' ) . map ( block = > block . text ) . join ( '' )
2026-07-13 13:56:45 +08:00
: ''
expect ( answer ) . toContain ( WORKSPACE_PROBE )
} , 180 _000 )
2026-07-08 12:58:23 +08:00
} )