bunsen 0.42.0

bunsen is a batteries included common library for burn
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Choosing: composing a query from a context and a request, and running it.
//!
//! The pipeline that runs is a concatenation; nothing is merged:
//!
//! ```text
//! the tool's prefix ++ defaults ++ the request (each `use` spliced in place) ++ the tail
//! ```
//!
//! - **The prefix** ([`PresetSelection::prefix_stage`]) is the applicability of
//!   the presets a tool will apply: one `keep: {$presets: [..]}`, first. No
//!   stage adds a row, so every row of the result can take those presets,
//!   whatever stages follow, and the query needn't mention them.
//! - **The defaults** ([`SelectionContext::defaults`]) apply to every request
//!   but one whose only name is one build. They should hold only stages that
//!   commute: `from`, fixed tests, and multiplying `weigh`s.
//! - **The request**: its names first (a named selection's name is a `use`; the
//!   other names are one `from`), then its stages.
//! - **Labels**: a stage labeled `as: L` can be removed by a later `unset: L`,
//!   so a request can drop a hard default. Naming a label no earlier stage has,
//!   or giving two stages one label, is an error.
//! - **The tail**: `order: {desc: $weight}` when nothing orders, and, when
//!   choosing, `limit: 1`.
//!
//! A query only filters, weighs and orders: it never selects presets. The
//! caller applies the presets it chose.

use alloc::rc::Rc;

use serde_json::Value;

use crate::{
    data::catalog::{
        document::{
            AxisKind,
            CatalogEntry,
            EntryKind,
        },
        error::CatalogErrorExt,
        lookup::LeafMatch,
        name::CatalogName,
        presets::PresetSelection,
        select::{
            query::{
                LabeledStage,
                NameList,
                PerStage,
                Predicate,
                SelectQuery,
                SortKey,
                SortKeys,
                Stage,
                query_error,
            },
            run::MEMBERSHIP_KINDS,
            state::{
                SelectBuild,
                SelectEnv,
                SelectRun,
                SelectTable,
                StageOrigin,
            },
        },
        stack::BunsenCatalogStack,
    },
    errors::{
        BunsenError,
        BunsenResult,
    },
};

/// A stage, with its label and where it came from.
#[derive(Clone, Debug, PartialEq)]
pub struct OriginStage {
    /// The stage and its label.
    pub stage: LabeledStage,
    /// Where it came from.
    pub origin: StageOrigin,
}

impl OriginStage {
    /// `stage`, unlabeled, from `origin`.
    pub fn new(
        stage: Stage,
        origin: StageOrigin,
    ) -> Self {
        Self::labeled(stage.into(), origin)
    }

    /// `stage`, with its label, from `origin`.
    pub fn labeled(
        stage: LabeledStage,
        origin: StageOrigin,
    ) -> Self {
        Self { stage, origin }
    }
}

/// A stored list of stages a request names: `use: en`.
#[derive(Clone, Debug, PartialEq)]
pub struct NamedSelection {
    /// Its name.
    pub name: String,
    /// A line for a listing.
    pub description: String,
    /// The stages it splices in.
    pub run: Vec<LabeledStage>,
}

/// What a tool chooses with: the defaults every request starts with, and
/// the named selections.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SelectionContext {
    /// The default stages, unless the request's only name is one build.
    pub defaults: Vec<LabeledStage>,
    /// The named selections.
    pub named: Vec<NamedSelection>,
}

/// What a request asks: a kind, names, then stages.
#[derive(Clone, Debug, PartialEq)]
pub struct SelectRequest {
    /// The kind it selects; `build` by default.
    pub kind: EntryKind,
    /// Names, in order: a named selection's name is a `use`, and the others
    /// are one `from`.
    pub names: Vec<String>,
    /// The stages, in order.
    pub stages: Vec<OriginStage>,
    /// The query's floor; the default when absent.
    pub floor: Option<f64>,
}

impl Default for SelectRequest {
    fn default() -> Self {
        Self {
            kind: EntryKind::Build,
            names: Vec::new(),
            stages: Vec::new(),
            floor: None,
        }
    }
}

impl SelectRequest {
    /// A request for entries of `kind`.
    pub fn of_kind(kind: EntryKind) -> Self {
        Self {
            kind,
            ..Self::default()
        }
    }

    /// This request with the name `name`.
    pub fn name(
        mut self,
        name: impl Into<String>,
    ) -> Self {
        self.names.push(name.into());
        self
    }

    /// This request with `stage`, from `origin`.
    pub fn stage(
        mut self,
        stage: Stage,
        origin: StageOrigin,
    ) -> Self {
        self.stages.push(OriginStage::new(stage, origin));
        self
    }
}

/// A composed query: the stages and where each came from.
#[derive(Clone, Debug, PartialEq)]
pub struct ComposedQuery {
    /// The query.
    pub query: SelectQuery,
    /// Each stage's origin, by index.
    pub origins: Vec<StageOrigin>,
    /// The build the request named alone, when it did.
    pub exact: Option<CatalogName>,
}

impl PresetSelection {
    /// The prefix a tool injects for these presets, which it will apply: one
    /// `keep: {$presets: [..]}`, attributed to `flags` (the flags as written).
    /// `None` when there are no presets.
    pub fn prefix_stage(
        &self,
        flags: Vec<String>,
    ) -> Option<OriginStage> {
        if self.presets.is_empty() {
            return None;
        }
        let names = self.presets.iter().cloned().map(Value::String).collect();
        Some(OriginStage::new(
            Stage::Keep(Predicate::Test {
                key: "$presets".to_string(),
                value: Value::Array(names),
            }),
            StageOrigin::Prefix(flags),
        ))
    }
}

impl BunsenCatalogStack {
    /// The member of the bool axis `axis` (a lookup) that `--on` (`on`) or
    /// `--off` names: `X` or `no-X`. It is read from the axis's declaration, so
    /// it needs no build.
    ///
    /// # Errors
    /// [`CatalogRule::Selection`](crate::data::catalog::CatalogRule::Selection)
    /// when `axis` names no axis, or one that isn't bool.
    pub fn bool_axis_member(
        &self,
        axis: &str,
        on: bool,
    ) -> BunsenResult<String> {
        let name = self
            .find(axis, &[EntryKind::Axis], LeafMatch::Exact)
            .map_err(|e| query_error(format!("`{axis}`: {}", e.problem_message())))?;
        let decl = self.declaration(&name).expect("found");
        match decl.body() {
            CatalogEntry::Axis(a) if a.axis == AxisKind::Bool => Ok(if on {
                a.name.clone()
            } else {
                a.negate(&a.name).expect("a bool axis negates its positive")
            }),
            _ => Err(query_error(format!(
                "`{name}` is not a bool axis: name its member as a preset"
            ))),
        }
    }
}

impl SelectRun {
    /// The error for this empty result, with the stages that removed rows.
    pub fn empty_error(&self) -> BunsenError {
        let stages: Vec<String> = self
            .trace
            .stages
            .iter()
            .filter(|s| s.dropped > 0)
            .map(|s| {
                format!(
                    "stage {} (`{}`, {}) dropped {}",
                    s.index, s.op, s.origin, s.dropped
                )
            })
            .collect();
        let mut message = "no build is left to choose".to_string();
        if !stages.is_empty() {
            message.push_str(&format!(": {}", stages.join("; ")));
        }
        query_error(message)
    }
}

/// How deep `use` may nest.
const MAX_USE_DEPTH: usize = 8;

impl SelectionContext {
    /// The named selection called `name`.
    pub fn named(
        &self,
        name: &str,
    ) -> Option<&NamedSelection> {
        self.named
            .iter()
            .find(|n| n.name.eq_ignore_ascii_case(name))
    }

    /// The pipeline `request` makes over the context, with `prefix` first,
    /// as a query of the request's kind; `choosing` adds the tail's
    /// `limit: 1`.
    ///
    /// # Errors
    /// [`CatalogRule::Selection`](crate::data::catalog::CatalogRule::Selection)
    /// for a `use` of a name no named selection has, or one nested too deep.
    pub fn compose(
        &self,
        env: &SelectEnv<'_>,
        prefix: Option<OriginStage>,
        request: &SelectRequest,
        choosing: bool,
    ) -> BunsenResult<ComposedQuery> {
        let mut stages: Vec<OriginStage> = prefix.into_iter().collect();
        let (uses, refs): (Vec<&String>, Vec<&String>) =
            request.names.iter().partition(|n| self.named(n).is_some());
        let exact = match (refs.as_slice(), uses.is_empty()) {
            ([only], true) if request.kind == EntryKind::Build => env
                .stack()
                .find(only, &MEMBERSHIP_KINDS, LeafMatch::Exact)
                .ok()
                .filter(|n| {
                    env.stack()
                        .declaration(n)
                        .is_some_and(|d| d.kind() == EntryKind::Build)
                }),
            _ => None,
        };
        if exact.is_none() {
            for stage in &self.defaults {
                self.push(&mut stages, stage, StageOrigin::Default, 0)?;
            }
        }
        for name in uses {
            let stage = Stage::Use(name.clone()).into();
            self.push(&mut stages, &stage, StageOrigin::Query, 0)?;
        }
        if !refs.is_empty() {
            stages.push(OriginStage::new(
                Stage::From(NameList(refs.into_iter().cloned().collect())),
                StageOrigin::Query,
            ));
        }
        for stage in &request.stages {
            self.push(&mut stages, &stage.stage, stage.origin.clone(), 0)?;
        }
        if !stages
            .iter()
            .any(|s| matches!(s.stage.stage, Stage::Order(_)))
        {
            stages.push(OriginStage::new(
                Stage::Order(SortKeys(vec![SortKey::desc("$weight")])),
                StageOrigin::Tail,
            ));
        }
        if choosing {
            stages.push(OriginStage::new(Stage::Limit(1), StageOrigin::Tail));
        }
        let (run, origins) = stages.into_iter().map(|s| (s.stage, s.origin)).unzip();
        Ok(ComposedQuery {
            query: SelectQuery {
                kind: request.kind,
                floor: request.floor,
                run,
            },
            origins,
            exact,
        })
    }

    /// Pushes `stage`: a `use` splices its stages in place, an `unset`
    /// removes the earlier stage with its label, and a `per` has the `use`s
    /// in its stages spliced.
    fn push(
        &self,
        stages: &mut Vec<OriginStage>,
        stage: &LabeledStage,
        origin: StageOrigin,
        depth: usize,
    ) -> BunsenResult<()> {
        match &stage.stage {
            Stage::Use(name) => {
                for inner in &self.named_run(name, depth)?.run {
                    self.push(stages, inner, StageOrigin::Use(name.clone()), depth + 1)?;
                }
            }
            Stage::Unset(label) => {
                let at = stages
                    .iter()
                    .position(|s| s.stage.label.as_ref() == Some(label))
                    .ok_or_else(|| {
                        query_error(format!(
                            "`unset: {label}`: no earlier stage is labeled `{label}`"
                        ))
                    })?;
                stages.remove(at);
            }
            other => {
                if let Some(label) = &stage.label
                    && stages.iter().any(|s| s.stage.label.as_ref() == Some(label))
                {
                    return Err(query_error(format!("two stages are labeled `{label}`")));
                }
                let mut stage = stage.clone();
                if let Stage::Per(per) = other {
                    let mut inner = Vec::new();
                    for s in &per.run {
                        self.push(&mut inner, s, StageOrigin::Query, depth)?;
                    }
                    stage.stage = Stage::Per(PerStage {
                        group: per.group.clone(),
                        run: inner.into_iter().map(|s| s.stage).collect(),
                    });
                }
                stages.push(OriginStage::labeled(stage, origin));
            }
        }
        Ok(())
    }

    /// The named selection `name`, for a `use` nested `depth` deep.
    fn named_run(
        &self,
        name: &str,
        depth: usize,
    ) -> BunsenResult<&NamedSelection> {
        if depth >= MAX_USE_DEPTH {
            return Err(query_error(format!(
                "`use: {name}` nests more than {MAX_USE_DEPTH} deep"
            )));
        }
        self.named(name)
            .ok_or_else(|| query_error(format!("`use: {name}`: no named selection `{name}`")))
    }

    /// Runs `composed` over the entries of its kind in `env`'s stack (all of
    /// them, or those in `universe`), traced.
    ///
    /// # Errors
    /// As [`SelectQuery::compile`], and when the request named one build
    /// alone that the run removed, with the reason.
    pub fn run_composed(
        &self,
        env: &SelectEnv<'_>,
        universe: Option<&[CatalogName]>,
        composed: &ComposedQuery,
    ) -> BunsenResult<SelectRun> {
        let mut compiled = composed.query.compile(env)?;
        for (index, origin) in composed.origins.iter().enumerate() {
            compiled.set_origin(index, origin.clone());
        }
        let kind = composed.query.kind;
        let table = match universe {
            Some(names) => SelectTable::of(kind, names.iter().cloned()),
            None => SelectTable::universe(env, kind),
        };
        let run = compiled.run_traced(env, table)?;
        if let Some(build) = &composed.exact
            && !run.table.rows.iter().any(|r| &r.entry.path == build)
        {
            let why = run
                .trace
                .dropped
                .iter()
                .find(|d| &d.entry.path == build)
                .map(|d| format!(": {}", d.why))
                .unwrap_or_default();
            return Err(query_error(format!(
                "the build `{build}` was named, but can't be chosen{why}"
            )));
        }
        Ok(run)
    }

    /// Every entry `request` (with `prefix` first) selects, in order, traced.
    ///
    /// # Errors
    /// As [`compose`](Self::compose) and [`run_composed`](Self::run_composed).
    pub fn rank(
        &self,
        env: &SelectEnv<'_>,
        universe: Option<&[CatalogName]>,
        prefix: Option<OriginStage>,
        request: &SelectRequest,
    ) -> BunsenResult<SelectRun> {
        let composed = self.compose(env, prefix, request, false)?;
        self.run_composed(env, universe, &composed)
    }

    /// The one build `request` (with `prefix` first) chooses, as loading a
    /// model does: the first of the result, by the tail's `limit: 1`.
    ///
    /// # Errors
    /// As [`rank`](Self::rank), and [`SelectRun::empty_error`] when no build is
    /// left.
    pub fn choose(
        &self,
        env: &SelectEnv<'_>,
        universe: Option<&[CatalogName]>,
        prefix: Option<OriginStage>,
        request: &SelectRequest,
    ) -> BunsenResult<(Rc<SelectBuild>, SelectRun)> {
        if request.kind != EntryKind::Build {
            return Err(query_error(format!(
                "choosing takes a build, not a `{}`; rank a query of other kinds",
                request.kind
            )));
        }
        let composed = self.compose(env, prefix, request, true)?;
        let run = self.run_composed(env, universe, &composed)?;
        let Some(row) = run.table.rows.first() else {
            return Err(run.empty_error());
        };
        let build = env.build(&row.entry.path)?;
        Ok((build, run))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::catalog::testing::fixtures;

    fn leaves(run: &SelectRun) -> Vec<String> {
        run.table
            .names()
            .iter()
            .map(|n| n.leaf().to_string())
            .collect()
    }

    fn stages(yaml: &str) -> Vec<LabeledStage> {
        serde_saphyr::from_str(yaml).unwrap()
    }

    fn context() -> SelectionContext {
        SelectionContext {
            defaults: stages("[{keep: instruct}, {order: {desc: parameters}}]"),
            named: vec![NamedSelection {
                name: "en".into(),
                description: "English speech.".into(),
                run: stages("[{from: whisper}, {keep: {language: en}}]"),
            }],
        }
    }

    #[test]
    fn test_defaults_apply_unless_one_build_is_named() {
        let stack = fixtures::example_stack().unwrap();
        let env = SelectEnv::new(&stack);
        let run = context()
            .rank(&env, None, None, &SelectRequest::default())
            .unwrap();
        assert_eq!(leaves(&run), ["qwen3-4b-thinking-2507", "qwen3-0.6b"]);
        // One build named alone skips the defaults.
        let base = SelectRequest::default().name("qwen3-0.6b-base");
        let run = context().rank(&env, None, None, &base).unwrap();
        assert_eq!(leaves(&run), ["qwen3-0.6b-base"]);
    }

    #[test]
    fn test_a_named_selection_is_spliced_where_it_is_named() {
        let stack = fixtures::example_stack().unwrap();
        let env = SelectEnv::new(&stack);
        let context = SelectionContext {
            defaults: Vec::new(),
            ..context()
        };
        let request = SelectRequest::default().name("en");
        let composed = context.compose(&env, None, &request, false).unwrap();
        assert_eq!(
            composed.origins,
            [
                StageOrigin::Use("en".into()),
                StageOrigin::Use("en".into()),
                StageOrigin::Tail
            ]
        );
        let run = context.run_composed(&env, None, &composed).unwrap();
        assert_eq!(run.table.len(), 4);
        let unknown = SelectRequest::default().stage(Stage::Use("fr".into()), StageOrigin::Query);
        assert!(context.compose(&env, None, &unknown, false).is_err());
    }

    #[test]
    fn test_the_prefix_holds_for_every_row_whatever_follows() {
        let stack = fixtures::example_stack().unwrap();
        let env = SelectEnv::new(&stack);
        let off = stack.bool_axis_member("thinking", false).unwrap();
        assert_eq!(off, "no-thinking");
        let prefix =
            || PresetSelection::of([off.as_str()]).prefix_stage(vec!["--off thinking".into()]);
        // A `limit` first still yields only builds that can take the preset.
        let request = SelectRequest::default()
            .stage(Stage::Limit(1), StageOrigin::Flag("--limit 1".into()))
            .name("qwen3");
        let context = SelectionContext::default();
        let run = context.rank(&env, None, prefix(), &request).unwrap();
        assert_eq!(leaves(&run), ["qwen3-0.6b"]);
        assert_eq!(
            run.trace.stages[0].origin,
            StageOrigin::Prefix(vec!["--off thinking".into()])
        );
        // A build named alone that can't take it is an error, with why.
        let named = SelectRequest::default().name("qwen3-4b-thinking-2507");
        let err = context.rank(&env, None, prefix(), &named).unwrap_err();
        assert!(
            err.problem_message()
                .contains("was named, but can't be chosen"),
            "{err}"
        );
        assert!(err.problem_message().contains("no-thinking"), "{err}");
        assert!(stack.bool_axis_member("task", true).is_err());
    }

    #[test]
    fn test_choose_takes_the_first_and_an_empty_result_says_why() {
        let stack = fixtures::example_stack().unwrap();
        let env = SelectEnv::new(&stack);
        let (build, _) = context()
            .choose(&env, None, None, &SelectRequest::default())
            .unwrap();
        assert_eq!(build.build.name.leaf(), "qwen3-4b-thinking-2507");
        let none = SelectRequest::default().name("whisper");
        let err = context().choose(&env, None, None, &none).unwrap_err();
        assert!(
            err.problem_message()
                .starts_with("no build is left to choose"),
            "{err}"
        );
        assert!(err.problem_message().contains("defaults"), "{err}");
    }
}