octomind 0.20.0

Session-based AI development assistant with conversational codebase interaction, multimodal vision support, built-in MCP tools, and multi-provider AI integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Tests for the Developer MCP provider

#[cfg(test)]
mod tests {
	use crate::mcp::dev::ast_grep::execute_ast_grep_command;
	use crate::mcp::dev::shell::execute_shell_command;
	use crate::mcp::{extract_mcp_content, McpToolCall};
	use serde_json::json;
	use tokio;

	// Helper function to create a shell tool call
	fn create_shell_call(command: &str, background: Option<bool>) -> McpToolCall {
		let mut params = json!({
			"command": command
		});

		if let Some(bg) = background {
			params["background"] = json!(bg);
		}

		McpToolCall {
			tool_name: "shell".to_string(),
			parameters: params,
			tool_id: "test-call-id".to_string(),
		}
	}

	// Helper function to create an ast_grep tool call
	fn create_ast_grep_call(pattern: &str, language: Option<&str>) -> McpToolCall {
		let mut params = json!({
			"pattern": pattern
		});

		if let Some(lang) = language {
			params["language"] = json!(lang);
		}

		McpToolCall {
			tool_name: "ast_grep".to_string(),
			parameters: params,
			tool_id: "test-call-id".to_string(),
		}
	}

	#[tokio::test]
	async fn test_shell_foreground_simple_command() {
		let call = create_shell_call("echo 'Hello, World!'", Some(false));
		let result = execute_shell_command(&call).await;

		assert!(result.is_ok());
		let result = result.unwrap();
		assert_eq!(result.tool_name, "shell");

		let output = result.result.as_object().unwrap();
		// Check MCP-compliant success format
		assert_eq!(output["isError"], false);

		// Extract content using MCP protocol
		let content = extract_mcp_content(&result.result);
		assert!(content.contains("Hello, World!"));

		// These fields don't exist in foreground MCP format
		assert!(!output.contains_key("background"));
		assert!(!output.contains_key("pid"));
	}

	#[tokio::test]
	async fn test_shell_foreground_command_with_error() {
		let call = create_shell_call("ls /nonexistent/directory/path", Some(false));
		let result = execute_shell_command(&call).await;

		assert!(result.is_ok());
		let result = result.unwrap();

		let output = result.result.as_object().unwrap();
		// Check MCP-compliant error format
		assert_eq!(output["isError"], true);

		// Extract content using MCP protocol - error messages include command and exit code info
		let content = extract_mcp_content(&result.result);
		assert!(content.contains("Command failed with exit code"));
	}

	#[tokio::test]
	async fn test_shell_background_simple_command() {
		// Use a command that runs for a short time but long enough to test background execution
		let call = create_shell_call("sleep 2", Some(true));
		let result = execute_shell_command(&call).await;

		assert!(result.is_ok());
		let result = result.unwrap();
		assert_eq!(result.tool_name, "shell");

		let output = result.result.as_object().unwrap();
		assert_eq!(output["success"], true);
		assert_eq!(output["background"], true);
		assert!(output.contains_key("pid"));
		assert!(output["message"].as_str().unwrap().contains("background"));

		let pid = output["pid"].as_u64().unwrap();
		assert!(pid > 0);

		// Verify the process is actually running by checking if we can kill it
		let kill_call = create_shell_call(&format!("kill {}", pid), Some(false));
		let kill_result = execute_shell_command(&kill_call).await;
		assert!(kill_result.is_ok());
	}

	#[tokio::test]
	async fn test_shell_background_long_running_process() {
		// Test with a longer running process
		let call = create_shell_call("sleep 10", Some(true));
		let result = execute_shell_command(&call).await;

		assert!(result.is_ok());
		let result = result.unwrap();

		let output = result.result.as_object().unwrap();
		assert_eq!(output["success"], true);
		assert_eq!(output["background"], true);

		let pid = output["pid"].as_u64().unwrap();

		// Immediately kill the background process to clean up
		let kill_call = create_shell_call(&format!("kill {}", pid), Some(false));
		let kill_result = execute_shell_command(&kill_call).await;
		assert!(kill_result.is_ok());
	}

	#[tokio::test]
	async fn test_shell_cancellation_foreground() {
		// Test that the shell command itself no longer handles cancellation
		// The centralized architecture means individual tools don't need cancellation logic
		let call = create_shell_call("echo 'test'", Some(false));

		// Direct tool call should work without cancellation_token parameter
		let result = execute_shell_command(&call).await;
		assert!(result.is_ok());

		let result = result.unwrap();
		let output = result.result.as_object().unwrap();
		// Should succeed normally since cancellation is handled at wrapper level
		assert_eq!(output["isError"], false);

		// Extract content using MCP protocol
		let content = extract_mcp_content(&result.result);
		assert!(content.contains("test"));
	}

	#[tokio::test]
	async fn test_centralized_cancellation_architecture() {
		// Test that demonstrates the centralized cancellation architecture
		// Internal tools no longer accept cancellation_token parameters

		// Shell command works without cancellation_token
		let shell_call = create_shell_call("echo 'centralized cancellation test'", Some(false));
		let shell_result = execute_shell_command(&shell_call).await;
		assert!(shell_result.is_ok());

		// Tool returns proper MCP-compliant results
		let shell_result = shell_result.unwrap();
		let output = shell_result.result.as_object().unwrap();

		// Should succeed normally since cancellation is handled at wrapper level
		assert_eq!(output["isError"], false);

		// Extract content using MCP protocol
		let content = extract_mcp_content(&shell_result.result);
		assert!(content.contains("centralized cancellation test"));

		// This test validates that:
		// - Internal tools focus purely on business logic
		// - No cancellation_token parameters in internal tool signatures
		// - Centralized wrapper (try_execute_tool_call) handles cancellation
		// - MCP-compliant results are returned consistently
	}

	#[tokio::test]
	async fn test_shell_default_background_parameter() {
		// Test that background defaults to false when not specified
		let call = create_shell_call("echo 'test'", None);
		let result = execute_shell_command(&call).await;

		assert!(result.is_ok());
		let result = result.unwrap();

		let output = result.result.as_object().unwrap();
		// Check MCP-compliant success format
		assert_eq!(output["isError"], false);

		// These fields don't exist in foreground MCP format
		assert!(!output.contains_key("background"));

		// Extract content using MCP protocol
		let content = extract_mcp_content(&result.result);
		assert!(content.contains("test"));
	}

	#[tokio::test]
	async fn test_ast_grep_simple_search() {
		// Create a temporary file for testing
		let temp_content = r#"
function testFunction() {
    console.log("test");
    return true;
}
"#;

		// Write to a temporary file
		let temp_file = "test_ast_grep_temp.js";
		std::fs::write(temp_file, temp_content).unwrap();

		// Test ast-grep search
		let call = create_ast_grep_call("console.log($$$)", Some("javascript"));
		let result = execute_ast_grep_command(&call).await;

		// Clean up
		let _ = std::fs::remove_file(temp_file);

		assert!(result.is_ok());
		let result = result.unwrap();
		assert_eq!(result.tool_name, "ast_grep");

		let output = result.result.as_object().unwrap();
		// Note: ast-grep might not find matches if the temp file isn't in the search path
		// This test mainly verifies the tool doesn't crash
		assert!(output.contains_key("success"));
	}

	#[tokio::test]
	async fn test_ast_grep_with_invalid_pattern() {
		let call = create_ast_grep_call("", Some("javascript"));
		let result = execute_ast_grep_command(&call).await;

		// Should handle empty pattern gracefully
		assert!(result.is_ok());
		let result = result.unwrap();
		let output = result.result.as_object().unwrap();
		assert_eq!(output["isError"], true);
		let error_content = &output["content"][0]["text"];
		assert!(error_content
			.as_str()
			.unwrap()
			.contains("Pattern parameter cannot be empty"));
	}

	#[tokio::test]
	async fn test_ast_grep_glob_pattern_expansion() {
		// Create temporary test files
		let temp_dir = tempfile::tempdir().unwrap();
		let temp_dir_path = temp_dir.path();

		// Create a subdirectory structure
		let src_dir = temp_dir_path.join("src");
		std::fs::create_dir_all(&src_dir).unwrap();

		// Create test files
		let test_file1 = src_dir.join("test1.rs");
		let test_file2 = src_dir.join("test2.rs");
		let test_file3 = temp_dir_path.join("other.txt");

		std::fs::write(&test_file1, "fn test_function() { println!(\"test1\"); }").unwrap();
		std::fs::write(
			&test_file2,
			"fn another_function() { println!(\"test2\"); }",
		)
		.unwrap();
		std::fs::write(&test_file3, "not rust code").unwrap();

		// Test with glob pattern
		let glob_pattern = format!("{}/**/*.rs", temp_dir_path.display());
		let params = json!({
			"pattern": "fn $NAME($ARGS) { $$$ }",
			"language": "rust",
			"paths": [glob_pattern]
		});

		let call = McpToolCall {
			tool_name: "ast_grep".to_string(),
			parameters: params,
			tool_id: "test-glob-call-id".to_string(),
		};

		let result = execute_ast_grep_command(&call).await;

		assert!(result.is_ok());
		let result = result.unwrap();
		assert_eq!(result.tool_name, "ast_grep");

		let output = result.result.as_object().unwrap();
		assert!(output.contains_key("success"));

		// The test should succeed even if no matches are found, as long as glob expansion works
		// (ast-grep might not find matches depending on the pattern, but it shouldn't error)
	}

	#[tokio::test]
	async fn test_shell_command_history_integration() {
		// Test that commands are added to shell history
		let call = create_shell_call("echo 'history test'", Some(false));
		let result = execute_shell_command(&call).await;

		assert!(result.is_ok());
		let result = result.unwrap();

		let output = result.result.as_object().unwrap();
		// Check MCP-compliant success format
		assert_eq!(output["isError"], false);

		// Extract content using MCP protocol
		let content = extract_mcp_content(&result.result);
		assert!(content.contains("history test"));
	}

	#[tokio::test]
	async fn test_multiple_background_processes() {
		// Test starting multiple background processes
		let mut pids = Vec::new();

		for i in 1..=3 {
			let call = create_shell_call(&format!("sleep {}", i * 2), Some(true));
			let result = execute_shell_command(&call).await;

			assert!(result.is_ok());
			let result = result.unwrap();
			let output = result.result.as_object().unwrap();
			assert_eq!(output["background"], true);

			let pid = output["pid"].as_u64().unwrap();
			pids.push(pid);
		}

		// Clean up all background processes
		for pid in pids {
			let kill_call = create_shell_call(&format!("kill {}", pid), Some(false));
			let _ = execute_shell_command(&kill_call).await;
		}
	}

	#[tokio::test]
	async fn test_shell_missing_command_parameter() {
		let call = McpToolCall {
			tool_name: "shell".to_string(),
			parameters: json!({}), // Missing command parameter
			tool_id: "test-call-id".to_string(),
		};

		let result = execute_shell_command(&call).await;
		assert!(result.is_ok());
		let result = result.unwrap();
		let output = result.result.as_object().unwrap();
		assert_eq!(output["isError"], true);
		let error_content = &output["content"][0]["text"];
		assert!(error_content
			.as_str()
			.unwrap()
			.contains("Missing required 'command' parameter"));
	}

	#[tokio::test]
	async fn test_ast_grep_missing_pattern_parameter() {
		let call = McpToolCall {
			tool_name: "ast_grep".to_string(),
			parameters: json!({}), // Missing pattern parameter
			tool_id: "test-call-id".to_string(),
		};

		let result = execute_ast_grep_command(&call).await;
		assert!(result.is_ok());
		let result = result.unwrap();
		let output = result.result.as_object().unwrap();
		assert_eq!(output["isError"], true);
		let error_content = &output["content"][0]["text"];
		assert!(error_content
			.as_str()
			.unwrap()
			.contains("Missing required 'pattern' parameter"));
	}

	#[tokio::test]
	async fn test_ast_grep_output_grouping() {
		// Test that ast_grep output is properly grouped by file for token efficiency
		// Create temporary Rust files for testing
		let temp_content1 = r#"
fn main() {
    println!("Hello from main");
}

fn helper() {
    println!("Helper function");
}
"#;

		let temp_content2 = r#"
pub fn public_func() {
    println!("Public function");
}

fn private_func() {
    println!("Private function");
}
"#;

		let temp_file1 = "test_ast_grep_group1.rs";
		let temp_file2 = "test_ast_grep_group2.rs";
		std::fs::write(temp_file1, temp_content1).unwrap();
		std::fs::write(temp_file2, temp_content2).unwrap();

		// Test with a pattern that should match multiple functions
		let params = json!({
			"pattern": "fn $NAME($ARGS) { $$$ }",
			"language": "rust",
			"paths": [temp_file1, temp_file2]
		});

		let call = McpToolCall {
			tool_name: "ast_grep".to_string(),
			parameters: params,
			tool_id: "test-call-id".to_string(),
		};

		let result = execute_ast_grep_command(&call).await;

		// Clean up
		let _ = std::fs::remove_file(temp_file1);
		let _ = std::fs::remove_file(temp_file2);

		// The command should execute without shell parsing errors
		assert!(result.is_ok());
		let result = result.unwrap();
		assert_eq!(result.tool_name, "ast_grep");

		let output = result.result.as_object().unwrap();
		assert!(output.contains_key("success"));

		// Check that we don't get shell parsing errors
		let output_text = output["output"].as_str().unwrap_or("");
		assert!(
			!output_text.contains("syntax error near unexpected token"),
			"Shell parsing error detected: {}",
			output_text
		);
		assert!(
			!output_text.contains("sh: -c: line 0:"),
			"Shell command line error detected: {}",
			output_text
		);

		// Print the output to see the grouping format
		println!("ast_grep grouped output:\n{}", output_text);
	}

	#[tokio::test]
	async fn test_ast_grep_basic_functionality() {
		// Test that ast_grep works correctly without tool-level truncation
		// Create a temporary Rust file with many functions
		let temp_content = r#"
fn function1() { println!("1"); }
fn function2() { println!("2"); }
fn function3() { println!("3"); }
fn function4() { println!("4"); }
fn function5() { println!("5"); }
fn function6() { println!("6"); }
fn function7() { println!("7"); }
fn function8() { println!("8"); }
fn function9() { println!("9"); }
fn function10() { println!("10"); }
fn function11() { println!("11"); }
fn function12() { println!("12"); }
fn function13() { println!("13"); }
fn function14() { println!("14"); }
fn function15() { println!("15"); }
"#;

		let temp_file = "test_ast_grep_basic.rs";
		std::fs::write(temp_file, temp_content).unwrap();

		// Test basic ast_grep functionality (no max_lines parameter)
		let params = json!({
			"pattern": "fn $NAME() { $$$ }",
			"language": "rust",
			"paths": [temp_file]
		});

		let call = McpToolCall {
			tool_name: "ast_grep".to_string(),
			parameters: params,
			tool_id: "test-call-id".to_string(),
		};

		let result = execute_ast_grep_command(&call).await;

		// Clean up
		let _ = std::fs::remove_file(temp_file);

		// Should execute successfully
		assert!(result.is_ok());
		let result = result.unwrap();
		let output = result.result.as_object().unwrap();

		// Check basic result structure (no max_lines parameter)
		let params = &output["parameters"];
		assert!(params.get("max_lines").is_none()); // max_lines should not be present
		assert_eq!(params["pattern"], "fn $NAME() { $$$ }");
		assert_eq!(params["language"], "rust");

		// Should have successful execution
		assert_eq!(output["success"], true);
		assert_eq!(output["operation"], "search");

		// Should have output with function matches
		let output_text = output["output"].as_str().unwrap_or("");
		println!("ast_grep basic test output:\n{}", output_text);

		// Test with context parameter
		let params_with_context = json!({
			"pattern": "fn $NAME() { $$$ }",
			"language": "rust",
			"paths": [temp_file],
			"context": 1
		});

		// Recreate the file for the second test
		std::fs::write(temp_file, temp_content).unwrap();

		let call_with_context = McpToolCall {
			tool_name: "ast_grep".to_string(),
			parameters: params_with_context,
			tool_id: "test-call-id".to_string(),
		};

		let result_with_context = execute_ast_grep_command(&call_with_context).await;
		let _ = std::fs::remove_file(temp_file);

		assert!(result_with_context.is_ok());
		let result_with_context = result_with_context.unwrap();
		let output_with_context = result_with_context.result.as_object().unwrap();

		// Should have context parameter
		assert_eq!(output_with_context["parameters"]["context"], 1);
	}

	#[tokio::test]
	async fn test_ast_grep_complex_pattern_with_special_characters() {
		// Test that complex patterns with special characters are properly escaped
		// Create a temporary Rust file for testing
		let temp_content = r#"
async fn process(input: String) -> Result<String, Error> {
    println!("Processing: {}", input);
    Ok(format!("Processed: {}", input))
}

pub async fn handle_request(req: Request) -> Response {
    let result = process(req.body).await;
    Response::new(result)
}
"#;

		let temp_file = "test_ast_grep_complex.rs";
		std::fs::write(temp_file, temp_content).unwrap();

		// Test with a complex pattern that includes special characters
		let params = json!({
			"pattern": "async fn process($ARGS) { $$$ }",
			"language": "rust",
			"paths": [temp_file]
		});

		let call = McpToolCall {
			tool_name: "ast_grep".to_string(),
			parameters: params,
			tool_id: "test-call-id".to_string(),
		};

		let result = execute_ast_grep_command(&call).await;

		// Clean up
		let _ = std::fs::remove_file(temp_file);

		// The command should execute without shell parsing errors
		assert!(result.is_ok());
		let result = result.unwrap();
		assert_eq!(result.tool_name, "ast_grep");

		let output = result.result.as_object().unwrap();
		assert!(output.contains_key("success"));

		// Most importantly, check that we don't get shell parsing errors
		let output_text = output["output"].as_str().unwrap_or("");
		assert!(
			!output_text.contains("syntax error near unexpected token"),
			"Shell parsing error detected: {}",
			output_text
		);
		assert!(
			!output_text.contains("sh: -c: line 0:"),
			"Shell command line error detected: {}",
			output_text
		);

		// The test passes if we don't get shell parsing errors
		// Whether ast-grep finds matches depends on if sg is installed
		println!("ast_grep output: {}", output_text);
	}
}