agent-file-tools 0.56.0

Agent File Tools — tree-sitter powered code analysis 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
use std::collections::BTreeMap;
use std::fmt;
use std::path::Path;

use serde::{Deserialize, Serialize};

/// Shapes recognized by the search ranking engine.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SearchShape {
    Identifier,
    CodeLiteral,
    Short,
    NaturalLanguage,
    LogExcerpt,
    Path,
    Regex,
}

impl SearchShape {
    pub const ALL: [SearchShape; 7] = [
        SearchShape::Identifier,
        SearchShape::CodeLiteral,
        SearchShape::Short,
        SearchShape::NaturalLanguage,
        SearchShape::LogExcerpt,
        SearchShape::Path,
        SearchShape::Regex,
    ];

    pub fn as_str(&self) -> &'static str {
        match self {
            SearchShape::Identifier => "identifier",
            SearchShape::CodeLiteral => "code_literal",
            SearchShape::Short => "short",
            SearchShape::NaturalLanguage => "nl",
            SearchShape::LogExcerpt => "log_excerpt",
            SearchShape::Path => "path",
            SearchShape::Regex => "regex",
        }
    }

    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "identifier" => Some(SearchShape::Identifier),
            "code_literal" => Some(SearchShape::CodeLiteral),
            "short" | "mixed" => Some(SearchShape::Short),
            "nl" | "natural_language" => Some(SearchShape::NaturalLanguage),
            "log_excerpt" | "error_code" => Some(SearchShape::LogExcerpt),
            "path" => Some(SearchShape::Path),
            "regex" => Some(SearchShape::Regex),
            _ => None,
        }
    }
}

impl fmt::Display for SearchShape {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Lanes participating in search ranking.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SearchLaneKind {
    Symbol,
    Exact,
    Anchored,
    Lexical,
    Variants,
    Semantic,
    PathLookup,
    FallbackWalk,
    ReadinessDisclosure,
}

impl SearchLaneKind {
    pub const ALL: [SearchLaneKind; 9] = [
        SearchLaneKind::Symbol,
        SearchLaneKind::Exact,
        SearchLaneKind::Anchored,
        SearchLaneKind::Lexical,
        SearchLaneKind::Variants,
        SearchLaneKind::Semantic,
        SearchLaneKind::PathLookup,
        SearchLaneKind::FallbackWalk,
        SearchLaneKind::ReadinessDisclosure,
    ];

    pub fn as_str(&self) -> &'static str {
        match self {
            SearchLaneKind::Symbol => "symbol",
            SearchLaneKind::Exact => "exact",
            SearchLaneKind::Anchored => "anchored",
            SearchLaneKind::Lexical => "lexical",
            SearchLaneKind::Variants => "variants",
            SearchLaneKind::Semantic => "semantic",
            SearchLaneKind::PathLookup => "path_lookup",
            SearchLaneKind::FallbackWalk => "fallback_walk",
            SearchLaneKind::ReadinessDisclosure => "readiness_disclosure",
        }
    }

    pub fn from_str(s: &str) -> Option<Self> {
        match s {
            "symbol" => Some(SearchLaneKind::Symbol),
            "exact" => Some(SearchLaneKind::Exact),
            "anchored" => Some(SearchLaneKind::Anchored),
            "lexical" => Some(SearchLaneKind::Lexical),
            "variants" => Some(SearchLaneKind::Variants),
            "semantic" => Some(SearchLaneKind::Semantic),
            "path_lookup" => Some(SearchLaneKind::PathLookup),
            "fallback_walk" => Some(SearchLaneKind::FallbackWalk),
            "readiness_disclosure" => Some(SearchLaneKind::ReadinessDisclosure),
            _ => None,
        }
    }

    pub fn default_plan_order_index(&self) -> usize {
        match self {
            SearchLaneKind::Symbol => 0,
            SearchLaneKind::Exact => 1,
            SearchLaneKind::Anchored => 2,
            SearchLaneKind::Lexical => 3,
            SearchLaneKind::Variants => 4,
            SearchLaneKind::Semantic => 5,
            SearchLaneKind::PathLookup => 6,
            SearchLaneKind::FallbackWalk => 7,
            SearchLaneKind::ReadinessDisclosure => 8,
        }
    }

    pub const fn is_scored(self) -> bool {
        matches!(self, Self::Lexical | Self::Semantic)
    }
}

impl fmt::Display for SearchLaneKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// A plan entry for a specific (shape, lane) pair.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LanePlanEntry {
    pub weight: Option<f32>,
    pub rrf_constant: Option<f32>,
    pub plan_order_index: usize,
}

/// Shape-indexed plan table over the complete (shape, lane) cross product.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PlanTable {
    pub entries: BTreeMap<String, BTreeMap<String, LanePlanEntry>>,
}

/// Startup or verification error naming the (shape, lane, field) triple.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlanTableError {
    pub shape: String,
    pub lane: String,
    pub field: String,
    pub message: String,
}

impl fmt::Display for PlanTableError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "plan table error for ({}, {}, {}): {}",
            self.shape, self.lane, self.field, self.message
        )
    }
}

impl std::error::Error for PlanTableError {}

impl PlanTable {
    /// Construct the authoritative running plan table from code constants.
    pub fn running_table() -> Self {
        let mut entries = BTreeMap::new();

        for shape in SearchShape::ALL {
            let shape_str = shape.as_str().to_string();
            let mut lane_map = BTreeMap::new();

            let weights = match shape {
                SearchShape::Identifier | SearchShape::Short => (0.8f32, 0.2f32),
                SearchShape::CodeLiteral | SearchShape::LogExcerpt | SearchShape::Path => {
                    (0.9f32, 0.1f32)
                }
                SearchShape::NaturalLanguage => (0.4f32, 0.6f32),
                SearchShape::Regex => (1.0f32, 0.0f32),
            };

            for lane in SearchLaneKind::ALL {
                let weight = match lane {
                    SearchLaneKind::Lexical => Some(weights.0),
                    SearchLaneKind::Semantic => Some(weights.1),
                    _ => None,
                };
                lane_map.insert(
                    lane.as_str().to_string(),
                    LanePlanEntry {
                        weight,
                        rrf_constant: weight.map(|_| 60.0),
                        plan_order_index: lane.default_plan_order_index(),
                    },
                );
            }

            entries.insert(shape_str, lane_map);
        }

        Self { entries }
    }

    /// Load pinned plan table from a JSON string.
    pub fn from_json(json_str: &str) -> Result<Self, PlanTableError> {
        serde_json::from_str(json_str).map_err(|e| PlanTableError {
            shape: "all".to_string(),
            lane: "all".to_string(),
            field: "json".to_string(),
            message: format!("failed to parse plan-table.json: {e}"),
        })
    }

    /// Load pinned plan table from a file path.
    pub fn from_file(path: &Path) -> Result<Self, PlanTableError> {
        let content = std::fs::read_to_string(path).map_err(|e| PlanTableError {
            shape: "all".to_string(),
            lane: "all".to_string(),
            field: "file".to_string(),
            message: format!("failed to read {}: {e}", path.display()),
        })?;
        Self::from_json(&content)
    }

    /// Verify this plan table against another (e.g. running table against pinned table).
    ///
    /// Asserts:
    /// - Complete (shape, lane) cross product: missing pair or extra pair is an error.
    /// - Every exact lane MUST have weight == None and rrf_constant == None.
    /// - Every weight, rrf_constant, and plan_order_index matches.
    ///
    /// Every error names the (shape, lane, field) triple.
    pub fn verify_against(&self, pinned: &PlanTable) -> Result<(), PlanTableError> {
        // Check for missing shapes or lanes in pinned relative to self (running)
        for shape in SearchShape::ALL {
            let shape_str = shape.as_str();
            let running_lanes = match self.entries.get(shape_str) {
                Some(l) => l,
                None => {
                    return Err(PlanTableError {
                        shape: shape_str.to_string(),
                        lane: "all".to_string(),
                        field: "pair".to_string(),
                        message: "shape missing from running table".to_string(),
                    })
                }
            };

            let pinned_lanes = match pinned.entries.get(shape_str) {
                Some(l) => l,
                None => {
                    return Err(PlanTableError {
                        shape: shape_str.to_string(),
                        lane: "all".to_string(),
                        field: "pair".to_string(),
                        message: "shape missing from pinned table".to_string(),
                    })
                }
            };

            for lane in SearchLaneKind::ALL {
                let lane_str = lane.as_str();
                let running_entry = match running_lanes.get(lane_str) {
                    Some(e) => e,
                    None => {
                        return Err(PlanTableError {
                            shape: shape_str.to_string(),
                            lane: lane_str.to_string(),
                            field: "pair".to_string(),
                            message: "pair missing from running table".to_string(),
                        })
                    }
                };

                let pinned_entry = match pinned_lanes.get(lane_str) {
                    Some(e) => e,
                    None => {
                        return Err(PlanTableError {
                            shape: shape_str.to_string(),
                            lane: lane_str.to_string(),
                            field: "pair".to_string(),
                            message: "pair missing from pinned table".to_string(),
                        })
                    }
                };

                // Exact lane MUST have null weight and null rrf_constant
                if lane == SearchLaneKind::Exact {
                    if pinned_entry.weight.is_some() {
                        return Err(PlanTableError {
                            shape: shape_str.to_string(),
                            lane: lane_str.to_string(),
                            field: "weight".to_string(),
                            message: format!(
                                "exact lane must have null weight, got {:?}",
                                pinned_entry.weight
                            ),
                        });
                    }
                    if pinned_entry.rrf_constant.is_some() {
                        return Err(PlanTableError {
                            shape: shape_str.to_string(),
                            lane: lane_str.to_string(),
                            field: "rrf_constant".to_string(),
                            message: format!(
                                "exact lane must have null rrf_constant, got {:?}",
                                pinned_entry.rrf_constant
                            ),
                        });
                    }
                }

                // Check weight equality
                match (running_entry.weight, pinned_entry.weight) {
                    (Some(w_run), Some(w_pin)) => {
                        if (w_run - w_pin).abs() > 1e-6 {
                            return Err(PlanTableError {
                                shape: shape_str.to_string(),
                                lane: lane_str.to_string(),
                                field: "weight".to_string(),
                                message: format!(
                                    "weight mismatch: running={w_run}, pinned={w_pin}"
                                ),
                            });
                        }
                    }
                    (None, None) => {}
                    (r, p) => {
                        return Err(PlanTableError {
                            shape: shape_str.to_string(),
                            lane: lane_str.to_string(),
                            field: "weight".to_string(),
                            message: format!("weight mismatch: running={r:?}, pinned={p:?}"),
                        });
                    }
                }

                // Check rrf_constant equality
                match (running_entry.rrf_constant, pinned_entry.rrf_constant) {
                    (Some(k_run), Some(k_pin)) => {
                        if (k_run - k_pin).abs() > 1e-6 {
                            return Err(PlanTableError {
                                shape: shape_str.to_string(),
                                lane: lane_str.to_string(),
                                field: "rrf_constant".to_string(),
                                message: format!(
                                    "rrf_constant mismatch: running={k_run}, pinned={k_pin}"
                                ),
                            });
                        }
                    }
                    (None, None) => {}
                    (r, p) => {
                        return Err(PlanTableError {
                            shape: shape_str.to_string(),
                            lane: lane_str.to_string(),
                            field: "rrf_constant".to_string(),
                            message: format!("rrf_constant mismatch: running={r:?}, pinned={p:?}"),
                        });
                    }
                }

                // Check plan_order_index equality
                if running_entry.plan_order_index != pinned_entry.plan_order_index {
                    return Err(PlanTableError {
                        shape: shape_str.to_string(),
                        lane: lane_str.to_string(),
                        field: "plan_order_index".to_string(),
                        message: format!(
                            "plan_order_index mismatch: running={}, pinned={}",
                            running_entry.plan_order_index, pinned_entry.plan_order_index
                        ),
                    });
                }
            }

            // Check for extra lanes in pinned table
            for pinned_lane in pinned_lanes.keys() {
                if !running_lanes.contains_key(pinned_lane) {
                    return Err(PlanTableError {
                        shape: shape_str.to_string(),
                        lane: pinned_lane.clone(),
                        field: "pair".to_string(),
                        message: "extra lane in pinned table".to_string(),
                    });
                }
            }
        }

        // Check for extra shapes in pinned table
        for pinned_shape in pinned.entries.keys() {
            if !self.entries.contains_key(pinned_shape) {
                return Err(PlanTableError {
                    shape: pinned_shape.clone(),
                    lane: "all".to_string(),
                    field: "pair".to_string(),
                    message: "extra shape in pinned table".to_string(),
                });
            }
        }

        Ok(())
    }
}

/// Embedded pinned plan table constant compiled into the binary.
///
/// The pinned table is a product input, so it lives beside this module: a
/// product build must not reach outside `crates/` (the release image copies
/// only the crate tree). The benchmark keeps its own copy under
/// `benchmarks/aft-search/engine-fixtures/plan-table.json`, and
/// `engine_plan_table_test` asserts the two are byte-identical so neither can
/// drift from the other.
pub const PINNED_PLAN_TABLE_JSON: &str =
    include_str!("../../../assets/search-plan-table.pinned.json");

/// Load and verify the pinned plan table at startup against the running table.
pub fn verify_pinned_plan_table_at_startup() -> Result<(), PlanTableError> {
    let running = PlanTable::running_table();
    let pinned = PlanTable::from_json(PINNED_PLAN_TABLE_JSON)?;
    running.verify_against(&pinned)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn running_table_matches_pinned_json() {
        verify_pinned_plan_table_at_startup()
            .expect("pinned plan-table.json must match running table");
    }

    #[test]
    #[ignore = "fixture regeneration is an explicit maintainer action"]
    fn regenerate_pinned_plan_table() {
        let fixture = Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../../benchmarks/aft-search/engine-fixtures/plan-table.json");
        let json = serde_json::to_string_pretty(&PlanTable::running_table())
            .expect("serialize running plan table");
        std::fs::write(fixture, format!("{json}\n")).expect("write pinned plan table");
    }

    #[test]
    fn detects_missing_pair() {
        let running = PlanTable::running_table();
        let mut pinned = PlanTable::running_table();
        pinned
            .entries
            .get_mut("identifier")
            .unwrap()
            .remove("lexical");

        let err = running.verify_against(&pinned).unwrap_err();
        assert_eq!(err.shape, "identifier");
        assert_eq!(err.lane, "lexical");
        assert_eq!(err.field, "pair");
    }

    #[test]
    fn detects_extra_pair() {
        let running = PlanTable::running_table();
        let mut pinned = PlanTable::running_table();
        pinned.entries.get_mut("identifier").unwrap().insert(
            "custom".to_string(),
            LanePlanEntry {
                weight: Some(0.5),
                rrf_constant: Some(60.0),
                plan_order_index: 3,
            },
        );

        let err = running.verify_against(&pinned).unwrap_err();
        assert_eq!(err.shape, "identifier");
        assert_eq!(err.lane, "custom");
        assert_eq!(err.field, "pair");
    }

    #[test]
    fn detects_weight_change() {
        let running = PlanTable::running_table();
        let mut pinned = PlanTable::running_table();
        pinned
            .entries
            .get_mut("identifier")
            .unwrap()
            .get_mut("lexical")
            .unwrap()
            .weight = Some(0.75);

        let err = running.verify_against(&pinned).unwrap_err();
        assert_eq!(err.shape, "identifier");
        assert_eq!(err.lane, "lexical");
        assert_eq!(err.field, "weight");
    }

    #[test]
    fn detects_all_shape_rrf_change() {
        let running = PlanTable::running_table();
        let mut pinned = PlanTable::running_table();
        for (_, lanes) in pinned.entries.iter_mut() {
            for (lane_name, entry) in lanes.iter_mut() {
                if lane_name != "exact" {
                    entry.rrf_constant = Some(50.0);
                }
            }
        }

        let err = running.verify_against(&pinned).unwrap_err();
        assert_eq!(err.field, "rrf_constant");
    }

    #[test]
    fn detects_exact_lane_zero_weight() {
        let running = PlanTable::running_table();
        let mut pinned = PlanTable::running_table();
        pinned
            .entries
            .get_mut("identifier")
            .unwrap()
            .get_mut("exact")
            .unwrap()
            .weight = Some(0.0);

        let err = running.verify_against(&pinned).unwrap_err();
        assert_eq!(err.shape, "identifier");
        assert_eq!(err.lane, "exact");
        assert_eq!(err.field, "weight");
    }
}