gid-core 0.3.2

Graph-Indexed Development core library — graph-based project management and code analysis for AI agents
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
//! Tool Gating — configuration-driven access control for source code operations.
//!
//! When no ritual is active, gated operations (write to source paths, run build commands)
//! are blocked. This forces the developer/agent to go through the ritual pipeline
//! (design → implement → verify) instead of ad-hoc edits.
//!
//! Config lives in `.gid/config.yml` under `ritual.gating`.

use std::path::Path;
use serde::{Deserialize, Serialize};
use tracing::{debug, warn};

// ═══════════════════════════════════════════════════════════════════════════════
// Types
// ═══════════════════════════════════════════════════════════════════════════════

/// Top-level gating configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GatingConfig {
    /// Whether gating is enabled at all.
    #[serde(default = "default_true")]
    pub enabled: bool,
    /// Paths that require an active ritual to write to (glob patterns).
    #[serde(default)]
    pub gated_paths: Vec<String>,
    /// Paths that are always allowed (overrides gated_paths).
    #[serde(default = "default_ungated_paths")]
    pub ungated_paths: Vec<String>,
    /// Shell command patterns that require an active ritual.
    #[serde(default)]
    pub gated_commands: Vec<CommandPattern>,
    /// Verify command (build + test).
    #[serde(default)]
    pub verify_command: Option<String>,
}

/// A command pattern — either glob or regex.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandPattern {
    pub pattern: String,
    #[serde(default = "default_regex")]
    pub r#type: PatternType,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum PatternType {
    Glob,
    Regex,
}

fn default_true() -> bool { true }
fn default_regex() -> PatternType { PatternType::Regex }

fn default_ungated_paths() -> Vec<String> {
    vec![
        "DESIGN.md".into(),
        ".gid/**".into(),
        "docs/**".into(),
        "memory/**".into(),
        "AGENTS.md".into(),
        "TOOLS.md".into(),
        "MEMORY.md".into(),
        "*.md".into(),
    ]
}

impl Default for GatingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            gated_paths: vec![],
            ungated_paths: default_ungated_paths(),
            gated_commands: vec![],
            verify_command: None,
        }
    }
}

impl CommandPattern {
    pub fn regex(pattern: &str) -> Self {
        Self {
            pattern: pattern.to_string(),
            r#type: PatternType::Regex,
        }
    }

    pub fn glob(pattern: &str) -> Self {
        Self {
            pattern: pattern.to_string(),
            r#type: PatternType::Glob,
        }
    }

    /// Check if a command string matches this pattern.
    pub fn matches(&self, command: &str) -> bool {
        match self.r#type {
            PatternType::Regex => {
                match regex::Regex::new(&self.pattern) {
                    Ok(re) => re.is_match(command),
                    Err(e) => {
                        warn!(pattern = %self.pattern, error = %e, "Invalid gating regex");
                        false
                    }
                }
            }
            PatternType::Glob => {
                glob_match(&self.pattern, command)
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Language defaults
// ═══════════════════════════════════════════════════════════════════════════════

use super::composer::ProjectLanguage;

impl GatingConfig {
    /// Generate a default gating config for a detected project language.
    pub fn for_language(lang: &ProjectLanguage) -> Self {
        match lang {
            ProjectLanguage::Rust => Self {
                enabled: true,
                gated_paths: vec![
                    "src/**".into(), "tests/**".into(), "crates/**".into(),
                    "Cargo.toml".into(), "build.rs".into(),
                ],
                ungated_paths: default_ungated_paths(),
                gated_commands: vec![
                    CommandPattern::regex(r"^cargo\s+(build|run|test|check)\b"),
                    CommandPattern::regex(r"^rustc\b"),
                ],
                verify_command: Some("cargo build 2>&1 && cargo test 2>&1".into()),
            },
            ProjectLanguage::TypeScript => Self {
                enabled: true,
                gated_paths: vec![
                    "src/**".into(), "lib/**".into(), "tests/**".into(),
                    "package.json".into(), "tsconfig.json".into(),
                ],
                ungated_paths: default_ungated_paths(),
                gated_commands: vec![
                    CommandPattern::regex(r"^npm\s+run\s+(build|start)\b"),
                    CommandPattern::regex(r"^tsc\b"),
                    CommandPattern::regex(r"^npx\b"),
                ],
                verify_command: Some("npm run build 2>&1 && npm test 2>&1".into()),
            },
            ProjectLanguage::Python => Self {
                enabled: true,
                gated_paths: vec![
                    "**/*.py".into(), "setup.py".into(), "pyproject.toml".into(),
                ],
                ungated_paths: default_ungated_paths(),
                gated_commands: vec![
                    CommandPattern::regex(r"^python\b"),
                    CommandPattern::regex(r"^pip\s+install"),
                ],
                verify_command: Some("python -m pytest 2>&1".into()),
            },
            ProjectLanguage::Go => Self {
                enabled: true,
                gated_paths: vec![
                    "**/*.go".into(), "go.mod".into(), "go.sum".into(),
                ],
                ungated_paths: default_ungated_paths(),
                gated_commands: vec![
                    CommandPattern::regex(r"^go\s+(build|run|test)\b"),
                ],
                verify_command: Some("go build ./... 2>&1 && go test ./... 2>&1".into()),
            },
            ProjectLanguage::Other(_) => Self {
                enabled: true,
                verify_command: None,
                ..Default::default()
            },
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Config loading
// ═══════════════════════════════════════════════════════════════════════════════

/// Wrapper for the full `.gid/config.yml` file.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GidConfig {
    #[serde(default)]
    pub ritual: RitualConfigSection,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RitualConfigSection {
    #[serde(default)]
    pub gating: GatingConfig,
}

/// Load gating config from `.gid/config.yml`.
/// Returns default config if file doesn't exist or is malformed.
pub fn load_gating_config(project_root: &Path) -> GatingConfig {
    let config_path = project_root.join(".gid").join("config.yml");

    if !config_path.exists() {
        debug!("No .gid/config.yml, using default gating config");
        return GatingConfig::default();
    }

    match std::fs::read_to_string(&config_path) {
        Ok(content) => {
            match serde_yaml::from_str::<GidConfig>(&content) {
                Ok(config) => config.ritual.gating,
                Err(e) => {
                    warn!(error = %e, "Failed to parse .gid/config.yml, using defaults");
                    GatingConfig::default()
                }
            }
        }
        Err(e) => {
            warn!(error = %e, "Failed to read .gid/config.yml, using defaults");
            GatingConfig::default()
        }
    }
}

/// Save gating config to `.gid/config.yml`.
pub fn save_gating_config(project_root: &Path, config: &GatingConfig) -> anyhow::Result<()> {
    let config_path = project_root.join(".gid").join("config.yml");

    // Ensure .gid/ exists
    if let Some(parent) = config_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    let full_config = GidConfig {
        ritual: RitualConfigSection {
            gating: config.clone(),
        },
    };

    let yaml = serde_yaml::to_string(&full_config)?;
    std::fs::write(&config_path, yaml)?;
    Ok(())
}

// ═══════════════════════════════════════════════════════════════════════════════
// Gating check
// ═══════════════════════════════════════════════════════════════════════════════

/// Result of a gating check.
#[derive(Debug)]
pub enum GatingResult {
    /// Operation is allowed.
    Allowed,
    /// Operation is blocked — must start a ritual first.
    Blocked { reason: String },
}

/// Check if a tool call is gated (requires active ritual).
///
/// Returns `Allowed` if:
/// - Gating is disabled
/// - A ritual is currently active (`ritual_active = true`)
/// - The operation doesn't match any gated pattern
/// - The path is in the ungated whitelist
///
/// Returns `Blocked` if the operation matches a gated pattern and no ritual is active.
pub fn check_gating(
    config: &GatingConfig,
    tool_name: &str,
    path: Option<&str>,
    command: Option<&str>,
    ritual_active: bool,
) -> GatingResult {
    // Gating disabled
    if !config.enabled {
        return GatingResult::Allowed;
    }

    // Active ritual → all tools allowed (inner ToolScope handles phase restrictions)
    if ritual_active {
        return GatingResult::Allowed;
    }

    // No gated paths configured → no restrictions
    if config.gated_paths.is_empty() && config.gated_commands.is_empty() {
        return GatingResult::Allowed;
    }

    // Check file write operations
    if matches!(tool_name, "write_file" | "edit_file" | "Write" | "Edit" | "create_file") {
        if let Some(file_path) = path {
            // Ungated whitelist takes priority
            if config.ungated_paths.iter().any(|p| glob_match(p, file_path)) {
                return GatingResult::Allowed;
            }
            // Check gated paths
            if config.gated_paths.iter().any(|p| glob_match(p, file_path)) {
                return GatingResult::Blocked {
                    reason: format!(
                        "⚠️ Writing to `{}` requires an active ritual.\n\
                         Use `/ritual <task description>` to start one.\n\
                         This ensures design → implement → verify quality gates.",
                        file_path
                    ),
                };
            }
        }
    }

    // Check shell commands
    if matches!(tool_name, "exec" | "Bash" | "bash" | "shell") {
        if let Some(cmd) = command {
            if config.gated_commands.iter().any(|gc| gc.matches(cmd)) {
                return GatingResult::Blocked {
                    reason: format!(
                        "⚠️ Running `{}` requires an active ritual.\n\
                         Use `/ritual <task description>` to start one.",
                        truncate_cmd(cmd, 50)
                    ),
                };
            }
        }
    }

    GatingResult::Allowed
}

fn truncate_cmd(s: &str, max: usize) -> String {
    if s.len() <= max {
        s.to_string()
    } else {
        format!("{}...", &s[..max])
    }
}

/// Simple glob matching (supports `*`, `**`, `?`).
fn glob_match(pattern: &str, path: &str) -> bool {
    // Normalize
    let pattern = pattern.trim();
    let path = path.trim().trim_start_matches("./");

    // Exact match
    if pattern == path {
        return true;
    }

    // ** matches any path depth
    if pattern == "**" {
        return true;
    }

    // pattern/**  matches any file under pattern/
    if let Some(prefix) = pattern.strip_suffix("/**") {
        return path.starts_with(prefix) && path.len() > prefix.len();
    }

    // **/pattern matches pattern at any depth (including root)
    if let Some(suffix) = pattern.strip_prefix("**/") {
        // suffix could be "*.py" — need to match it as a glob
        if suffix.starts_with("*.") {
            // **/*.ext — match extension at any depth
            let ext = &suffix[1..]; // ".py"
            return path.ends_with(ext);
        }
        return path == suffix || path.ends_with(&format!("/{}", suffix));
    }

    // *.ext matches any file with that extension (root level only)
    if let Some(ext) = pattern.strip_prefix("*.") {
        // Only match files without path separators (root level)
        return !path.contains('/') && path.ends_with(&format!(".{}", ext));
    }

    // pattern/* matches direct children
    if let Some(prefix) = pattern.strip_suffix("/*") {
        if let Some(rest) = path.strip_prefix(prefix) {
            let rest = rest.trim_start_matches('/');
            return !rest.contains('/');
        }
        return false;
    }

    false
}

// ═══════════════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;

    fn rust_config() -> GatingConfig {
        GatingConfig::for_language(&ProjectLanguage::Rust)
    }

    // ── Glob matching ──

    #[test]
    fn test_glob_exact() {
        assert!(glob_match("Cargo.toml", "Cargo.toml"));
        assert!(!glob_match("Cargo.toml", "cargo.toml"));
    }

    #[test]
    fn test_glob_dir_wildcard() {
        assert!(glob_match("src/**", "src/main.rs"));
        assert!(glob_match("src/**", "src/utils/helpers.rs"));
        assert!(!glob_match("src/**", "tests/test.rs"));
    }

    #[test]
    fn test_glob_recursive() {
        assert!(glob_match("**/*.py", "main.py"));
        assert!(glob_match("**/*.py", "src/utils/helper.py"));
    }

    #[test]
    fn test_glob_extension() {
        assert!(glob_match("*.rs", "main.rs"));
        assert!(glob_match("*.rs", "lib.rs"));
        assert!(!glob_match("*.rs", "src/main.rs")); // *.rs only matches root
    }

    #[test]
    fn test_glob_md() {
        assert!(glob_match("*.md", "DESIGN.md"));
        assert!(glob_match("*.md", "README.md"));
    }

    // ── Ungated paths ──

    #[test]
    fn test_ungated_design_md() {
        let config = rust_config();
        let result = check_gating(&config, "write_file", Some("DESIGN.md"), None, false);
        assert!(matches!(result, GatingResult::Allowed));
    }

    #[test]
    fn test_ungated_gid_dir() {
        let config = rust_config();
        let result = check_gating(&config, "write_file", Some(".gid/graph.yml"), None, false);
        assert!(matches!(result, GatingResult::Allowed));
    }

    #[test]
    fn test_ungated_docs() {
        let config = rust_config();
        let result = check_gating(&config, "write_file", Some("docs/api.md"), None, false);
        assert!(matches!(result, GatingResult::Allowed));
    }

    // ── Gated paths ──

    #[test]
    fn test_gated_src() {
        let config = rust_config();
        let result = check_gating(&config, "write_file", Some("src/main.rs"), None, false);
        assert!(matches!(result, GatingResult::Blocked { .. }));
    }

    #[test]
    fn test_gated_cargo_toml() {
        let config = rust_config();
        let result = check_gating(&config, "edit_file", Some("Cargo.toml"), None, false);
        assert!(matches!(result, GatingResult::Blocked { .. }));
    }

    #[test]
    fn test_gated_tests() {
        let config = rust_config();
        let result = check_gating(&config, "Write", Some("tests/test_foo.rs"), None, false);
        assert!(matches!(result, GatingResult::Blocked { .. }));
    }

    // ── Gated commands ──

    #[test]
    fn test_gated_cargo_build() {
        let config = rust_config();
        let result = check_gating(&config, "exec", None, Some("cargo build"), false);
        assert!(matches!(result, GatingResult::Blocked { .. }));
    }

    #[test]
    fn test_gated_cargo_test() {
        let config = rust_config();
        let result = check_gating(&config, "Bash", None, Some("cargo test --lib"), false);
        assert!(matches!(result, GatingResult::Blocked { .. }));
    }

    #[test]
    fn test_ungated_cargo_doc() {
        let config = rust_config();
        // cargo doc is NOT gated
        let result = check_gating(&config, "exec", None, Some("cargo doc"), false);
        assert!(matches!(result, GatingResult::Allowed));
    }

    #[test]
    fn test_ungated_ls_command() {
        let config = rust_config();
        let result = check_gating(&config, "exec", None, Some("ls -la"), false);
        assert!(matches!(result, GatingResult::Allowed));
    }

    // ── Ritual active bypasses gating ──

    #[test]
    fn test_ritual_active_allows_all() {
        let config = rust_config();
        let result = check_gating(&config, "write_file", Some("src/main.rs"), None, true);
        assert!(matches!(result, GatingResult::Allowed));

        let result = check_gating(&config, "exec", None, Some("cargo build"), true);
        assert!(matches!(result, GatingResult::Allowed));
    }

    // ── Gating disabled ──

    #[test]
    fn test_gating_disabled() {
        let mut config = rust_config();
        config.enabled = false;
        let result = check_gating(&config, "write_file", Some("src/main.rs"), None, false);
        assert!(matches!(result, GatingResult::Allowed));
    }

    // ── Command patterns ──

    #[test]
    fn test_command_pattern_regex() {
        let pat = CommandPattern::regex(r"^cargo\s+(build|run|test|check)\b");
        assert!(pat.matches("cargo build"));
        assert!(pat.matches("cargo test --lib"));
        assert!(pat.matches("cargo check"));
        assert!(!pat.matches("cargo doc"));
        assert!(!pat.matches("echo cargo build"));
    }

    // ── Language defaults ──

    #[test]
    fn test_typescript_defaults() {
        let config = GatingConfig::for_language(&ProjectLanguage::TypeScript);
        assert!(config.gated_paths.contains(&"src/**".to_string()));
        assert_eq!(
            config.verify_command.as_deref(),
            Some("npm run build 2>&1 && npm test 2>&1")
        );
    }

    #[test]
    fn test_python_defaults() {
        let config = GatingConfig::for_language(&ProjectLanguage::Python);
        let result = check_gating(&config, "exec", None, Some("python main.py"), false);
        assert!(matches!(result, GatingResult::Blocked { .. }));
    }

    // ── Serialization ──

    #[test]
    fn test_config_roundtrip() {
        let config = rust_config();
        let yaml = serde_yaml::to_string(&GidConfig {
            ritual: RitualConfigSection { gating: config.clone() },
        }).unwrap();

        let parsed: GidConfig = serde_yaml::from_str(&yaml).unwrap();
        assert_eq!(parsed.ritual.gating.enabled, config.enabled);
        assert_eq!(parsed.ritual.gating.gated_paths.len(), config.gated_paths.len());
    }
}