@@ -87,6 +87,78 @@ public async Task ExecuteAsync_Should_RecordTerminalHistory_When_CommandRuns()
8787 session . SessionState . TerminalHistory [ 0 ] . StandardOutput . Should ( ) . Be ( "Passed!" ) ;
8888 }
8989
90+ [ Fact ]
91+ public async Task ExecuteAsync_Should_UpdateSessionWorkingDirectory_ForFollowUpFileTools ( )
92+ {
93+ string workspaceRoot = Path . Combine (
94+ Path . GetTempPath ( ) ,
95+ $ "NanoAgent-ShellCwd-{ Guid . NewGuid ( ) : N} ") ;
96+ Directory . CreateDirectory ( Path . Combine ( workspaceRoot , "ToDoApp" ) ) ;
97+
98+ try
99+ {
100+ ReplSessionContext session = TestSessionFactory . Create ( workspaceRoot ) ;
101+ Mock < IShellCommandService > shellCommandService = new ( MockBehavior . Strict ) ;
102+ shellCommandService
103+ . Setup ( service => service . ExecuteAsync (
104+ It . Is < ShellCommandExecutionRequest > ( request =>
105+ request . Command == "cd ToDoApp" &&
106+ request . WorkingDirectory == "." ) ,
107+ It . IsAny < CancellationToken > ( ) ) )
108+ . ReturnsAsync ( new ShellCommandExecutionResult (
109+ "cd ToDoApp" ,
110+ "." ,
111+ 0 ,
112+ string . Empty ,
113+ string . Empty ) ) ;
114+
115+ ShellCommandTool shellTool = new ( shellCommandService . Object ) ;
116+ ToolResult shellResult = await shellTool . ExecuteAsync (
117+ CreateContext ( """{ "command": "cd ToDoApp" }""" , session ) ,
118+ CancellationToken . None ) ;
119+
120+ shellResult . Status . Should ( ) . Be ( ToolResultStatus . Success ) ;
121+ shellResult . Message . Should ( ) . Contain ( "Session working directory is now 'ToDoApp'" ) ;
122+ session . WorkingDirectory . Should ( ) . Be ( "ToDoApp" ) ;
123+
124+ Mock < IWorkspaceFileService > workspaceFileService = new ( MockBehavior . Strict ) ;
125+ workspaceFileService
126+ . Setup ( service => service . WriteFileWithTrackingAsync (
127+ "ToDoApp/Program.cs" ,
128+ "class Program {}" ,
129+ true ,
130+ It . IsAny < CancellationToken > ( ) ) )
131+ . ReturnsAsync ( new WorkspaceFileWriteExecutionResult (
132+ new WorkspaceFileWriteResult (
133+ "ToDoApp/Program.cs" ,
134+ false ,
135+ 16 ,
136+ 1 ,
137+ 0 ,
138+ [ new WorkspaceFileWritePreviewLine ( 1 , "add" , "class Program {}" ) ] ,
139+ 0 ) ,
140+ new WorkspaceFileEditTransaction (
141+ "file_write (ToDoApp/Program.cs)" ,
142+ [ new WorkspaceFileEditState ( "ToDoApp/Program.cs" , exists : false , content : null ) ] ,
143+ [ new WorkspaceFileEditState ( "ToDoApp/Program.cs" , exists : true , content : "class Program {}" ) ] ) ) ) ;
144+
145+ FileWriteTool fileWriteTool = new ( workspaceFileService . Object ) ;
146+ ToolResult writeResult = await fileWriteTool . ExecuteAsync (
147+ CreateFileWriteContext ( """{ "path": "Program.cs", "content": "class Program {}" }""" , session ) ,
148+ CancellationToken . None ) ;
149+
150+ writeResult . Status . Should ( ) . Be ( ToolResultStatus . Success ) ;
151+ workspaceFileService . VerifyAll ( ) ;
152+ }
153+ finally
154+ {
155+ if ( Directory . Exists ( workspaceRoot ) )
156+ {
157+ Directory . Delete ( workspaceRoot , recursive : true ) ;
158+ }
159+ }
160+ }
161+
90162 [ Fact ]
91163 public async Task ExecuteAsync_Should_ForwardSandboxEscalationArguments_When_Provided ( )
92164 {
@@ -178,13 +250,27 @@ [new ShellCommandTool(Mock.Of<IShellCommandService>())],
178250 ] ) ;
179251 }
180252
181- private static ToolExecutionContext CreateContext ( string argumentsJson )
253+ private static ToolExecutionContext CreateContext (
254+ string argumentsJson ,
255+ ReplSessionContext ? session = null )
182256 {
183257 using JsonDocument document = JsonDocument . Parse ( argumentsJson ) ;
184258 return new ToolExecutionContext (
185259 "call_1" ,
186260 "shell_command" ,
187261 document . RootElement . Clone ( ) ,
188- TestSessionFactory . Create ( ) ) ;
262+ session ?? TestSessionFactory . Create ( ) ) ;
263+ }
264+
265+ private static ToolExecutionContext CreateFileWriteContext (
266+ string argumentsJson ,
267+ ReplSessionContext session )
268+ {
269+ using JsonDocument document = JsonDocument . Parse ( argumentsJson ) ;
270+ return new ToolExecutionContext (
271+ "call_2" ,
272+ "file_write" ,
273+ document . RootElement . Clone ( ) ,
274+ session ) ;
189275 }
190276}
0 commit comments