khive_runtime/daemon/dispatch_trait.rs
1use super::*;
2
3// ── dispatch trait ────────────────────────────────────────────────────────────
4
5/// Transport-agnostic dispatch interface for the daemon server.
6///
7/// The MCP crate implements this by dispatching through the shared request body
8/// while honoring [`DaemonRequestFrame::from_wire`] (so subhandler visibility is
9/// gated by request origin, not by transport); any future transport can do the
10/// same.
11#[cfg(unix)]
12#[async_trait]
13pub trait DaemonDispatch: Clone + Send + Sync + 'static {
14 /// Explicit host-owned background domain, absent for registry-only test dispatchers.
15 fn background_resources(&self) -> Option<crate::resource_admission::BackgroundDomainControl> {
16 None
17 }
18
19 /// Named retained resources or unknown inventory that prevents idle exit.
20 /// An implementor must explicitly account for its resources before retiring.
21 fn idle_retirement_blockers(&self) -> Vec<String> {
22 vec!["dispatcher_resource_inventory_unknown".to_owned()]
23 }
24
25 /// Describe syntax and loaded catalog membership without dispatching.
26 fn plan(&self, ops: &str) -> String;
27
28 /// Dispatch a verb-DSL request string and return the rendered result.
29 ///
30 /// `from_wire` carries the origin discriminator from
31 /// [`DaemonRequestFrame::from_wire`]: when `true`, the implementor enforces
32 /// verb visibility (rejects `Visibility::Subhandler` verbs); when `false`,
33 /// the request is from a trusted operator surface and subhandlers pass.
34 ///
35 /// `identity` is the per-request identity context threaded from the frame
36 /// (ADR-096): `Some(..)` when serving a request forwarded over the
37 /// daemon socket (built from `frame.namespace` / `frame.actor_id` /
38 /// `frame.visible_namespaces` by the connection handler), `None` for any
39 /// other dispatch path. Implementors should mint the storage/gate token from
40 /// `identity` when present and fall back to their own construction-baked
41 /// identity when absent, so pure local (non-daemon) dispatch is unchanged.
42 #[allow(clippy::too_many_arguments)]
43 async fn dispatch(
44 &self,
45 ops: String,
46 presentation: Option<String>,
47 presentation_per_op: Option<Vec<Option<String>>>,
48 format: Option<String>,
49 format_per_op: Option<Vec<Option<String>>>,
50 from_wire: bool,
51 identity: Option<RequestIdentity>,
52 ) -> Result<String, String>;
53
54 /// Read-deadline ceiling for one request. The default is the operator
55 /// ceiling; an implementor that understands the request may grant a
56 /// longer bounded allowance (a long poll's declared wait plus a margin).
57 fn request_read_timeout(&self, _ops: &str) -> std::time::Duration {
58 khive_storage::request_read_timeout_from_env()
59 }
60
61 /// Preserve structured dispatch errors without breaking string-only implementors.
62 #[allow(clippy::too_many_arguments)]
63 async fn dispatch_with_error_detail(
64 &self,
65 ops: String,
66 presentation: Option<String>,
67 presentation_per_op: Option<Vec<Option<String>>>,
68 format: Option<String>,
69 format_per_op: Option<Vec<Option<String>>>,
70 from_wire: bool,
71 identity: Option<RequestIdentity>,
72 ) -> Result<String, DaemonDispatchError> {
73 self.dispatch(
74 ops,
75 presentation,
76 presentation_per_op,
77 format,
78 format_per_op,
79 from_wire,
80 identity,
81 )
82 .await
83 .map_err(|message| DaemonDispatchError::new(message, None))
84 }
85
86 /// The startup gate for the events daemon this daemon supervises, if any
87 /// ([`crate::events_split::EventsReadiness`]). A request that dispatches
88 /// verbs waits at it before dispatch and is refused, with nothing
89 /// committed, when it does not open; probe, plan, metrics and handover
90 /// frames never wait. `None`, the default, dispatches without waiting.
91 fn events_readiness(&self) -> Option<crate::events_split::EventsReadiness> {
92 None
93 }
94
95 /// Warm every pack's in-memory state (ANN indexes, etc.).
96 async fn warm_all(&self);
97
98 /// The namespace this dispatcher was configured for.
99 fn namespace(&self) -> &str;
100
101 /// Fingerprint of this dispatcher's resolved runtime config (packs, db
102 /// target, embedders). Used to reject forwarded requests from clients whose
103 /// config differs, so a restricted client cannot dispatch through a broader
104 /// daemon.
105 fn config_id(&self) -> &str;
106
107 /// Return the pool to use for background WAL checkpointing, if available.
108 ///
109 /// Implementors backed by a file-based SQLite database should return
110 /// `Some(pool_arc)`. In-memory or test dispatchers that have no pool
111 /// return `None` and the checkpoint task is not spawned.
112 ///
113 /// The default implementation returns `None`.
114 fn pool_for_checkpoint(&self) -> Option<Arc<ConnectionPool>> {
115 None
116 }
117
118 /// File-backed backend pools beyond [`Self::pool_for_checkpoint`]'s pool
119 /// (ADR-091 Amendment 3): one checkpoint task is spawned per entry here,
120 /// in addition to the one spawned for the primary pool, so a
121 /// multi-backend deployment gets PASSIVE/TRUNCATE checkpointing and
122 /// sidecar enumeration on every file-backed backend it wired, not only
123 /// the main one.
124 ///
125 /// The default implementation returns an empty `Vec` — an implementor
126 /// with only one backend (or none) needs no override.
127 fn secondary_pools_for_checkpoint(&self) -> Vec<Arc<ConnectionPool>> {
128 Vec::new()
129 }
130
131 /// Return the audit `EventStore` the checkpoint task should append
132 /// ADR-094 lifecycle events (`CheckpointOutcomeRecorded`) to, if any.
133 ///
134 /// Mirrors [`Self::pool_for_checkpoint`]'s default-`None` shape: an
135 /// implementor with no configured event store (or no pool at all) simply
136 /// gets a checkpoint task that never appends events — the checkpoint
137 /// task itself remains fully functional either way.
138 ///
139 /// The default implementation returns `None`.
140 fn event_store_for_checkpoint(&self) -> Option<Arc<dyn khive_storage::EventStore>> {
141 None
142 }
143}
144
145#[cfg(unix)]
146pub(super) struct CheckpointTaskSpec {
147 pub(super) pool: Arc<ConnectionPool>,
148 pub(super) lifecycle_owner: Option<CheckpointLifecycleOwner>,
149 pub(super) is_main: bool,
150}
151
152/// Build checkpoint-task fan-out and designate one lifecycle owner.
153///
154/// The main checkpoint task owns lifecycle emission when it exists. If the
155/// main backend is in-memory and therefore has no checkpoint task, the first
156/// file-backed secondary owns emission instead. All remaining tasks are
157/// explicit non-owners.
158#[cfg(unix)]
159pub(super) fn checkpoint_task_specs(
160 main_pool: Option<Arc<ConnectionPool>>,
161 secondary_pools: Vec<Arc<ConnectionPool>>,
162 event_store: Option<Arc<dyn khive_storage::EventStore>>,
163 namespace: String,
164) -> Vec<CheckpointTaskSpec> {
165 let mut tasks = Vec::with_capacity(usize::from(main_pool.is_some()) + secondary_pools.len());
166 if let Some(pool) = main_pool {
167 tasks.push(CheckpointTaskSpec {
168 pool,
169 lifecycle_owner: None,
170 is_main: true,
171 });
172 }
173 tasks.extend(secondary_pools.into_iter().map(|pool| CheckpointTaskSpec {
174 pool,
175 lifecycle_owner: None,
176 is_main: false,
177 }));
178
179 if let (Some(task), Some(event_store)) = (tasks.first_mut(), event_store) {
180 task.lifecycle_owner = Some(CheckpointLifecycleOwner::new(event_store, namespace));
181 }
182 tasks
183}
184
185// ── tracked background tasks ─────────────────────────────────────────────────
186//
187// Pack handlers (e.g. memory.recall's ADR-081 serve-ledger append) fire
188// fire-and-forget `tokio::spawn`ed work off the response path so the caller
189// never waits on a cross-pack dispatch or a SQL write. Left untracked, that
190// work is invisible to `drain()`: a SIGTERM landing between the response
191// returning and the spawned task completing can abort it mid-flight with no
192// log and no row. `track_background_task` gives such spawns a process-wide
193// presence that `drain()` waits on, exactly like the `active` counter does
194// for in-flight connections: the caller still only pays for the spawn +
195// counter increment, never the task's own work.
196/// Set once by the boot path that takes the daemon role, and never cleared: a
197/// process that is not the warm daemon has no path to becoming one except exec.
198pub(super) static WARM_INDEX_HOST: std::sync::atomic::AtomicBool =
199 std::sync::atomic::AtomicBool::new(false);
200
201/// Declare this process the warm index host. Called by the serve path as soon as
202/// the daemon role is decided, before any runtime is built, so nothing warms
203/// under the wrong answer.
204pub fn mark_warm_index_host() {
205 WARM_INDEX_HOST.store(true, std::sync::atomic::Ordering::Release);
206}
207
208/// Whether this process is the warm index host.
209///
210/// Building an ANN index from the full corpus is minutes of CPU and hundreds of
211/// megabytes of segment rewrite, and it pays for itself only across a process
212/// that outlives the request. A short-lived client that does it pays the whole
213/// cost, discards the result at exit, and publishes a checkpoint that every
214/// other reader on the root must then re-read. Consumers use this to decide
215/// whether to build or to serve degraded and let the daemon build.
216pub fn is_warm_index_host() -> bool {
217 WARM_INDEX_HOST.load(std::sync::atomic::Ordering::Acquire)
218}
219
220pub(super) static BACKGROUND_TASKS: std::sync::OnceLock<Arc<std::sync::atomic::AtomicUsize>> =
221 std::sync::OnceLock::new();
222
223pub(super) fn background_tasks() -> &'static Arc<std::sync::atomic::AtomicUsize> {
224 BACKGROUND_TASKS.get_or_init(|| Arc::new(std::sync::atomic::AtomicUsize::new(0)))
225}
226
227/// Decrements the shared background-task counter from `Drop`, so the count
228/// comes back down whether the tracked future returns normally, panics, or
229/// is cancelled — a plain post-`await` `fetch_sub` only covers the return
230/// path and leaks the count forever on a panic, since unwinding skips every
231/// statement after the panic point.
232// ── outstanding background-task names ────────────────────────────────────────
233//
234// Names of the tasks the background counter is currently holding, so a drain
235// timeout can say which ones held it open instead of printing a bare count.
236// Registered and released at exactly the points the counter is incremented and
237// decremented, and in the order that keeps the counter authoritative: the name
238// goes in after the increment and comes out before the decrement, so a reported
239// name always belongs to a task the counter already holds. The reverse ordering
240// would let the warning name a task that had already finished, which is the one
241// reading that would send someone looking in the wrong place.
242pub(super) static BACKGROUND_TASK_NAMES: std::sync::OnceLock<
243 std::sync::Mutex<std::collections::HashMap<&'static str, usize>>,
244> = std::sync::OnceLock::new();
245
246pub(super) fn background_task_names_registry(
247) -> &'static std::sync::Mutex<std::collections::HashMap<&'static str, usize>> {
248 BACKGROUND_TASK_NAMES.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()))
249}
250
251/// Name recorded for tasks spawned through the unnamed entry points, which
252/// stay on the public API of a published crate.
253pub const UNNAMED_BACKGROUND_TASK: &str = "unnamed";
254
255pub(super) fn register_background_task_name(name: &'static str) {
256 let mut names = background_task_names_registry()
257 .lock()
258 .unwrap_or_else(std::sync::PoisonError::into_inner);
259 *names.entry(name).or_insert(0) += 1;
260}
261
262pub(super) fn release_background_task_name(name: &'static str) {
263 let mut names = background_task_names_registry()
264 .lock()
265 .unwrap_or_else(std::sync::PoisonError::into_inner);
266 if let Some(count) = names.get_mut(name) {
267 *count -= 1;
268 if *count == 0 {
269 names.remove(name);
270 }
271 }
272}
273
274/// Names of the in-flight tracked background tasks, sorted and deduplicated.
275/// A diagnostic beside [`background_task_count`], never a substitute for it:
276/// the count is what drain waits on.
277pub fn background_task_names() -> Vec<String> {
278 let names = background_task_names_registry()
279 .lock()
280 .unwrap_or_else(std::sync::PoisonError::into_inner);
281 let mut out: Vec<String> = names.keys().map(|name| (*name).to_string()).collect();
282 out.sort();
283 out
284}
285
286#[cfg(unix)]
287pub(super) fn idle_retirement_blockers<D: DaemonDispatch>(dispatcher: &D) -> Vec<String> {
288 let mut blockers = dispatcher.idle_retirement_blockers();
289 if !khive_storage::tx_registry::snapshot().is_empty() {
290 blockers.push("open_sql_transaction".to_owned());
291 }
292 blockers.extend(
293 active_phase_names()
294 .into_iter()
295 .map(|name| format!("active_phase:{name}")),
296 );
297 let count = background_task_count();
298 let names = background_task_names_registry()
299 .lock()
300 .unwrap_or_else(std::sync::PoisonError::into_inner);
301 if names.values().sum::<usize>() != count {
302 blockers.push("tracked_worker_inventory_unsettled".to_owned());
303 }
304 // Only these inspected loops maintain replaceable caches/checkpoint state.
305 // Their tracked lifetime still participates in the final drain.
306 for name in names.keys() {
307 if !matches!(
308 *name,
309 "wal_checkpoint" | "memory_ann_rotation_watch" | "knowledge_ann_rotation_watch"
310 ) {
311 blockers.push(format!("unsettled_worker:{name}"));
312 }
313 }
314 blockers.sort();
315 blockers.dedup();
316 blockers
317}
318
319#[cfg(unix)]
320pub(super) async fn wait_for_idle<D: DaemonDispatch>(dispatcher: &D, lifecycle: &DaemonLifecycle) {
321 if lifecycle.options.lifetime == DaemonLifetime::Persistent {
322 std::future::pending::<()>().await;
323 }
324 loop {
325 if lifecycle.try_idle(|| idle_retirement_blockers(dispatcher)) {
326 return;
327 }
328 tokio::time::sleep(std::time::Duration::from_millis(100)).await;
329 }
330}
331
332pub(crate) struct BackgroundTaskGuard {
333 pub(super) counter: Arc<std::sync::atomic::AtomicUsize>,
334 pub(super) name: &'static str,
335}
336
337impl Drop for BackgroundTaskGuard {
338 fn drop(&mut self) {
339 release_background_task_name(self.name);
340 self.counter
341 .fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
342 }
343}
344
345/// Spawn a task that daemon shutdown's `drain()` waits for and return its join
346/// handle. Retaining the handle lets boot coordinators form an explicit barrier;
347/// dropping it deliberately detaches the task while the background counter still
348/// keeps daemon drain aware of its lifetime. The decrement happens via
349/// `BackgroundTaskGuard`'s `Drop`, including panic and cancellation paths.
350pub fn spawn_tracked_task<F, T>(fut: F) -> tokio::task::JoinHandle<T>
351where
352 F: std::future::Future<Output = T> + Send + 'static,
353 T: Send + 'static,
354{
355 spawn_named_tracked_task(UNNAMED_BACKGROUND_TASK, fut)
356}
357
358/// [`spawn_tracked_task`] with a name that a drain timeout can print.
359///
360/// The name is a short static string describing the task, never a formatted or
361/// caller-supplied value: it is read by an operator staring at a shutdown that
362/// would not finish, so it is a label for a call site, not a record of one
363/// occurrence.
364pub fn spawn_named_tracked_task<F, T>(name: &'static str, fut: F) -> tokio::task::JoinHandle<T>
365where
366 F: std::future::Future<Output = T> + Send + 'static,
367 T: Send + 'static,
368{
369 let guard = register_background_task(name);
370 tokio::spawn(async move {
371 let _guard = guard;
372 fut.await
373 })
374}
375
376/// Register one real tracked job before its executor/queue handoff.
377/// Shared wrappers may hold this same RAII guard through actual child exit.
378pub(crate) fn register_background_task(name: &'static str) -> BackgroundTaskGuard {
379 background_tasks().fetch_add(1, std::sync::atomic::Ordering::SeqCst);
380 register_background_task_name(name);
381 BackgroundTaskGuard {
382 counter: background_tasks().clone(),
383 name,
384 }
385}
386
387/// Spawn a fire-and-forget task through [`spawn_tracked_task`].
388///
389/// Callers that need a boot or shutdown barrier should retain and await the
390/// returned handle from [`spawn_tracked_task`] instead of detaching it here.
391pub fn track_background_task<F>(fut: F)
392where
393 F: std::future::Future<Output = ()> + Send + 'static,
394{
395 track_named_background_task(UNNAMED_BACKGROUND_TASK, fut);
396}
397
398/// [`track_background_task`] with a name that a drain timeout can print. See
399/// [`spawn_named_tracked_task`] for what belongs in the name.
400pub fn track_named_background_task<F>(name: &'static str, fut: F)
401where
402 F: std::future::Future<Output = ()> + Send + 'static,
403{
404 drop(spawn_named_tracked_task(name, fut));
405}
406
407/// Current count of in-flight tasks started via [`track_background_task`].
408/// Exposed for tests; `drain()` reads the shared counter directly.
409pub fn background_task_count() -> usize {
410 background_tasks().load(std::sync::atomic::Ordering::SeqCst)
411}
412
413/// Process-wide daemon shutdown signal (ADR-119).
414///
415/// Cancelled exactly once, when the daemon's unified shutdown future resolves
416/// — before `drain()` begins waiting on tracked tasks — so long-running
417/// daemon components supervised outside this module observe shutdown through
418/// the same path the daemon itself does, rather than inventing their own.
419/// Clones share the underlying token; child tokens derived from it are
420/// cancelled transitively.
421///
422/// In non-daemon processes the token simply never fires.
423pub fn daemon_shutdown_token() -> tokio_util::sync::CancellationToken {
424 static TOKEN: std::sync::OnceLock<tokio_util::sync::CancellationToken> =
425 std::sync::OnceLock::new();
426 TOKEN
427 .get_or_init(tokio_util::sync::CancellationToken::new)
428 .clone()
429}
430
431// ── active background phase names (ADR-103) ──────────────────────────────────
432//
433// A lightweight, best-effort process-wide gauge of which named background
434// phases (e.g. `ann_warm`) are in flight right now, read by `comm.health`'s
435// resource self-report so a caller can see "what is the daemon doing" at a
436// glance without correlating timestamps across the event log itself. Counted
437// per name rather than boolean, since more than one occurrence of the same
438// named phase can legitimately overlap (e.g. two embedding models warming
439// concurrently) — the name only drops out of the reported set once every
440// concurrent occurrence has ended.
441pub(super) static ACTIVE_PHASES: std::sync::OnceLock<
442 std::sync::Mutex<std::collections::HashMap<String, usize>>,
443> = std::sync::OnceLock::new();
444
445pub(super) fn active_phases() -> &'static std::sync::Mutex<std::collections::HashMap<String, usize>>
446{
447 ACTIVE_PHASES.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()))
448}
449
450/// RAII guard for one occurrence of a named background phase. Increments the
451/// phase's count on creation (see [`register_active_phase`]); decrements on
452/// `Drop`, so the count comes back down whether the guarded work returns
453/// normally, panics, or is cancelled — the same rationale as
454/// `BackgroundTaskGuard` above.
455pub struct PhaseGuard {
456 pub(super) name: String,
457}
458
459impl Drop for PhaseGuard {
460 fn drop(&mut self) {
461 let mut map = active_phases()
462 .lock()
463 .unwrap_or_else(std::sync::PoisonError::into_inner);
464 if let Some(count) = map.get_mut(&self.name) {
465 *count -= 1;
466 if *count == 0 {
467 map.remove(&self.name);
468 }
469 }
470 }
471}
472
473/// Register one occurrence of a named background phase as currently active.
474/// Returns a guard: drop it (or let it fall out of scope) when the phase
475/// ends. Best-effort process-wide gauge only, read by `comm.health` — never
476/// load-bearing for correctness.
477pub fn register_active_phase(name: &str) -> PhaseGuard {
478 let mut map = active_phases()
479 .lock()
480 .unwrap_or_else(std::sync::PoisonError::into_inner);
481 *map.entry(name.to_string()).or_insert(0) += 1;
482 PhaseGuard {
483 name: name.to_string(),
484 }
485}
486
487/// Currently active background-phase names, sorted for deterministic output.
488/// Empty when no tracked phase is in flight.
489pub fn active_phase_names() -> Vec<String> {
490 let map = active_phases()
491 .lock()
492 .unwrap_or_else(std::sync::PoisonError::into_inner);
493 let mut names: Vec<String> = map.keys().cloned().collect();
494 names.sort();
495 names
496}
497
498// ── server ────────────────────────────────────────────────────────────────────
499
500/// Build a point-in-time [`MetricsSnapshot`] of this process's server-side
501/// gauges. Called only from `handle_conn`'s `metrics_only` arm — a
502/// process-global, read-only assembly with no side effects of its own.
503/// See `docs/api/daemon.md#build_metrics_snapshot` for where each gauge is sourced
504/// from and why.
505#[cfg(unix)]
506pub(super) fn build_metrics_snapshot<D: DaemonDispatch>(dispatcher: &D) -> MetricsSnapshot {
507 let open_tx_count = khive_storage::tx_registry::snapshot().len();
508 // ADR-091 Amendment 3: deliberately the process-wide aggregate, not a
509 // backend-attributed view — this gauge reports "oldest pinned tx in this
510 // process" across every wired backend, not an attribution claim about
511 // which database it belongs to. Attribution consumers (the session sweep,
512 // the per-backend checkpoint tasks) use the scoped `oldest_for` views in
513 // khive-db instead.
514 let (oldest_pinned_tx_micros, oldest_pinned_tx_label) =
515 match khive_storage::tx_registry::oldest() {
516 Some((_id, age, label)) => (Some(age.as_micros() as u64), label),
517 None => (None, None),
518 };
519
520 let checkpoint_pool = dispatcher.pool_for_checkpoint();
521 let mut secondary_index = 0;
522 let wal_checkpoint_stores = checkpoint_task_specs(
523 checkpoint_pool.clone(),
524 dispatcher.secondary_pools_for_checkpoint(),
525 None,
526 String::new(),
527 )
528 .into_iter()
529 .map(|task| {
530 let (store_id, role) = if task.is_main {
531 ("main".to_string(), "main".to_string())
532 } else {
533 let store_id = format!("secondary:{secondary_index}");
534 secondary_index += 1;
535 (store_id, "secondary".to_string())
536 };
537 CheckpointStoreMetrics {
538 store_id,
539 role,
540 database: task
541 .pool
542 .canonical_path()
543 .and_then(std::path::Path::file_name)
544 .map(|name| name.to_string_lossy().into_owned()),
545 timing: khive_db::checkpoint::checkpoint_timing(&task.pool),
546 }
547 })
548 .collect();
549 let routine_wal = checkpoint_pool
550 .as_deref()
551 .and_then(khive_db::checkpoint::routine_wal_observation);
552 let writer_stages = checkpoint_pool
553 .as_deref()
554 .and_then(khive_db::writer_task::last_writer_stage_observation);
555 let (write_queue_depth, write_queue_capacity) = checkpoint_pool
556 .as_ref()
557 .and_then(|pool| pool.writer_task_handle().ok().flatten())
558 .map(|handle| (Some(handle.queue_depth()), Some(handle.capacity())))
559 .unwrap_or((None, None));
560
561 MetricsSnapshot {
562 lifecycle: None,
563 wal_pages: routine_wal.as_ref().map(|sample| sample.log_frames),
564 wal_log_frames: routine_wal.as_ref().map(|sample| sample.log_frames),
565 wal_checkpointed_frames: routine_wal
566 .as_ref()
567 .map(|sample| sample.checkpointed_frames),
568 wal_pending_frames: routine_wal.as_ref().map(|sample| sample.pending_frames),
569 wal_physical_bytes: routine_wal
570 .as_ref()
571 .and_then(|sample| sample.physical_wal_bytes),
572 wal_observed_at_unix_ms: routine_wal
573 .as_ref()
574 .map(|sample| sample.observed_at_unix_ms),
575 wal_checkpoint_stores,
576 wal_truncate_attempts: khive_db::checkpoint::truncate_attempts(),
577 wal_truncate_consecutive_failures: khive_db::checkpoint::truncate_consecutive_failures(),
578 wal_checkpoint_skipped_ticks: khive_db::checkpoint::checkpoint_skipped_ticks(),
579 wal_checkpoint_consecutive_skips: khive_db::checkpoint::checkpoint_consecutive_skips(),
580 wal_checkpoint_last_skip_wal_pages: khive_db::checkpoint::checkpoint_last_skip_wal_pages(),
581 oldest_pinned_tx_micros,
582 oldest_pinned_tx_label,
583 open_tx_count,
584 write_queue_depth,
585 write_queue_capacity,
586 write_last_queue_wait_micros: writer_stages
587 .as_ref()
588 .map(|sample| sample.queue_wait_micros),
589 write_last_transaction_acquire_micros: writer_stages
590 .as_ref()
591 .map(|sample| sample.transaction_acquire_micros),
592 write_last_body_micros: writer_stages.as_ref().map(|sample| sample.body_micros),
593 write_last_commit_micros: writer_stages.as_ref().map(|sample| sample.commit_micros),
594 write_last_total_micros: writer_stages.as_ref().map(|sample| sample.total_micros),
595 write_last_observed_at_unix_ms: writer_stages
596 .as_ref()
597 .map(|sample| sample.observed_at_unix_ms),
598 connections: None,
599 recall_ledger: Some(recall_ledger_snapshot()),
600 }
601}