sqry-db 9.0.19

Salsa-style incremental computation engine for sqry semantic code search
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
//! Unused / dead-code detection derived queries.
//!
//! Migrates `sqry-core::query::executor::graph_unused::{find_unused_nodes,
//! compute_reachable_set_graph, is_node_unused}` into cache-aware queries
//! that can be shared between the `sqry-db` planner, the MCP handlers (once
//! DB15+ wires them), and future analysis callers.
//!
//! # Pipeline
//!
//! ```text
//!   EntryPointsQuery   ──▶  (Arc<HashSet<NodeId>>)
//!//!//!   ReachableFromEntryPointsQuery  ──▶  (Arc<HashSet<NodeId>>)
//!//!//!   UnusedQuery / IsNodeUnusedQuery
//! ```
//!
//! # Entry-point classification
//!
//! A node is an entry point when *any* of the following is true:
//! - Its visibility is `public` / `pub`
//! - Its short name is `main`, begins with `test_`, or ends with `_test`
//! - Its [`NodeKind`] is [`NodeKind::Test`] or [`NodeKind::Export`]
//!
//! This mirrors [`sqry_core::query::executor::graph_unused`]'s
//! `is_entry_point_node` exactly so migration preserves the legacy set of
//! unused-free symbols.
//!
//! # Reachability edges
//!
//! The traversal follows `Calls`, `References`, `Imports`, `Inherits`,
//! `Implements`, and `TypeOf` edges — identical to the legacy
//! `is_reachability_edge` predicate.

use std::collections::HashSet;
use std::sync::Arc;

use sqry_core::graph::unified::concurrent::GraphSnapshot;
use sqry_core::graph::unified::edge::kind::EdgeKind;
use sqry_core::graph::unified::node::id::NodeId;
use sqry_core::graph::unified::node::kind::NodeKind;
use sqry_core::graph::unified::storage::arena::NodeEntry;
use sqry_core::query::UnusedScope;

use crate::QueryDb;
use crate::dependency::record_file_dep;
use crate::query::DerivedQuery;

// ============================================================================
// EntryPointsQuery
// ============================================================================

/// Set of node IDs that qualify as reachability roots.
///
/// # Invalidation
///
/// `TRACKS_METADATA_REVISION = true`: visibility, kind, and node name are
/// all stored on `NodeEntry`, which mutates with metadata revisions rather
/// than edge revisions. We also track edges at `TRACKS_EDGE_REVISION = true`
/// because edge additions can introduce new `Test`/`Export` nodes.
pub struct EntryPointsQuery;

impl DerivedQuery for EntryPointsQuery {
    type Key = ();
    type Value = Arc<HashSet<NodeId>>;
    const QUERY_TYPE_ID: u32 = crate::queries::type_ids::ENTRY_POINTS;
    const TRACKS_EDGE_REVISION: bool = true;
    const TRACKS_METADATA_REVISION: bool = true;

    fn execute(_key: &(), _db: &QueryDb, snapshot: &GraphSnapshot) -> Arc<HashSet<NodeId>> {
        for (fid, _) in snapshot.file_segments().iter() {
            record_file_dep(fid);
        }
        let mut entry_points = HashSet::new();
        for (node_id, entry) in snapshot.nodes().iter() {
            // Gate 0d iter-2 fix: skip unified losers from
            // `EntryPointsQuery`. Losers are inert duplicates; their
            // winner carries the real visibility/name. See
            // `NodeEntry::is_unified_loser`.
            if entry.is_unified_loser() {
                continue;
            }
            if is_entry_point(snapshot, entry) {
                entry_points.insert(node_id);
            }
        }
        Arc::new(entry_points)
    }
}

fn is_entry_point(snapshot: &GraphSnapshot, entry: &NodeEntry) -> bool {
    let is_public = entry
        .visibility
        .and_then(|id| snapshot.strings().resolve(id))
        .is_some_and(|v| {
            let s = v.as_ref();
            s == "public" || s == "pub"
        });
    let is_main_or_test = snapshot.strings().resolve(entry.name).is_some_and(|name| {
        let n = name.as_ref();
        n == "main" || n.starts_with("test_") || n.ends_with("_test")
    });
    let is_export = matches!(entry.kind, NodeKind::Export);
    let is_test_node = matches!(entry.kind, NodeKind::Test);
    is_public || is_main_or_test || is_export || is_test_node
}

// ============================================================================
// ReachableFromEntryPointsQuery
// ============================================================================

/// Set of nodes reachable from the entry-point set by following reachability
/// edges (`Calls`, `References`, `Imports`, `Inherits`, `Implements`,
/// `TypeOf`).
///
/// `TRACKS_EDGE_REVISION = true`: any edge change redraws the reachable set.
pub struct ReachableFromEntryPointsQuery;

impl DerivedQuery for ReachableFromEntryPointsQuery {
    type Key = ();
    type Value = Arc<HashSet<NodeId>>;
    const QUERY_TYPE_ID: u32 = crate::queries::type_ids::REACHABLE_FROM_ENTRY_POINTS;
    const TRACKS_EDGE_REVISION: bool = true;
    const TRACKS_METADATA_REVISION: bool = true;

    fn execute(_key: &(), db: &QueryDb, snapshot: &GraphSnapshot) -> Arc<HashSet<NodeId>> {
        for (fid, _) in snapshot.file_segments().iter() {
            record_file_dep(fid);
        }
        let entry_points = db.get::<EntryPointsQuery>(&());
        let mut reachable: HashSet<NodeId> = entry_points.as_ref().clone();
        let mut worklist: Vec<NodeId> = reachable.iter().copied().collect();
        while let Some(node_id) = worklist.pop() {
            for edge in &snapshot.edges().edges_from(node_id) {
                if is_reachability_edge(&edge.kind) && reachable.insert(edge.target) {
                    worklist.push(edge.target);
                }
            }
        }
        Arc::new(reachable)
    }
}

fn is_reachability_edge(kind: &EdgeKind) -> bool {
    matches!(
        kind,
        EdgeKind::Calls { .. }
            | EdgeKind::References
            | EdgeKind::Imports { .. }
            | EdgeKind::Inherits
            | EdgeKind::Implements
            | EdgeKind::TypeOf { .. }
    )
}

// ============================================================================
// UnusedQuery
// ============================================================================

// PN3 cold-start persistence: UnusedKey and IsNodeUnusedKey are serialized via
// postcard at cache-insert time. UnusedScope gains Serialize/Deserialize in
// sqry-core/src/query/unused_config.rs (PN3 SERDE_DERIVES). NodeId already
// derives Serialize/Deserialize from sqry-core.

/// Type alias for the value produced by [`UnusedQuery`].
/// `Arc` is serde-transparent when the workspace `serde` `rc` feature is enabled.
pub type UnusedValue = Arc<Vec<NodeId>>;

/// Type alias for the value produced by [`IsNodeUnusedQuery`].
pub type IsNodeUnusedValue = bool;

/// Cache key for [`UnusedQuery`].
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct UnusedKey {
    /// Scope filter (All / Public / Private / Function / Struct).
    pub scope: UnusedScope,
    /// Maximum number of unused nodes to return.
    pub max_results: usize,
}

/// Returns the sorted list of unused nodes matching the scope filter.
///
/// A node is unused when:
/// 1. It matches the [`UnusedKey::scope`] filter.
/// 2. It is not an entry point (`main`, test, export, or `NodeKind::Test`).
/// 3. It is not reachable from any entry point.
pub struct UnusedQuery;

impl DerivedQuery for UnusedQuery {
    type Key = UnusedKey;
    type Value = Arc<Vec<NodeId>>;
    const QUERY_TYPE_ID: u32 = crate::queries::type_ids::UNUSED;
    const TRACKS_EDGE_REVISION: bool = true;
    const TRACKS_METADATA_REVISION: bool = true;

    fn execute(key: &UnusedKey, db: &QueryDb, snapshot: &GraphSnapshot) -> Arc<Vec<NodeId>> {
        for (fid, _) in snapshot.file_segments().iter() {
            record_file_dep(fid);
        }
        let reachable = db.get::<ReachableFromEntryPointsQuery>(&());
        let mut unused: Vec<NodeId> = Vec::new();
        for (node_id, entry) in snapshot.nodes().iter() {
            if unused.len() >= key.max_results {
                break;
            }
            // Gate 0d iter-2 fix: never report a unified loser as
            // "unused". See `NodeEntry::is_unified_loser`.
            if entry.is_unified_loser() {
                continue;
            }
            if !scope_matches(entry, snapshot, key.scope) {
                continue;
            }
            if is_always_entry_point(snapshot, entry) {
                continue;
            }
            if !reachable.contains(&node_id) {
                unused.push(node_id);
            }
        }
        unused.sort_unstable_by_key(|id| (id.index(), id.generation()));
        Arc::new(unused)
    }
}

fn scope_matches(entry: &NodeEntry, snapshot: &GraphSnapshot, scope: UnusedScope) -> bool {
    match scope {
        UnusedScope::All => true,
        UnusedScope::Public => entry
            .visibility
            .and_then(|id| snapshot.strings().resolve(id))
            .is_some_and(|v| {
                let s = v.as_ref();
                s == "public" || s == "pub"
            }),
        UnusedScope::Private => {
            let vis = entry
                .visibility
                .and_then(|id| snapshot.strings().resolve(id));
            vis.is_none()
                || vis.is_some_and(|v| {
                    let s = v.as_ref();
                    s != "public" && s != "pub"
                })
        }
        UnusedScope::Function => matches!(entry.kind, NodeKind::Function | NodeKind::Method),
        UnusedScope::Struct => matches!(entry.kind, NodeKind::Struct | NodeKind::Class),
    }
}

/// Entry points that should never be marked unused regardless of scope.
/// Mirrors the secondary entry-point check in
/// [`sqry_core::query::executor::graph_unused::is_node_unused`].
fn is_always_entry_point(snapshot: &GraphSnapshot, entry: &NodeEntry) -> bool {
    let is_main_or_test = snapshot.strings().resolve(entry.name).is_some_and(|name| {
        let n = name.as_ref();
        n == "main" || n.starts_with("test_") || n.ends_with("_test")
    });
    let is_export = matches!(entry.kind, NodeKind::Export);
    let is_test_node = matches!(entry.kind, NodeKind::Test);
    is_main_or_test || is_export || is_test_node
}

// ============================================================================
// IsNodeUnusedQuery
// ============================================================================

/// Cache key for [`IsNodeUnusedQuery`].
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct IsNodeUnusedKey {
    /// The node to check.
    pub node_id: NodeId,
    /// Scope filter to apply.
    pub scope: UnusedScope,
}

/// Returns `true` when the given node is unused per the rules in
/// [`UnusedQuery`]'s documentation. Used by the MCP `find_unused` single-node
/// tool and the CLI `sqry unused` check mode.
pub struct IsNodeUnusedQuery;

impl DerivedQuery for IsNodeUnusedQuery {
    type Key = IsNodeUnusedKey;
    type Value = bool;
    const QUERY_TYPE_ID: u32 = crate::queries::type_ids::IS_NODE_UNUSED;
    const TRACKS_EDGE_REVISION: bool = true;
    const TRACKS_METADATA_REVISION: bool = true;

    fn execute(key: &IsNodeUnusedKey, db: &QueryDb, snapshot: &GraphSnapshot) -> bool {
        let Some(entry) = snapshot.nodes().get(key.node_id) else {
            return false;
        };
        record_file_dep(entry.file);
        if !scope_matches(entry, snapshot, key.scope) {
            return false;
        }
        if is_always_entry_point(snapshot, entry) {
            return false;
        }
        let reachable = db.get::<ReachableFromEntryPointsQuery>(&());
        !reachable.contains(&key.node_id)
    }
}

// ============================================================================
// PN3 serde roundtrip tests
// ============================================================================

#[cfg(test)]
mod serde_roundtrip {
    use super::*;
    use postcard::{from_bytes, to_allocvec};
    use sqry_core::graph::unified::node::id::NodeId;
    use sqry_core::query::UnusedScope;

    #[test]
    fn unused_key_roundtrip() {
        let original = UnusedKey {
            scope: UnusedScope::Public,
            max_results: 250,
        };
        let bytes = to_allocvec(&original).expect("serialize failed");
        let decoded: UnusedKey = from_bytes(&bytes).expect("deserialize failed");
        assert_eq!(decoded, original);
    }

    #[test]
    fn unused_key_all_scopes_roundtrip() {
        for scope in [
            UnusedScope::All,
            UnusedScope::Public,
            UnusedScope::Private,
            UnusedScope::Function,
            UnusedScope::Struct,
        ] {
            let original = UnusedKey {
                scope,
                max_results: 100,
            };
            let bytes = to_allocvec(&original).expect("serialize failed");
            let decoded: UnusedKey = from_bytes(&bytes).expect("deserialize failed");
            assert_eq!(decoded, original, "scope={scope:?}");
        }
    }

    #[test]
    fn is_node_unused_key_roundtrip() {
        let original = IsNodeUnusedKey {
            node_id: NodeId::new(99, 3),
            scope: UnusedScope::Function,
        };
        let bytes = to_allocvec(&original).expect("serialize failed");
        let decoded: IsNodeUnusedKey = from_bytes(&bytes).expect("deserialize failed");
        assert_eq!(decoded, original);
    }

    #[test]
    fn unused_value_roundtrip() {
        // UnusedValue = Arc<Vec<NodeId>>
        let original: UnusedValue = Arc::new(vec![
            NodeId::new(1, 1),
            NodeId::new(2, 1),
            NodeId::new(3, 1),
        ]);
        let bytes = to_allocvec(&original).expect("serialize failed");
        let decoded: UnusedValue = from_bytes(&bytes).expect("deserialize failed");
        assert_eq!(decoded.as_ref(), original.as_ref());
    }

    #[test]
    fn is_node_unused_value_roundtrip() {
        // IsNodeUnusedValue = bool
        for val in [true, false] {
            let bytes = to_allocvec(&val).expect("serialize failed");
            let decoded: IsNodeUnusedValue = from_bytes(&bytes).expect("deserialize failed");
            assert_eq!(decoded, val);
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::QueryDbConfig;
    use sqry_core::graph::unified::concurrent::CodeGraph;
    use std::path::Path;

    fn alloc_fn_with_vis(graph: &mut CodeGraph, name: &str, vis: Option<&str>) -> NodeId {
        let file = graph.files_mut().register(Path::new("main.rs")).unwrap();
        let name_id = graph.strings_mut().intern(name).unwrap();
        let mut entry =
            NodeEntry::new(NodeKind::Function, name_id, file).with_qualified_name(name_id);
        if let Some(v) = vis {
            let vid = graph.strings_mut().intern(v).unwrap();
            entry = entry.with_visibility(vid);
        }
        graph.nodes_mut().alloc(entry).unwrap()
    }

    fn add_call(graph: &mut CodeGraph, src: NodeId, tgt: NodeId) {
        let file = graph.nodes().get(src).unwrap().file;
        graph.edges_mut().add_edge(
            src,
            tgt,
            EdgeKind::Calls {
                argument_count: 0,
                is_async: false,
            },
            file,
        );
    }

    fn build_db(graph: CodeGraph) -> QueryDb {
        let snapshot = Arc::new(graph.snapshot());
        let mut db = QueryDb::new(snapshot, QueryDbConfig::default());
        db.register::<EntryPointsQuery>();
        db.register::<ReachableFromEntryPointsQuery>();
        db.register::<UnusedQuery>();
        db.register::<IsNodeUnusedQuery>();
        db
    }

    #[test]
    fn entry_points_include_main_test_public_export() {
        let mut graph = CodeGraph::new();
        let main = alloc_fn_with_vis(&mut graph, "main", None);
        let pub_fn = alloc_fn_with_vis(&mut graph, "exported_api", Some("public"));
        let test_fn = alloc_fn_with_vis(&mut graph, "test_thing", None);
        let private_fn = alloc_fn_with_vis(&mut graph, "helper", None);

        let db = build_db(graph);
        let entry_points = db.get::<EntryPointsQuery>(&());
        assert!(entry_points.contains(&main));
        assert!(entry_points.contains(&pub_fn));
        assert!(entry_points.contains(&test_fn));
        assert!(!entry_points.contains(&private_fn));
    }

    #[test]
    fn unused_query_reports_unreachable_private_symbols() {
        let mut graph = CodeGraph::new();
        let main = alloc_fn_with_vis(&mut graph, "main", None);
        let used = alloc_fn_with_vis(&mut graph, "used_helper", None);
        let unused = alloc_fn_with_vis(&mut graph, "unused_helper", None);
        add_call(&mut graph, main, used);

        let db = build_db(graph);
        let key = UnusedKey {
            scope: UnusedScope::All,
            max_results: 100,
        };
        let result = db.get::<UnusedQuery>(&key);
        assert_eq!(result.as_ref(), &vec![unused]);
    }

    #[test]
    fn is_node_unused_honours_scope_filter() {
        let mut graph = CodeGraph::new();
        let main = alloc_fn_with_vis(&mut graph, "main", None);
        let unused = alloc_fn_with_vis(&mut graph, "ghost", None);
        let _ = main;

        let db = build_db(graph);
        let all_key = IsNodeUnusedKey {
            node_id: unused,
            scope: UnusedScope::All,
        };
        assert!(db.get::<IsNodeUnusedQuery>(&all_key));
        let struct_key = IsNodeUnusedKey {
            node_id: unused,
            scope: UnusedScope::Struct,
        };
        assert!(!db.get::<IsNodeUnusedQuery>(&struct_key));
    }

    #[test]
    fn reachable_set_follows_reachability_edges() {
        let mut graph = CodeGraph::new();
        let main = alloc_fn_with_vis(&mut graph, "main", None);
        let mid = alloc_fn_with_vis(&mut graph, "mid", None);
        let deep = alloc_fn_with_vis(&mut graph, "deep", None);
        add_call(&mut graph, main, mid);
        add_call(&mut graph, mid, deep);

        let db = build_db(graph);
        let reachable = db.get::<ReachableFromEntryPointsQuery>(&());
        assert!(reachable.contains(&main));
        assert!(reachable.contains(&mid));
        assert!(reachable.contains(&deep));
    }

    /// Gate 0d iter-2 regression: `EntryPointsQuery` and
    /// `UnusedQuery` MUST skip Phase 4c-prime unified losers. We
    /// construct the end-state `merge_node_into` leaves in the arena
    /// (name == INVALID + content-addressable fields cleared)
    /// directly, because the merge helper is `pub(crate)` inside
    /// sqry-core and not reachable from sqry-db tests.
    #[test]
    fn entry_points_and_unused_exclude_unified_losers() {
        use sqry_core::graph::unified::string::StringId;

        let mut graph = CodeGraph::new();
        let main = alloc_fn_with_vis(&mut graph, "main", None);
        let _ = main;

        // Build a winner (public) + loser pair sharing a qualified
        // name in different files.
        let file_a = graph.files_mut().register(Path::new("a.rs")).unwrap();
        let file_b = graph.files_mut().register(Path::new("b.rs")).unwrap();
        let name_id = graph.strings_mut().intern("shared").unwrap();
        let qn_id = graph.strings_mut().intern("mod::shared").unwrap();
        let pub_id = graph.strings_mut().intern("public").unwrap();

        let (winner, loser): (NodeId, NodeId) = {
            let arena = graph.nodes_mut();
            let mut w = NodeEntry::new(NodeKind::Function, name_id, file_a).with_visibility(pub_id);
            w.qualified_name = Some(qn_id);
            let w_id = arena.alloc(w).unwrap();
            let mut l = NodeEntry::new(NodeKind::Function, name_id, file_b).with_visibility(pub_id);
            l.qualified_name = Some(qn_id);
            let l_id = arena.alloc(l).unwrap();
            (w_id, l_id)
        };
        graph.files_mut().record_node(file_a, winner);
        graph.files_mut().record_node(file_b, loser);

        // Simulate the end-state `merge_node_into` leaves in the
        // arena for a loser. This matches the clearing contract in
        // `sqry-core/src/graph/unified/build/unification.rs`.
        {
            let arena = graph.nodes_mut();
            let l_mut = arena.get_mut(loser).expect("loser present");
            l_mut.name = StringId::INVALID;
            l_mut.qualified_name = None;
            l_mut.signature = None;
            l_mut.body_hash = None;
            l_mut.doc = None;
            l_mut.visibility = None;
        }
        graph.rebuild_indices();

        let db = build_db(graph);
        let entry_points = db.get::<EntryPointsQuery>(&());
        assert!(
            !entry_points.contains(&loser),
            "EntryPointsQuery leaked unified loser"
        );
        // Note: the winner lost its visibility metadata when we
        // simulated unification on the loser (the winner is
        // distinct), so the winner still carries "public" and is
        // reported as an entry point.
        assert!(
            entry_points.contains(&winner),
            "EntryPointsQuery must still see the winner"
        );

        let unused_key = UnusedKey {
            scope: UnusedScope::All,
            max_results: 100,
        };
        let unused = db.get::<UnusedQuery>(&unused_key);
        assert!(!unused.contains(&loser), "UnusedQuery leaked unified loser");
    }
}