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
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::Path;
use std::time::Duration;

use rusqlite::{params, Connection, OptionalExtension};

use crate::callgraph_store::{
    initialize_schema, join, set_meta_ready, CallGraphStoreError, Result, PROVENANCE_TREESITTER,
};

/// Cold materialization, also used to upgrade databases without diff metadata.
pub fn materialize_manifest_view_database(
    database_path: &Path,
    callgraph_blob_database: &Path,
    manifest: &crate::views::Manifest,
) -> Result<()> {
    materialize(database_path, callgraph_blob_database, manifest, None).map(|_| ())
}

/// Counts affected graph and dependency-cache rows, excluding readiness and metadata.
/// Replacements count both the deletion and insertion; relinks include refs and edges.
/// Resolution counters describe work performed, not SQLite writes.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct MaterializeStats {
    pub deleted: usize,
    pub inserted: usize,
    pub relinked_deleted: usize,
    pub relinked_inserted: usize,
    pub dependency_deleted: usize,
    pub dependency_inserted: usize,
    pub dependent_files: usize,
    pub resolved_files: usize,
    pub resolved_refs: usize,
    pub resolved_bindings: usize,
    pub rebuilt_surface_entries: usize,
    pub decoded_caller_blobs: usize,
    pub full_resolution: bool,
    pub unattributed_callers: usize,
}

impl MaterializeStats {
    pub fn graph_rows_written(&self) -> usize {
        self.deleted + self.inserted + self.relinked_deleted + self.relinked_inserted
    }

    pub fn rows_written(&self) -> usize {
        self.graph_rows_written() + self.dependency_deleted + self.dependency_inserted
    }
}

/// Apply to a private copy of the base generation's database, never a published file.
/// The caller owns copying, durability and pointer publication. All graph changes and
/// the fingerprint commit atomically; errors roll back to the supplied base. An old
/// binding-cache schema takes the cold path; a mismatched manifest is refused.
pub fn apply_manifest_diff(
    database_path: &Path,
    base_manifest: &crate::views::Manifest,
    new_manifest: &crate::views::Manifest,
    callgraph_blob_database: &Path,
) -> Result<MaterializeStats> {
    apply_manifest_diff_profiled(
        database_path,
        base_manifest,
        new_manifest,
        callgraph_blob_database,
    )
    .map(|(stats, _)| stats)
}

pub(crate) fn apply_manifest_diff_profiled(
    database_path: &Path,
    base_manifest: &crate::views::Manifest,
    new_manifest: &crate::views::Manifest,
    callgraph_blob_database: &Path,
) -> Result<(MaterializeStats, profile::PhaseTimings)> {
    materialize(
        database_path,
        callgraph_blob_database,
        new_manifest,
        Some(base_manifest),
    )
}

pub(crate) mod profile;
mod resolution_facts;

const MATERIALIZATION_VERSION: &str = "5";

fn fingerprint(manifest: &crate::views::Manifest) -> Result<String> {
    let bytes = manifest
        .to_json_bytes()
        .map_err(|error| CallGraphStoreError::Unavailable(error.to_string()))?;
    Ok(blake3::hash(&bytes).to_hex().to_string())
}

fn manifest_callgraph_equivalent(
    left: &crate::views::Manifest,
    right: &crate::views::Manifest,
) -> bool {
    let mut left = left.entries();
    let mut right = right.entries();
    loop {
        match (left.next(), right.next()) {
            (None, None) => return true,
            (Some((left_path, left_entry)), Some((right_path, right_entry)))
                if left_path == right_path
                    && match (left_entry, right_entry) {
                        (
                            crate::views::ManifestEntry::Regular {
                                mode: left_mode,
                                planes: left_planes,
                                resolution_input: left_resolution_input,
                            },
                            crate::views::ManifestEntry::Regular {
                                mode: right_mode,
                                planes: right_planes,
                                resolution_input: right_resolution_input,
                            },
                        ) => {
                            left_mode == right_mode
                                && left_resolution_input == right_resolution_input
                                && left_planes.callgraph == right_planes.callgraph
                        }
                        _ => left_entry == right_entry,
                    } => {}
            _ => return false,
        }
    }
}

fn materialize(
    database_path: &Path,
    callgraph_blob_database: &Path,
    manifest: &crate::views::Manifest,
    mut base: Option<&crate::views::Manifest>,
) -> Result<(MaterializeStats, profile::PhaseTimings)> {
    let mut profile = profile::PhaseTimer::new(if base.is_some() {
        "incremental"
    } else {
        "cold"
    });
    let mut connection = if base.is_some() {
        Connection::open_with_flags(database_path, rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE)?
    } else {
        Connection::open(database_path)?
    };
    configure_materialization_connection(&connection)?;
    if base.is_none() {
        initialize_schema(&connection)?;
    }
    let transaction =
        connection.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
    if let Some(base_manifest) = base {
        let recorded: Option<String> = transaction
            .query_row(
                "SELECT v FROM meta WHERE k = 'view_manifest_fingerprint'",
                [],
                |row| row.get(0),
            )
            .optional()?;
        let version: Option<String> = transaction
            .query_row(
                "SELECT v FROM meta WHERE k = 'view_materialization_version'",
                [],
                |row| row.get(0),
            )
            .optional()?;
        if recorded.is_none() && version.is_none() {
            // Cloned generations written before diff metadata existed must be
            // rebuilt; their graph cannot safely seed dependency invalidation.
            base = None;
        } else if recorded.as_deref() != Some(fingerprint(base_manifest)?.as_str()) {
            return Err(CallGraphStoreError::Unavailable(
                "derived manifest fingerprint mismatch; cold materialization required".into(),
            ));
        } else if version.as_deref() != Some(MATERIALIZATION_VERSION) {
            base = None;
        } else if base_manifest == manifest {
            profile.finish("load_bindings_select");
            return Ok((MaterializeStats::default(), profile.into_timings()));
        } else if manifest_callgraph_equivalent(base_manifest, manifest) {
            profile.finish("load_bindings_select");
            transaction.execute(
                "INSERT OR REPLACE INTO meta(k, v) VALUES('view_manifest_fingerprint', ?1)",
                [fingerprint(manifest)?],
            )?;
            transaction.commit()?;
            profile.finish("commit");
            return Ok((MaterializeStats::default(), profile.into_timings()));
        }
    }
    transaction.execute_batch("CREATE TABLE IF NOT EXISTS view_bindings (file_path TEXT PRIMARY KEY, payload TEXT NOT NULL)")?;
    let changed = base.map(|base| {
        base.entries()
            .chain(manifest.entries())
            .filter(|(path, _)| base.get(path) != manifest.get(path))
            .map(|(path, _)| path.as_bytes().to_vec())
            .collect::<BTreeSet<_>>()
    });
    let cached = if base.is_some() {
        load_bindings(&transaction)?
    } else {
        BTreeMap::new()
    };
    let blob_connection = Connection::open(callgraph_blob_database)?;
    let mut fact_invalidated = BTreeSet::new();
    let mut fallback_count = 0;
    let selected = match (&changed, base) {
        (Some(changed), Some(base)) if !requires_full_resolution(base, manifest, changed) => {
            let diff = resolution_facts::diff_inputs(base, manifest, changed, &blob_connection)?;
            for (caller, binding) in &cached {
                if !binding.consulted_facts.is_disjoint(&diff.changed) {
                    fact_invalidated.insert(caller.clone());
                }
                if diff.inputs_changed && binding.unattributed {
                    fallback_count += 1;
                }
            }
            if diff.unknown != 0 || fallback_count != 0 {
                fallback_count += diff.unknown;
                log::warn!("views materialization: full resolution (reason=unattributed_reads count={fallback_count})");
                None
            } else {
                // Configuration bytes are invalidated by their consulted fields.
                // Presence changes still seed ordinary missing-path dependencies.
                let mut seeds = changed
                    .iter()
                    .filter(|path| {
                        !join::view_resolution_config(path) || {
                            let rel =
                                crate::views::RelPath::new((*path).clone()).expect("manifest path");
                            base.get(&rel).is_some() != manifest.get(&rel).is_some()
                        }
                    })
                    .cloned()
                    .collect::<BTreeSet<_>>();
                seeds.extend(fact_invalidated.iter().map(|path| path.as_bytes().to_vec()));
                if changed.iter().any(|path| {
                    join::view_resolution_config(path) && {
                        let rel = crate::views::RelPath::new(path.clone()).expect("manifest path");
                        base.get(&rel).is_some() != manifest.get(&rel).is_some()
                    }
                }) {
                    seeds.insert(join::VIEW_CONFIG_MEMBERSHIP_DOMAIN.as_bytes().to_vec());
                }
                Some(dependent_closure(&transaction, &seeds)?)
            }
        }
        _ => None,
    };
    let mut stats = MaterializeStats {
        full_resolution: selected.is_none(),
        unattributed_callers: fallback_count,
        ..MaterializeStats::default()
    };
    profile.finish("load_bindings_select");
    if let Some(changed) = &changed {
        for path in changed {
            let Ok(path) = std::str::from_utf8(path) else {
                // Non-UTF-8 entries cannot have rows in the cold materialization.
                continue;
            };
            // Edges are owned through their ref_id, not their target. Delete them
            // before the refs so that cross-file incoming edges remain available.
            stats.deleted += transaction.execute("DELETE FROM edges WHERE ref_id IN (SELECT ref_id FROM refs WHERE caller_file = ?1)", [path])?;
            stats.deleted +=
                transaction.execute("DELETE FROM refs WHERE caller_file = ?1", [path])?;
            stats.deleted +=
                transaction.execute("DELETE FROM nodes WHERE file_path = ?1", [path])?;
            stats.deleted += transaction.execute("DELETE FROM files WHERE path = ?1", [path])?;
            stats.dependency_deleted += transaction
                .execute("DELETE FROM file_dependencies WHERE file_path = ?1", [path])?;
            stats.dependency_deleted +=
                transaction.execute("DELETE FROM view_bindings WHERE file_path = ?1", [path])?;
        }
    } else {
        for table in ["edges", "refs", "nodes", "files"] {
            stats.deleted += transaction.execute(&format!("DELETE FROM {table}"), [])?;
        }
    }
    if changed.is_none() {
        for table in ["file_dependencies", "view_bindings"] {
            stats.dependency_deleted += transaction.execute(&format!("DELETE FROM {table}"), [])?;
        }
    }
    profile.finish("delete_rows");
    let mut parsed = BTreeMap::new();
    let mut nodes = HashMap::new();
    let mut loaded_paths = BTreeSet::new();
    for (path, entry) in manifest.entries() {
        if changed
            .as_ref()
            .is_some_and(|paths| !paths.contains(path.as_bytes()))
        {
            continue;
        }
        let crate::views::ManifestEntry::Regular {
            planes,
            resolution_input,
            ..
        } = entry
        else {
            continue;
        };
        let Some(key) = planes.callgraph.as_deref() else {
            continue;
        };
        let key_bytes = decode_manifest_full_key(key).ok_or_else(|| {
            CallGraphStoreError::Unavailable(format!("invalid manifest callgraph key {key}"))
        })?;
        let payload = blob_connection
            .query_row(
                "SELECT payload FROM blob_payloads WHERE full_key = ?1",
                [key_bytes],
                |row| row.get::<_, Vec<u8>>(0),
            )
            .optional()?
            .ok_or_else(|| {
                CallGraphStoreError::Unavailable(format!("missing manifest callgraph blob {key}"))
            })?;
        let blob = join::CallgraphBlob::from_bytes(&payload)
            .map_err(|error| CallGraphStoreError::Unavailable(error.to_string()))?;
        let Some(parse) = blob.parse() else {
            continue;
        };
        let path = String::from_utf8(path.as_bytes().to_vec()).map_err(|_| {
            CallGraphStoreError::Unavailable("non-UTF-8 manifest callgraph path".to_string())
        })?;
        loaded_paths.insert(path.clone());
        let write_owned = changed
            .as_ref()
            .is_none_or(|paths| paths.contains(path.as_bytes()));
        if write_owned {
            stats.inserted += transaction.execute(
                "INSERT OR REPLACE INTO files
             (path, content_hash, mtime_ns, size, lang, is_dead_code_root, is_public_api,
              surface_fingerprint, indexed_at)
             VALUES (?1, ?2, 0, 0, ?3, 0, 0, '', 0)",
                params![path, key, parse.language],
            )?;
        }
        for symbol in &parse.symbols {
            let id = format!("view:{path}:{}:{}", symbol.scoped_name, symbol.ordinal);
            if write_owned {
                stats.inserted += transaction.execute(
                    "INSERT OR REPLACE INTO nodes
                 (id, file_path, name, scoped_name, kind, start_line, start_col, end_line,
                  end_col, range_ordinal, signature, exported, is_default_export,
                  is_type_like, is_callgraph_entry_point, provenance)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, 0, ?12, ?14)",
                    params![
                        id,
                        path,
                        symbol.name,
                        symbol.scoped_name,
                        symbol.kind,
                        i64::from(symbol.start_line),
                        i64::from(symbol.start_col),
                        i64::from(symbol.end_line),
                        i64::from(symbol.end_col),
                        i64::from(symbol.ordinal),
                        symbol.signature,
                        i64::from(symbol.exported),
                        i64::from(symbol.is_default_export),
                        PROVENANCE_TREESITTER,
                    ],
                )?;
            }
            nodes.insert((path.clone(), symbol.scoped_name.clone()), id.clone());
            nodes
                .entry((path.clone(), symbol.name.clone()))
                .or_insert(id);
        }
        if !resolution_input {
            parsed.insert(
                path,
                parse
                    .refs
                    .iter()
                    .fold(BTreeMap::new(), |mut by_ordinal, reference| {
                        // Match the cold writer's original first-reference lookup
                        // when structural references share an AST ordinal.
                        by_ordinal
                            .entry(reference.ordinal)
                            .or_insert_with(|| reference.clone());
                        by_ordinal
                    }),
            );
        }
    }

    profile.finish("owned_blob_decode_and_insert");
    let reader = ManifestViewBlobReader {
        connection: &blob_connection,
    };
    let changed_strings = changed.as_ref().map_or_else(BTreeSet::new, |paths| {
        paths
            .iter()
            .filter_map(|path| String::from_utf8(path.clone()).ok())
            .collect()
    });
    let mut membership_changed = base.map_or_else(BTreeSet::new, |base| {
        base.entries()
            .chain(manifest.entries())
            .filter(|(path, _)| {
                base.get(path).map(crate::views::ManifestEntry::kind)
                    != manifest.get(path).map(crate::views::ManifestEntry::kind)
            })
            .filter_map(|(path, _)| String::from_utf8(path.as_bytes().to_vec()).ok())
            .collect()
    });
    if membership_changed
        .iter()
        .any(|path| join::view_resolution_config(path.as_bytes()))
    {
        membership_changed.insert(join::VIEW_CONFIG_MEMBERSHIP_DOMAIN.into());
    }
    let joined = join::join_selected_manifest_reusing_surfaces(
        manifest,
        &reader,
        selected.as_ref(),
        &cached,
        &changed_strings,
        &membership_changed,
        &fact_invalidated,
    )
    .map_err(|error| CallGraphStoreError::Unavailable(error.to_string()))?;
    profile.finish("selected_join");
    stats.unattributed_callers = joined
        .bindings
        .values()
        .filter(|binding| binding.unattributed)
        .count()
        .max(stats.unattributed_callers);
    stats.rebuilt_surface_entries = joined.rebuilt_surface_entries;
    stats.decoded_caller_blobs = joined.decoded_caller_blobs;
    stats.resolved_refs = joined.result.rows.len();
    stats.resolved_bindings = joined.resolved_bindings;
    stats.resolved_files = joined.resolved_callers.len();
    stats.dependent_files = joined
        .resolved_callers
        .iter()
        .filter(|path| !changed_strings.contains(*path) && changed.is_some())
        .count();
    for (path, binding) in &joined.bindings {
        let old = cached.get(path).filter(|_| {
            changed
                .as_ref()
                .is_some_and(|paths| !paths.contains(path.as_bytes()))
        });
        if old == Some(binding) {
            continue;
        }
        let empty = BTreeSet::new();
        let previous = old.map_or(&empty, |old| &old.dependencies);
        for dependency in previous.difference(&binding.dependencies) {
            stats.dependency_deleted += transaction.execute(
                "DELETE FROM file_dependencies WHERE file_path=?1 AND dep_file=?2",
                params![path, dependency],
            )?;
        }
        for dependency in binding.dependencies.difference(previous) {
            stats.dependency_inserted += transaction.execute(
                "INSERT INTO file_dependencies(file_path, dep_file) VALUES(?1, ?2)",
                params![path, dependency],
            )?;
        }
        stats.dependency_deleted +=
            transaction.execute("DELETE FROM view_bindings WHERE file_path=?1", [path])?;
        let payload = serde_json::to_string(binding)
            .map_err(|error| CallGraphStoreError::Unavailable(error.to_string()))?;
        stats.dependency_inserted += transaction.execute(
            "INSERT INTO view_bindings(file_path, payload) VALUES(?1, ?2)",
            params![path, payload],
        )?;
    }
    profile.finish("write_bindings");
    // Retain prepared statements across the fan-out. Preparing each statement
    // again costs more than binding many of these small reference rows.
    {
        let mut same_ref = transaction.prepare("SELECT EXISTS(SELECT 1 FROM refs WHERE ref_id = ?1 AND caller_node IS ?2
                 AND status = ?3 AND target_node IS ?4 AND target_file IS ?5 AND target_symbol IS ?6)")?;
        let mut delete_edge = transaction.prepare("DELETE FROM edges WHERE ref_id = ?1")?;
        let mut delete_ref = transaction.prepare("DELETE FROM refs WHERE ref_id = ?1")?;
        let mut insert_ref = transaction.prepare(
            "INSERT OR REPLACE INTO refs
             (ref_id, caller_node, caller_file, kind, short_name, full_ref, module_path,
              import_kind, local_name, requested_name, namespace_alias, wildcard, line,
              byte_start, byte_end, status, target_node, target_file, target_symbol, provenance)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14,
                     ?15, ?16, ?17, ?18, ?19, ?20)",
        )?;
        let mut insert_edge = transaction.prepare(
            "INSERT OR REPLACE INTO edges
                     (edge_id, ref_id, source_node, target_node, target_file, target_symbol,
                      kind, line, provenance)
                     VALUES (?1, ?2, ?3, ?4, ?5, ?6, 'call', ?7, ?8)",
        )?;
        for row in joined.result.rows {
            let caller_path = String::from_utf8(row.caller_path.clone()).map_err(|_| {
                CallGraphStoreError::Unavailable("non-UTF-8 manifest caller path".to_string())
            })?;
            ensure_manifest_path(
                &caller_path,
                manifest,
                &blob_connection,
                &mut loaded_paths,
                &mut parsed,
                &mut nodes,
            )?;
            if let Some(target) = row
                .target_path
                .as_ref()
                .and_then(|path| std::str::from_utf8(path).ok())
            {
                ensure_manifest_path(
                    target,
                    manifest,
                    &blob_connection,
                    &mut loaded_paths,
                    &mut parsed,
                    &mut nodes,
                )?;
            }
            let Some(parse) = parsed.get(&caller_path) else {
                continue;
            };
            let Some(reference) = parse.get(&row.ref_ordinal) else {
                continue;
            };
            let caller_node = reference
                .caller_symbol
                .as_ref()
                .and_then(|symbol| nodes.get(&(caller_path.clone(), symbol.clone())))
                .cloned();
            let target_path = row
                .target_path
                .as_ref()
                .and_then(|path| String::from_utf8(path.clone()).ok());
            let target_symbol = row.target_symbol.clone();
            let target_node = target_path
                .as_ref()
                .zip(target_symbol.as_ref())
                .and_then(|(path, symbol)| nodes.get(&(path.clone(), symbol.clone())))
                .cloned();
            let ref_id = format!("view:{caller_path}:{}", row.ref_ordinal);
            let relink = changed
                .as_ref()
                .is_some_and(|paths| !paths.contains(caller_path.as_bytes()));
            let status = if row.status == join::ResolutionStatus::Resolved {
                "resolved"
            } else {
                "unresolved"
            };
            if relink {
                // Symbol IDs encode path, scoped name and ordinal. Even an unchanged
                // caller must be re-linked when target ordinals or resolution change.
                // Resolve against the complete new manifest: additions, reexports and
                // configuration changes can affect callers with no previous target.
                let same: bool = same_ref.query_row(
                    params![
                        ref_id,
                        caller_node,
                        status,
                        target_node,
                        target_path,
                        target_symbol
                    ],
                    |row| row.get(0),
                )?;
                if same {
                    continue;
                }
                stats.relinked_deleted += delete_edge.execute([&ref_id])?;
                stats.relinked_deleted += delete_ref.execute([&ref_id])?;
            }
            let inserted = if relink {
                &mut stats.relinked_inserted
            } else {
                &mut stats.inserted
            };
            *inserted += insert_ref.execute(params![
                ref_id,
                caller_node,
                caller_path,
                manifest_ref_kind(row.kind),
                reference.short_name,
                reference.full_ref,
                reference.module_path,
                reference.import_kind,
                reference.local_name,
                reference.requested_name,
                reference.namespace_alias,
                i64::from(reference.wildcard),
                i64::from(reference.line),
                reference.byte_start as i64,
                reference.byte_end as i64,
                if row.status == join::ResolutionStatus::Resolved {
                    "resolved"
                } else {
                    "unresolved"
                },
                target_node,
                target_path,
                target_symbol,
                PROVENANCE_TREESITTER,
            ])?;
            if row.kind == join::BlobRefKind::Call {
                if let (Some(source_node), Some(target_file), Some(target_symbol)) =
                    (caller_node, target_path, target_symbol)
                {
                    *inserted += insert_edge.execute(params![
                        format!("edge:{ref_id}"),
                        ref_id,
                        source_node,
                        target_node,
                        target_file,
                        target_symbol,
                        i64::from(reference.line),
                        PROVENANCE_TREESITTER,
                    ])?;
                }
            }
        }
    }
    profile.finish("emit_refs_edges");
    set_meta_ready(&transaction, true)?;
    transaction.execute(
        "INSERT OR REPLACE INTO meta(k, v) VALUES('view_manifest_fingerprint', ?1)",
        [fingerprint(manifest)?],
    )?;
    transaction.execute(
        "INSERT OR REPLACE INTO meta(k, v) VALUES('view_materialization_version', ?1)",
        [MATERIALIZATION_VERSION],
    )?;
    transaction.commit()?;
    profile.finish("commit");
    Ok((stats, profile.into_timings()))
}

struct ManifestViewBlobReader<'a> {
    connection: &'a Connection,
}

impl join::ManifestBlobReader for ManifestViewBlobReader<'_> {
    fn read_callgraph_blob(
        &self,
        full_key: &str,
    ) -> std::result::Result<Option<Vec<u8>>, join::ManifestJoinError> {
        let Some(key) = decode_manifest_full_key(full_key) else {
            return Ok(None);
        };
        self.connection
            .query_row(
                "SELECT payload FROM blob_payloads WHERE full_key = ?1",
                [key],
                |row| row.get(0),
            )
            .optional()
            .map_err(|error| join::ManifestJoinError::InvalidBlob(error.to_string()))
    }
}

fn decode_manifest_full_key(value: &str) -> Option<Vec<u8>> {
    if value.len() != 64 {
        return None;
    }
    (0..value.len())
        .step_by(2)
        .map(|index| u8::from_str_radix(&value[index..index + 2], 16).ok())
        .collect()
}

const fn manifest_ref_kind(kind: join::BlobRefKind) -> &'static str {
    match kind {
        join::BlobRefKind::Call => "call",
        join::BlobRefKind::ValueRef => "value_ref",
        join::BlobRefKind::Import => "import",
        join::BlobRefKind::Module => "module",
        join::BlobRefKind::Reexport => "reexport",
        join::BlobRefKind::ExportAlias => "export_alias",
    }
}

fn configure_materialization_connection(connection: &Connection) -> Result<()> {
    connection.busy_timeout(Duration::from_secs(5))?;
    connection.pragma_update(None, "journal_mode", "WAL")?;
    // Publication can expose the generation while its pages remain in the WAL,
    // so the transaction commit itself must survive power loss.
    connection.pragma_update(None, "synchronous", "FULL")?;
    // A detached checkpoint moves these pages into the main file. Keeping the
    // automatic threshold disabled makes that work observable and off-path.
    connection.pragma_update(None, "wal_autocheckpoint", 0)?;
    Ok(())
}

#[cfg(test)]
mod tests;

fn load_bindings(
    connection: &Connection,
) -> Result<BTreeMap<String, join::ViewBindingDependencies>> {
    let mut statement = connection.prepare("SELECT file_path, payload FROM view_bindings")?;
    let rows = statement.query_map([], |row| {
        Ok((row.get::<_, String>(0)?, row.get::<_, String>(1)?))
    })?;
    rows.map(|row| {
        let (path, payload) = row?;
        let binding = serde_json::from_str(&payload)
            .map_err(|error| CallGraphStoreError::Unavailable(error.to_string()))?;
        Ok((path, binding))
    })
    .collect()
}

fn dependent_closure(
    connection: &Connection,
    changed: &BTreeSet<Vec<u8>>,
) -> Result<BTreeSet<String>> {
    let mut selected = changed
        .iter()
        .filter_map(|path| String::from_utf8(path.clone()).ok())
        .collect::<BTreeSet<_>>();
    if !changed.is_empty() {
        // Rust inline-module and parent queries can inspect the crate-wide index.
        // Recheck that domain even for non-.rs paths: explicit module paths may
        // name other extensions, and membership changes can expose new modules.
        selected.insert(join::VIEW_RUST_MODULE_DOMAIN.to_string());
    }
    let mut pending = selected.iter().cloned().collect::<Vec<_>>();
    let mut dependents =
        connection.prepare("SELECT file_path FROM file_dependencies WHERE dep_file = ?1")?;
    while let Some(path) = pending.pop() {
        for caller in dependents.query_map([path], |row| row.get::<_, String>(0))? {
            let caller = caller?;
            if selected.insert(caller.clone()) {
                pending.push(caller);
            }
        }
    }
    Ok(selected)
}

fn requires_full_resolution(
    base: &crate::views::Manifest,
    next: &crate::views::Manifest,
    changed: &BTreeSet<Vec<u8>>,
) -> bool {
    changed.iter().any(|path| {
        base.entries()
            .chain(next.entries())
            .any(|(candidate, entry)| {
                candidate.as_bytes() == path
                    && matches!(
                        entry,
                        crate::views::ManifestEntry::Synthetic { .. }
                            | crate::views::ManifestEntry::Symlink { .. }
                            | crate::views::ManifestEntry::Gitlink { .. }
                    )
            })
    })
}

/// Unchanged blobs are decoded for row emission only when a selected reference
/// actually needs their caller data or target IDs. The join builds its own index;
/// decoding every blob again here would erase much of the incremental saving.
fn ensure_manifest_path(
    path: &str,
    manifest: &crate::views::Manifest,
    blobs: &Connection,
    loaded: &mut BTreeSet<String>,
    parsed: &mut BTreeMap<String, BTreeMap<u32, join::BlobRef>>,
    nodes: &mut HashMap<(String, String), String>,
) -> Result<()> {
    if !loaded.insert(path.to_string()) {
        return Ok(());
    }
    let Ok(rel) = crate::views::RelPath::new(path.as_bytes().to_vec()) else {
        return Ok(());
    };
    let Some(crate::views::ManifestEntry::Regular {
        planes,
        resolution_input,
        ..
    }) = manifest.get(&rel)
    else {
        return Ok(());
    };
    let Some(key) = &planes.callgraph else {
        return Ok(());
    };
    let key_bytes = decode_manifest_full_key(key).ok_or_else(|| {
        CallGraphStoreError::Unavailable(format!("invalid manifest callgraph key {key}"))
    })?;
    let payload = blobs
        .query_row(
            "SELECT payload FROM blob_payloads WHERE full_key=?1",
            [key_bytes],
            |row| row.get::<_, Vec<u8>>(0),
        )
        .optional()?
        .ok_or_else(|| {
            CallGraphStoreError::Unavailable(format!("missing manifest callgraph blob {key}"))
        })?;
    let blob = join::CallgraphBlob::from_bytes(&payload)
        .map_err(|error| CallGraphStoreError::Unavailable(error.to_string()))?;
    let Some(parse) = blob.parse() else {
        return Ok(());
    };
    for symbol in &parse.symbols {
        let id = format!("view:{path}:{}:{}", symbol.scoped_name, symbol.ordinal);
        nodes.insert((path.to_string(), symbol.scoped_name.clone()), id.clone());
        nodes
            .entry((path.to_string(), symbol.name.clone()))
            .or_insert(id);
    }
    if !resolution_input {
        parsed.insert(
            path.to_string(),
            parse
                .refs
                .iter()
                .fold(BTreeMap::new(), |mut by_ordinal, reference| {
                    by_ordinal
                        .entry(reference.ordinal)
                        .or_insert_with(|| reference.clone());
                    by_ordinal
                }),
        );
    }
    Ok(())
}