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
//! The rule index: its entries and the merge of several rule sources.
//!
//! Rules stay lazy: the model sees each document's name and first line and
//! reads the rest on demand. Where the documents come from is the
//! [`RuleProvider`](crate::interface::instruction_provider::RuleProvider)
//! contract; the directory-backed sources are in `prompt_files`.
use crate::interface::instruction_provider::RuleProvider;
use std::path::PathBuf;
use std::sync::Arc;
/// One discovered rule document.
#[derive(Debug, Clone, PartialEq)]
pub struct RuleEntry {
/// Filename stem (e.g. "security" for `security.md`).
pub name: String,
/// First non-empty line of the file, `#`/whitespace-stripped — same
/// "fallback to first body line" convention as skills. Capped at
/// `prompt_files::MAX_RULE_DESCRIPTION_CHARS` regardless of how verbose the line is.
pub description: String,
/// Absolute path — always resolvable by the Read tool regardless of
/// which tier (project/scene/global) the file came from.
pub path: PathBuf,
}
/// Rule sources merged into one index: a later source's entry replaces an
/// earlier one of the same name, and the result is sorted by name.
#[derive(Clone)]
pub struct RuleTiers {
sources: Vec<Arc<dyn RuleProvider>>,
}
impl RuleTiers {
pub fn new(sources: Vec<Arc<dyn RuleProvider>>) -> Self {
Self { sources }
}
}
impl RuleProvider for RuleTiers {
fn id(&self) -> &str {
"tiers"
}
fn rules(&self) -> Vec<RuleEntry> {
let mut by_name: std::collections::HashMap<String, RuleEntry> =
std::collections::HashMap::new();
for source in &self.sources {
for entry in source.rules() {
by_name.insert(entry.name.clone(), entry);
}
}
let mut entries: Vec<RuleEntry> = by_name.into_values().collect();
entries.sort_by(|a, b| a.name.cmp(&b.name));
entries
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::interface::instruction_provider::StaticRules;
fn entry(name: &str, description: &str, path: &str) -> RuleEntry {
RuleEntry {
name: name.into(),
description: description.into(),
path: PathBuf::from(path),
}
}
#[test]
fn the_merge_is_sorted_by_name() {
let tiers = RuleTiers::new(vec![Arc::new(StaticRules::new(
"s",
vec![entry("b", "", "/b.md"), entry("a", "", "/a.md")],
))]);
let names: Vec<String> = tiers.rules().into_iter().map(|e| e.name).collect();
assert_eq!(names, ["a", "b"]);
}
/// The tier list is a composition of sources, not three paths written
/// into the discovery function — so a fourth source merges by the same
/// last-wins rule the tiers use.
#[test]
fn a_source_the_engine_does_not_own_takes_part_in_the_merge() {
let sources: Vec<Arc<dyn RuleProvider>> = vec![
Arc::new(StaticRules::new(
"service",
vec![RuleEntry {
name: "testing".into(),
description: "from the service".into(),
path: PathBuf::from("/srv/testing.md"),
}],
)),
Arc::new(StaticRules::new(
"override",
vec![RuleEntry {
name: "testing".into(),
description: "from the later source".into(),
path: PathBuf::from("/late/testing.md"),
}],
)),
];
let entries = RuleTiers::new(sources).rules();
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].description, "from the later source");
}
}