rustcodegraph 1.2.10

Local-first code intelligence graph and CLI for AI agents
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
//! Regression tests for adaptive `rustcodegraph_explore` sizing: sibling
//! skeletonization.
//!
//! This is the Rust port of `__tests__/adaptive-explore-sizing.test.ts`.
//! The cases exercise the Rust `CodeGraph` facade and MCP tool handler directly
//! so adaptive sibling skeletonization stays active in the Rust path.

use std::env;
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Mutex, MutexGuard};
use std::time::{SystemTime, UNIX_EPOCH};

use rustcodegraph::mcp::tools::{ToolHandler, ToolResult};
use rustcodegraph::types::{EdgeKind, NodeKind};
use rustcodegraph::{CodeGraph, IndexOptions};
use serde_json::{Map, Value, json};

const SKELETON_MARK: &str = "ยท skeleton (signatures only";

const QUERY: &str = "dispatch proceed handleLogging LoggingInterceptor BridgeInterceptor CacheInterceptor RetryInterceptor ResponseFormatter";

const SPARE_QUERY: &str = "dispatch proceed handleLogging LoggingInterceptor BridgeInterceptor CacheInterceptor RetryInterceptor ResponseFormatter authenticate encode AuthInterceptor Codec JsonCodec";

const ADAPTIVE_EXPLORE_STATUS: &str = "Rust CodeGraph adaptive explore sizing cases are active";
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
static ADAPTIVE_ENV_LOCK: Mutex<()> = Mutex::new(());

fn section_for(text: &str, basename: &str) -> String {
    let lines = text.lines().collect::<Vec<_>>();
    let Some(start) = lines
        .iter()
        .position(|line| line.starts_with("#### ") && line.contains(basename))
    else {
        return String::new();
    };

    let end = lines
        .iter()
        .enumerate()
        .skip(start + 1)
        .find_map(|(i, line)| (line.starts_with("### ") || line.starts_with("#### ")).then_some(i))
        .unwrap_or(lines.len());

    lines[start..end].join("\n")
}

struct TempProject {
    root: PathBuf,
}

impl TempProject {
    fn new(prefix: &str) -> Self {
        for attempt in 0..100 {
            let nanos = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("system time should be after the Unix epoch")
                .as_nanos();
            let counter = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
            let root = env::temp_dir().join(format!(
                "{prefix}-{}-{nanos}-{counter}-{attempt}",
                std::process::id()
            ));
            match fs::create_dir(&root) {
                Ok(()) => {
                    fs::create_dir(root.join("src")).expect("failed to create fixture src dir");
                    return Self { root };
                }
                Err(err) if err.kind() == ErrorKind::AlreadyExists => continue,
                Err(err) => panic!("failed to create temp dir {}: {err}", root.display()),
            }
        }
        panic!("failed to allocate a unique adaptive explore temp directory")
    }

    fn path(&self) -> &Path {
        &self.root
    }

    fn write_src(&self, name: &str, body: &str) {
        fs::write(self.root.join("src").join(name), body.trim_start())
            .expect("failed to write fixture source file");
    }
}

impl Drop for TempProject {
    fn drop(&mut self) {
        if self.root.exists() {
            let _ = fs::remove_dir_all(&self.root);
        }
    }
}

struct Fixture {
    _temp: TempProject,
    cg: CodeGraph,
    handler: ToolHandler,
}

impl Fixture {
    fn new() -> Self {
        let temp = TempProject::new("rustcodegraph-adaptive-explore");

        // The interchangeable contract: 4 implementers below => sibling family.
        temp.write_src(
            "interceptor.ts",
            r#"
export interface Interceptor {
  intercept(request: string): string;
}
"#,
        );

        // The mechanism + the spine: dispatch -> proceed -> handleLogging.
        temp.write_src(
            "dispatcher.ts",
            r#"
import { LoggingInterceptor } from './logging-interceptor';

export class RequestDispatcher {
  dispatch(): string {
    const chain = new InterceptorChain();
    return chain.proceed();
  }
}

export class InterceptorChain {
  proceed(): string {
    const exemplar = new LoggingInterceptor();
    return exemplar.handleLogging();
  }
}
"#,
        );

        // On-spine exemplar: full source must be preserved.
        temp.write_src(
            "logging-interceptor.ts",
            r#"
import { Interceptor } from './interceptor';

export class LoggingInterceptor implements Interceptor {
  handleLogging(): string {
    const tag = 'LOGGING_BODY_MARKER';
    return this.intercept(tag);
  }
  intercept(request: string): string {
    return 'logged:' + request;
  }
}
"#,
        );

        // Off-spine siblings: signatures survive, bodies are elided.
        temp.write_src(
            "bridge-interceptor.ts",
            r#"
import { Interceptor } from './interceptor';

export class BridgeInterceptor implements Interceptor {
  intercept(request: string): string {
    const detail = 'BRIDGE_BODY_MARKER';
    return 'bridged:' + request + detail;
  }
}
"#,
        );
        temp.write_src(
            "cache-interceptor.ts",
            r#"
import { Interceptor } from './interceptor';

export class CacheInterceptor implements Interceptor {
  intercept(request: string): string {
    const detail = 'CACHE_BODY_MARKER';
    return 'cached:' + request + detail;
  }
}
"#,
        );
        temp.write_src(
            "retry-interceptor.ts",
            r#"
import { Interceptor } from './interceptor';

export class RetryInterceptor implements Interceptor {
  intercept(request: string): string {
    const detail = 'RETRY_BODY_MARKER';
    return 'retried:' + request + detail;
  }
}
"#,
        );

        // A 1:1 interface->impl pair: off-spine, but not a sibling family.
        temp.write_src(
            "formatter.ts",
            r#"
export interface Formatter {
  format(input: string): string;
}
"#,
        );
        temp.write_src(
            "response-formatter.ts",
            r#"
import { Formatter } from './formatter';
import { JsonCodec } from './codec';

export class ResponseFormatter implements Formatter {
  format(input: string): string {
    const detail = 'FORMATTER_BODY_MARKER';
    return new JsonCodec().encode(input) + detail;
  }
}
"#,
        );

        // Off-spine sibling with a uniquely named callable.
        temp.write_src(
            "auth-interceptor.ts",
            r#"
import { Interceptor } from './interceptor';

export class AuthInterceptor implements Interceptor {
  authenticate(token: string): string {
    const detail = 'AUTH_BODY_MARKER';
    return 'auth:' + token + detail;
  }
  intercept(request: string): string {
    return this.authenticate(request);
  }
}
"#,
        );

        // Base + subclasses in one file: focused view keeps the named base body.
        temp.write_src(
            "codec.ts",
            r#"
export class Codec {
  encode(input: string): string {
    const detail = 'CODEC_BASE_MARKER';
    return input + detail;
  }
}
export class JsonCodec extends Codec {
  encode(input: string): string { return '{' + input + '}'; }
}
export class XmlCodec extends Codec {
  encode(input: string): string {
    const detail = 'XML_BODY_MARKER';
    return '<' + input + detail + '>';
  }
}
export class YamlCodec extends Codec {
  encode(input: string): string { return '- ' + input; }
}
"#,
        );

        let mut cg = CodeGraph::init_sync(temp.path()).expect("failed to initialize CodeGraph");
        let _ = cg.index_all(IndexOptions::default());
        let mut handler = ToolHandler::new(true);
        handler.set_default_code_graph(&cg);

        Self {
            _temp: temp,
            cg,
            handler,
        }
    }

    fn explore(&mut self, query: &str, max_files: usize) -> String {
        let result = self
            .handler
            .execute("rustcodegraph_explore", &args(query, max_files));
        first_text(&result).to_string()
    }
}

impl Drop for Fixture {
    fn drop(&mut self) {
        self.handler.close_all();
        self.cg.destroy();
    }
}

fn args(query: &str, max_files: usize) -> Map<String, Value> {
    let mut args = Map::new();
    args.insert("query".to_string(), json!(query));
    args.insert("maxFiles".to_string(), json!(max_files));
    args
}

fn first_text(result: &ToolResult) -> &str {
    result
        .content
        .first()
        .map(|content| content.text.as_str())
        .unwrap_or("")
}

fn reset_adaptive_env() {
    // Rust 2024 treats process-wide environment mutation as unsafe because
    // other threads may read it concurrently.
    unsafe {
        env::remove_var("RUSTCODEGRAPH_ADAPTIVE_EXPLORE");
    }
}

fn set_adaptive_env_disabled() {
    unsafe {
        env::set_var("RUSTCODEGRAPH_ADAPTIVE_EXPLORE", "0");
    }
}

fn lock_adaptive_env() -> MutexGuard<'static, ()> {
    match ADAPTIVE_ENV_LOCK.lock() {
        Ok(guard) => guard,
        Err(poisoned) => poisoned.into_inner(),
    }
}

#[test]
fn section_for_returns_file_section_until_next_header() {
    let text = "\
### Files
#### src/a.ts ยท full
line a
#### src/b.ts ยท skeleton (signatures only)
line b
### Relationships
edge";

    assert_eq!(section_for(text, "a.ts"), "#### src/a.ts ยท full\nline a");
    assert_eq!(
        section_for(text, "b.ts"),
        "#### src/b.ts ยท skeleton (signatures only)\nline b"
    );
    assert_eq!(section_for(text, "missing.ts"), "");
}

mod adaptive_codegraph_explore_sizing_sibling_skeletonization {
    use super::*;

    #[test]
    fn fixture_sanity_interceptor_has_at_least_3_implementers_formatter_has_less_than_3() {
        let _guard = lock_adaptive_env();
        reset_adaptive_env();
        let mut fixture = Fixture::new();

        let find = |cg: &mut CodeGraph, name: &str, kind: NodeKind| {
            cg.search_nodes(name, None)
                .into_iter()
                .map(|result| result.node)
                .find(|node| node.name == name && node.kind == kind)
        };

        let interceptor = find(&mut fixture.cg, "Interceptor", NodeKind::Interface)
            .expect("Interceptor interface should be indexed");
        let formatter = find(&mut fixture.cg, "Formatter", NodeKind::Interface)
            .expect("Formatter interface should be indexed");

        let mut implementers = |id: &str| {
            fixture
                .cg
                .get_incoming_edges(id)
                .into_iter()
                .filter(|edge| matches!(edge.kind, EdgeKind::Implements | EdgeKind::Extends))
                .count()
        };

        assert!(
            implementers(&interceptor.id) >= 3,
            "Interceptor should produce the >=3 sibling-family signal"
        );
        assert!(
            implementers(&formatter.id) < 3,
            "Formatter should remain below the sibling-family threshold"
        );
    }

    #[test]
    fn skeletonizes_off_spine_polymorphic_siblings_bodies_elided_signatures_kept() {
        let _guard = lock_adaptive_env();
        reset_adaptive_env();
        let mut fixture = Fixture::new();
        let text = fixture.explore(QUERY, 12);

        assert!(
            text.contains("## Flow (call path among the symbols you queried)"),
            "the spine must form before sibling skeletonization can apply"
        );

        for (file, marker) in [
            ("bridge-interceptor.ts", "BRIDGE_BODY_MARKER"),
            ("cache-interceptor.ts", "CACHE_BODY_MARKER"),
            ("retry-interceptor.ts", "RETRY_BODY_MARKER"),
        ] {
            let section = section_for(&text, file);
            assert!(
                !section.is_empty(),
                "{file} should be present in explore output"
            );
            assert!(
                section.contains(SKELETON_MARK),
                "{file} should be skeletonized"
            );
            assert!(
                section.contains("intercept(request"),
                "{file} should keep the signature"
            );
            assert!(
                !section.contains(marker),
                "{file} body marker must not survive skeletonization"
            );
        }
    }

    #[test]
    fn keeps_the_on_spine_exemplar_full_even_though_it_is_a_sibling() {
        let _guard = lock_adaptive_env();
        reset_adaptive_env();
        let mut fixture = Fixture::new();
        let text = fixture.explore(QUERY, 12);

        let section = section_for(&text, "logging-interceptor.ts");
        assert!(
            !section.is_empty(),
            "logging-interceptor.ts should be present"
        );
        assert!(
            !section.contains(SKELETON_MARK),
            "on-spine exemplar must not be skeletonized"
        );
        assert!(section.contains("LOGGING_BODY_MARKER"));
    }

    #[test]
    fn keeps_a_distinct_step_full_off_spine_but_supertype_has_less_than_3_implementers() {
        let _guard = lock_adaptive_env();
        reset_adaptive_env();
        let mut fixture = Fixture::new();
        let text = fixture.explore(QUERY, 12);

        let section = section_for(&text, "response-formatter.ts");
        assert!(
            !section.is_empty(),
            "response-formatter.ts should be present"
        );
        assert!(
            !section.contains(SKELETON_MARK),
            "a 1:1 interface impl is not a sibling and must stay full"
        );
        assert!(section.contains("FORMATTER_BODY_MARKER"));
    }

    #[test]
    fn rustcodegraph_adaptive_explore_0_disables_skeletonization_siblings_render_full() {
        let _guard = lock_adaptive_env();
        reset_adaptive_env();
        set_adaptive_env_disabled();

        let result = std::panic::catch_unwind(|| {
            let mut fixture = Fixture::new();
            let text = fixture.explore(QUERY, 12);

            assert!(
                !text.contains(SKELETON_MARK),
                "no file should be skeletonized with the flag off"
            );
            let section = section_for(&text, "bridge-interceptor.ts");
            assert!(!section.is_empty());
            assert!(section.contains("BRIDGE_BODY_MARKER"));
        });

        reset_adaptive_env();
        if let Err(payload) = result {
            std::panic::resume_unwind(payload);
        }
    }

    #[test]
    fn spares_an_off_spine_sibling_when_the_agent_named_a_callable_in_it_realcall_fix() {
        let _guard = lock_adaptive_env();
        reset_adaptive_env();
        let mut fixture = Fixture::new();
        let text = fixture.explore(SPARE_QUERY, 15);

        assert!(text.contains("## Flow (call path among the symbols you queried)"));

        let auth = section_for(&text, "auth-interceptor.ts");
        assert!(!auth.is_empty(), "auth-interceptor.ts should be present");
        assert!(
            !auth.contains(SKELETON_MARK),
            "a file holding an agent-named callable must not be skeletonized"
        );
        assert!(auth.contains("AUTH_BODY_MARKER"));

        let bridge = section_for(&text, "bridge-interceptor.ts");
        assert!(
            bridge.contains(SKELETON_MARK),
            "a sibling named only by type still skeletonizes"
        );
        assert!(!bridge.contains("BRIDGE_BODY_MARKER"));
    }

    #[test]
    fn collapses_a_base_and_subclasses_family_file_to_a_focused_view_compiler_py() {
        let _guard = lock_adaptive_env();
        reset_adaptive_env();
        let mut fixture = Fixture::new();
        let text = fixture.explore(SPARE_QUERY, 15);

        let codec = section_for(&text, "codec.ts");
        assert!(!codec.is_empty(), "codec.ts should be present");
        assert!(
            codec.contains("ยท focused"),
            "a named family file collapses to a focused, not full, view"
        );
        assert!(
            codec.contains("CODEC_BASE_MARKER"),
            "the named base method body is kept"
        );
        assert!(
            !codec.contains("XML_BODY_MARKER"),
            "a non-named subclass body is elided to a signature"
        );
    }

    #[test]
    fn naming_a_shared_polymorphic_method_does_not_spare_the_siblings_uniqueness_aware() {
        let _guard = lock_adaptive_env();
        reset_adaptive_env();
        let mut fixture = Fixture::new();
        let text = fixture.explore(&format!("{QUERY} intercept"), 12);

        let bridge = section_for(&text, "bridge-interceptor.ts");
        assert!(
            bridge.contains(SKELETON_MARK),
            "a sibling named only via a shared method is not spared"
        );
        assert!(
            !bridge.contains("BRIDGE_BODY_MARKER"),
            "a shared method does not earn a body in a non-supertype leaf"
        );
    }
}

#[test]
fn adaptive_explore_cases_are_active_for_this_port() {
    assert_eq!(
        ADAPTIVE_EXPLORE_STATUS,
        "Rust CodeGraph adaptive explore sizing cases are active"
    );
}