octocode 0.24.0

AI-powered code intelligence with semantic search, knowledge graphs, and built-in MCP server. Transform your codebase into a queryable knowledge graph for AI assistants.
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
use crate::indexer::code_region_extractor::extract_meaningful_regions;
use crate::indexer::languages::php::Php;
use crate::indexer::languages::Language;
use tree_sitter::Parser;

#[test]
fn test_php_method_chunking() {
	let php_code = r#"<?php

namespace Test\Example;

use Some\Other\Class;

/**
 * Test class for PHP method chunking
 */
class BasePayload
{
    private $request;
    private $settings;

    /**
     * Create payload from request
     */
    public function fromRequest($request)
    {
        $this->request = $request;
        return $this;
    }

    /**
     * Get handler class name
     */
    public function getHandlerClassName(): string
    {
        return static::class . 'Handler';
    }

    /**
     * Get processor information
     */
    public function getInfo(): array
    {
        return [
            'name' => 'BasePayload',
            'version' => '1.0.0'
        ];
    }

    /**
     * Get available processors
     */
    public function getProcessors(): array
    {
        return [
            'default' => DefaultProcessor::class,
            'advanced' => AdvancedProcessor::class
        ];
    }

    /**
     * Check if request matches
     */
    public function hasMatch(): bool
    {
        return !empty($this->request);
    }

    /**
     * Set settings
     */
    public function setSettings($settings): void
    {
        $this->settings = $settings;
    }
}

/**
 * Standalone function outside class
 */
function standalone_function($param)
{
    return $param * 2;
}
"#;

	let php_lang = Php {};
	let mut parser = Parser::new();
	parser.set_language(&php_lang.get_ts_language()).unwrap();

	let tree = parser.parse(php_code, None).unwrap();
	let mut regions = Vec::new();

	extract_meaningful_regions(tree.root_node(), php_code, &php_lang, &mut regions);

	// Print regions for debugging
	println!("Found {} regions:", regions.len());
	for (i, region) in regions.iter().enumerate() {
		println!(
			"Region {}: {} (lines {}-{})",
			i + 1,
			region.node_kind,
			region.start_line + 1,
			region.end_line + 1
		);
		println!("  Symbols: {:?}", region.symbols);
		println!(
			"  Content preview: {}",
			region
				.content
				.lines()
				.take(3)
				.collect::<Vec<_>>()
				.join("\\n")
		);
		println!();
	}

	// Verify we have individual methods, not entire class
	let method_regions: Vec<_> = regions
		.iter()
		.filter(|r| r.node_kind == "method_declaration")
		.collect();

	let function_regions: Vec<_> = regions
		.iter()
		.filter(|r| r.node_kind == "function_definition")
		.collect();

	let class_regions: Vec<_> = regions
		.iter()
		.filter(|r| r.node_kind == "class_declaration")
		.collect();

	// Assertions
	assert_eq!(
		class_regions.len(),
		0,
		"Should not have any class_declaration regions"
	);
	assert_eq!(
		method_regions.len(),
		6,
		"Should have 6 individual method regions"
	);
	assert_eq!(
		function_regions.len(),
		1,
		"Should have 1 standalone function region"
	);

	// Verify method names are extracted correctly
	let method_names: Vec<String> = method_regions
		.iter()
		.flat_map(|r| &r.symbols)
		.cloned()
		.collect();

	let expected_methods = vec![
		"fromRequest",
		"getHandlerClassName",
		"getInfo",
		"getProcessors",
		"hasMatch",
		"setSettings",
	];

	for expected_method in expected_methods {
		assert!(
			method_names.contains(&expected_method.to_string()),
			"Should contain method: {}",
			expected_method
		);
	}

	// Verify standalone function
	let function_names: Vec<String> = function_regions
		.iter()
		.flat_map(|r| &r.symbols)
		.cloned()
		.collect();

	assert!(
		function_names.contains(&"standalone_function".to_string()),
		"Should contain standalone function"
	);

	// Verify no region is excessively large (no more than ~15 lines per method)
	for region in &regions {
		let line_count = region.end_line - region.start_line + 1;
		assert!(
			line_count <= 20,
			"Region {} should not exceed 20 lines, got {}",
			region.node_kind,
			line_count
		);
	}

	println!("✅ All PHP method chunking tests passed!");
}

#[test]
fn test_php_no_massive_class_chunks() {
	// Test case based on the user's BasePayload.php issue
	let php_code = r#"<?php

/*
 Copyright (c) 2024-present, Manticore Software LTD (https://manticoresearch.com)
*/

use Manticoresearch\Buddy\Core\ManticoreSearch\Settings;
use Manticoresearch\Buddy\Core\Network\Request;
use Manticoresearch\Buddy\Core\Process\BaseProcessor;
use Manticoresearch\Buddy\Core\Tool\SqlQueryParser;

/**
 * @phpstan-template T of array
 */
class BasePayload
{
	protected Request $request;
	protected Settings $manticoreSettings;
	protected ?SqlQueryParser $sqlQueryParser = null;

	public static function fromRequest(Request $request): static
	{
		$self = new static();
		$self->request = $request;
		return $self;
	}

	public function getHandlerClassName(): string
	{
		$ns = substr(static::class, 0, strrpos(static::class, '\\'));
		return $ns . '\\Handler';
	}

	public function getInfo(): array
	{
		return [
			'name' => 'BasePayload',
			'version' => '1.0.0'
		];
	}

	public function getProcessors(): array
	{
		return [
			BaseProcessor::class,
		];
	}

	public function hasMatch(): bool
	{
		return true;
	}

	public function getRequiredVersion(): string
	{
		return '1.0.0';
	}

	public function setSettings(Settings $settings): static
	{
		$this->manticoreSettings = $settings;
		return $this;
	}

	public function getSettings(): Settings
	{
		return $this->manticoreSettings;
	}

	public function setParser(SqlQueryParser $sqlQueryParser): static
	{
		$this->sqlQueryParser = $sqlQueryParser;
		return $this;
	}
}
"#;

	let php_lang = Php {};
	let mut parser = Parser::new();
	parser.set_language(&php_lang.get_ts_language()).unwrap();

	let tree = parser.parse(php_code, None).unwrap();
	let mut regions = Vec::new();

	extract_meaningful_regions(tree.root_node(), php_code, &php_lang, &mut regions);

	// Print regions for debugging
	println!(
		"Found {} regions for BasePayload-like class:",
		regions.len()
	);
	for (i, region) in regions.iter().enumerate() {
		let line_count = region.end_line - region.start_line + 1;
		println!(
			"Region {}: {} (lines {}-{}, {} lines total)",
			i + 1,
			region.node_kind,
			region.start_line + 1,
			region.end_line + 1,
			line_count
		);
		println!("  Symbols: {:?}", region.symbols);
		println!();
	}

	// Critical assertions to prevent regression
	let class_regions: Vec<_> = regions
		.iter()
		.filter(|r| r.node_kind == "class_declaration")
		.collect();

	let method_regions: Vec<_> = regions
		.iter()
		.filter(|r| r.node_kind == "method_declaration")
		.collect();

	// MAIN ASSERTION: No massive class chunks
	assert_eq!(class_regions.len(), 0,
		"❌ REGRESSION: Found {} class_declaration regions! This means entire classes are being chunked again.",
		class_regions.len());

	// Should have individual methods instead
	assert!(
		method_regions.len() >= 7,
		"Should have at least 7 individual method regions, got {}",
		method_regions.len()
	);

	// Verify no region is excessively large (the original issue was 84 lines)
	for region in &regions {
		let line_count = region.end_line - region.start_line + 1;
		assert!(line_count <= 25,
			"❌ REGRESSION: Region '{}' has {} lines (too large)! Original issue was 84-line class chunks.",
			region.node_kind, line_count);
	}

	// Verify we have the expected method names
	let method_names: Vec<String> = method_regions
		.iter()
		.flat_map(|r| &r.symbols)
		.cloned()
		.collect();

	let expected_methods = vec![
		"fromRequest",
		"getHandlerClassName",
		"getInfo",
		"getProcessors",
		"hasMatch",
		"getRequiredVersion",
		"setSettings",
		"getSettings",
		"setParser",
	];

	for expected_method in expected_methods {
		assert!(
			method_names.contains(&expected_method.to_string()),
			"Should contain method: {}",
			expected_method
		);
	}

	println!("✅ PHP class chunking fix verified - no more massive class chunks!");
}

#[test]
fn test_php_meaningful_kinds_excludes_class() {
	let php_lang = Php {};
	let meaningful_kinds = php_lang.get_meaningful_kinds();

	// Critical assertion: class_declaration should NOT be in meaningful kinds
	assert!(!meaningful_kinds.contains(&"class_declaration"),
		"❌ REGRESSION: class_declaration found in meaningful_kinds! This will cause massive class chunks again.");

	// Should still have method and function declarations
	assert!(
		meaningful_kinds.contains(&"method_declaration"),
		"method_declaration should be in meaningful_kinds"
	);
	assert!(
		meaningful_kinds.contains(&"function_definition"),
		"function_definition should be in meaningful_kinds"
	);

	println!(
		"✅ PHP meaningful_kinds configuration verified - class_declaration properly excluded"
	);
}

#[test]
fn test_braced_namespace_splits_into_individual_functions() {
	// Non-trivial content so the smart single-line merge pass doesn't recombine them.
	let php_code = r#"<?php

namespace Foo {
    function f() {
        $x = 1;
        $y = 2;
        $z = $x + $y;
        return $z;
    }
    function g() {
        $a = 10;
        $b = 20;
        $c = $a * $b;
        return $c;
    }
}
"#;

	let php_lang = Php {};
	let mut parser = Parser::new();
	parser.set_language(&php_lang.get_ts_language()).unwrap();

	let tree = parser.parse(php_code, None).unwrap();
	let mut regions = Vec::new();
	extract_meaningful_regions(tree.root_node(), php_code, &php_lang, &mut regions);

	let namespace_regions: Vec<_> = regions
		.iter()
		.filter(|r| r.node_kind == "namespace_definition")
		.collect();
	assert_eq!(
		namespace_regions.len(),
		0,
		"braced namespace with functions inside should not collapse into one region, got {:?}",
		regions.iter().map(|r| &r.node_kind).collect::<Vec<_>>()
	);

	let function_regions: Vec<_> = regions
		.iter()
		.filter(|r| r.node_kind == "function_definition")
		.collect();
	assert_eq!(
		function_regions.len(),
		2,
		"expected a region per function inside braced namespace"
	);
}

#[test]
fn test_unbraced_namespace_stays_single_region() {
	let php_code = r#"<?php

namespace Foo\Bar;

function standalone() {
    return 1;
}
"#;

	let php_lang = Php {};
	let mut parser = Parser::new();
	parser.set_language(&php_lang.get_ts_language()).unwrap();

	let tree = parser.parse(php_code, None).unwrap();
	let mut regions = Vec::new();
	extract_meaningful_regions(tree.root_node(), php_code, &php_lang, &mut regions);

	let namespace_regions: Vec<_> = regions
		.iter()
		.filter(|r| r.node_kind == "namespace_definition")
		.collect();
	assert_eq!(
		namespace_regions.len(),
		1,
		"unbraced namespace declaration should remain its own single region unchanged, got {:?}",
		regions.iter().map(|r| &r.node_kind).collect::<Vec<_>>()
	);
}