aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-cli / src / rules.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use colored::*; |
| 17 | use std::collections::HashSet; |
| 18 | use std::fs; |
| 19 | use std::path::{Path, PathBuf}; |
| 20 | |
| 21 | #[derive(Debug, Clone)] |
| 22 | pub struct Rule { |
| 23 | pub name: String, |
| 24 | pub path: PathBuf, |
| 25 | pub content: String, |
| 26 | } |
| 27 | |
| 28 | pub fn discover_rules(cwd: &Path) -> Vec<Rule> { |
| 29 | let mut rules = Vec::new(); |
| 30 | let mut current = Some(cwd.to_path_buf()); |
| 31 | let mut seen_paths = HashSet::new(); |
| 32 | |
| 33 | while let Some(dir) = current { |
| 34 | // 1. Check AGENTS.md |
| 35 | let agents_md = dir.join("AGENTS.md"); |
| 36 | if agents_md.is_file() { |
| 37 | if let Ok(canon) = agents_md.canonicalize() { |
| 38 | if seen_paths.insert(canon.clone()) { |
| 39 | if let Ok(content) = fs::read_to_string(&canon) { |
| 40 | rules.push(Rule { |
| 41 | name: format!("AGENTS.md ({})", dir.display()), |
| 42 | path: canon, |
| 43 | content, |
| 44 | }); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // 2. Check GEMINI.md |
| 51 | let gemini_md = dir.join("GEMINI.md"); |
| 52 | if gemini_md.is_file() { |
| 53 | if let Ok(canon) = gemini_md.canonicalize() { |
| 54 | if seen_paths.insert(canon.clone()) { |
| 55 | if let Ok(content) = fs::read_to_string(&canon) { |
| 56 | rules.push(Rule { |
| 57 | name: format!("GEMINI.md ({})", dir.display()), |
| 58 | path: canon, |
| 59 | content, |
| 60 | }); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // 3. Check .agents/rules/*.md |
| 67 | let rules_dir = dir.join(".agents/rules"); |
| 68 | if rules_dir.is_dir() { |
| 69 | if let Ok(entries) = fs::read_dir(&rules_dir) { |
| 70 | for entry in entries.flatten() { |
| 71 | let path = entry.path(); |
| 72 | if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("md") { |
| 73 | if let Ok(canon) = path.canonicalize() { |
| 74 | if seen_paths.insert(canon.clone()) { |
| 75 | if let Ok(content) = fs::read_to_string(&canon) { |
| 76 | let fname = canon |
| 77 | .file_name() |
| 78 | .map(|n| n.to_string_lossy().to_string()) |
| 79 | .unwrap_or_else(|| "rule".to_string()); |
| 80 | rules.push(Rule { |
| 81 | name: format!("{} ({})", fname, dir.display()), |
| 82 | path: canon, |
| 83 | content, |
| 84 | }); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Stop ascending if we reached git repository root or root filesystem |
| 94 | if dir.join(".git").exists() || dir.parent().is_none() { |
| 95 | break; |
| 96 | } |
| 97 | current = dir.parent().map(|p| p.to_path_buf()); |
| 98 | } |
| 99 | |
| 100 | rules |
| 101 | } |
| 102 | |
| 103 | pub fn format_rules_for_prompt(rules: &[Rule]) -> String { |
| 104 | if rules.is_empty() { |
| 105 | return String::new(); |
| 106 | } |
| 107 | let mut out = String::from( |
| 108 | " |
| 109 | ACTIVE REPOSITORY & WORKSPACE RULES: |
| 110 | ", |
| 111 | ); |
| 112 | for r in rules { |
| 113 | out.push_str(&format!( |
| 114 | " |
| 115 | [Rule: {}] |
| 116 | ", |
| 117 | r.name |
| 118 | )); |
| 119 | out.push_str(&r.content); |
| 120 | out.push_str( |
| 121 | " |
| 122 | [/Rule] |
| 123 | ", |
| 124 | ); |
| 125 | } |
| 126 | out |
| 127 | } |
| 128 | |
| 129 | pub fn format_rules_tui(cwd: &Path) -> String { |
| 130 | let rules = discover_rules(cwd); |
| 131 | let mut out = String::new(); |
| 132 | out.push_str(&format!( |
| 133 | "{} |
| 134 | ", |
| 135 | "=== Active Antigravity Repository Rules ===".cyan().bold() |
| 136 | )); |
| 137 | out.push_str(&format!( |
| 138 | "Working Directory: {} |
| 139 | |
| 140 | ", |
| 141 | cwd.display().to_string().dimmed() |
| 142 | )); |
| 143 | |
| 144 | if rules.is_empty() { |
| 145 | out.push_str(&format!( |
| 146 | "{} |
| 147 | ", |
| 148 | "No active rules discovered in current working tree.".dimmed() |
| 149 | )); |
| 150 | return out; |
| 151 | } |
| 152 | |
| 153 | for (i, r) in rules.iter().enumerate() { |
| 154 | out.push_str(&format!( |
| 155 | "{}. {} ({}) |
| 156 | ", |
| 157 | i + 1, |
| 158 | r.name.green().bold(), |
| 159 | r.path.display().to_string().dimmed() |
| 160 | )); |
| 161 | } |
| 162 | out |
| 163 | } |