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
//! Worker spawners — global-search workers: view searches, per-hit context, whole-file fetches
//! (moved from app/workers.rs, plans/0021 M2 — a pure move).
//!
//! 0030: every spawn records JobStarted with its semantic identity
//! under a fresh operation id; the worker body runs inside
//! `in_operation` so backend transport records inherit the
//! correlation, and records its real outcome (counts, classification,
//! duration, send failure) on exit. Producers are free while tracing
//! is disabled.
use super::{App, fetch_blob_capped};
use crate::app::diagnostics;
use crate::components::global_search::SearchKind;
use crate::event::AppEvent;
use rootle_trace::EventKind;
use serde_json::json;
use std::time::Instant;
impl App {
pub(crate) fn spawn_view_search(
&self,
gen_id: crate::request::ViewGeneration,
kind: SearchKind,
query: String,
scope: String,
extension: String,
) {
let provider = self.provider.clone();
let tx = self.tx.clone();
let op = rootle_trace::operation_id();
rootle_trace::in_operation(op, || {
rootle_trace::record_with(EventKind::JobStarted, || {
json!({
"job": "view_search",
"gen": gen_id,
"kind": kind.slug(),
"query": diagnostics::text(&query),
"scope": scope,
"extension": diagnostics::text(&extension),
})
});
});
let ticket = self.outstanding.track();
std::thread::spawn(move || {
let _ticket = ticket;
rootle_trace::in_operation(op, || {
let started = rootle_trace::enabled().then(Instant::now);
// Streamed batches go straight to the event loop as they
// arrive (v1.3, plans/0011) — the worker stays blocked in
// the provider call until the final metadata reply. The
// sender is not Sync, so the sink holds it through a mutex
// (one lock per batch).
let batches = std::sync::atomic::AtomicU64::new(0);
let counter = &batches;
let sink_tx = std::sync::Mutex::new(tx.clone());
let on_hits = move |hits: Vec<crate::components::global_search::RawHit>| {
if rootle_trace::enabled() {
counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
if let Ok(tx) = sink_tx.lock()
&& tx
.send(AppEvent::GlobalSearchDelta { gen_id, hits })
.is_err()
{
rootle_trace::record_with(
EventKind::Error,
|| json!({"job":"view_search_delta","gen":gen_id,"send_failed":true}),
);
}
};
let event = match crate::components::global_search::run_view_search(
provider.as_ref(),
kind,
&query,
&scope,
&extension,
&on_hits,
) {
Ok(outcome) => {
rootle_trace::record_with(EventKind::JobFinished, || {
json!({
"job": "view_search",
"outcome": "ok",
"gen": gen_id,
"batches": batches.load(std::sync::atomic::Ordering::Relaxed),
"clipped": outcome.clipped,
"index_as_of": outcome.index_as_of.is_some(),
"client_filtered": outcome.client_filtered,
"unfiltered": outcome.unfiltered.len(),
"duration_us": started.map(|clock| clock.elapsed().as_micros()),
})
});
AppEvent::GlobalSearchResults {
gen_id,
hits: Vec::new(),
clipped: outcome.clipped,
index: outcome.index_as_of,
client_filtered: outcome.client_filtered,
unfiltered: outcome.unfiltered,
}
}
Err(error) => {
rootle_trace::record_with(EventKind::JobFinished, || {
json!({
"job": "view_search",
"outcome": "err",
"gen": gen_id,
"batches": batches.load(std::sync::atomic::Ordering::Relaxed),
"error": diagnostics::describe_error(&error),
"duration_us": started.map(|clock| clock.elapsed().as_micros()),
})
});
AppEvent::GlobalSearchFailed { gen_id, error }
}
};
if tx.send(event).is_err() {
rootle_trace::record_with(
EventKind::Error,
|| json!({"job": "view_search", "send_failed": true}),
);
}
});
});
}
pub(crate) fn spawn_search(&self, gen_id: crate::request::SearchGeneration) {
let Some(popup) = &self.popup else {
diagnostics::record_job_rejected("search", "no_search_popup", || json!({}));
return;
};
let query = popup.input.value();
let provider = self.provider.clone();
let tx = self.tx.clone();
let op = rootle_trace::operation_id();
rootle_trace::in_operation(op, || {
rootle_trace::record_with(EventKind::JobStarted, || {
json!({
"job": "search",
"gen": gen_id,
"query": diagnostics::text(&query),
})
});
});
let ticket = self.outstanding.track();
std::thread::spawn(move || {
let _ticket = ticket;
rootle_trace::in_operation(op, || {
let started = rootle_trace::enabled().then(Instant::now);
let event = match provider.search(&query) {
Ok(items) => {
rootle_trace::record_with(EventKind::JobFinished, || {
json!({
"job": "search",
"outcome": "ok",
"gen": gen_id,
"items": items.len(),
"duration_us": started.map(|clock| clock.elapsed().as_micros()),
})
});
AppEvent::SearchResults { gen_id, items }
}
Err(error) => {
rootle_trace::record_with(EventKind::JobFinished, || {
json!({
"job": "search",
"outcome": "err",
"gen": gen_id,
"error": diagnostics::describe_error(&error),
"duration_us": started.map(|clock| clock.elapsed().as_micros()),
})
});
AppEvent::SearchFailed { gen_id, error }
}
};
if tx.send(event).is_err() {
rootle_trace::record_with(
EventKind::Error,
|| json!({"job": "search", "send_failed": true}),
);
}
});
});
}
/// Lazy per-hit context (plans/0006 §1): fetch the selected bare
/// hit's blob and locate the query's context. Cache-first, so the
/// second visit of a hit is free.
pub(crate) fn spawn_hit_context(
&mut self,
gen_id: crate::request::ViewGeneration,
hit: crate::components::global_search::SearchHit,
query: String,
) {
let generation = self.requests.hit_context.issue();
let provider = self.provider.clone();
let tx = self.tx.clone();
let op = rootle_trace::operation_id();
rootle_trace::in_operation(op, || {
rootle_trace::record_with(EventKind::JobStarted, || {
json!({
"job": "hit_context",
"gen": gen_id,
"repo": hit.repo,
"path": hit.path,
"sha": hit.sha,
"query": diagnostics::text(&query),
})
});
});
let ticket = self.outstanding.track();
std::thread::spawn(move || {
let _ticket = ticket;
rootle_trace::in_operation(op, || {
let started = rootle_trace::enabled().then(Instant::now);
let sha = hit.sha.clone();
let event = match fetch_blob_capped(
provider.as_ref(),
&rootle_provider::RepoId::from(hit.repo.as_str()),
&rootle_provider::Sha::from(sha.as_str()),
) {
Ok(bytes) => {
let needles: Vec<String> =
query.split_whitespace().map(str::to_string).collect();
let located =
crate::components::global_search::locate_in_blob(&bytes, &needles);
match located {
Some((line, preview, count)) => {
rootle_trace::record_with(EventKind::JobFinished, || {
json!({
"job": "hit_context",
"outcome": "located",
"sha": sha,
"line": line,
"matches": count,
"bytes": bytes.len(),
"duration_us": started.map(|clock| clock.elapsed().as_micros()),
})
});
AppEvent::HitContextLoaded {
generation,
gen_id,
repo: hit.repo,
path: hit.path,
sha,
line,
preview,
match_count: count,
query,
}
}
None => {
// Blob fetched but nothing matched — the hit
// is unlocatable, not just pending (§4).
rootle_trace::record_with(EventKind::JobFinished, || {
json!({
"job": "hit_context",
"outcome": "unlocatable",
"sha": sha,
"bytes": bytes.len(),
"duration_us": started.map(|clock| clock.elapsed().as_micros()),
})
});
AppEvent::HitContextMissing {
generation,
gen_id,
sha,
}
}
}
}
Err(error) => {
rootle_trace::record_with(EventKind::JobFinished, || {
json!({
"job": "hit_context",
"outcome": "err",
"sha": sha,
"error": diagnostics::describe_error(&error),
"duration_us": started.map(|clock| clock.elapsed().as_micros()),
})
});
// Auth/throttle surface a status line; other kinds
// stay quiet (plans/0008 §2).
AppEvent::HitContextFailed {
generation,
gen_id,
sha,
error,
}
}
};
if tx.send(event).is_err() {
rootle_trace::record_with(
EventKind::Error,
|| json!({"job": "hit_context", "send_failed": true}),
);
}
});
});
}
/// The expanded file pane (plans/0012 M2): fetch the hit's whole
/// blob. Cache-first — the lazy context fetch usually warmed the
/// exact (repo, sha), so expanding a located hit is free.
pub(crate) fn spawn_hit_file(
&mut self,
gen_id: crate::request::ViewGeneration,
hit: crate::components::global_search::SearchHit,
) {
let generation = self.requests.hit_file.issue();
let provider = self.provider.clone();
let tx = self.tx.clone();
let op = rootle_trace::operation_id();
rootle_trace::in_operation(op, || {
rootle_trace::record_with(EventKind::JobStarted, || {
json!({
"job": "hit_file",
"gen": gen_id,
"repo": hit.repo,
"path": hit.path,
"sha": hit.sha,
})
});
});
let ticket = self.outstanding.track();
std::thread::spawn(move || {
let _ticket = ticket;
rootle_trace::in_operation(op, || {
let started = rootle_trace::enabled().then(Instant::now);
let sha = hit.sha.clone();
let event = match fetch_blob_capped(
provider.as_ref(),
&rootle_provider::RepoId::from(hit.repo.as_str()),
&rootle_provider::Sha::from(sha.as_str()),
) {
Ok(bytes) => {
rootle_trace::record_with(EventKind::JobFinished, || {
json!({
"job": "hit_file",
"outcome": "ok",
"sha": sha,
"bytes": bytes.len(),
"duration_us": started.map(|clock| clock.elapsed().as_micros()),
})
});
AppEvent::HitFileLoaded {
generation,
gen_id,
repo: hit.repo,
path: hit.path,
sha,
bytes,
}
}
Err(error) => {
rootle_trace::record_with(EventKind::JobFinished, || {
json!({
"job": "hit_file",
"outcome": "err",
"sha": sha,
"error": diagnostics::describe_error(&error),
"duration_us": started.map(|clock| clock.elapsed().as_micros()),
})
});
AppEvent::HitFileFailed {
generation,
gen_id,
sha,
error,
}
}
};
if tx.send(event).is_err() {
rootle_trace::record_with(
EventKind::Error,
|| json!({"job": "hit_file", "send_failed": true}),
);
}
});
});
}
}