basemind 0.29.1

Full AI context layer over MCP — tree-sitter code-map, document RAG (PDF/Office/HTML/email + OCR + reranker), shared agent memory, on-demand web crawl, git history + blame + per-symbol diff. 300+ languages, 10+ coding-agent harnesses, content-addressed Fjall + LanceDB.
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
mod code;
mod comms;
pub mod daemon;
mod documents;
mod documents_validate;
pub(crate) mod layered;
mod overrides;
mod resources;
pub mod root_guard;
pub mod rules;
mod shells;
mod source;
pub mod trust;
mod v1;
mod validate;

use std::path::{Path, PathBuf};

use thiserror::Error;

pub use code::CodeSearchConfig;
pub use comms::CommsConfig;
pub use documents::{
    ApiKey, DocLanguageConfig, DocumentsConfig, KeywordAlgorithm, KeywordsConfig, LlmConfig, NerBackend, NerConfig,
    OcrBackend, OcrConfig, OutputConfig, OutputFormat, RerankerConfig, SecretString, SummarizationConfig,
    SummarizationStrategy,
};
pub use layered::{ConfigLayers, LoadedConfig, defaults_only, merge_layers};
pub use overrides::DocumentsCliOverrides;
pub use resources::{DocumentModelProfile, FootprintKeyword, MaxFootprint, OnnxProvider, ResourcesConfig};
pub use shells::{ShellsConfig, TerminalChoice, VisualMode};
pub use source::{ConfigSource, ProvenanceMap};
pub use v1::{CodeIntelConfig, ConfigV1, CrawlConfig, LanguageConfig};

pub type Config = ConfigV1;

pub const CONFIG_FILE_NAME: &str = "basemind.toml";
pub const BASEMIND_DIR: &str = ".basemind";
/// Project-level `.config/` convention directory (https://github.com/pi0/config-dir).
pub const CONFIG_CONVENTION_DIR: &str = ".config";
/// Tool-named subdirectory of the convention: `<root>/.config/basemind/`.
pub const CONFIG_CONVENTION_SUBDIR: &str = "basemind";
/// Generic config filename used by the nested convention form: `<root>/.config/basemind/config.toml`.
pub const CONFIG_NESTED_FILE_NAME: &str = "config.toml";

#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("config file not found at {0}")]
    NotFound(PathBuf),
    #[error("failed to read {path}: {source}")]
    Io {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("invalid TOML in {path}: {source}")]
    Toml {
        path: PathBuf,
        #[source]
        source: toml::de::Error,
    },
    #[error("config is missing required \"$schema\" field — add `\"$schema\" = \"v1\"`")]
    MissingSchema,
    #[error("unknown schema version {0:?} — supported: v1")]
    UnknownSchema(String),
    #[error("schema validation failed:\n{0}")]
    SchemaValidation(String),
    #[error("config does not match v1 shape after validation: {0}")]
    Deserialize(#[source] serde_json::Error),
}

/// Load the TOML config (no overrides). Existing call sites use this — the
/// layered merger is reached via [`load_with_overrides`] when CLI flags or
/// env vars are involved.
pub fn load(root: &Path) -> Result<Config, ConfigError> {
    let path = resolve_config_path(root);
    if !path.exists() {
        return Err(ConfigError::NotFound(path));
    }
    let io_error = |source| ConfigError::Io {
        path: path.clone(),
        source,
    };
    // `basemind.toml -> ~/.aws/credentials` would make a parse error quote a secret back to the
    // caller, so a link is only followed when it stays inside the workspace.
    let resolved = path.canonicalize().map_err(io_error)?;
    let workspace = root.canonicalize().map_err(io_error)?;
    if !resolved.starts_with(&workspace) {
        return Err(io_error(std::io::Error::new(
            std::io::ErrorKind::PermissionDenied,
            "config file resolves outside the workspace root",
        )));
    }
    let raw = std::fs::read_to_string(&resolved).map_err(io_error)?;
    let mut config = parse_str(&raw).map_err(|e| annotate_path(e, &path))?;
    trust::sanitize_repo_config(&mut config, trust::Grants::from_env());
    trust::warn_inert_fields(&config);
    Ok(config)
}

/// Load the TOML config (if present) plus optional env / CLI override layers,
/// produce a fully-resolved `LoadedConfig` with provenance.
///
/// `env_overrides` and `cli_overrides` are accepted separately so future
/// tooling can report "this came from `BASEMIND_*`" vs "this came from
/// `--flag`" distinctly. Today clap collapses both into a single
/// `DocumentsCliOverrides` per command — callers typically pass the parsed
/// `args.documents` as `cli_overrides` and `None` as `env_overrides`.
pub fn load_with_overrides(
    root: &Path,
    env_overrides: Option<DocumentsCliOverrides>,
    cli_overrides: Option<DocumentsCliOverrides>,
) -> Result<LoadedConfig, ConfigError> {
    let toml_file = match load(root) {
        Ok(cfg) => Some(cfg),
        Err(ConfigError::NotFound(_)) => None,
        Err(e) => return Err(e),
    };
    let loaded = merge_layers(
        ConfigV1::with_defaults(),
        ConfigLayers {
            toml_file,
            env: env_overrides,
            cli: cli_overrides,
        },
    );
    validate_merged(&loaded.config)?;
    Ok(loaded)
}

/// Re-check the invariants the schema and `parse_str` enforce on the file, against the merged
/// result: env / CLI / MCP overrides are applied after the file is validated and would otherwise
/// bypass them.
pub fn validate_merged(config: &Config) -> Result<(), ConfigError> {
    config
        .documents
        .validate()
        .and_then(|()| config.code_search.validate())
        .map_err(ConfigError::SchemaValidation)
}

/// Like [`load`], but a broken file is logged rather than returned: for callers that only want an
/// optional setting out of the config and must still never fail on it. `None` for a missing or
/// unreadable file.
pub fn load_logged(root: &Path) -> Option<Config> {
    match load(root) {
        Ok(config) => Some(config),
        Err(ConfigError::NotFound(_)) => None,
        Err(error) => {
            tracing::warn!(root = %root.display(), %error, "ignoring unreadable basemind.toml");
            None
        }
    }
}

/// [`load_logged`] falling back to per-root defaults.
pub fn load_or_default_logged(root: &Path) -> Config {
    load_logged(root).unwrap_or_else(|| default_for_root(root))
}

/// Resolve the repository root by walking UP from `start` to the nearest ancestor that carries a
/// committed config marker — a root `basemind.toml`, or one under the project-level `.config/`
/// convention (`.config/basemind.toml` / `.config/basemind/config.toml`) — falling back to git
/// discovery, then to `start` unchanged. Lets basemind commands run from a monorepo subfolder
/// attach to the configured root.
///
/// The cache moved out of the repo (it is a machine-global XDG store now), so there is no longer a
/// `.basemind/` directory in the tree to anchor on — the committed `basemind.toml` is the durable
/// in-repo marker of "this is a basemind-managed root".
///
/// The upward `basemind.toml` search is **bounded by the closest enclosing git repository**: it
/// never ascends above that repo's workdir. Without the bound, running from inside a nested subrepo
/// (a git repo checked out inside a polyrepo that has its own root `basemind.toml`) would climb
/// across the subrepo boundary and wrongly attach to the parent polyrepo. The subrepo's own root is
/// the ceiling.
///
/// Precedence:
/// 1. The nearest ancestor of `start` (including `start` itself, up to and including the enclosing
///    git root) that holds a config marker (root `basemind.toml` or a `.config/` convention file).
/// 2. Else the git workdir discovered from `start`.
/// 3. Else `start` unchanged.
///
/// Assumes `start` is already canonicalized by the caller.
pub fn discover_root_with_basemind(start: &Path) -> PathBuf {
    let git_root = crate::git::Repo::discover(start).ok().map(|repo| {
        repo.workdir()
            .canonicalize()
            .unwrap_or_else(|_| repo.workdir().to_path_buf())
    });

    let mut current = start;
    loop {
        if has_config_marker(current) {
            return current.to_path_buf();
        }
        if git_root.as_deref() == Some(current) {
            break;
        }
        match current.parent() {
            Some(parent) if parent != current => current = parent,
            _ => break,
        }
    }
    git_root.unwrap_or_else(|| start.to_path_buf())
}

/// Root for `basemind init`: the project you are initializing, never a parent that merely already
/// holds a `.basemind/`.
///
/// This is deliberately NOT [`discover_root_with_basemind`]. That function *attaches* to an existing
/// index and so walks up to an ancestor `.basemind/`; using it for `init` makes `init` "travel" to a
/// parent polyrepo's root and scaffold there instead of in the current repo. `init` *creates* config,
/// so it anchors to the closest enclosing git repository (the committed repo root the scaffold is
/// meant for), falling back to `start` (typically the cwd) when not inside a git repo.
///
/// Assumes `start` is already canonicalized by the caller.
pub fn init_root(start: &Path) -> PathBuf {
    match crate::git::Repo::discover(start) {
        Ok(repo) => repo.workdir().to_path_buf(),
        Err(_) => start.to_path_buf(),
    }
}

/// Canonical (write) location of the config: `<root>/basemind.toml`.
///
/// The config moved from inside `.basemind/` to the repo root so it can be committed — the
/// `.basemind/` cache is wiped on every schema-version bump and is gitignored, which made an
/// in-cache config non-durable. `basemind init` writes here; [`resolve_config_path`] still reads
/// the legacy in-cache location for back-compat.
pub fn config_path(root: &Path) -> PathBuf {
    root.join(CONFIG_FILE_NAME)
}

/// Legacy config location: `<root>/.basemind/basemind.toml`. Read-only fallback kept so existing
/// checkouts that still carry an in-cache config keep loading it until the user re-runs
/// `basemind init` (which writes the root location).
pub fn legacy_config_path(root: &Path) -> PathBuf {
    root.join(BASEMIND_DIR).join(CONFIG_FILE_NAME)
}

/// Flat project-level `.config/` convention: `<root>/.config/basemind.toml`.
pub fn convention_flat_config_path(root: &Path) -> PathBuf {
    root.join(CONFIG_CONVENTION_DIR).join(CONFIG_FILE_NAME)
}

/// Nested project-level `.config/` convention: `<root>/.config/basemind/config.toml`.
pub fn convention_nested_config_path(root: &Path) -> PathBuf {
    root.join(CONFIG_CONVENTION_DIR)
        .join(CONFIG_CONVENTION_SUBDIR)
        .join(CONFIG_NESTED_FILE_NAME)
}

/// Every location a config may live at, in precedence order: the canonical root file wins, then the
/// legacy in-cache file, then the project-level `.config/` convention (flat `.config/basemind.toml`
/// before the nested `.config/basemind/config.toml`). [`load`] reads the first that exists;
/// [`has_config_marker`] treats any of them as the durable "this is a basemind-managed root" marker.
pub fn config_candidate_paths(root: &Path) -> Vec<PathBuf> {
    vec![
        config_path(root),
        legacy_config_path(root),
        convention_flat_config_path(root),
        convention_nested_config_path(root),
    ]
}

/// True when `root` carries a config file at any [`config_candidate_paths`] location.
///
/// This is the marker root discovery and the workspace-root guard key off, so a project that stores
/// its config under the `.config/` convention is recognized exactly like one with a root
/// `basemind.toml`.
pub fn has_config_marker(root: &Path) -> bool {
    config_candidate_paths(root).iter().any(|path| path.is_file())
}

/// Resolve which config file to read: the first existing [`config_candidate_paths`] entry, else the
/// canonical root path (so a not-found error names the location we tell users to create).
pub fn resolve_config_path(root: &Path) -> PathBuf {
    config_candidate_paths(root)
        .into_iter()
        .find(|path| path.exists())
        .unwrap_or_else(|| config_path(root))
}

pub fn parse_str(raw: &str) -> Result<Config, ConfigError> {
    let config = parse_v1(raw)?;
    config.documents.validate().map_err(ConfigError::SchemaValidation)?;
    Ok(config)
}

fn parse_v1(raw: &str) -> Result<Config, ConfigError> {
    let toml_value: toml::Value = toml::from_str(raw).map_err(|source| ConfigError::Toml {
        path: PathBuf::new(),
        source,
    })?;
    let json_value: serde_json::Value =
        serde_json::to_value(&toml_value).expect("toml::Value → serde_json::Value never fails");

    let schema_tag = json_value
        .as_object()
        .and_then(|o| o.get("$schema"))
        .and_then(|v| v.as_str())
        .ok_or(ConfigError::MissingSchema)?;

    match schema_tag {
        "v1" | "https://basemind.dev/schema/v1.json" => {
            validate::validate_v1(&json_value)?;
            let config = serde_json::from_value::<ConfigV1>(json_value).map_err(ConfigError::Deserialize)?;
            rules::validate(&config).map_err(ConfigError::SchemaValidation)?;
            Ok(config)
        }
        other => Err(ConfigError::UnknownSchema(other.to_string())),
    }
}

pub fn default_for_root(_root: &Path) -> Config {
    ConfigV1::with_defaults()
}

fn annotate_path(err: ConfigError, path: &Path) -> ConfigError {
    match err {
        ConfigError::Toml { source, .. } => ConfigError::Toml {
            path: path.to_path_buf(),
            source,
        },
        other => other,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(unix)]
    #[test]
    fn a_config_symlinked_outside_the_root_is_refused_without_echoing_it() {
        let outside = tempfile::tempdir().expect("tempdir");
        let secret = outside.path().join("credentials");
        std::fs::write(&secret, "aws_secret_access_key = AKIASECRETVALUE\n").expect("write");
        let root = tempfile::tempdir().expect("tempdir");
        std::os::unix::fs::symlink(&secret, root.path().join(CONFIG_FILE_NAME)).expect("symlink");
        let error = load(root.path()).expect_err("must refuse").to_string();
        assert!(!error.contains("AKIASECRETVALUE"), "{error}");
        assert!(error.contains("outside the workspace"), "{error}");
    }

    #[test]
    fn parse_extra_roots_through_schema_validation() {
        let raw = "\"$schema\" = \"v1\"\n[scan]\nextra_roots = [\"/opt/ext\", \"/var/cache/bazel\"]\n";
        let cfg = parse_str(raw).expect("extra_roots is a valid, schema-accepted scan field");
        assert_eq!(
            cfg.scan.extra_roots,
            vec![PathBuf::from("/opt/ext"), PathBuf::from("/var/cache/bazel"),]
        );
    }

    #[test]
    fn extra_roots_defaults_to_empty() {
        let cfg = parse_str("\"$schema\" = \"v1\"\n").unwrap();
        assert!(cfg.scan.extra_roots.is_empty());
    }

    #[test]
    fn code_intel_precise_resolution_defaults_true_and_parses_override() {
        let default_cfg = parse_str("\"$schema\" = \"v1\"\n").unwrap();
        assert!(
            default_cfg.code_intel.precise_resolution,
            "precise_resolution defaults to true when [code_intel] is absent"
        );
        let overridden = parse_str("\"$schema\" = \"v1\"\n[code_intel]\nprecise_resolution = false\n")
            .expect("[code_intel] precise_resolution is a valid, schema-accepted field");
        assert!(
            !overridden.code_intel.precise_resolution,
            "an explicit precise_resolution = false is honored"
        );
    }

    #[test]
    fn resources_section_parses_embed_batch_size_and_defaults_when_absent() {
        let default_cfg = parse_str("\"$schema\" = \"v1\"\n").unwrap();
        assert_eq!(
            default_cfg.resources.embed_batch_size, 32,
            "embed_batch_size defaults to 32 when [resources] is absent"
        );
        assert_eq!(default_cfg.resources.scan_threads, 0);
        assert_eq!(
            default_cfg.resources.document_models,
            crate::config::DocumentModelProfile::Full
        );
        let overridden = parse_str("\"$schema\" = \"v1\"\n[resources]\nembed_batch_size = 8\n")
            .expect("[resources] embed_batch_size is a valid, schema-accepted field");
        assert_eq!(
            overridden.resources.embed_batch_size, 8,
            "an explicit embed_batch_size = 8 is honored"
        );
    }

    #[test]
    fn discover_root_walks_up_to_ancestor_config_marker() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path().canonicalize().expect("canonicalize");
        std::fs::write(root.join(CONFIG_FILE_NAME), "\"$schema\" = \"v1\"\n").expect("write basemind.toml");
        let sub = root.join("a").join("b");
        std::fs::create_dir_all(&sub).expect("mkdir sub");
        assert_eq!(discover_root_with_basemind(&sub), root);
    }

    #[test]
    fn discover_root_returns_start_when_no_basemind_or_git() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let start = tmp.path().canonicalize().expect("canonicalize");
        assert_eq!(discover_root_with_basemind(&start), start);
    }

    #[test]
    fn resolve_reads_flat_config_convention_when_root_absent() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path();
        let path = convention_flat_config_path(root);
        std::fs::create_dir_all(path.parent().expect("convention dir")).expect("mkdir .config");
        std::fs::write(&path, "\"$schema\" = \"v1\"\n[scan]\nmax_file_bytes = 1234\n")
            .expect("write convention config");

        assert_eq!(resolve_config_path(root), path);
        assert_eq!(load(root).expect("convention config loads").scan.max_file_bytes, 1234);
    }

    #[test]
    fn resolve_reads_nested_config_convention_when_root_absent() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path();
        let path = convention_nested_config_path(root);
        std::fs::create_dir_all(path.parent().expect("convention dir")).expect("mkdir .config/basemind");
        std::fs::write(&path, "\"$schema\" = \"v1\"\n[scan]\nmax_file_bytes = 4321\n")
            .expect("write nested convention");

        assert_eq!(resolve_config_path(root), path);
        assert_eq!(load(root).expect("nested config loads").scan.max_file_bytes, 4321);
    }

    #[test]
    fn root_config_wins_over_the_convention() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path();
        let flat = convention_flat_config_path(root);
        std::fs::create_dir_all(flat.parent().expect("convention dir")).expect("mkdir .config");
        std::fs::write(
            config_path(root),
            "\"$schema\" = \"v1\"\n[scan]\nmax_file_bytes = 4096\n",
        )
        .expect("root");
        std::fs::write(&flat, "\"$schema\" = \"v1\"\n[scan]\nmax_file_bytes = 9999\n").expect("convention");

        assert_eq!(resolve_config_path(root), config_path(root));
        assert_eq!(load(root).expect("load").scan.max_file_bytes, 4096);
    }

    #[test]
    fn flat_convention_wins_over_nested_convention() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path();
        let flat = convention_flat_config_path(root);
        let nested = convention_nested_config_path(root);
        std::fs::create_dir_all(flat.parent().expect("convention dir")).expect("mkdir .config");
        std::fs::create_dir_all(nested.parent().expect("nested dir")).expect("mkdir .config/basemind");
        std::fs::write(&flat, "\"$schema\" = \"v1\"\n[scan]\nmax_file_bytes = 1111\n").expect("flat");
        std::fs::write(&nested, "\"$schema\" = \"v1\"\n[scan]\nmax_file_bytes = 2222\n").expect("nested");

        assert_eq!(resolve_config_path(root), flat);
    }

    #[test]
    fn discover_root_walks_up_to_the_convention_marker() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let root = tmp.path().canonicalize().expect("canonicalize");
        let path = convention_nested_config_path(&root);
        std::fs::create_dir_all(path.parent().expect("convention dir")).expect("mkdir");
        std::fs::write(&path, "\"$schema\" = \"v1\"\n").expect("write marker");
        let sub = root.join("crates").join("inner");
        std::fs::create_dir_all(&sub).expect("mkdir sub");

        assert_eq!(
            discover_root_with_basemind(&sub),
            root,
            "the .config marker anchors the walk"
        );
    }

    #[test]
    fn has_config_marker_recognizes_every_candidate_location() {
        let bare = tempfile::tempdir().expect("tempdir");
        assert!(!has_config_marker(bare.path()), "a bare directory is not a marker");

        for candidate in ["root", "legacy", "flat", "nested"] {
            let tmp = tempfile::tempdir().expect("tempdir");
            let root = tmp.path();
            let path = match candidate {
                "root" => config_path(root),
                "legacy" => legacy_config_path(root),
                "flat" => convention_flat_config_path(root),
                _ => convention_nested_config_path(root),
            };
            std::fs::create_dir_all(path.parent().expect("parent")).expect("mkdir");
            std::fs::write(&path, "\"$schema\" = \"v1\"\n").expect("write");
            assert!(has_config_marker(root), "{candidate} config is a marker");
        }
    }
}