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
//! Thesaurus capability for `TerraphimService` (knowledge-graph thesaurus
//! loading and building). Split from lib.rs as part of the Gitea #1910
//! god-file decomposition; behaviour unchanged. Methods remain on
//! `TerraphimService` via an additional impl block, so the public API is identical.
use ahash::AHashMap;
use terraphim_automata::builder::{Logseq, ThesaurusBuilder, compute_kg_source_hash};
use terraphim_automata::load_thesaurus;
use terraphim_config::ConfigState;
use terraphim_middleware::thesaurus::build_thesaurus_from_haystack;
use terraphim_persistence::Persistable;
use terraphim_rolegraph::{RoleGraph, RoleGraphSync};
use terraphim_types::{RoleName, SearchQuery, Thesaurus};
use super::{Result, ServiceError, TerraphimService};
impl TerraphimService {
/// Build a thesaurus from the haystack and update the knowledge graph automata URL
pub(crate) async fn build_thesaurus(&mut self, search_query: &SearchQuery) -> Result<()> {
Ok(build_thesaurus_from_haystack(&mut self.config_state, search_query).await?)
}
/// load thesaurus from config object and if absent make sure it's loaded from automata_url
pub async fn ensure_thesaurus_loaded(&mut self, role_name: &RoleName) -> Result<Thesaurus> {
async fn load_thesaurus_from_automata_path(
config_state: &ConfigState,
role_name: &RoleName,
rolegraphs: &mut AHashMap<RoleName, RoleGraphSync>,
) -> Result<Thesaurus> {
// CRITICAL: clone the role out, then drop the config lock before
// doing I/O. Holding the lock across the network/disk operations
// below blocks every other endpoint that touches /config (e.g.
// GET /config from `roles select`, `roles list`, etc.) for the
// duration of the thesaurus load + persistence + RoleGraph build.
let role = {
let config = config_state.config.lock().await;
let Some(role) = config.roles.get(role_name).cloned() else {
return Err(ServiceError::Config(format!(
"Role '{}' not found in config",
role_name
)));
};
role
};
if let Some(kg) = &role.kg {
if let Some(automata_path) = &kg.automata_path {
log::info!("Loading Role `{}` - URL: {:?}", role_name, automata_path);
// Try to load from automata path first
match load_thesaurus(automata_path).await {
Ok(mut thesaurus) => {
log::info!("Successfully loaded thesaurus from automata path");
// Save thesaurus to persistence to ensure it's available for future loads
match thesaurus.save().await {
Ok(_) => {
log::info!(
"Thesaurus for role `{}` saved to persistence",
role_name
);
// Reload from persistence to get canonical version
match thesaurus.load().await {
Ok(persisted_thesaurus) => {
thesaurus = persisted_thesaurus;
log::debug!("Reloaded thesaurus from persistence");
}
Err(e) => {
log::warn!(
"Failed to reload thesaurus from persistence, using in-memory version: {:?}",
e
);
}
}
}
Err(e) => {
log::warn!("Failed to save thesaurus to persistence: {:?}", e);
}
}
let rolegraph =
RoleGraph::new(role_name.clone(), thesaurus.clone()).await;
match rolegraph {
Ok(rolegraph) => {
let rolegraph_value = RoleGraphSync::from(rolegraph);
rolegraphs.insert(role_name.clone(), rolegraph_value);
}
Err(e) => {
log::error!("Failed to update role and thesaurus: {:?}", e)
}
}
Ok(thesaurus)
}
Err(e) => {
log::warn!("Failed to load thesaurus from automata path: {:?}", e);
// Fallback to building from local KG if available
if let Some(kg_local) = &kg.knowledge_graph_local {
log::info!(
"Fallback: building thesaurus from local KG for role {}",
role_name
);
let logseq_builder = Logseq::default();
match logseq_builder
.build(
role_name.as_lowercase().to_string(),
kg_local.path.clone(),
)
.await
{
Ok(mut thesaurus) => {
// Save thesaurus to persistence to ensure it's available for future loads
match thesaurus.save().await {
Ok(_) => {
log::info!(
"Fallback thesaurus for role `{}` saved to persistence",
role_name
);
// Reload from persistence to get canonical version
match thesaurus.load().await {
Ok(persisted_thesaurus) => {
thesaurus = persisted_thesaurus;
log::debug!(
"Reloaded fallback thesaurus from persistence"
);
}
Err(e) => {
log::warn!(
"Failed to reload fallback thesaurus from persistence, using in-memory version: {:?}",
e
);
}
}
}
Err(e) => {
log::warn!(
"Failed to save fallback thesaurus to persistence: {:?}",
e
);
}
}
let rolegraph =
RoleGraph::new(role_name.clone(), thesaurus.clone())
.await;
match rolegraph {
Ok(rolegraph) => {
let rolegraph_value =
RoleGraphSync::from(rolegraph);
rolegraphs
.insert(role_name.clone(), rolegraph_value);
}
Err(e) => log::error!(
"Failed to update role and thesaurus: {:?}",
e
),
}
Ok(thesaurus)
}
Err(e) => {
// Check if error is "file not found" (expected for optional files)
// and downgrade log level from ERROR to DEBUG
let is_file_not_found =
e.to_string().contains("file not found")
|| e.to_string().contains("not found:");
if is_file_not_found {
log::debug!(
"Failed to build thesaurus from local KG (optional file not found) for role {}: {:?}",
role_name,
e
);
} else {
log::error!(
"Failed to build thesaurus from local KG for role {}: {:?}",
role_name,
e
);
}
Err(ServiceError::Config(
"Failed to load or build thesaurus".into(),
))
}
}
} else {
log::warn!(
"No fallback available for role {}: no local KG path configured, returning empty thesaurus",
role_name
);
Ok(Thesaurus::new(role_name.as_lowercase().to_string()))
}
}
}
} else if let Some(kg_local) = &kg.knowledge_graph_local {
// Build thesaurus from local KG
log::info!(
"Role {} has no automata_path, building thesaurus from local KG files at {:?}",
role_name,
kg_local.path
);
let logseq_builder = Logseq::default();
match logseq_builder
.build(role_name.as_lowercase().to_string(), kg_local.path.clone())
.await
{
Ok(mut thesaurus) => {
log::info!(
"Successfully built thesaurus from local KG for role {}",
role_name
);
// Save thesaurus to persistence to ensure it's available for future loads
match thesaurus.save().await {
Ok(_) => {
log::info!(
"Local KG thesaurus for role `{}` saved to persistence",
role_name
);
// Reload from persistence to get canonical version
match thesaurus.load().await {
Ok(persisted_thesaurus) => {
log::info!(
"Reloaded local KG thesaurus from persistence: {} entries",
persisted_thesaurus.len()
);
thesaurus = persisted_thesaurus;
}
Err(e) => {
log::warn!(
"Failed to reload local KG thesaurus from persistence, using in-memory version: {:?}",
e
);
}
}
}
Err(e) => {
log::warn!(
"Failed to save local KG thesaurus to persistence: {:?}",
e
);
}
}
let rolegraph =
RoleGraph::new(role_name.clone(), thesaurus.clone()).await;
match rolegraph {
Ok(rolegraph) => {
let rolegraph_value = RoleGraphSync::from(rolegraph);
rolegraphs.insert(role_name.clone(), rolegraph_value);
}
Err(e) => {
log::error!("Failed to update role and thesaurus: {:?}", e)
}
}
Ok(thesaurus)
}
Err(e) => {
// Check if error is "file not found" (expected for optional files)
// and downgrade log level from ERROR to DEBUG
let is_file_not_found = e.to_string().contains("file not found");
if is_file_not_found {
log::debug!(
"Failed to build thesaurus from local KG (optional file not found) for role {}: {:?}",
role_name,
e
);
} else {
log::error!(
"Failed to build thesaurus from local KG for role {}: {:?}",
role_name,
e
);
}
Err(ServiceError::Config(format!(
"Failed to build thesaurus from local KG for role {}: {}",
role_name, e
)))
}
}
} else {
log::warn!(
"Role {} is configured for TerraphimGraph but has neither automata_path nor knowledge_graph_local defined.",
role_name
);
if let Some(kg_local) = &kg.knowledge_graph_local {
// Build thesaurus from local KG files during startup
log::info!(
"Building thesaurus from local KG files for role {} at {:?}",
role_name,
kg_local.path
);
let logseq_builder = Logseq::default();
match logseq_builder
.build(role_name.as_lowercase().to_string(), kg_local.path.clone())
.await
{
Ok(mut thesaurus) => {
log::info!(
"Successfully built thesaurus from local KG for role {}",
role_name
);
// Save thesaurus to persistence to ensure it's available for future loads
match thesaurus.save().await {
Ok(_) => {
log::info!(
"No-automata thesaurus for role `{}` saved to persistence",
role_name
);
// Reload from persistence to get canonical version
match thesaurus.load().await {
Ok(persisted_thesaurus) => {
thesaurus = persisted_thesaurus;
log::debug!(
"Reloaded no-automata thesaurus from persistence"
);
}
Err(e) => {
log::warn!(
"Failed to reload no-automata thesaurus from persistence, using in-memory version: {:?}",
e
);
}
}
}
Err(e) => {
log::warn!(
"Failed to save no-automata thesaurus to persistence: {:?}",
e
);
}
}
let rolegraph =
RoleGraph::new(role_name.clone(), thesaurus.clone()).await;
match rolegraph {
Ok(rolegraph) => {
let rolegraph_value = RoleGraphSync::from(rolegraph);
rolegraphs.insert(role_name.clone(), rolegraph_value);
}
Err(e) => {
// Check if error is "file not found" (expected for optional files)
// and downgrade log level from ERROR to DEBUG
let is_file_not_found =
e.to_string().contains("file not found");
if is_file_not_found {
log::debug!(
"Failed to update role and thesaurus (optional file not found): {:?}",
e
);
} else {
log::error!(
"Failed to update role and thesaurus: {:?}",
e
);
}
}
}
Ok(thesaurus)
}
Err(e) => {
log::error!(
"Failed to build thesaurus from local KG for role {}: {:?}",
role_name,
e
);
Err(ServiceError::Config(
"Failed to build thesaurus from local KG".into(),
))
}
}
} else {
log::debug!(
"Role '{}' has no local KG path, returning empty thesaurus",
role_name
);
Ok(Thesaurus::new(role_name.as_lowercase().to_string()))
}
}
} else {
log::debug!("Role '{}' has no knowledge graph configured", role_name);
Err(ServiceError::Config(format!(
"Knowledge graph not configured for role '{}'",
role_name
)))
}
}
log::debug!("Loading thesaurus for role: {}", role_name);
log::debug!("Role keys {:?}", self.config_state.roles.keys());
if let Some(rolegraph_value) = self.config_state.roles.get(role_name) {
let thesaurus_result = rolegraph_value.lock().await.thesaurus.clone().load().await;
match thesaurus_result {
Ok(thesaurus) => {
log::debug!("Thesaurus loaded: {:?}", thesaurus);
log::info!("Rolegraph loaded: for role name {:?}", role_name);
// Check if the cached thesaurus is stale by comparing source hashes
let is_stale = if let Some(ref cached_hash) = thesaurus.source_hash {
let role = {
let config = self.config_state.config.lock().await;
config.roles.get(role_name).cloned()
};
if let Some(role) = role {
if let Some(ref kg) = role.kg {
if let Some(ref kg_local) = kg.knowledge_graph_local {
match compute_kg_source_hash(&kg_local.path) {
Ok(Some(current_hash)) => {
let stale = current_hash != *cached_hash;
if stale {
log::info!(
"Thesaurus cache stale for role '{}': hash mismatch (cached {} != current {})",
role_name,
cached_hash,
current_hash
);
}
stale
}
Ok(None) => {
log::debug!(
"No markdown files found in KG path {:?}",
kg_local.path
);
false
}
Err(e) => {
log::warn!(
"Failed to compute source hash for role '{}': {}",
role_name,
e
);
false
}
}
} else {
false
}
} else {
false
}
} else {
false
}
} else {
log::debug!(
"No source_hash in cached thesaurus for role '{}'",
role_name
);
false
};
if is_stale {
let mut rolegraphs = self.config_state.roles.clone();
let result = load_thesaurus_from_automata_path(
&self.config_state,
role_name,
&mut rolegraphs,
)
.await;
if result.is_ok()
&& let Some(updated_rolegraph) = rolegraphs.get(role_name)
{
self.config_state
.roles
.insert(role_name.clone(), updated_rolegraph.clone());
log::info!(
"Updated config_state with rebuilt rolegraph for role: {}",
role_name
);
}
result
} else {
Ok(thesaurus)
}
}
Err(e) => {
// Check if error is "file not found" (expected for optional files)
// and downgrade log level from ERROR to DEBUG
let is_file_not_found = e.to_string().contains("file not found")
|| e.to_string().contains("not found:");
if is_file_not_found {
log::debug!("Thesaurus file not found (optional): {:?}", e);
} else {
log::error!("Failed to load thesaurus: {:?}", e);
}
// Try to build thesaurus from KG and update the config_state directly
let mut rolegraphs = self.config_state.roles.clone();
let result = load_thesaurus_from_automata_path(
&self.config_state,
role_name,
&mut rolegraphs,
)
.await;
// Update the actual config_state with the new rolegraph
if result.is_ok()
&& let Some(updated_rolegraph) = rolegraphs.get(role_name)
{
self.config_state
.roles
.insert(role_name.clone(), updated_rolegraph.clone());
log::info!(
"Updated config_state with new rolegraph for role: {}",
role_name
);
}
result
}
}
} else {
// Role not found, try to build from KG
let mut rolegraphs = self.config_state.roles.clone();
let result =
load_thesaurus_from_automata_path(&self.config_state, role_name, &mut rolegraphs)
.await;
// Update the actual config_state with the new rolegraph
if result.is_ok()
&& let Some(new_rolegraph) = rolegraphs.get(role_name)
{
self.config_state
.roles
.insert(role_name.clone(), new_rolegraph.clone());
log::info!(
"Added new rolegraph to config_state for role: {}",
role_name
);
}
result
}
}
}