Skip to main content

capsule_core/ml/
mod.rs

1//! On-device ML orchestration and the model inventory seam (SSoT: [AI/ML Integrations]).
2//!
3//! This module owns the *structure* the AI design fixes — independent of which model actually
4//! runs:
5//!
6//! - the `registry` — the **canonical model inventory** (one v1-committed row per task) and the
7//!   **embedding-provenance invariant** (every embedding carries `(model_id, model_version)`;
8//!   the [vector index](crate::ml::EmbeddingInsert) refuses inserts from **unknown** models, while a
9//!   *superseded-but-known* version is admitted as a stale-flagged row and excluded from
10//!   queries until regenerated). It owns the version-bump [swap
11//!   primitive](Registry::bump_version);
12//! - the `regen` loop — the background per-asset **regeneration orchestration** that consumes
13//!   the staleness a swap creates: it walks
14//!   [`stale_embedding_assets`](crate::ml::VectorIndex::stale_embedding_assets) and re-embeds
15//!   each asset at the new canonical version through an injected [`Embedder`]
16//!   seam, resumably and per-asset (never a global truncate). The production implementor is
17//!   [`RunnerEmbedder`], which rebuilds the derived index by re-reading
18//!   each **original** through [`AssetSource`] and re-running the
19//!   canonical model through [`ModelRunner`] — the same two seams first-time
20//!   indexing uses, so a regenerated vector equals what a fresh import would produce.
21//!
22//! - the `runner` seam — the [`ModelRunner`] trait every consumer routes
23//!   per-task inference over decoded pixels through, and the deterministic
24//!   [`FixtureRunner`] double. Real per-platform runners (ONNX/CoreML/NNAPI,
25//!   the CLIP runner) ride the **default-off, weight-fetching `inference` feature** — a follow-up,
26//!   never part of the default gate;
27//! - the `orchestrator` — the deterministic execution path for the v1-committed slots: run the
28//!   canonical model over an asset, land semantic + face vectors in the right `vec0` partition
29//!   ([platform-partition fallback](orchestrator::resolve_partition)), and write zero-shot
30//!   [AI tags](orchestrator::auto_tag) into `tags_ai` as a signed metadata update through the
31//!   [`AiTagSink`] seam — implemented by
32//!   `Workspace`, named as a trait so `ml` does not import
33//!   `lifecycle` (which imports `ml`); plus the device-bound batching/thermal policy.
34//!
35//! Real per-platform inference is deferred behind the [`Embedder`] /
36//! [`ModelRunner`] seams (the real runner is a later slice), exactly as live
37//! MLS group state is deferred behind [`AlbumAuthority`](crate::crypto::authority::AlbumAuthority)
38//! with `ReferenceAuthority` standing in; the deterministic
39//! [`DeterministicEmbedder`] and
40//! [`FixtureRunner`] drive every path with no model weights. The local
41//! vector index is the [`VectorIndex`] port; its SQLite adapter is `capsule-library`'s catalog. No model weights are committed to this repository.
42//!
43//! [AI/ML Integrations]: https://docs/design/ai/
44
45mod index;
46mod orchestrator;
47mod regen;
48mod registry;
49mod runner;
50
51pub use index::{
52    EmbeddingInsert, EmbeddingProvenance, EmbeddingRecord, KnnHit, VectorIndex, VectorIndexError,
53    VectorTableSpec,
54};
55pub use orchestrator::{
56    AiTagSink, AssetSource, BatchMode, CANONICAL_PARTITION, KnownAnswer, MICRO_BATCH_SIZES,
57    OrchestratorError, StoreError, auto_tag, choose_batch_mode, embed_and_store, micro_batch_size,
58    resolve_partition, semantic_search, should_pause_for_heat,
59};
60pub use regen::{
61    DeterministicEmbedder, EmbedError, Embedder, RegenError, RegenReport, RunnerEmbedder,
62    regenerate_stale,
63};
64pub use registry::{
65    DistanceMetric, EmbeddingDim, ModelId, ModelRow, ModelVersion, Registry, RegistryError,
66    TaskKind, TaskOutput,
67};
68pub use runner::{BBox, Detection, Embedding, FixtureRunner, Frame, ModelRunner, RunnerError};