episteme-local 0.2.0

Local-first knowledge ingestion and intelligence workflows
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
//! Parses CLI commands and coordinates local document-processing workflows.

use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result, bail};
use chrono::Utc;
use clap::{Parser, Subcommand};
use episteme::adapters::{
    BamlDocumentClassifier, BamlDocumentIntelligenceAnalyzer, BamlResearchAnalyzer,
    DocumentCliExtractor, DuckDbIngestionStore, HttpModelProbe, SystemDocumentTools,
    VaultFileStore, ZkIndexer, prepare_vault_directory,
};
use episteme::classification::{
    BatchOptions, CLASSIFICATION_POLICY_VERSION, CachePolicy, ClassificationBatch,
    ClassificationPipeline, RoutedDocumentClassifier,
};
use episteme::config::Settings;
use episteme::doctor::Doctor;
use episteme::domain::{
    AnalysisProvenance, ClassificationBatchSource, ClassificationFailure, ClassificationFailureCode,
};
use episteme::ingest::Ingestor;
use episteme::intelligence::DocumentIntelligencePipeline;
use episteme::stage::stage_source;
use episteme::watch::{StableFileTracker, WatchDecision};

#[derive(Debug, Parser)]
#[command(version, about)]
struct Cli {
    #[arg(long, default_value = "episteme.toml")]
    config: PathBuf,
    #[command(subcommand)]
    command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Check configuration and required executables.
    Doctor,
    /// Create configured runtime directories.
    Init,
    /// Ingest one stable source from the configured inbox.
    Ingest {
        /// Source path beneath the configured inbox.
        source: PathBuf,
    },
    /// Classify one stable source from the configured inbox.
    Classify {
        /// Ignore matching versioned cache entries.
        #[arg(long)]
        force: bool,
        /// Source path beneath the configured inbox.
        source: PathBuf,
    },
    /// Classify every supported source beneath the configured inbox.
    ClassifyBatch {
        /// Ignore matching versioned cache entries.
        #[arg(long)]
        force: bool,
        /// Retry sources whose latest attempt is retryable.
        #[arg(long)]
        retry_failed: bool,
        /// Maximum concurrent local model requests.
        #[arg(long, default_value_t = 2)]
        concurrency: usize,
        /// Durable batch identity used for resume and reconciliation.
        #[arg(long, default_value = "inbox")]
        batch_id: String,
        /// Optional path for one atomically reconciled JSON summary.
        #[arg(long)]
        summary: Option<PathBuf>,
    },
    /// Extract evidence-grounded summary, claims, entities, and relations.
    Analyze {
        #[arg(long)]
        force: bool,
        source: PathBuf,
    },
    /// Poll the configured inbox and ingest stable files.
    Watch,
}

#[tokio::main]
async fn main() -> ExitCode {
    match run().await {
        Ok(()) => ExitCode::SUCCESS,
        Err(error) => {
            eprintln!("error: {error:#}");
            ExitCode::FAILURE
        }
    }
}

async fn run() -> Result<()> {
    let cli = Cli::parse();
    let settings = Settings::load(&cli.config)
        .with_context(|| format!("failed to load {}", cli.config.display()))?;
    match cli.command {
        Command::Doctor => run_doctor(&settings),
        Command::Init => initialize_directories(&settings),
        Command::Ingest { source } => {
            let run = ingest_path(&settings, &source).await?;
            println!("{}: {}", run.digest, run.stage.as_str());
            Ok(())
        }
        Command::Classify { force, source } => {
            let record = classify_path(
                &settings,
                &source,
                if force {
                    CachePolicy::Refresh
                } else {
                    CachePolicy::Use
                },
            )
            .await?;
            println!("{}", serde_json::to_string_pretty(&record)?);
            Ok(())
        }
        Command::ClassifyBatch {
            force,
            retry_failed,
            concurrency,
            batch_id,
            summary,
        } => {
            let result = classify_batch(
                &settings,
                &batch_id,
                BatchOptions {
                    cache_policy: if force {
                        CachePolicy::Refresh
                    } else {
                        CachePolicy::Use
                    },
                    retry_failed,
                },
                concurrency,
            )
            .await?;
            if let Some(path) = summary {
                write_summary(&path, &result)?;
            }
            println!("{}", serde_json::to_string_pretty(&result)?);
            Ok(())
        }
        Command::Analyze { force, source } => {
            let result = analyze_path(
                &settings,
                &source,
                if force {
                    CachePolicy::Refresh
                } else {
                    CachePolicy::Use
                },
            )
            .await?;
            println!("{}", serde_json::to_string_pretty(&result)?);
            Ok(())
        }
        Command::Watch => watch(&settings).await,
    }
}

async fn analyze_path(
    settings: &Settings,
    source_path: &Path,
    cache_policy: CachePolicy,
) -> Result<episteme::domain::DocumentIntelligence> {
    let inbox = settings.vault_root.join(&settings.inbox_directory);
    let state_root = settings
        .database_path
        .parent()
        .context("database path must have a parent directory")?;
    let source = stage_source(
        &inbox,
        source_path,
        &state_root.join("staging"),
        settings.maximum_source_bytes,
    )?;
    let timeout = Duration::from_secs(settings.process_timeout_seconds);
    let tools = SystemDocumentTools::new(
        settings.tools.clone(),
        timeout,
        settings.maximum_output_bytes,
    );
    let extractor = DocumentCliExtractor::new(tools, settings.minimum_text_characters);
    let profiles = std::iter::once(settings.classifier.clone())
        .chain(settings.classifier_fallbacks.iter().cloned())
        .collect();
    let classifier = RoutedDocumentClassifier::new(
        profiles,
        BamlDocumentClassifier::new(timeout),
        HttpModelProbe::new(Duration::from_secs(
            settings.process_timeout_seconds.min(10),
        ))?,
        CLASSIFICATION_POLICY_VERSION,
    );
    let analyzer = BamlDocumentIntelligenceAnalyzer::new(settings.distiller.clone(), timeout);
    let store = DuckDbIngestionStore::open(&settings.database_path)?;
    DocumentIntelligencePipeline::new(
        extractor,
        classifier,
        analyzer,
        store,
        settings.distiller.model(),
    )
    .analyze(&source, cache_policy)
    .await
    .map_err(Into::into)
}

async fn classify_path(
    settings: &Settings,
    source_path: &Path,
    cache_policy: CachePolicy,
) -> Result<episteme::domain::ClassificationRecord> {
    let inbox = settings.vault_root.join(&settings.inbox_directory);
    let state_root = settings
        .database_path
        .parent()
        .context("database path must have a parent directory")?;
    let source = stage_source(
        &inbox,
        source_path,
        &state_root.join("staging"),
        settings.maximum_source_bytes,
    )?;
    let timeout = Duration::from_secs(settings.process_timeout_seconds);
    let tools = SystemDocumentTools::new(
        settings.tools.clone(),
        timeout,
        settings.maximum_output_bytes,
    );
    let extractor = DocumentCliExtractor::new(tools, settings.minimum_text_characters);
    let profiles = std::iter::once(settings.classifier.clone())
        .chain(settings.classifier_fallbacks.iter().cloned())
        .collect();
    let profile_classifier = BamlDocumentClassifier::new(timeout);
    let probe = HttpModelProbe::new(Duration::from_secs(
        settings.process_timeout_seconds.min(10),
    ))?;
    let classifier = RoutedDocumentClassifier::new(
        profiles,
        profile_classifier,
        probe,
        CLASSIFICATION_POLICY_VERSION,
    );
    let store = DuckDbIngestionStore::open(&settings.database_path)?;
    ClassificationPipeline::new(extractor, classifier, store)
        .classify(&source, cache_policy)
        .await
        .map_err(Into::into)
}

async fn classify_batch(
    settings: &Settings,
    batch_id: &str,
    options: BatchOptions,
    concurrency: usize,
) -> Result<episteme::domain::ClassificationBatchSummary> {
    let inbox = settings.vault_root.join(&settings.inbox_directory);
    let state_root = settings
        .database_path
        .parent()
        .context("database path must have a parent directory")?;
    let sources = discover_batch_sources(settings, &inbox, &state_root.join("staging"))?;
    let timeout = Duration::from_secs(settings.process_timeout_seconds);
    let tools = SystemDocumentTools::new(
        settings.tools.clone(),
        timeout,
        settings.maximum_output_bytes,
    );
    let extractor = DocumentCliExtractor::new(tools, settings.minimum_text_characters);
    let profiles = std::iter::once(settings.classifier.clone())
        .chain(settings.classifier_fallbacks.iter().cloned())
        .collect();
    let profile_classifier = BamlDocumentClassifier::new(timeout);
    let probe = HttpModelProbe::new(Duration::from_secs(
        settings.process_timeout_seconds.min(10),
    ))?;
    let classifier = RoutedDocumentClassifier::new(
        profiles,
        profile_classifier,
        probe,
        CLASSIFICATION_POLICY_VERSION,
    );
    let store = Arc::new(DuckDbIngestionStore::open(&settings.database_path)?);
    let processor = Arc::new(ClassificationPipeline::new(
        extractor,
        classifier,
        Arc::clone(&store),
    ));
    ClassificationBatch::new(processor, store, concurrency)?
        .run(batch_id, sources, options)
        .await
        .map_err(Into::into)
}

fn discover_batch_sources(
    settings: &Settings,
    inbox: &Path,
    staging: &Path,
) -> Result<Vec<ClassificationBatchSource>> {
    let canonical_inbox = inbox
        .canonicalize()
        .context("failed to resolve configured inbox")?;
    let mut directories = vec![canonical_inbox.clone()];
    let mut sources = Vec::new();
    while let Some(directory) = directories.pop() {
        let canonical_directory = directory
            .canonicalize()
            .context("failed to resolve inbox directory")?;
        if !canonical_directory.starts_with(&canonical_inbox) {
            bail!("inbox directory escaped configured root");
        }
        for entry in fs::read_dir(&canonical_directory)
            .context("failed to read configured inbox directory")?
        {
            let path = entry?.path();
            let relative = path
                .strip_prefix(&canonical_inbox)
                .context("discovered source escaped inbox")?
                .to_string_lossy()
                .into_owned();
            let metadata = fs::symlink_metadata(&path).context("failed to inspect inbox entry")?;
            if metadata.file_type().is_symlink() {
                sources.push(ClassificationBatchSource::Rejected {
                    relative_path: relative,
                    failure: ClassificationFailure::terminal(
                        ClassificationFailureCode::Staging,
                        "batch source traversed a symlink",
                    ),
                });
            } else if metadata.is_dir() {
                directories.push(path);
            } else if metadata.is_file() && supported_batch_source(&path) {
                match stage_source(
                    &canonical_inbox,
                    &path,
                    staging,
                    settings.maximum_source_bytes,
                ) {
                    Ok(source) => sources.push(ClassificationBatchSource::Ready {
                        relative_path: relative,
                        source,
                    }),
                    Err(_) => sources.push(ClassificationBatchSource::Rejected {
                        relative_path: relative,
                        failure: ClassificationFailure::terminal(
                            ClassificationFailureCode::Staging,
                            "batch source staging failed",
                        ),
                    }),
                }
            }
        }
    }
    Ok(sources)
}

fn supported_batch_source(path: &Path) -> bool {
    path.extension()
        .and_then(|extension| extension.to_str())
        .is_some_and(|extension| {
            matches!(
                extension.to_ascii_lowercase().as_str(),
                "pdf" | "html" | "htm" | "png" | "jpg" | "jpeg" | "tif" | "tiff" | "webp"
            )
        })
}

fn write_summary(
    path: &Path,
    summary: &episteme::domain::ClassificationBatchSummary,
) -> Result<()> {
    let parent = path
        .parent()
        .context("classification summary path must have a parent directory")?;
    fs::create_dir_all(parent).with_context(|| format!("failed to create {}", parent.display()))?;
    let mut temporary = tempfile::NamedTempFile::new_in(parent)?;
    serde_json::to_writer_pretty(&mut temporary, summary)?;
    temporary.write_all(b"\n")?;
    temporary.as_file().sync_all()?;
    temporary
        .persist_noclobber(path)
        .map_err(|error| error.error)
        .with_context(|| format!("failed to persist {}", path.display()))?;
    Ok(())
}

fn run_doctor(settings: &Settings) -> Result<()> {
    let report = Doctor::check_tools(&settings.required_tools());
    for check in &report.checks {
        let status = if check.available { "ok" } else { "missing" };
        println!("{status}: {} ({})", check.name, check.path.display());
    }
    if !report.is_healthy() {
        bail!("one or more required tools are unavailable");
    }
    Ok(())
}

fn initialize_directories(settings: &Settings) -> Result<()> {
    prepare_vault_directory(&settings.vault_root, &settings.inbox_directory)?;
    VaultFileStore::new(
        settings.vault_root.clone(),
        settings.research_directory.clone(),
        settings.archive_directory.clone(),
    )
    .prepare_directories()?;
    if let Some(parent) = settings.database_path.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("failed to create {}", parent.display()))?;
    }
    let _store = DuckDbIngestionStore::open(&settings.database_path)?;
    Ok(())
}

async fn ingest_path(
    settings: &Settings,
    source_path: &Path,
) -> Result<episteme::domain::IngestionRun> {
    let inbox = settings.vault_root.join(&settings.inbox_directory);
    let state_root = settings
        .database_path
        .parent()
        .context("database path must have a parent directory")?;
    let source = stage_source(
        &inbox,
        source_path,
        &state_root.join("staging"),
        settings.maximum_source_bytes,
    )?;
    let timeout = Duration::from_secs(settings.process_timeout_seconds);
    let tools = SystemDocumentTools::new(
        settings.tools.clone(),
        timeout,
        settings.maximum_output_bytes,
    );
    let extractor = DocumentCliExtractor::new(tools, settings.minimum_text_characters);
    let analyzer = BamlResearchAnalyzer::new(
        settings.classifier.endpoint.clone(),
        settings.distiller.clone(),
        timeout,
    );
    let vault = VaultFileStore::new(
        settings.vault_root.clone(),
        settings.research_directory.clone(),
        settings.archive_directory.clone(),
    );
    let store = DuckDbIngestionStore::open(&settings.database_path)?;
    let indexer = ZkIndexer::new(
        settings.tools.zk.clone(),
        settings.vault_root.clone(),
        timeout,
        settings.maximum_output_bytes,
    );
    let ingestor = Ingestor::new(extractor, analyzer, vault, store, indexer);
    ingestor
        .ingest(
            &source,
            AnalysisProvenance {
                function: "ResearchDistillationPipeline".to_owned(),
                client: "LocalClassifier+LocalDistiller".to_owned(),
                model: format!(
                    "classifier={};distiller={}",
                    settings.classifier.endpoint.model(),
                    settings.distiller.model()
                ),
                pipeline_version: env!("CARGO_PKG_VERSION").to_owned(),
                processed_at: Utc::now().to_rfc3339(),
            },
        )
        .await
        .map_err(Into::into)
}

async fn watch(settings: &Settings) -> Result<()> {
    let inbox = settings.vault_root.join(&settings.inbox_directory);
    let mut tracker = StableFileTracker::new(&inbox)?;
    let mut interval = tokio::time::interval(Duration::from_secs(settings.watch_interval_seconds));
    loop {
        tokio::select! {
            _ = interval.tick() => {
                for entry in fs::read_dir(&inbox)
                    .with_context(|| format!("failed to read {}", inbox.display()))?
                {
                    let path = entry?.path();
                    if let WatchDecision::Ready(path) = tracker.observe(&path)
                        && let Err(error) = ingest_path(settings, &path).await
                    {
                        eprintln!("ingestion failed for {}: {error:#}", path.display());
                    }
                }
            }
            signal = tokio::signal::ctrl_c() => {
                signal.context("failed to listen for shutdown signal")?;
                return Ok(());
            }
        }
    }
}