mimir_librarian/lib.rs
1//! `mimir-librarian` — Mimir librarian. Ingests prose memory drafts,
2//! sanitises them (separates observations from directives), structures
3//! them into canonical Mimir Lisp, and commits them to the canonical
4//! log via the in-process `mimir_core::Pipeline`.
5//!
6//! Category 1 of the 2026-04-21 Rolls Royce engineering plan
7//! for librarian-governed draft processing.
8//!
9//! # Status
10//!
11//! **Draft-ingestion foundation.** The scope-aware draft envelope,
12//! filesystem draft store, `submit` command, explicit file/directory
13//! `sweep` command, rename-based lifecycle transitions, one-shot
14//! `run` lifecycle skeleton, scratch-pipeline pre-emit validation,
15//! bounded LLM validation retry with durable commit, and shared
16//! workspace write-lock acquisition are wired. The remaining Category
17//! 1 work still lands one concern at a time per the build discipline
18//! documented in the Rolls Royce plan.
19//!
20//! See the crate `README.md` for the architectural decisions from
21//! the 2026-04-21 Category 1 design conversation and the roadmap
22//! of follow-up PRs that fill the skeleton in.
23//!
24//! # Architecture sketch
25//!
26//! ```text
27//! ┌───────────────┐ ┌───────────────┐ ┌─────────────────────┐
28//! │ Drafts surface│──▶│ LlmInvoker │──▶│ PreEmitValidator │
29//! │ (filesystem) │ │ (claude -p) │ │ (mimir_core Pipeline│
30//! │ │ │ │ │ clone-on-write) │
31//! └───────────────┘ └───────────────┘ └─────────┬───────────┘
32//! │
33//! ▼
34//! ┌──────────────────────┐
35//! │ mimir_core::Store │
36//! │ commit_batch │
37//! └──────────────────────┘
38//! ```
39//!
40//! The draft lifecycle, LLM invocation, bounded validation retry,
41//! validator box, `Store::commit_batch` durable commit,
42//! supersession-conflict policy, exact duplicate filtering across all
43//! four memory types, and configurable same-day `valid_at` dedup for
44//! Semantic / Inferential records are wired. The binary also exposes a
45//! polling `watch` scheduler, and librarian-specific observability is
46//! wired for runner and processor paths. Store commits acquire the
47//! shared `mimir_core::WorkspaceWriteLock`, so direct librarian runs
48//! and MCP write sessions exclude each other without introducing an
49//! MCP dependency. The earlier Python prototype is retired and is no
50//! longer shipped in the public tree.
51
52#![cfg_attr(not(test), forbid(unsafe_code))]
53
54mod config;
55mod drafts;
56mod error;
57mod llm;
58mod processor;
59mod quorum;
60mod runner;
61#[cfg(test)]
62mod test_tracing;
63mod validator;
64
65pub use config::LibrarianConfig;
66pub use drafts::{
67 Draft, DraftId, DraftMetadata, DraftSource, DraftSourceSurface, DraftState, DraftStore,
68 DraftTransition, DRAFT_SCHEMA_VERSION,
69};
70pub use error::LibrarianError;
71pub use llm::{ClaudeCliInvoker, LlmInvoker};
72pub use processor::{
73 DedupPolicy, RawArchiveDraftProcessor, RetryingDraftProcessor, SupersessionConflictPolicy,
74};
75pub use quorum::{
76 ConsensusLevel, DecisionStatus, ParticipantVote, QuorumAdapterRequest, QuorumEpisode,
77 QuorumEpisodeState, QuorumParticipant, QuorumParticipantOutput, QuorumResult, QuorumRound,
78 QuorumStore, VoteChoice, QUORUM_SCHEMA_VERSION,
79};
80pub use runner::{
81 run_once, DeferredDraftProcessor, DraftProcessingDecision, DraftProcessor, DraftRunItem,
82 DraftRunSummary, DEFAULT_PROCESSING_STALE_SECS,
83};
84pub use validator::PreEmitValidator;
85
86/// Canonical location of the librarian's system prompt.
87///
88/// Embedded at compile time via [`include_str!`] so the prompt is
89/// trivially versionable and reviewable as a text file.
90pub const SYSTEM_PROMPT: &str = include_str!("prompts/system_prompt.md");
91
92/// Default maximum number of per-record retries when a candidate
93/// record fails pre-emit validation.
94///
95/// 3 retries matches the bounded-retry discipline in the Rolls Royce
96/// plan § Category 1 acceptance criteria. Configurable per-run via
97/// [`LibrarianConfig::max_retries_per_record`].
98pub const DEFAULT_MAX_RETRIES_PER_RECORD: u32 = 3;
99
100/// Default `claude -p` invocation timeout, in seconds.
101///
102/// 120 s is comfortably above observed 10–30 s typical and the
103/// 50 s upper outlier observed during the iteration-3 run on real
104/// drafts.
105pub const DEFAULT_LLM_TIMEOUT_SECS: u64 = 120;
106
107/// Default duplicate-detection `valid_at` window, in seconds.
108///
109/// Semantic and Inferential candidates with otherwise-identical
110/// canonical fields inside this window are skipped before commit.
111pub const DEFAULT_DEDUP_VALID_AT_WINDOW_SECS: u64 = 86_400;