1use std::collections::BTreeMap;
43use std::ffi::OsStr;
44use std::io::{BufRead, BufReader};
45use std::path::{Path, PathBuf};
46use std::process::{Command, Stdio};
47use std::time::Instant;
48
49use ignore::WalkBuilder;
50use regex::Regex;
51use serde::Deserialize;
52use sha2::{Digest, Sha256};
53use thiserror::Error;
54use tracing::{debug, instrument, warn};
55
56use g8_core::{
57 Annotation, AnnotationKind, AnnotationValue, IntentKind, IntentSourceKind, SourceLocation,
58};
59
60#[derive(Debug, Error)]
64pub enum ExtractorError {
65 #[error(
74 "ast-grep binary not found; install with `cargo install ast-grep` or `brew install ast-grep`"
75 )]
76 BinaryNotFound,
77
78 #[error("ast-grep subprocess failed: {0}")]
80 SubprocessFailed(String),
81
82 #[error("failed to parse ast-grep JSON output: {0}")]
84 JsonParse(String),
85
86 #[error("invalid magic-comment annotation at {file}:{line}: {message}")]
88 InvalidAnnotation {
89 file: PathBuf,
90 line: u32,
91 message: String,
92 },
93
94 #[error(transparent)]
96 Io(#[from] std::io::Error),
97}
98
99#[derive(Debug, Default)]
103pub struct ScanResult {
104 pub annotations: Vec<Annotation>,
106 pub warnings: Vec<ScanWarning>,
108 pub stats: ScanStats,
110}
111
112#[derive(Debug)]
114pub struct ScanWarning {
115 pub file: PathBuf,
116 pub line: Option<u32>,
117 pub message: String,
118}
119
120#[derive(Debug, Default)]
122pub struct ScanStats {
123 pub files_scanned: u32,
124 pub annotations_found: u32,
125 pub duration_ms: u64,
126}
127
128pub struct Extractor {
135 rules_path: PathBuf,
136 ast_grep_bin: PathBuf,
137}
138
139const EMBEDDED_RULES: &str = include_str!("../rules/g8-all-languages.yaml");
146
147fn materialize_embedded_rules() -> Result<PathBuf, ExtractorError> {
155 let digest = {
156 let mut hasher = Sha256::new();
157 hasher.update(EMBEDDED_RULES.as_bytes());
158 let full = hasher.finalize();
159 full[..8]
161 .iter()
162 .map(|b| format!("{b:02x}"))
163 .collect::<String>()
164 };
165 let path = std::env::temp_dir().join(format!("g8-rules-{digest}.yaml"));
166 if !path.exists() {
167 let staging = path.with_extension(format!("yaml.{}.tmp", std::process::id()));
168 std::fs::write(&staging, EMBEDDED_RULES)?;
169 std::fs::rename(&staging, &path)?;
170 }
171 Ok(path)
172}
173
174impl Extractor {
175 pub fn new() -> Result<Self, ExtractorError> {
180 Self::with_binary(materialize_embedded_rules()?, find_ast_grep()?)
181 }
182
183 pub fn with_rules(rules_path: PathBuf) -> Result<Self, ExtractorError> {
185 Self::with_binary(rules_path, find_ast_grep()?)
186 }
187
188 pub fn with_binary(rules_path: PathBuf, ast_grep_bin: PathBuf) -> Result<Self, ExtractorError> {
190 Command::new(&ast_grep_bin)
192 .arg("--version")
193 .stdout(Stdio::null())
194 .stderr(Stdio::null())
195 .status()
196 .map_err(|_| ExtractorError::BinaryNotFound)?;
197 Ok(Self {
198 rules_path,
199 ast_grep_bin,
200 })
201 }
202
203 #[instrument(skip(self), fields(root = %root.display()))]
212 pub fn scan_dir(&self, root: &Path) -> Result<ScanResult, ExtractorError> {
213 let start = Instant::now();
214 let mut result = ScanResult::default();
215
216 let mut source_files: Vec<PathBuf> = Vec::new();
218 let mut sidecar_files: Vec<PathBuf> = Vec::new();
219
220 let walker = WalkBuilder::new(root)
221 .standard_filters(true) .build();
223
224 for entry in walker.flatten() {
225 let path = entry.into_path();
226 if !path.is_file() {
227 continue;
228 }
229
230 if path
232 .file_name()
233 .and_then(OsStr::to_str)
234 .map(|n| n == "INTENT_SUMMARY.md")
235 .unwrap_or(false)
236 {
237 continue;
238 }
239
240 match classify_file(&path) {
241 FileKind::Source => source_files.push(path),
242 FileKind::Sidecar(_sk) => sidecar_files.push(path.clone()),
243 FileKind::Other => {}
244 }
245 }
246
247 result.stats.files_scanned = (source_files.len() + sidecar_files.len()) as u32;
248
249 if !source_files.is_empty() {
251 let ast_annotations = self.run_ast_grep(root, &mut result.warnings)?;
252 result.annotations.extend(ast_annotations);
253 }
254
255 for sidecar in &sidecar_files {
257 let sk = sidecar_source_kind(sidecar);
258 match parse_sidecar(sidecar, sk) {
259 Ok(mut anns) => result.annotations.append(&mut anns),
260 Err(e) => result.warnings.push(ScanWarning {
261 file: sidecar.clone(),
262 line: None,
263 message: e.to_string(),
264 }),
265 }
266 }
267
268 dedup_annotations(&mut result.annotations);
270
271 result.stats.annotations_found = result.annotations.len() as u32;
272 result.stats.duration_ms = start.elapsed().as_millis() as u64;
273 Ok(result)
274 }
275
276 #[instrument(skip(self), fields(file = %file.display()))]
281 pub fn scan_file(&self, file: &Path) -> Result<Vec<Annotation>, ExtractorError> {
282 match classify_file(file) {
283 FileKind::Source => {
284 let mut warnings = Vec::new();
285 self.run_ast_grep(file, &mut warnings)
286 }
287 FileKind::Sidecar(sk) => parse_sidecar(file, sk).map_err(ExtractorError::Io),
288 FileKind::Other => Ok(Vec::new()),
289 }
290 }
291
292 #[instrument(skip(self, warnings), fields(target = %target.display()))]
294 fn run_ast_grep(
295 &self,
296 target: &Path,
297 warnings: &mut Vec<ScanWarning>,
298 ) -> Result<Vec<Annotation>, ExtractorError> {
299 let mut child = Command::new(&self.ast_grep_bin)
300 .args(["scan", "--rule"])
301 .arg(&self.rules_path)
302 .arg("--json=stream")
303 .arg(target)
304 .stdout(Stdio::piped())
305 .stderr(Stdio::null()) .spawn()
307 .map_err(|_| ExtractorError::BinaryNotFound)?;
308
309 let stdout = child.stdout.take().expect("piped stdout");
310 let reader = BufReader::new(stdout);
311
312 let name_re = Regex::new(r#"name\s*=\s*"([^"]+)""#).expect("static regex");
313 let mut annotations = Vec::new();
314
315 for line in reader.lines() {
316 let line = line?;
317 let line = line.trim();
318 if line.is_empty() {
319 continue;
320 }
321 let raw: RawAstMatch = match serde_json::from_str(line) {
322 Ok(m) => m,
323 Err(e) => {
324 warn!("failed to parse ast-grep NDJSON line: {e}");
325 continue;
326 }
327 };
328 debug!(rule = ?raw.rule_id, file = %raw.file, "ast-grep match");
329
330 match convert_ast_match(&raw, &name_re) {
331 Ok(Some(ann)) => annotations.push(ann),
332 Ok(None) => {} Err(e) => warnings.push(ScanWarning {
334 file: PathBuf::from(&raw.file),
335 line: Some(raw.range.start.line as u32 + 1),
336 message: e,
337 }),
338 }
339 }
340
341 let status = child.wait()?;
342 if !status.success() {
344 return Err(ExtractorError::SubprocessFailed(format!(
345 "exit code: {}",
346 status.code().unwrap_or(-1)
347 )));
348 }
349
350 Ok(annotations)
351 }
352}
353
354pub fn scan_project(root: &Path) -> Result<Vec<Annotation>, ExtractorError> {
361 let extractor = Extractor::new()?;
362 extractor.scan_dir(root).map(|r| r.annotations)
363}
364
365#[derive(Debug, Deserialize)]
369#[serde(rename_all = "camelCase")]
370struct RawAstMatch {
371 text: String,
372 file: String,
373 range: RawRange,
374 #[serde(default)]
375 rule_id: Option<String>,
376 #[serde(default)]
377 #[allow(dead_code)]
378 language: Option<String>,
379}
380
381#[derive(Debug, Deserialize)]
382struct RawRange {
383 start: RawPos,
384 #[allow(dead_code)]
385 end: RawPos,
386}
387
388#[derive(Debug, Deserialize)]
389struct RawPos {
390 line: usize,
391 column: usize,
392}
393
394#[derive(Debug)]
397enum FileKind {
398 Source,
399 Sidecar(IntentSourceKind),
400 Other,
401}
402
403fn classify_file(path: &Path) -> FileKind {
404 let file_name = path.file_name().and_then(OsStr::to_str).unwrap_or_default();
405
406 if file_name == "AGENTS.md" {
408 return FileKind::Sidecar(IntentSourceKind::AgentsMd);
409 }
410 if file_name == "CLAUDE.md" {
411 return FileKind::Sidecar(IntentSourceKind::ClaudeMd);
412 }
413
414 let ext = path.extension().and_then(OsStr::to_str).unwrap_or_default();
415
416 match ext {
417 "rs" | "ts" | "tsx" | "py" | "go" => FileKind::Source,
418 "md" | "markdown" => {
419 let stem = path.file_stem().and_then(OsStr::to_str).unwrap_or_default();
423 if stem.ends_with(".g8")
424 || file_name.ends_with(".g8.md")
425 || file_name.ends_with(".govern.md")
426 {
427 FileKind::Sidecar(IntentSourceKind::G8Sidecar)
428 } else {
429 FileKind::Other
430 }
431 }
432 _ => FileKind::Other,
433 }
434}
435
436fn sidecar_source_kind(path: &Path) -> IntentSourceKind {
437 let file_name = path.file_name().and_then(OsStr::to_str).unwrap_or_default();
438 if file_name == "AGENTS.md" {
439 IntentSourceKind::AgentsMd
440 } else if file_name == "CLAUDE.md" {
441 IntentSourceKind::ClaudeMd
442 } else {
443 IntentSourceKind::G8Sidecar
444 }
445}
446
447fn convert_ast_match(
452 raw: &RawAstMatch,
453 _name_re: &Regex, ) -> Result<Option<Annotation>, String> {
455 let text = strip_comment_prefixes(&raw.text);
457
458 let g8_start = match text.find("@g8.").or_else(|| text.find("@govern.")) {
460 Some(pos) => pos,
461 None => return Ok(None),
462 };
463 let annotation_text = &text[g8_start..];
464
465 let raw_ann = match g8_core::parse_annotation_grammar(annotation_text) {
467 Ok(r) => r,
468 Err(e) => return Err(e.to_string()),
469 };
470
471 let kind = annotation_kind_from_str(&raw_ann.kind_str)?;
472
473 let name = match kind {
475 AnnotationKind::Capability | AnnotationKind::Plan => raw_ann
476 .fields
477 .get("name")
478 .or_else(|| raw_ann.fields.get("title"))
479 .and_then(|v| {
480 if let AnnotationValue::String(s) = v {
481 Some(s.clone())
482 } else {
483 None
484 }
485 }),
486 _ => None,
487 };
488
489 let heading = if kind == AnnotationKind::Intent {
490 raw_ann
491 .fields
492 .get("description")
493 .or_else(|| raw_ann.fields.get("title"))
494 .and_then(|v| {
495 if let AnnotationValue::String(s) = v {
496 Some(s.clone())
497 } else {
498 None
499 }
500 })
501 } else {
502 None
503 };
504
505 let conditional = is_conditional(&raw.text, &raw_ann.fields);
507
508 let path = PathBuf::from(&raw.file);
510 let in_tests_dir = is_tests_dir(&path);
511 let conditional = conditional || in_tests_dir;
512
513 let source_kind = IntentSourceKind::InlineComment;
515
516 let source = SourceLocation {
518 file: path,
519 line: raw.range.start.line as u32 + 1,
520 column: raw.range.start.column as u32,
521 };
522
523 Ok(Some(Annotation {
524 kind,
525 name,
526 heading,
527 fields: raw_ann.fields,
528 source,
529 source_kind,
530 conditional,
531 raw_text: annotation_text.to_string(),
532 }))
533}
534
535fn strip_comment_prefixes(text: &str) -> String {
570 text.lines()
571 .map(|line| {
572 let trimmed = line.trim_start();
573 if let Some(rest) = trimmed.strip_prefix("// ") {
574 rest
575 } else if let Some(rest) = trimmed.strip_prefix("//") {
576 rest
577 } else if let Some(rest) = trimmed.strip_prefix("# ") {
578 rest
579 } else if let Some(rest) = trimmed.strip_prefix('#') {
580 rest
581 } else {
582 trimmed
583 }
584 })
585 .collect::<Vec<_>>()
586 .join(" ")
587}
588
589fn annotation_kind_from_str(s: &str) -> Result<AnnotationKind, String> {
591 match s {
592 "capability" => Ok(AnnotationKind::Capability),
593 "intent" => Ok(AnnotationKind::Intent),
594 "convergence_test" => Ok(AnnotationKind::ConvergenceTest),
595 "decision" => Ok(AnnotationKind::Decision),
596 "plan" => Ok(AnnotationKind::Plan),
597 other => Err(format!("unknown annotation kind: {other}")),
598 }
599}
600
601fn is_conditional(raw_text: &str, fields: &BTreeMap<String, AnnotationValue>) -> bool {
609 if let Some(AnnotationValue::Bool(true)) = fields.get("dev_only") {
611 return true;
612 }
613 if raw_text.contains("#[cfg(") || raw_text.contains("#[cfg_attr(") {
615 return true;
616 }
617 false
618}
619
620fn is_tests_dir(path: &Path) -> bool {
622 path.components().any(|c| {
623 if let std::path::Component::Normal(s) = c {
624 let s = s.to_string_lossy();
625 s == "tests" || s.starts_with("tests-") || s.ends_with("-tests")
626 } else {
627 false
628 }
629 })
630}
631
632fn parse_sidecar(
649 path: &Path,
650 source_kind: IntentSourceKind,
651) -> Result<Vec<Annotation>, std::io::Error> {
652 let content = std::fs::read_to_string(path)?;
653 let mut annotations = Vec::new();
654
655 let mut current_heading: Option<String> = None;
656 let mut current_lines: Vec<String> = Vec::new();
657 let mut current_line_number: u32 = 0;
658 let mut section_start_line: u32 = 1;
659
660 let flush =
661 |heading: &str, lines: &[String], line_num: u32, annotations: &mut Vec<Annotation>| {
662 let description = lines.join("\n").trim().to_string();
663 let (kind, is_owner, is_parked) = classify_heading(heading);
664
665 if is_owner {
666 let mut fields = BTreeMap::new();
670 parse_owner_block(&description, &mut fields);
671 fields.insert(
672 "heading".to_string(),
673 AnnotationValue::String(heading.to_string()),
674 );
675 annotations.push(Annotation {
676 kind: AnnotationKind::Intent,
677 name: None,
678 heading: Some(heading.to_string()),
679 fields,
680 source: SourceLocation {
681 file: path.to_path_buf(),
682 line: line_num,
683 column: 0,
684 },
685 source_kind,
686 conditional: false,
687 raw_text: description,
688 });
689 return;
690 }
691
692 if is_parked {
693 for (i, line) in lines.iter().enumerate() {
695 let line = line.trim();
696 if let Some(rest) = line.strip_prefix("- ") {
697 let (title, reason) = parse_parked_line(rest);
698 let mut fields = BTreeMap::new();
699 fields.insert("title".to_string(), AnnotationValue::String(title.clone()));
700 fields.insert(
701 "status".to_string(),
702 AnnotationValue::String("Parked".to_string()),
703 );
704 if let Some(r) = reason {
705 fields.insert("parked_reason".to_string(), AnnotationValue::String(r));
706 }
707 annotations.push(Annotation {
708 kind: AnnotationKind::Plan,
709 name: Some(title),
710 heading: None,
711 fields,
712 source: SourceLocation {
713 file: path.to_path_buf(),
714 line: line_num + i as u32,
715 column: 0,
716 },
717 source_kind,
718 conditional: false,
719 raw_text: line.to_string(),
720 });
721 } else if !line.is_empty() {
722 tracing::warn!(
725 file = %path.display(),
726 "parked-ideas line does not match `- <name>: <reason>` form: {line}"
727 );
728 }
729 }
730 return;
731 }
732
733 let mut fields = BTreeMap::new();
735 fields.insert(
736 "description".to_string(),
737 AnnotationValue::String(description.clone()),
738 );
739 fields.insert(
740 "heading".to_string(),
741 AnnotationValue::String(heading.to_string()),
742 );
743 if let AnnotationKind::Intent = AnnotationKind::Intent {
744 let kind_str = intent_kind_str(kind);
745 fields.insert(
746 "intent_kind".to_string(),
747 AnnotationValue::String(kind_str.to_string()),
748 );
749 }
750
751 annotations.push(Annotation {
752 kind: AnnotationKind::Intent,
753 name: None,
754 heading: Some(heading.to_string()),
755 fields,
756 source: SourceLocation {
757 file: path.to_path_buf(),
758 line: line_num,
759 column: 0,
760 },
761 source_kind,
762 conditional: false,
763 raw_text: description,
764 });
765 };
766
767 for line in content.lines() {
768 current_line_number += 1;
769
770 let heading_text = if let Some(h) = line.strip_prefix("## ") {
772 Some(h.trim())
773 } else {
774 line.strip_prefix("# ").map(|h| h.trim())
775 };
776
777 if let Some(h) = heading_text {
778 if let Some(prev_heading) = current_heading.take() {
780 flush(
781 &prev_heading,
782 ¤t_lines,
783 section_start_line,
784 &mut annotations,
785 );
786 current_lines.clear();
787 }
788 current_heading = Some(h.to_string());
789 section_start_line = current_line_number;
790 } else if current_heading.is_some() {
791 current_lines.push(line.to_string());
792 }
793 }
794
795 if let Some(prev_heading) = current_heading.take() {
797 flush(
798 &prev_heading,
799 ¤t_lines,
800 section_start_line,
801 &mut annotations,
802 );
803 }
804
805 Ok(annotations)
806}
807
808fn classify_heading(heading: &str) -> (IntentKind, bool, bool) {
812 let h = heading.to_lowercase();
813 let h = h.trim();
814
815 if matches!(h, "owner" | "ownership") {
817 return (IntentKind::Unclassified, true, false);
818 }
819
820 if matches!(h, "parked ideas" | "deferred") {
822 return (IntentKind::Unclassified, false, true);
823 }
824
825 if matches!(h, "active plans" | "in flight" | "in-flight") {
827 return (IntentKind::Unclassified, false, false);
828 }
829
830 let kind = if matches!(h, "architecture" | "design" | "overview") {
831 IntentKind::ArchitecturalScope
832 } else if matches!(h, "boundaries" | "never do" | "constraints") {
833 IntentKind::Boundary
834 } else if matches!(h, "commands" | "build" | "test" | "testing") {
835 IntentKind::Operational
836 } else if matches!(h, "stack" | "dependencies") {
837 IntentKind::TechStack
838 } else if h == "intent" {
839 IntentKind::ExplicitIntent
840 } else if matches!(h, "decisions" | "architectural decisions") {
841 IntentKind::Unclassified
844 } else {
845 IntentKind::Unclassified
846 };
847
848 (kind, false, false)
849}
850
851fn intent_kind_str(kind: IntentKind) -> &'static str {
853 match kind {
854 IntentKind::ArchitecturalScope => "architectural_scope",
855 IntentKind::Boundary => "boundary",
856 IntentKind::Operational => "operational",
857 IntentKind::TechStack => "tech_stack",
858 IntentKind::ExplicitIntent => "explicit_intent",
859 IntentKind::Unclassified => "unclassified",
860 }
861}
862
863fn parse_owner_block(content: &str, fields: &mut BTreeMap<String, AnnotationValue>) {
872 for line in content.lines() {
873 let line = line.trim();
874 if let Some(rest) = line.strip_prefix("agent:") {
875 fields.insert(
876 "owner_agent".to_string(),
877 AnnotationValue::String(rest.trim().to_string()),
878 );
879 } else if let Some(rest) = line.strip_prefix("team:") {
880 fields.insert(
881 "owner_team".to_string(),
882 AnnotationValue::String(rest.trim().to_string()),
883 );
884 } else if let Some(rest) = line.strip_prefix("contact:") {
885 fields.insert(
886 "owner_contact".to_string(),
887 AnnotationValue::String(rest.trim().to_string()),
888 );
889 }
890 }
891}
892
893fn parse_parked_line(rest: &str) -> (String, Option<String>) {
897 if let Some(colon_pos) = rest.find(": ") {
898 let name = rest[..colon_pos].trim().to_string();
899 let reason = rest[colon_pos + 2..].trim().to_string();
900 (name, Some(reason))
901 } else {
902 (rest.trim().to_string(), None)
903 }
904}
905
906fn dedup_annotations(annotations: &mut Vec<Annotation>) {
909 use std::collections::HashSet;
910 let mut seen: HashSet<(PathBuf, u32, AnnotationKind)> = HashSet::new();
911 annotations.retain(|a| {
912 let key = (a.source.file.clone(), a.source.line, a.kind);
913 seen.insert(key)
914 });
915}
916
917fn find_ast_grep() -> Result<PathBuf, ExtractorError> {
921 let candidates = [
923 "/opt/homebrew/bin/ast-grep",
924 "/usr/local/bin/ast-grep",
925 "/usr/bin/ast-grep",
926 ];
927 for candidate in candidates {
928 let path = PathBuf::from(candidate);
929 if path.exists() {
930 return Ok(path);
931 }
932 }
933 which_ast_grep()
935}
936
937fn which_ast_grep() -> Result<PathBuf, ExtractorError> {
938 let output = Command::new("which")
940 .arg("ast-grep")
941 .output()
942 .map_err(|_| ExtractorError::BinaryNotFound)?;
943 if output.status.success() {
944 let path_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
945 if !path_str.is_empty() {
946 return Ok(PathBuf::from(path_str));
947 }
948 }
949 Err(ExtractorError::BinaryNotFound)
950}
951
952#[cfg(test)]
955mod tests {
956 use super::*;
957
958 #[test]
961 fn strip_double_slash_prefix() {
962 let result = strip_comment_prefixes("// @g8.capability(name = \"x\")");
963 assert!(result.contains("@g8.capability"), "got: {result}");
964 }
965
966 #[test]
967 fn strip_hash_prefix() {
968 let result = strip_comment_prefixes("# @g8.capability(name = \"x\")");
969 assert!(result.contains("@g8.capability"), "got: {result}");
970 }
971
972 #[test]
973 fn strip_multiline_comment() {
974 let text =
975 "// @g8.capability(name = \"http-fetch\",\n// status = \"in_flight\")";
976 let result = strip_comment_prefixes(text);
977 assert!(result.contains("@g8.capability"), "got: {result}");
978 assert!(result.contains("http-fetch"), "got: {result}");
979 assert!(result.contains("in_flight"), "got: {result}");
980 }
981
982 #[test]
985 fn classify_architecture_heading() {
986 let (kind, is_owner, is_parked) = classify_heading("Architecture");
987 assert_eq!(kind, IntentKind::ArchitecturalScope);
988 assert!(!is_owner);
989 assert!(!is_parked);
990 }
991
992 #[test]
993 fn classify_boundaries_heading() {
994 let (kind, is_owner, is_parked) = classify_heading("Boundaries");
995 assert_eq!(kind, IntentKind::Boundary);
996 assert!(!is_owner);
997 assert!(!is_parked);
998 }
999
1000 #[test]
1001 fn classify_parked_ideas_heading() {
1002 let (_, _, is_parked) = classify_heading("Parked Ideas");
1003 assert!(is_parked);
1004 }
1005
1006 #[test]
1007 fn classify_owner_heading() {
1008 let (_, is_owner, _) = classify_heading("Owner");
1009 assert!(is_owner);
1010 }
1011
1012 #[test]
1013 fn classify_unknown_heading_is_unclassified() {
1014 let (kind, is_owner, is_parked) = classify_heading("Random Section");
1015 assert_eq!(kind, IntentKind::Unclassified);
1016 assert!(!is_owner);
1017 assert!(!is_parked);
1018 }
1019
1020 #[test]
1023 fn tests_dir_detection() {
1024 assert!(is_tests_dir(Path::new("src/tests/foo.rs")));
1025 assert!(is_tests_dir(Path::new(
1026 "crates/core/tests-integration/bar.rs"
1027 )));
1028 assert!(is_tests_dir(Path::new("unit-tests/baz.rs")));
1029 assert!(!is_tests_dir(Path::new("src/lib.rs")));
1030 assert!(!is_tests_dir(Path::new("src/test_helper.rs")));
1031 }
1032
1033 #[test]
1036 fn parse_parked_line_with_reason() {
1037 let (name, reason) = parse_parked_line("config-hot-reload: requires Arc<RwLock<Config>>");
1038 assert_eq!(name, "config-hot-reload");
1039 assert_eq!(reason.unwrap(), "requires Arc<RwLock<Config>>");
1040 }
1041
1042 #[test]
1043 fn parse_parked_line_without_reason() {
1044 let (name, reason) = parse_parked_line("ml-scoring");
1045 assert_eq!(name, "ml-scoring");
1046 assert!(reason.is_none());
1047 }
1048
1049 #[test]
1052 fn raw_ast_match_deserializes_from_real_ast_grep_output() {
1053 let line = r#"{"text":"// @g8.capability(name = \"test-only-cap\", status = \"in_flight\", dev_only = true)","range":{"byteOffset":{"start":163,"end":244},"start":{"line":4,"column":0},"end":{"line":4,"column":81}},"file":"/tmp/test.rs","lines":"// @g8.capability(name = \"test-only-cap\", status = \"in_flight\", dev_only = true)","charCount":{"leading":0,"trailing":0},"language":"Rust","ruleId":"g8-rust-magic-comment","severity":"hint","note":null,"message":"G8 magic-comment annotation (Rust)","labels":[]}"#;
1055 let result: Result<RawAstMatch, _> = serde_json::from_str(line);
1056 match &result {
1057 Err(e) => panic!("JSON parse failed: {e}\nLine: {line}"),
1058 Ok(m) => {
1059 assert_eq!(m.text, "// @g8.capability(name = \"test-only-cap\", status = \"in_flight\", dev_only = true)");
1060 assert_eq!(m.range.start.line, 4);
1061 assert_eq!(m.range.start.column, 0);
1062 assert_eq!(m.file, "/tmp/test.rs");
1063 assert_eq!(m.rule_id.as_deref(), Some("g8-rust-magic-comment"));
1064 }
1065 }
1066 }
1067
1068 #[test]
1069 fn convert_ast_match_roundtrip() {
1070 let line = r#"{"text":"// @g8.capability(name = \"test-only-cap\", status = \"in_flight\", dev_only = true)","range":{"byteOffset":{"start":163,"end":244},"start":{"line":4,"column":0},"end":{"line":4,"column":81}},"file":"/tmp/test.rs","lines":"// @g8.capability(name = \"test-only-cap\", status = \"in_flight\", dev_only = true)","charCount":{"leading":0,"trailing":0},"language":"Rust","ruleId":"g8-rust-magic-comment","severity":"hint","note":null,"message":"G8 magic-comment annotation (Rust)","labels":[]}"#;
1073 let raw: RawAstMatch = serde_json::from_str(line).expect("parse");
1074 let name_re = Regex::new(r#"name\s*=\s*"([^"]+)""#).unwrap();
1075 let result = convert_ast_match(&raw, &name_re);
1076 match result {
1077 Err(e) => panic!("convert_ast_match failed: {e}"),
1078 Ok(None) => panic!("convert_ast_match returned None unexpectedly"),
1079 Ok(Some(ann)) => {
1080 assert_eq!(ann.kind, AnnotationKind::Capability);
1081 assert_eq!(ann.name.as_deref(), Some("test-only-cap"));
1082 assert!(ann.conditional, "dev_only=true should set conditional");
1083 }
1084 }
1085 }
1086
1087 #[test]
1090 fn dev_only_sets_conditional() {
1091 let mut fields = BTreeMap::new();
1092 fields.insert("dev_only".to_string(), AnnotationValue::Bool(true));
1093 assert!(is_conditional("", &fields));
1094 }
1095
1096 #[test]
1097 fn cfg_attr_sets_conditional() {
1098 let fields = BTreeMap::new();
1099 assert!(is_conditional("#[cfg_attr(test, g8(...))]", &fields));
1100 }
1101
1102 #[test]
1105 fn embedded_rules_materialize_deterministically() {
1106 let first = materialize_embedded_rules().unwrap();
1107 let second = materialize_embedded_rules().unwrap();
1108 assert_eq!(first, second);
1109 assert_eq!(std::fs::read_to_string(&first).unwrap(), EMBEDDED_RULES);
1110 }
1111
1112 #[test]
1115 fn parse_agents_md_fixture() {
1116 let fixture = Path::new(concat!(
1117 env!("CARGO_MANIFEST_DIR"),
1118 "/tests/fixtures/markdown/AGENTS.md"
1119 ));
1120 if !fixture.exists() {
1121 return; }
1123 let annotations = parse_sidecar(fixture, IntentSourceKind::AgentsMd).unwrap();
1124 assert!(
1125 !annotations.is_empty(),
1126 "expected annotations from AGENTS.md fixture"
1127 );
1128 let has_arch = annotations.iter().any(|a| {
1130 a.fields
1131 .get("intent_kind")
1132 .and_then(|v| {
1133 if let AnnotationValue::String(s) = v {
1134 Some(s.as_str())
1135 } else {
1136 None
1137 }
1138 })
1139 .map(|s| s == "architectural_scope")
1140 .unwrap_or(false)
1141 });
1142 assert!(has_arch, "expected at least one architectural_scope intent");
1143 }
1144}