fallow-api 3.27.0

Programmatic API contract types for fallow
Documentation
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Local-provider orchestration for advisory similar-code discovery.

use std::fmt::Write as _;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use fallow_engine::source::similar_code::SimilarCodeSourceDigest;
use rustc_hash::FxHashMap;

mod cache;
mod protocol;
mod transport;

pub use protocol::SimilarCodeProviderStatus;

const EMBED_BATCH_SIZE: usize = 1;
const EMBED_RUN_TIMEOUT: Duration = Duration::from_mins(15);

/// Classified local-provider failure used by the programmatic runtime.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ProviderError {
    /// The exact companion or its pinned model is not installed yet.
    NotReady(String),
    /// An installed companion failed execution, provenance, or protocol checks.
    Failed(String),
}

impl ProviderError {
    pub(crate) fn message(&self) -> &str {
        match self {
            Self::NotReady(message) | Self::Failed(message) => message,
        }
    }
}

/// Opaque, validated handle obtained before project source is read.
pub(crate) struct ReadyProvider {
    path: PathBuf,
    pub(crate) status: SimilarCodeProviderStatus,
}

/// One transient source fragment selected for local embedding.
pub(crate) struct EmbeddingInput<'a> {
    pub(crate) source_sha256: SimilarCodeSourceDigest,
    pub(crate) source: &'a str,
}

/// Privacy-safe cache and provider accounting for one embedding pass.
pub(crate) struct EmbeddingResult {
    /// Vectors in input order. Missing entries represent a bounded partial pass.
    pub(crate) vectors: Vec<Option<Vec<f32>>>,
    pub(crate) cache_hits: usize,
    pub(crate) cache_misses: usize,
    pub(crate) cache_writes: usize,
    pub(crate) cache_invalid_entries: usize,
    pub(crate) cache_disabled: bool,
    pub(crate) cache_problem: Option<String>,
    pub(crate) provider_problem: Option<String>,
    pub(crate) inference_ms: f64,
    pub(crate) truncated_functions: usize,
}

/// Provider-neutral validated batch used by the private orchestration seam.
pub(crate) struct EmbeddingBatch {
    pub(crate) vectors: Vec<EmbeddingBatchVector>,
    pub(crate) inference_ms: f64,
    pub(crate) problem: Option<String>,
}

/// One validated vector returned by a private provider session.
pub(crate) struct EmbeddingBatchVector {
    pub(crate) key: u32,
    pub(crate) values: Vec<f32>,
    pub(crate) truncated: bool,
}

/// Private injectable session used by production transport and hermetic tests.
pub(crate) trait EmbeddingSession {
    fn embed(&mut self, functions: &[(u32, &str)]) -> Result<EmbeddingBatch, String>;
}

/// Private lazy session factory. The provider is not started on an all-cache-hit run.
pub(crate) trait EmbeddingSessionFactory {
    fn spawn(&mut self) -> Result<Box<dyn EmbeddingSession>, String>;
}

struct LocalEmbeddingSession {
    inner: transport::ProviderSession,
}

impl EmbeddingSession for LocalEmbeddingSession {
    fn embed(&mut self, functions: &[(u32, &str)]) -> Result<EmbeddingBatch, String> {
        let response = self.inner.embed(functions)?;
        let problem = (response.status != protocol::EmbedCompletionStatus::Complete)
            .then(|| transport::embed_problem(&response));
        Ok(EmbeddingBatch {
            vectors: response
                .vectors
                .into_iter()
                .map(|vector| EmbeddingBatchVector {
                    key: vector.key,
                    values: vector.values,
                    truncated: vector.truncated,
                })
                .collect(),
            inference_ms: response.timing.inference_ms,
            problem,
        })
    }
}

struct LocalEmbeddingSessionFactory<'a> {
    path: &'a Path,
}

impl EmbeddingSessionFactory for LocalEmbeddingSessionFactory<'_> {
    fn spawn(&mut self) -> Result<Box<dyn EmbeddingSession>, String> {
        transport::ProviderSession::spawn(self.path)
            .map(|inner| Box::new(LocalEmbeddingSession { inner }) as Box<dyn EmbeddingSession>)
    }
}

struct EmbeddingMiss {
    representative_index: usize,
    occurrence_indices: Vec<usize>,
}

struct EmbeddingPlan {
    vectors: Vec<Option<Vec<f32>>>,
    misses: Vec<EmbeddingMiss>,
    cache_hits: usize,
    cache_misses: usize,
    truncated_functions: usize,
}

/// Return the installed local provider status.
///
/// # Errors
///
/// Returns a structured message when the trusted sibling is missing, cannot be
/// executed, or reports incompatible provenance.
pub fn status() -> Result<SimilarCodeProviderStatus, String> {
    let sidecar = transport::discover_provider()?;
    let status = transport::provider_status(&sidecar)?;
    validate_status(&status)?;
    Ok(status)
}

/// Download and verify the pinned local model through the installed provider.
///
/// Callers must obtain explicit human confirmation before invoking this API.
/// MCP, NAPI, project configuration, and agent workflows intentionally do not
/// expose this mutation.
///
/// # Errors
///
/// Returns a structured message when setup fails or provenance is invalid.
pub fn setup_local() -> Result<SimilarCodeProviderStatus, String> {
    let sidecar = transport::discover_provider()?;
    let status = transport::setup_provider(&sidecar)?;
    validate_status(&status)?;
    if !status.model_ready {
        return Err("similar-code setup completed without a verified local model".to_owned());
    }
    Ok(status)
}

/// Immutable local provider identity used by public provenance output.
#[must_use]
pub fn provider_identity() -> (&'static str, &'static str, usize, &'static str) {
    (
        protocol::MODEL_ID,
        protocol::MODEL_REVISION,
        protocol::MODEL_DIMENSIONS,
        protocol::MODEL_LICENSE,
    )
}

/// Total bytes downloaded by an explicit local model setup.
#[must_use]
pub fn model_download_bytes() -> u64 {
    protocol::MODEL_ARTIFACTS
        .iter()
        .map(|artifact| artifact.size)
        .sum()
}

/// Resolve and validate the exact local companion before reading project source.
pub(crate) fn ready_provider() -> Result<ReadyProvider, ProviderError> {
    let path = transport::discover_provider().map_err(ProviderError::NotReady)?;
    let status = transport::provider_status(&path).map_err(ProviderError::Failed)?;
    validate_status(&status).map_err(ProviderError::Failed)?;
    if !status.model_ready {
        let problem = status.problem.unwrap_or_else(|| {
            "the pinned local model is not installed; run `fallow similar-code setup --local`"
                .to_owned()
        });
        return Err(ProviderError::NotReady(problem));
    }
    Ok(ReadyProvider { path, status })
}

/// Validate an exact companion path already resolved and signature-verified
/// by an official distribution adapter.
///
/// This exists for in-process hosts such as Node, whose process executable is
/// not the Fallow binary and therefore has no meaningful sibling discovery.
/// The path is never read from project config, PATH, or a model response.
pub(crate) fn ready_provider_from_adapter_path(
    path: &Path,
) -> Result<ReadyProvider, ProviderError> {
    if !path.is_file() {
        return Err(ProviderError::NotReady(format!(
            "verified similar-code companion is unavailable at {}",
            path.display()
        )));
    }
    let path = path.to_path_buf();
    let status = transport::provider_status(&path).map_err(ProviderError::Failed)?;
    validate_status(&status).map_err(ProviderError::Failed)?;
    if !status.model_ready {
        let problem = status.problem.unwrap_or_else(|| {
            "the pinned local model is not installed; run `fallow similar-code setup --local`"
                .to_owned()
        });
        return Err(ProviderError::NotReady(problem));
    }
    Ok(ReadyProvider { path, status })
}

/// Embed selected source fragments using the persistent source-digest cache.
pub(crate) fn embed_selected(
    provider: &ReadyProvider,
    project_root: &Path,
    no_cache: bool,
    inputs: &[EmbeddingInput<'_>],
) -> Result<EmbeddingResult, ProviderError> {
    let mut factory = LocalEmbeddingSessionFactory {
        path: &provider.path,
    };
    embed_selected_with_factory(
        Path::new(&provider.status.cache_dir),
        project_root,
        no_cache,
        inputs,
        EMBED_RUN_TIMEOUT,
        &mut factory,
    )
}

/// Private injectable embedding seam used by production transport and crate tests.
pub(crate) fn embed_selected_with_factory(
    provider_cache_dir: &Path,
    project_root: &Path,
    no_cache: bool,
    inputs: &[EmbeddingInput<'_>],
    run_timeout: Duration,
    factory: &mut dyn EmbeddingSessionFactory,
) -> Result<EmbeddingResult, ProviderError> {
    let mut cache = cache::VectorCache::load(provider_cache_dir, project_root, no_cache);
    let cache_invalid_entries = usize::from(cache.load_state == cache::CacheLoadState::Corrupt);
    let cache_disabled = cache.load_state == cache::CacheLoadState::Disabled;
    let mut plan = prepare_embedding_plan(&mut cache, inputs);
    let mut inference_ms = 0.0f64;
    let mut provider_problem = None;
    if !plan.misses.is_empty() {
        let started = Instant::now();
        let mut session = None;
        let mut batch_start = 0usize;
        while batch_start < plan.misses.len() {
            if started.elapsed() >= run_timeout {
                provider_problem =
                    Some("similar-code embedding stopped at the bounded run limit".to_owned());
                break;
            }
            let session = match session.as_mut() {
                Some(session) => session,
                None => session.insert(factory.spawn().map_err(ProviderError::Failed)?),
            };
            let batch_end = batch_start
                .saturating_add(EMBED_BATCH_SIZE)
                .min(plan.misses.len());
            let request = (batch_start..batch_end)
                .map(|group_index| {
                    let miss = &plan.misses[group_index];
                    let key = u32::try_from(group_index).map_err(|_| {
                        ProviderError::Failed(
                            "similar-code digest group exceeded protocol capacity".to_owned(),
                        )
                    })?;
                    Ok((key, inputs[miss.representative_index].source))
                })
                .collect::<Result<Vec<_>, ProviderError>>()?;
            match session.embed(&request) {
                Ok(batch) => {
                    validate_embedding_batch(&batch, &request).map_err(ProviderError::Failed)?;
                    inference_ms += batch.inference_ms;
                    if batch.problem.is_some() {
                        provider_problem = batch.problem;
                    }
                    for vector in batch.vectors {
                        let group_index = usize::try_from(vector.key).map_err(|_| {
                            ProviderError::Failed(
                                "similar-code provider returned an invalid digest-group key"
                                    .to_owned(),
                            )
                        })?;
                        apply_embedding_vector(
                            &mut cache,
                            inputs,
                            &mut plan,
                            group_index,
                            &vector.values,
                            vector.truncated,
                        )?;
                    }
                }
                Err(error) => {
                    provider_problem = Some(error);
                    break;
                }
            }
            batch_start = batch_end;
        }
    }
    let save = cache.save();

    Ok(EmbeddingResult {
        vectors: plan.vectors,
        cache_hits: plan.cache_hits,
        cache_misses: plan.cache_misses,
        cache_writes: save.durable_writes,
        cache_invalid_entries,
        cache_disabled,
        cache_problem: save.problem,
        provider_problem,
        inference_ms,
        truncated_functions: plan.truncated_functions,
    })
}

fn validate_embedding_batch(batch: &EmbeddingBatch, request: &[(u32, &str)]) -> Result<(), String> {
    if !batch.inference_ms.is_finite() || batch.inference_ms < 0.0 {
        return Err("similar-code provider returned invalid timing".to_owned());
    }
    let mut expected = request.iter().map(|(key, _)| *key).collect::<Vec<_>>();
    let mut actual = batch
        .vectors
        .iter()
        .map(|vector| vector.key)
        .collect::<Vec<_>>();
    expected.sort_unstable();
    actual.sort_unstable();
    if actual.windows(2).any(|pair| pair[0] == pair[1])
        || actual
            .iter()
            .any(|key| expected.binary_search(key).is_err())
        || batch.vectors.iter().any(|vector| {
            vector.values.len() != protocol::MODEL_DIMENSIONS
                || vector.values.iter().any(|value| !value.is_finite())
                || vector.values.iter().all(|value| *value == 0.0)
        })
        || (batch.vectors.len() == request.len()) == batch.problem.is_some()
    {
        return Err("similar-code provider returned an invalid embedding batch".to_owned());
    }
    Ok(())
}

fn prepare_embedding_plan(
    cache: &mut cache::VectorCache,
    inputs: &[EmbeddingInput<'_>],
) -> EmbeddingPlan {
    let mut vectors = vec![None; inputs.len()];
    let mut misses = Vec::<EmbeddingMiss>::new();
    let mut miss_groups: FxHashMap<SimilarCodeSourceDigest, usize> = FxHashMap::default();
    let mut cache_hits = 0usize;
    let mut cache_misses = 0usize;
    let mut truncated_functions = 0usize;
    for (index, input) in inputs.iter().enumerate() {
        if let Some(entry) = cache.get(&input.source_sha256) {
            vectors[index] = Some(entry.values.clone());
            cache_hits = cache_hits.saturating_add(1);
            truncated_functions =
                truncated_functions.saturating_add(usize::from(entry.token_truncated));
            continue;
        }

        cache_misses = cache_misses.saturating_add(1);
        if let Some(group_index) = miss_groups.get(&input.source_sha256).copied() {
            misses[group_index].occurrence_indices.push(index);
        } else {
            let group_index = misses.len();
            miss_groups.insert(input.source_sha256, group_index);
            misses.push(EmbeddingMiss {
                representative_index: index,
                occurrence_indices: vec![index],
            });
        }
    }
    EmbeddingPlan {
        vectors,
        misses,
        cache_hits,
        cache_misses,
        truncated_functions,
    }
}

fn apply_embedding_vector(
    cache: &mut cache::VectorCache,
    inputs: &[EmbeddingInput<'_>],
    plan: &mut EmbeddingPlan,
    group_index: usize,
    values: &[f32],
    token_truncated: bool,
) -> Result<(), ProviderError> {
    let miss = plan.misses.get(group_index).ok_or_else(|| {
        ProviderError::Failed(
            "similar-code provider returned an unknown digest-group key".to_owned(),
        )
    })?;
    let digest = inputs[miss.representative_index].source_sha256;
    cache.insert(digest, values.to_owned(), token_truncated);
    if token_truncated {
        plan.truncated_functions = plan
            .truncated_functions
            .saturating_add(miss.occurrence_indices.len());
    }
    for occurrence_index in &miss.occurrence_indices {
        plan.vectors[*occurrence_index] = Some(values.to_owned());
    }
    Ok(())
}

/// Remove the model-specific vector cache. Model artifacts remain installed.
fn clear_vector_cache(provider_cache_dir: &Path, project_root: &Path) -> Result<bool, String> {
    cache::clear(provider_cache_dir, project_root)
}

/// Remove only persisted similar-code vectors for one project.
///
/// The downloaded model is user-level state and is never removed here.
///
/// # Errors
///
/// Returns an error when config resolution or cache removal fails.
pub fn clear_project_cache(
    root: &Path,
    config_path: Option<&Path>,
    allow_remote_extends: bool,
) -> Result<bool, String> {
    let project = fallow_engine::project_config::config_for_project_with_load_options(
        root,
        config_path,
        fallow_config::ConfigLoadOptions {
            allow_remote_extends,
        },
    )
    .map_err(|error| format!("failed to load config: {error}"))?;
    let provider = status()?;
    clear_vector_cache(Path::new(&provider.cache_dir), &project.config.root)
}

pub(crate) fn model_artifact_sha256() -> &'static str {
    protocol::MODEL_ARTIFACTS
        .first()
        .map_or("", |artifact| artifact.sha256)
}

pub(crate) fn parameter_sha256() -> String {
    let digest = cache::parameter_digest();
    digest.iter().fold(
        String::with_capacity(digest.len().saturating_mul(2)),
        |mut output, byte| {
            let _ = write!(output, "{byte:02x}");
            output
        },
    )
}

pub(crate) const fn embedding_batch_size() -> usize {
    EMBED_BATCH_SIZE
}

pub(crate) const fn embedding_semantics_version() -> u32 {
    protocol::EMBEDDING_SEMANTICS_VERSION
}

fn validate_status(status: &SimilarCodeProviderStatus) -> Result<(), String> {
    if status.protocol_version != protocol::WIRE_PROTOCOL_VERSION
        || status.embedding_semantics_version != protocol::EMBEDDING_SEMANTICS_VERSION
        || status.sidecar_version != env!("CARGO_PKG_VERSION")
        || status.model_id != protocol::MODEL_ID
        || status.model_revision != protocol::MODEL_REVISION
        || status.dimensions != protocol::MODEL_DIMENSIONS
        || status.max_tokens != protocol::MODEL_MAX_TOKENS
        || status.license != protocol::MODEL_LICENSE
        || status.download_bytes != model_download_bytes()
        || !status.analysis_offline
        || (status.model_ready && !status.integrity_verified)
    {
        return Err(
            "similar-code companion provenance does not match this Fallow release".to_owned(),
        );
    }
    Ok(())
}

#[cfg(test)]
#[expect(
    clippy::unwrap_used,
    reason = "test fixture construction must fail immediately"
)]
mod tests {
    use super::*;

    #[test]
    fn duplicate_digest_is_inferred_once_and_warm_truncation_is_occurrence_aware() {
        let temp = tempfile::tempdir().unwrap();
        let cache_root = temp.path().join("user-cache");
        let provider_cache_dir = cache_root.join("models").join(protocol::MODEL_REVISION);
        let project_root = temp.path().join("project");
        std::fs::create_dir_all(&cache_root).unwrap();
        std::fs::create_dir_all(&project_root).unwrap();
        let digest = SimilarCodeSourceDigest::new([8; 32]);
        let inputs = [
            EmbeddingInput {
                source_sha256: digest,
                source: "function same() { return 1; }",
            },
            EmbeddingInput {
                source_sha256: digest,
                source: "function same() { return 1; }",
            },
        ];

        let mut cache = cache::VectorCache::load(&provider_cache_dir, &project_root, false);
        let mut cold = prepare_embedding_plan(&mut cache, &inputs);
        assert_eq!(cold.cache_misses, 2);
        assert_eq!(cold.misses.len(), 1);
        assert_eq!(cold.misses[0].occurrence_indices, vec![0, 1]);
        apply_embedding_vector(
            &mut cache,
            &inputs,
            &mut cold,
            0,
            &[0.25; protocol::MODEL_DIMENSIONS],
            true,
        )
        .unwrap();
        assert_eq!(cold.truncated_functions, 2);
        assert!(cold.vectors.iter().all(Option::is_some));
        assert_eq!(cache.save().durable_writes, 1);

        let mut cache = cache::VectorCache::load(&provider_cache_dir, &project_root, false);
        let warm = prepare_embedding_plan(&mut cache, &inputs);
        assert_eq!(warm.cache_hits, 2);
        assert_eq!(warm.cache_misses, 0);
        assert_eq!(warm.truncated_functions, 2);
        assert!(warm.misses.is_empty());
        assert!(warm.vectors.iter().all(Option::is_some));
    }
}