basis 0.9.0

The basis SDK: workspace discovery, run lifecycle, one event stream, and the two seams. No protocol, no transport, no TTY.
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
//! Discovery of the skills directories.
//!
//! Skills are on-demand instructions the model loads by name — description
//! first, body only when it asks. mentra owns the loading (frontmatter
//! parsing, dedup, the `load_skill` tool); basis owns *where to look*, which is
//! the convention half.
//!
//! # Four roots, one order
//!
//! The whole point of the SKILL.md format is that a skill written once is found
//! by every harness that speaks it, so *where to look* is not basis's to invent:
//! `.agents/skills/` and `~/.agents/skills/` are the directories pi and Claude
//! Code already read, and a repository that carries one is making a statement
//! basis has no reason to ignore. `.basis/skills/` and the global config
//! directory's `skills/` are what basis added on top, and they stay — a
//! workspace that wants skills for *this* harness alone has to be able to say
//! so.
//!
//! [`discover`] returns every one that exists, most specific first:
//!
//! 1. `<workspace>/.basis/skills` — this repository, this harness
//! 2. `<workspace>/.agents/skills` — this repository, any harness
//! 3. `<global config dir>/skills` — this user, this harness
//! 4. `$HOME/.agents/skills` — this user, any harness
//!
//! Within each scope the basis-specific root comes first because naming basis
//! is the more specific statement: a directory that says *this harness* was
//! written knowing which harness would read it. Roots layer rather than
//! replace (`Runtime::register_skills_dirs` is additive, and an earlier root's
//! name wins), so a repository shadows a personal skill by name and inherits
//! everything it did not shadow.
//!
//! # `disable-model-invocation`
//!
//! A `SKILL.md` may set `disable-model-invocation: true` in its frontmatter
//! (the underscore spelling is accepted too). mentra then leaves the skill out
//! of the list the model is shown and refuses it from `load_skill`, while
//! keeping it in `Runtime::skills()` with `model_invocable: false` — a skill a
//! person invokes deliberately rather than one a model reaches for.
//!
//! basis reads the flag nowhere: discovery is about *where to look*, and what
//! a `SKILL.md` says is mentra's to parse. What basis does is carry the answer
//! out, on [`LoadedSkill::model_invocable`](crate::run::LoadedSkill), so a host
//! listing a workspace's skills can tell which of them the model will never
//! reach. It is deliberately **not** offered as a `/name` command beside
//! templates: rendering one would mean re-reading and re-parsing the
//! `SKILL.md` basis handed mentra (mentra exposes the body of a skill it will
//! not invoke through no API at all), and giving it an argument convention
//! `SKILL.md` does not define — which is [`crate::templates`] a second time,
//! under a second set of rules.

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

use crate::context::ContextScope;

/// Where basis looks inside a workspace, relative to its root.
pub const DEFAULT_WORKSPACE_SKILLS_DIR: &str = ".basis/skills";

/// The directory other harnesses share, relative to the workspace root and to
/// `$HOME`. One constant because it is one convention: the same spelling names
/// the project scope and the user scope, which is what makes it portable.
pub const SHARED_SKILLS_DIR: &str = ".agents/skills";

/// Where basis looks inside the global config directory.
pub const DEFAULT_GLOBAL_SKILLS_DIR: &str = "skills";

/// How to look for skills.
///
/// Four fields, one per root (D9): the shared `.agents/skills` roots carry no
/// path of their own — a fixed spelling is what makes them shared, and
/// relocating one would stop it being the convention `.agents/skills` names —
/// so those two are plain switches, on by default. The two basis-specific
/// roots carry the path *and* the switch in one `Option`: `None` disables the
/// root, `Some(path)` both enables it and says where. Every root disables
/// independently of the other three.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SkillsConfig {
    /// `<workspace>/.basis/skills`, relative to the workspace root. `None`
    /// disables this root alone.
    pub workspace_subdir: Option<PathBuf>,
    /// Whether `<workspace>/.agents/skills` — the convention `pi` and Claude
    /// Code already read — is one of the roots.
    pub shared_workspace_dir: bool,
    /// The global config directory, if any. `skills/` inside it is the third
    /// root; `None` disables that root alone and says nothing about
    /// [`shared_home_dir`](Self::shared_home_dir).
    pub global_dir: Option<PathBuf>,
    /// Whether `$HOME/.agents/skills` — the shared convention's personal
    /// scope — is one of the roots. Gated on its own switch rather than on
    /// [`global_dir`](Self::global_dir), unlike before D9: a caller can now
    /// keep this while disabling basis's own global root, or the reverse.
    pub shared_home_dir: bool,
}

impl Default for SkillsConfig {
    fn default() -> Self {
        Self {
            workspace_subdir: Some(PathBuf::from(DEFAULT_WORKSPACE_SKILLS_DIR)),
            shared_workspace_dir: true,
            global_dir: crate::context::ContextConfig::default().global_dir,
            shared_home_dir: true,
        }
    }
}

/// A skills directory that exists on disk.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SkillsSource {
    pub path: PathBuf,
    pub scope: ContextScope,
}

/// Every skills directory that exists, most specific first.
///
/// Precedence order, because that is the order
/// `Runtime::register_skills_dirs` wants: an earlier root's skill shadows the
/// same name in a later one.
pub fn discover(workspace: &Path, config: &SkillsConfig) -> Vec<SkillsSource> {
    discover_in(workspace, config, user_home().as_deref())
}

/// [`discover`] with the user's home directory passed in rather than read from
/// the environment, so a test can state one instead of inheriting whichever
/// machine it runs on.
fn discover_in(workspace: &Path, config: &SkillsConfig, home: Option<&Path>) -> Vec<SkillsSource> {
    let mut sources = Vec::new();

    if let Some(subdir) = &config.workspace_subdir {
        push(
            &mut sources,
            workspace.join(subdir),
            ContextScope::Workspace,
        );
    }
    if config.shared_workspace_dir {
        push(
            &mut sources,
            workspace.join(SHARED_SKILLS_DIR),
            ContextScope::Workspace,
        );
    }

    if let Some(global) = &config.global_dir {
        push(
            &mut sources,
            global.join(DEFAULT_GLOBAL_SKILLS_DIR),
            ContextScope::Global,
        );
    }
    // `~/.agents/skills` is a fixed path by convention, not somewhere a host
    // relocates, so the only question a caller answers about it (D9) is
    // whether it is read at all — independently of the global root above,
    // unlike before D9, when one `Option` gated both.
    if config.shared_home_dir
        && let Some(home) = home
    {
        push(
            &mut sources,
            home.join(SHARED_SKILLS_DIR),
            ContextScope::Global,
        );
    }

    sources
}

/// Appends `path` when it is a directory no earlier root already names.
///
/// Two roots can reach one directory — a symlink, a global config directory
/// that *is* the workspace, a `$HOME` that is the workspace root. Registering
/// the same directory twice is a duplicate-name error in mentra and noise in
/// the run header either way, and the first mention is the stronger one, so
/// the later mention is what goes.
fn push(sources: &mut Vec<SkillsSource>, path: PathBuf, scope: ContextScope) {
    if !path.is_dir()
        || sources
            .iter()
            .any(|source| crate::paths::same_dir(&source.path, &path))
    {
        return;
    }

    sources.push(SkillsSource { path, scope });
}

/// `$HOME`, the only place the shared user root can be. `None` leaves that
/// root out rather than guessing at one.
fn user_home() -> Option<PathBuf> {
    std::env::var_os("HOME").map(PathBuf::from)
}

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

    /// The default three roots enabled, `global_dir` as given — the shape
    /// most tests below vary along exactly one axis.
    fn config(global: Option<PathBuf>) -> SkillsConfig {
        SkillsConfig {
            workspace_subdir: Some(PathBuf::from(DEFAULT_WORKSPACE_SKILLS_DIR)),
            shared_workspace_dir: true,
            global_dir: global,
            shared_home_dir: true,
        }
    }

    /// Every test states its own `$HOME`. Reading the real one would make the
    /// results depend on whether whoever runs the suite keeps skills there.
    fn found(workspace: &Path, config: &SkillsConfig, home: Option<&Path>) -> Vec<SkillsSource> {
        discover_in(workspace, config, home)
    }

    fn dir(parent: &Path, relative: &str) -> PathBuf {
        let path = parent.join(relative);
        std::fs::create_dir_all(&path).expect("create dir");
        path
    }

    #[test]
    fn nothing_on_disk_means_no_sources() {
        let tmp = tempfile::tempdir().expect("tempdir");

        assert!(found(tmp.path(), &config(None), None).is_empty());
    }

    #[test]
    fn a_workspace_directory_is_found() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let skills = dir(tmp.path(), DEFAULT_WORKSPACE_SKILLS_DIR);

        let sources = found(tmp.path(), &config(None), None);

        assert_eq!(sources.len(), 1);
        assert_eq!(sources[0].scope, ContextScope::Workspace);
        assert_eq!(sources[0].path, skills);
    }

    #[test]
    fn the_shared_workspace_directory_is_found() {
        // The point of the SKILL.md format: a repository writes `.agents/skills`
        // once and every harness that speaks the format reads it.
        let tmp = tempfile::tempdir().expect("tempdir");
        let shared = dir(tmp.path(), SHARED_SKILLS_DIR);

        let sources = found(tmp.path(), &config(None), None);

        assert_eq!(sources.len(), 1);
        assert_eq!(sources[0].scope, ContextScope::Workspace);
        assert_eq!(sources[0].path, shared);
    }

    #[test]
    fn the_basis_directory_outranks_the_shared_one_because_it_names_basis() {
        // Both roots describe the same repository, so the tie is broken by
        // which is the more specific statement: `.basis/skills` was written
        // knowing which harness would read it.
        let tmp = tempfile::tempdir().expect("tempdir");
        let basis = dir(tmp.path(), DEFAULT_WORKSPACE_SKILLS_DIR);
        let shared = dir(tmp.path(), SHARED_SKILLS_DIR);

        let sources = found(tmp.path(), &config(None), None);

        assert_eq!(
            sources
                .iter()
                .map(|source| &source.path)
                .collect::<Vec<_>>(),
            vec![&basis, &shared],
            "registration is strongest-first, so a name defined in both loads from .basis/skills"
        );
    }

    #[test]
    fn the_workspace_directory_outranks_the_global_one() {
        let tmp = tempfile::tempdir().expect("tempdir");
        dir(tmp.path(), DEFAULT_WORKSPACE_SKILLS_DIR);
        let global = tmp.path().join("global");
        dir(&global, DEFAULT_GLOBAL_SKILLS_DIR);

        let sources = found(tmp.path(), &config(Some(global)), None);

        assert_eq!(sources.len(), 2);
        assert_eq!(sources[0].scope, ContextScope::Workspace);
        assert_eq!(sources[1].scope, ContextScope::Global);
    }

    #[test]
    fn a_global_directory_alone_is_used() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let global = tmp.path().join("global");
        dir(&global, DEFAULT_GLOBAL_SKILLS_DIR);

        let sources = found(tmp.path(), &config(Some(global)), None);

        assert_eq!(sources.len(), 1);
        assert_eq!(sources[0].scope, ContextScope::Global);
    }

    #[test]
    fn the_shared_user_directory_is_found_behind_the_basis_one() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let global = tmp.path().join("global");
        let home = tmp.path().join("home");
        let basis_global = dir(&global, DEFAULT_GLOBAL_SKILLS_DIR);
        let shared_user = dir(&home, SHARED_SKILLS_DIR);

        let sources = found(tmp.path(), &config(Some(global)), Some(&home));

        assert_eq!(
            sources
                .iter()
                .map(|source| &source.path)
                .collect::<Vec<_>>(),
            vec![&basis_global, &shared_user]
        );
        assert!(
            sources
                .iter()
                .all(|source| source.scope == ContextScope::Global)
        );
    }

    #[test]
    fn all_four_roots_come_back_most_specific_first() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let workspace = tmp.path().join("repo");
        let global = tmp.path().join("global");
        let home = tmp.path().join("home");

        let expected = [
            dir(&workspace, DEFAULT_WORKSPACE_SKILLS_DIR),
            dir(&workspace, SHARED_SKILLS_DIR),
            dir(&global, DEFAULT_GLOBAL_SKILLS_DIR),
            dir(&home, SHARED_SKILLS_DIR),
        ];

        let sources = found(&workspace, &config(Some(global)), Some(&home));

        assert_eq!(
            sources
                .iter()
                .map(|source| &source.path)
                .collect::<Vec<_>>(),
            expected.iter().collect::<Vec<_>>()
        );
    }

    #[test]
    fn a_missing_root_is_simply_absent() {
        // Three of the four exist; the fourth leaves no hole and no error.
        let tmp = tempfile::tempdir().expect("tempdir");
        let workspace = tmp.path().join("repo");
        let global = tmp.path().join("global");
        let home = tmp.path().join("home");

        let basis = dir(&workspace, DEFAULT_WORKSPACE_SKILLS_DIR);
        let basis_global = dir(&global, DEFAULT_GLOBAL_SKILLS_DIR);
        let shared_user = dir(&home, SHARED_SKILLS_DIR);

        let sources = found(&workspace, &config(Some(global)), Some(&home));

        assert_eq!(
            sources
                .iter()
                .map(|source| &source.path)
                .collect::<Vec<_>>(),
            vec![&basis, &basis_global, &shared_user]
        );
    }

    #[test]
    fn disabling_the_shared_home_root_alone_leaves_the_other_three() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let workspace = tmp.path().join("repo");
        let global = tmp.path().join("global");
        let home = tmp.path().join("home");
        dir(&workspace, DEFAULT_WORKSPACE_SKILLS_DIR);
        dir(&global, DEFAULT_GLOBAL_SKILLS_DIR);
        dir(&home, SHARED_SKILLS_DIR);

        let sources = found(
            &workspace,
            &SkillsConfig {
                shared_home_dir: false,
                ..config(Some(global))
            },
            Some(&home),
        );

        assert_eq!(sources.len(), 2, "workspace root and global root only");
        assert!(
            sources
                .iter()
                .all(|source| source.path != home.join(SHARED_SKILLS_DIR))
        );
    }

    #[test]
    fn a_missing_global_dir_no_longer_silences_the_home_root() {
        // Before D9 one `Option` gated both the global root and the home
        // root; each disables on its own switch now, so a caller can keep
        // this one while turning the other off, or the reverse.
        let tmp = tempfile::tempdir().expect("tempdir");
        let home = tmp.path().join("home");
        let shared_user = dir(&home, SHARED_SKILLS_DIR);

        let sources = found(tmp.path(), &config(None), Some(&home));

        assert_eq!(
            sources,
            vec![SkillsSource {
                path: shared_user,
                scope: ContextScope::Global,
            }]
        );
    }

    #[test]
    fn disabling_the_global_dir_alone_leaves_the_home_root() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let global = tmp.path().join("global");
        let home = tmp.path().join("home");
        dir(&global, DEFAULT_GLOBAL_SKILLS_DIR);
        let shared_user = dir(&home, SHARED_SKILLS_DIR);

        let sources = found(
            tmp.path(),
            &SkillsConfig {
                global_dir: None,
                ..config(Some(global))
            },
            Some(&home),
        );

        assert_eq!(
            sources,
            vec![SkillsSource {
                path: shared_user,
                scope: ContextScope::Global,
            }]
        );
    }

    #[test]
    fn disabling_the_basis_workspace_root_alone_leaves_the_shared_one() {
        let tmp = tempfile::tempdir().expect("tempdir");
        dir(tmp.path(), DEFAULT_WORKSPACE_SKILLS_DIR);
        let shared = dir(tmp.path(), SHARED_SKILLS_DIR);

        let sources = found(
            tmp.path(),
            &SkillsConfig {
                workspace_subdir: None,
                ..config(None)
            },
            None,
        );

        assert_eq!(
            sources,
            vec![SkillsSource {
                path: shared,
                scope: ContextScope::Workspace,
            }]
        );
    }

    #[test]
    fn disabling_the_shared_workspace_root_alone_leaves_the_basis_one() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let basis = dir(tmp.path(), DEFAULT_WORKSPACE_SKILLS_DIR);
        dir(tmp.path(), SHARED_SKILLS_DIR);

        let sources = found(
            tmp.path(),
            &SkillsConfig {
                shared_workspace_dir: false,
                ..config(None)
            },
            None,
        );

        assert_eq!(
            sources,
            vec![SkillsSource {
                path: basis,
                scope: ContextScope::Workspace,
            }]
        );
    }

    #[test]
    fn a_file_where_the_directory_should_be_is_ignored() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let skills = tmp.path().join(DEFAULT_WORKSPACE_SKILLS_DIR);
        std::fs::create_dir_all(skills.parent().expect("parent")).expect("create .basis");
        std::fs::write(&skills, "not a directory").expect("write file");

        assert!(found(tmp.path(), &config(None), None).is_empty());
    }

    #[test]
    fn the_same_directory_reached_twice_is_reported_once() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let global = tmp.path().join("global");
        dir(&global, DEFAULT_GLOBAL_SKILLS_DIR);

        // Point the workspace subdir at the very same place.
        let sources = found(
            &global,
            &SkillsConfig {
                workspace_subdir: Some(PathBuf::from(DEFAULT_GLOBAL_SKILLS_DIR)),
                shared_workspace_dir: false,
                global_dir: Some(global.clone()),
                shared_home_dir: false,
            },
            None,
        );

        assert_eq!(sources.len(), 1);
        assert_eq!(sources[0].scope, ContextScope::Workspace);
    }

    #[test]
    fn a_home_that_is_the_workspace_does_not_register_one_directory_twice() {
        // The shared spelling is the same in both scopes, so a workspace
        // opened at `$HOME` reaches `.agents/skills` from either side.
        let tmp = tempfile::tempdir().expect("tempdir");
        let shared = dir(tmp.path(), SHARED_SKILLS_DIR);
        let global = tmp.path().join("global");
        dir(&global, DEFAULT_GLOBAL_SKILLS_DIR);

        let sources = found(tmp.path(), &config(Some(global)), Some(tmp.path()));

        assert_eq!(
            sources
                .iter()
                .filter(|source| source.path == shared)
                .count(),
            1
        );
        assert_eq!(sources[0].scope, ContextScope::Workspace);
    }
}