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
//! `WorkspaceIndex`: multi-repo search orchestration using `SessionManager`.
use std::path::{Path, PathBuf};
use crate::graph::unified::NodeKind;
use crate::query::QueryExecutor;
use crate::session::SessionManager;
use super::error::{WorkspaceError, WorkspaceResult};
use super::registry::{WorkspaceRegistry, WorkspaceRepository};
/// A workspace-level index that orchestrates queries across multiple repositories.
///
/// `WorkspaceIndex` delegates to `SessionManager` for individual repository queries,
/// leveraging its caching and lazy-loading capabilities. Results are aggregated
/// and tagged with repository metadata.
pub struct WorkspaceIndex {
registry: WorkspaceRegistry,
session: SessionManager,
workspace_root: PathBuf,
}
impl WorkspaceIndex {
/// Open a workspace index from the registry file at the given path.
///
/// # Arguments
///
/// * `workspace_root` - Root directory of the workspace
/// * `registry_path` - Path to the .sqry-workspace registry file
///
/// # Example
///
/// ```no_run
/// use sqry_core::workspace::WorkspaceIndex;
/// use std::path::Path;
///
/// let workspace = Path::new("/path/to/workspace");
/// let registry = workspace.join(".sqry-workspace");
/// let mut index = WorkspaceIndex::open(workspace, ®istry).unwrap();
/// ```
///
/// # Errors
///
/// Returns [`WorkspaceError`] when registry loading or session creation fails.
pub fn open(workspace_root: impl Into<PathBuf>, registry_path: &Path) -> WorkspaceResult<Self> {
let workspace_root = workspace_root.into();
let registry = WorkspaceRegistry::load(registry_path)?;
let session = SessionManager::new().map_err(|e| WorkspaceError::QueryParsing {
message: format!("Failed to create SessionManager: {e}"),
})?;
Ok(Self {
registry,
session,
workspace_root,
})
}
/// Create a new workspace index with an existing registry and session.
///
/// Useful for testing or when you want to provide a custom `SessionManager`.
pub fn new(
workspace_root: impl Into<PathBuf>,
registry: WorkspaceRegistry,
session: SessionManager,
) -> Self {
Self {
registry,
session,
workspace_root: workspace_root.into(),
}
}
/// Execute a query across all repositories in the workspace.
///
/// This method:
/// 1. Parses the query into AST and extracts repo filter
/// 2. Filters repositories based on the repo predicates
/// 3. Executes the normalized query (repo predicates stripped) against each matching repository
/// 4. Aggregates results with repository metadata
///
/// # Arguments
///
/// * `query` - The query string to execute (may include repo: predicates)
///
/// # Returns
///
/// A list of matches with their associated repository information.
///
/// # Example
///
/// ```no_run
/// use sqry_core::workspace::WorkspaceIndex;
/// use std::path::Path;
///
/// let workspace = Path::new("/path/to/workspace");
/// let registry = workspace.join(".sqry-workspace");
/// let mut index = WorkspaceIndex::open(workspace, ®istry).unwrap();
///
/// let results = index.query("kind:function AND repo:backend-*").unwrap();
/// ```
///
/// # Errors
///
/// Returns [`WorkspaceError`] when parsing the query or executing repository queries fails.
pub fn query(&mut self, query: &str) -> WorkspaceResult<Vec<NodeWithRepo>> {
// Parse query into AST to extract repo filter.
let executor = QueryExecutor::new();
let parsed = executor
.parse_query_ast(query)
.map_err(|e| WorkspaceError::QueryParsing {
message: format!("Failed to parse query: {e}"),
})?;
// Filter repositories based on the repo predicates
let repos: Vec<&WorkspaceRepository> = self
.registry
.repositories
.iter()
.filter(|repo| parsed.repo_filter.matches(&repo.name))
.collect();
if repos.is_empty() {
return Ok(Vec::new());
}
// Use normalized query string (repo predicates stripped) for repository execution
// The repo filtering has already been done at the workspace level above
let query_str = parsed.normalized.as_ref();
// Execute query against each repository and aggregate results
let mut all_results = Vec::new();
for repo in repos {
// Resolve absolute path for the repository
let repo_path = if repo.root.is_absolute() {
repo.root.clone()
} else {
self.workspace_root.join(&repo.root)
};
// Query the repository via SessionManager
// SessionManager will parse the query again and execute it against the single-repo index
match self.session.query(&repo_path, query_str) {
Ok(results) => {
// Tag each result with repo metadata
for m in results.iter() {
let match_info = MatchInfo {
name: m.name().map(|s| s.to_string()).unwrap_or_default(),
qualified_name: m.qualified_name().map(|s| s.to_string()),
kind: m.kind(),
language: m.language().map(|lang| lang.to_string()),
file_path: m.relative_path().unwrap_or_default(),
start_line: m.start_line(),
start_column: m.start_column(),
end_line: m.end_line(),
end_column: m.end_column(),
is_static: m.is_static(),
signature: m.signature().map(|s| s.to_string()),
doc: m.doc().map(|s| s.to_string()),
};
all_results.push(NodeWithRepo {
match_info,
repo_name: repo.name.clone(),
repo_id: repo.id.clone(),
repo_path: repo_path.clone(),
});
}
}
Err(err) => {
// Log error but continue with other repos
eprintln!(
"Warning: Failed to query repository '{}': {}",
repo.name, err
);
}
}
}
Ok(all_results)
}
/// Get coarse workspace-level statistics.
///
/// Returns aggregated stats across all repositories in the workspace.
///
/// `total_symbols` sums each repository's
/// [`super::registry::WorkspaceRepository::symbol_count_at_registration`],
/// the last-known snapshot captured when the repository was
/// registered (`discover_repositories` / `sqry workspace add`), not a
/// live re-read of `.sqry/graph/manifest.json`. It goes stale after a
/// direct `sqry index --force` reindex until the member is removed
/// and re-added. Prefer [`Self::detailed_stats`] (backs `sqry
/// workspace stats`) when the count must reflect the current index.
#[must_use]
pub fn stats(&self) -> WorkspaceStats {
let total_repos = self.registry.repositories.len();
let indexed_repos = self
.registry
.repositories
.iter()
.filter(|r| r.last_indexed_at.is_some())
.count();
let total_symbols = self
.registry
.repositories
.iter()
.filter_map(|r| r.symbol_count_at_registration)
.sum();
WorkspaceStats {
total_repos,
indexed_repos,
total_symbols,
}
}
/// Get detailed workspace statistics including staleness tracking.
///
/// Returns comprehensive stats with freshness buckets, health scores,
/// and other detailed metrics. Unlike [`Self::stats`], the symbol
/// counts here are read **live** from each member's
/// `.sqry/graph/manifest.json` at call time (see
/// [`super::stats::DetailedWorkspaceStats::from_registry`]), so a
/// direct `sqry index --force` reindex of a member is reflected
/// immediately without a `workspace remove` + `workspace add`
/// round-trip.
#[must_use]
pub fn detailed_stats(&self) -> super::stats::DetailedWorkspaceStats {
super::stats::DetailedWorkspaceStats::from_registry(&self.registry)
}
/// Get a reference to the underlying registry.
#[must_use]
pub fn registry(&self) -> &WorkspaceRegistry {
&self.registry
}
/// Get a mutable reference to the underlying registry.
pub fn registry_mut(&mut self) -> &mut WorkspaceRegistry {
&mut self.registry
}
/// Get the workspace root directory.
#[must_use]
pub fn workspace_root(&self) -> &Path {
&self.workspace_root
}
}
/// Query match information from `CodeGraph`.
#[derive(Debug, Clone)]
pub struct MatchInfo {
/// Node name
pub name: String,
/// Canonical qualified name, if available.
pub qualified_name: Option<String>,
/// Node kind (function, class, etc.)
pub kind: NodeKind,
/// Language derived from the backing file, if available.
pub language: Option<String>,
/// File path where the node is defined (relative to repo root)
pub file_path: PathBuf,
/// Starting line number (1-based)
pub start_line: u32,
/// Starting column (1-based)
pub start_column: u32,
/// Ending line number (1-based)
pub end_line: u32,
/// Ending column (1-based)
pub end_column: u32,
/// Whether this match represents a static member.
pub is_static: bool,
/// Optional signature
pub signature: Option<String>,
/// Optional documentation
pub doc: Option<String>,
}
/// A query match tagged with repository metadata for workspace-level queries.
#[derive(Debug, Clone)]
pub struct NodeWithRepo {
/// The match info from the query.
pub match_info: MatchInfo,
/// The name of the repository containing this node.
pub repo_name: String,
/// The unique identifier of the repository containing this node.
pub repo_id: super::registry::WorkspaceRepoId,
/// Absolute path to the repository containing this node.
pub repo_path: PathBuf,
}
/// Aggregated statistics for a workspace.
#[derive(Debug, Clone)]
pub struct WorkspaceStats {
/// Total number of repositories in the workspace.
pub total_repos: usize,
/// Number of repositories that have been indexed.
pub indexed_repos: usize,
/// Total symbol count across all indexed repositories.
pub total_symbols: u64,
}