Skip to main content

base/interface/
instruction_provider.rs

1//! `InstructionProvider` and `RuleProvider` — where the project's standing
2//! instructions come from.
3//!
4//! Two things reach the model without anyone asking for them: the instruction
5//! file (`AGENTS.md` / `CLAUDE.md`), injected verbatim, and the `.agix/rules/`
6//! index, advertised by name and first line so the model knows the documents
7//! exist. Both were a fixed answer to a question a deployment may answer
8//! differently — one path out of `settings.prompt.instruction_files`, and three
9//! directory tiers written into the discovery function. A host whose
10//! conventions live in a service, a monorepo tool that composes its own tier
11//! list, or an embedder with no filesystem at all had nothing to replace.
12//!
13//! The two stay separate contracts because they are read differently:
14//! instruction documents are content that goes into the prompt, rules are an
15//! index whose content is deliberately *not* loaded until the model asks for
16//! it. The sources that read files (`InstructionFile`, `RuleDirectory`) live
17//! in `prompt_files`; what is here is the contract and the in-memory ones.
18
19use crate::rules::RuleEntry;
20use std::path::PathBuf;
21
22/// One standing-instruction document.
23pub struct InstructionDoc {
24    /// Where it came from, for the header the engine writes when more than
25    /// one document applies. Need not exist on disk.
26    pub path: PathBuf,
27    pub content: String,
28}
29
30/// A place standing instructions come from.
31pub trait InstructionProvider: Send + Sync {
32    fn id(&self) -> &str;
33
34    /// The documents that apply, outermost first — the engine treats the last
35    /// one as the most specific and lets it win a conflict.
36    fn documents(&self) -> Vec<InstructionDoc>;
37}
38
39/// Instructions the host already has in memory.
40///
41/// A daemon that fetched the project's conventions over its own API, or a
42/// test harness that wants a known instruction text without a temp directory,
43/// hands them over directly rather than staging a file for the engine to read
44/// back.
45pub struct InlineInstructions {
46    label: PathBuf,
47    text: String,
48}
49
50impl InlineInstructions {
51    pub fn new(label: impl Into<PathBuf>, text: impl Into<String>) -> Self {
52        Self {
53            label: label.into(),
54            text: text.into(),
55        }
56    }
57}
58
59impl InstructionProvider for InlineInstructions {
60    fn id(&self) -> &str {
61        "inline"
62    }
63
64    fn documents(&self) -> Vec<InstructionDoc> {
65        if self.text.trim().is_empty() {
66            return Vec::new();
67        }
68        vec![InstructionDoc {
69            path: self.label.clone(),
70            content: self.text.clone(),
71        }]
72    }
73}
74
75/// A place rule documents come from.
76///
77/// Ordered by the caller: a later source's entry replaces an earlier one of
78/// the same name, which is how project beats scene beats global.
79pub trait RuleProvider: Send + Sync {
80    fn id(&self) -> &str;
81
82    fn rules(&self) -> Vec<RuleEntry>;
83}
84
85/// Rules the host already knows about.
86///
87/// The index is a name, a one-line summary and something the Read tool can
88/// open — none of which has to come from a directory scan. A deployment
89/// serving its rule documents out of a repository service builds the entries
90/// itself and keeps the paths its own tooling understands.
91pub struct StaticRules {
92    id: String,
93    entries: Vec<RuleEntry>,
94}
95
96impl StaticRules {
97    pub fn new(id: impl Into<String>, entries: Vec<RuleEntry>) -> Self {
98        Self {
99            id: id.into(),
100            entries,
101        }
102    }
103}
104
105impl RuleProvider for StaticRules {
106    fn id(&self) -> &str {
107        &self.id
108    }
109
110    fn rules(&self) -> Vec<RuleEntry> {
111        self.entries.clone()
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn a_host_can_supply_instructions_without_a_file() {
121        let docs = InlineInstructions::new("service://conventions", "Be brief.").documents();
122        assert_eq!(docs.len(), 1);
123        assert_eq!(docs[0].content, "Be brief.");
124        assert!(InlineInstructions::new("x", "  ").documents().is_empty());
125    }
126
127    #[test]
128    fn a_host_can_supply_a_rule_index_it_built_itself() {
129        let entries = vec![RuleEntry {
130            name: "testing".into(),
131            description: "How we test".into(),
132            path: PathBuf::from("/srv/rules/testing.md"),
133        }];
134        let source = StaticRules::new("service", entries.clone());
135        assert_eq!(source.id(), "service");
136        assert_eq!(source.rules(), entries);
137    }
138}