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
//! Configuration model for standalone and federated Talon processes.
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
mod auth;
mod chunker;
mod defaults;
mod endpoints;
pub mod keychain;
mod scope_filter;
mod search;
#[doc(hidden)]
pub mod test_literals;
use crate::indexer::build_include_globset;
pub use auth::{CredentialEntry, CredentialsConfig, EndpointAuthConfig, ResolvedAuth};
pub use chunker::ChunkerConfig;
pub use endpoints::{
ChatAdapter, ChatAskConfig, ChatExpansionConfig, ChatSection, EmbeddingAdapter,
EmbeddingConfig, McpConfig, McpHooksConfig, RerankAdapter, RerankConfig, RerankScoreScale,
};
pub use scope_filter::ScopeFilter;
pub use search::{InspectConfig, SearchConfig};
/// Priority tier for scope-based ranking.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum ScopePriority {
/// Compiled knowledge scope.
Boosted,
/// Active-work scope.
Elevated,
/// Neutral (1.0x multiplier).
#[default]
Normal,
/// Low-priority scope.
Muted,
/// Explicit-opt-in scope.
Buried,
}
impl ScopePriority {
/// Returns the post-rerank score multiplier.
#[must_use]
pub const fn multiplier(self) -> f64 {
match self {
Self::Boosted => 1.2,
Self::Elevated => 1.1,
Self::Normal => 1.0,
Self::Muted => 0.85,
Self::Buried => 0.5,
}
}
/// Applies the multiplier only when it is allowed by the relevance gate.
///
/// Positive priority boosts are gated so a weak match in a high-priority
/// scope cannot shout over a stronger match elsewhere. Negative weights
/// still apply below the floor because muted/buried scopes are provenance
/// signals, not relevance claims.
#[must_use]
pub fn apply_to_score(self, score: f64) -> f64 {
apply_scope_multiplier(score, self.multiplier())
}
/// Applies scope priority while honoring an explicit user-selected scope.
///
/// `--scope NAME` is an additive request: default scopes remain in play,
/// but matches from the requested scope should not be muted below neutral.
#[must_use]
pub fn apply_to_score_with_explicit(self, score: f64, explicitly_requested: bool) -> f64 {
let multiplier = if explicitly_requested {
self.multiplier().max(Self::Normal.multiplier())
} else {
self.multiplier()
};
apply_scope_multiplier(score, multiplier)
}
}
fn apply_scope_multiplier(score: f64, multiplier: f64) -> f64 {
const POSITIVE_BOOST_RELEVANCE_FLOOR: f64 = 0.4;
if multiplier > 1.0 && score < POSITIVE_BOOST_RELEVANCE_FLOOR {
score
} else {
score * multiplier
}
}
/// Resolution result for a file-to-scope lookup.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScopeResolution {
/// Resolved priority tier.
pub priority: ScopePriority,
/// Whether this scope is in the default search set.
pub default: bool,
}
impl Default for ScopeResolution {
fn default() -> Self {
Self {
priority: ScopePriority::Normal,
default: true,
}
}
}
/// Glob patterns for a scope.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ScopeGlob {
/// Single glob string.
Single(String),
/// Array of glob strings.
Multiple(Vec<String>),
}
impl ScopeGlob {
/// Returns all glob patterns for this scope.
#[must_use]
pub fn patterns(&self) -> Vec<&str> {
match self {
Self::Single(g) => vec![g.as_str()],
Self::Multiple(g) => g.iter().map(String::as_str).collect(),
}
}
}
/// Scope name keyed map.
///
/// Uses `IndexMap` so iteration follows TOML declaration order — narrower or
/// more sensitive scopes declared above broader ones win when their globs
/// overlap (per spec §6.3).
pub type ScopesConfig = indexmap::IndexMap<String, Scope>;
/// A single scope definition.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Scope {
/// Glob pattern(s) matching files in this scope.
pub glob: ScopeGlob,
/// Priority tier for ranking.
pub priority: ScopePriority,
/// Whether this scope is included in the default search set.
pub default: bool,
/// Whether `talon inspect` reports findings for files in this scope.
///
/// Files in `inspect = false` scopes are still indexed and used for link
/// resolution (so a wikilink target in `daily/` still satisfies a wiki
/// note's link), but no findings are emitted with `from_path` in this
/// scope. Defaults to true.
#[serde(default = "default_true")]
pub inspect: bool,
}
const fn default_true() -> bool {
true
}
/// Full Talon runtime configuration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TalonConfig {
/// Host or standalone vault path.
pub vault_path: PathBuf,
/// `SQLite` index path.
pub db_path: PathBuf,
/// Path to the loaded config file (not serialized; injected at load time).
#[serde(skip)]
pub config_file_path: Option<PathBuf>,
/// Glob-style include patterns.
#[serde(default)]
pub include_patterns: Vec<String>,
/// Glob-style ignore patterns.
#[serde(default)]
pub ignore_patterns: Vec<String>,
/// Named API credentials referenced by capability blocks.
#[serde(default)]
pub credentials: CredentialsConfig,
/// Embedding endpoint configuration.
pub embedding: EmbeddingConfig,
/// Rerank endpoint configuration.
pub rerank: RerankConfig,
/// Chat endpoints for expansion and ask.
pub chat: ChatSection,
/// MCP runtime settings.
#[serde(default)]
pub mcp: McpConfig,
/// Named scopes for vault partitioning and ranking.
#[serde(default)]
pub scopes: ScopesConfig,
/// Search defaults and cache/client tunables.
#[serde(default)]
pub search: SearchConfig,
/// Lint settings (global ignore globs, etc.).
#[serde(default)]
pub inspect: InspectConfig,
/// Chunker settings from the `[indexer]` table.
#[serde(default, rename = "indexer")]
pub chunker: ChunkerConfig,
}
impl TalonConfig {
/// Returns the configured vault path.
#[must_use]
pub fn vault_path(&self) -> &Path {
&self.vault_path
}
/// Returns the configured database path.
#[must_use]
pub fn db_path(&self) -> &Path {
&self.db_path
}
/// Returns the resolved scope for a file path.
///
/// Walks scopes in declaration order; first match wins.
/// Returns the default scope if no scope matches.
#[must_use]
pub fn resolve_scope(&self, path: &Path) -> ScopeResolution {
for scope in self.scopes.values() {
if matches_path_glob(path, &scope.glob) {
return ScopeResolution {
priority: scope.priority,
default: scope.default,
};
}
}
// Unmatched files fall into synthetic unscoped bucket: normal priority, default true
ScopeResolution::default()
}
/// Returns the name of the scope this path resolves to, or `None` for the
/// synthetic unscoped bucket.
#[must_use]
pub fn resolve_scope_name(&self, path: &Path) -> Option<&str> {
for (name, scope) in &self.scopes {
if matches_path_glob(path, &scope.glob) {
return Some(name.as_str());
}
}
None
}
/// Returns true when `path` should be excluded from `inspect` findings.
///
/// Excludes paths that are either (1) in a scope with `inspect = false`, or
/// (2) matched by any glob in `[inspect].ignore`. The global ignore list takes
/// precedence — even paths in `inspect = true` scopes are excluded if they
/// match an ignore glob. Excluded paths remain in the index and continue
/// to satisfy link-target resolution.
#[must_use]
pub fn inspect_excluded(&self, path: &Path) -> bool {
let path_str = path.to_string_lossy();
let ignored = self
.inspect
.ignore
.iter()
.any(|glob| glob_matches_path(glob, path_str.as_ref()));
if ignored {
return true;
}
for scope in self.scopes.values() {
if matches_path_glob(path, &scope.glob) {
return !scope.inspect;
}
}
false
}
/// Returns the set of scope names that are in the default search set.
#[must_use]
pub fn default_scope_names(&self) -> Vec<&String> {
self.scopes
.iter()
.filter(|(_, s)| s.default)
.map(|(n, _)| n)
.collect()
}
/// Returns the scope with the given name, or an error.
///
/// # Errors
///
/// Returns [`TalonError::InvalidScope`] if the scope name is not found.
pub fn get_scope(&self, name: &str) -> Result<&Scope, crate::error::TalonError> {
self.scopes
.get(name)
.ok_or_else(|| crate::error::TalonError::InvalidScope {
name: name.to_string(),
})
}
}
/// Checks whether a path matches any of the glob patterns in a scope.
fn matches_path_glob(path: &Path, glob: &ScopeGlob) -> bool {
let path_str = path.to_string_lossy();
glob.patterns()
.iter()
.any(|pattern| glob_matches_path(pattern, path_str.as_ref()))
}
fn glob_matches_path(pattern: &str, path: &str) -> bool {
build_include_globset(&[pattern.to_string()]).is_ok_and(|set| set.is_match(path))
}