|
| 1 | +# Design & Implementation Plan: Knowledge Graph Schema Linter |
| 2 | + |
| 3 | +**Status:** Ready for Implementation |
| 4 | +**Priority:** Medium |
| 5 | +**Origin:** PR #294 (conflicting, extract KG linter only) |
| 6 | +**Date:** 2025-12-31 |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## 1. Summary of Target Behavior |
| 11 | + |
| 12 | +A CLI tool and library to validate Knowledge Graph markdown schemas: |
| 13 | + |
| 14 | +1. **Validate KG markdown files** against schema rules |
| 15 | +2. **Report lint issues** with severity, code, and message |
| 16 | +3. **JSON output** for CI/CD integration |
| 17 | +4. **Auto-fix capability** for common issues (future) |
| 18 | +5. **Skill integration** for agentic loop validation |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## 2. Key Components from PR #294 |
| 23 | + |
| 24 | +### New Crate: `terraphim_kg_linter` |
| 25 | + |
| 26 | +``` |
| 27 | +crates/terraphim_kg_linter/ |
| 28 | +├── Cargo.toml |
| 29 | +├── src/ |
| 30 | +│ ├── lib.rs # Core linting logic |
| 31 | +│ └── main.rs # CLI binary |
| 32 | +└── tests/ |
| 33 | + └── basic.rs # Integration tests |
| 34 | +``` |
| 35 | + |
| 36 | +### Schema Structures |
| 37 | + |
| 38 | +| Structure | Purpose | |
| 39 | +|-----------|---------| |
| 40 | +| `CommandDef` | Command definitions with args, permissions | |
| 41 | +| `CommandArg` | Argument with name, type, required, default | |
| 42 | +| `TypesBlock` | Type definitions (name -> field -> type) | |
| 43 | +| `RolePermissions` | Role with allow/deny permission rules | |
| 44 | +| `LintIssue` | Issue report with path, severity, code, message | |
| 45 | + |
| 46 | +### Lint Rules |
| 47 | + |
| 48 | +| Code | Severity | Description | |
| 49 | +|------|----------|-------------| |
| 50 | +| `E001` | Error | Missing required field | |
| 51 | +| `E002` | Error | Invalid type reference | |
| 52 | +| `E003` | Error | Undefined command reference | |
| 53 | +| `W001` | Warning | Unused type definition | |
| 54 | +| `W002` | Warning | Missing description | |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## 3. Implementation Plan |
| 59 | + |
| 60 | +### Step 1: Create Crate Structure |
| 61 | + |
| 62 | +```bash |
| 63 | +cargo new --lib crates/terraphim_kg_linter |
| 64 | +``` |
| 65 | + |
| 66 | +**Cargo.toml dependencies:** |
| 67 | +```toml |
| 68 | +[dependencies] |
| 69 | +regex = "1" |
| 70 | +serde = { version = "1", features = ["derive"] } |
| 71 | +serde_yaml = "0.9" |
| 72 | +serde_json = "1" |
| 73 | +thiserror = "1" |
| 74 | +walkdir = "2" |
| 75 | +clap = { version = "4", features = ["derive"] } |
| 76 | +terraphim_automata = { path = "../terraphim_automata" } |
| 77 | + |
| 78 | +[dev-dependencies] |
| 79 | +tempfile = "3" |
| 80 | +``` |
| 81 | + |
| 82 | +### Step 2: Implement Core Types |
| 83 | + |
| 84 | +- `LintError` enum with IO, YAML, Schema, Automata variants |
| 85 | +- `CommandDef`, `CommandArg`, `TypesBlock`, `RolePermissions` |
| 86 | +- `LintIssue` with severity levels |
| 87 | +- `SchemaFragments` aggregating parsed schemas |
| 88 | + |
| 89 | +### Step 3: Implement Linter |
| 90 | + |
| 91 | +```rust |
| 92 | +pub struct KgLinter { |
| 93 | + strict: bool, |
| 94 | + fragments: SchemaFragments, |
| 95 | +} |
| 96 | + |
| 97 | +impl KgLinter { |
| 98 | + pub fn new(strict: bool) -> Self; |
| 99 | + pub fn lint_directory(&mut self, path: &Path) -> Result<Vec<LintIssue>>; |
| 100 | + pub fn lint_file(&mut self, path: &Path) -> Result<Vec<LintIssue>>; |
| 101 | + fn validate_command(&self, cmd: &CommandDef) -> Vec<LintIssue>; |
| 102 | + fn validate_types(&self, types: &TypesBlock) -> Vec<LintIssue>; |
| 103 | + fn validate_permissions(&self, role: &RolePermissions) -> Vec<LintIssue>; |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Step 4: Implement CLI |
| 108 | + |
| 109 | +```rust |
| 110 | +#[derive(Parser)] |
| 111 | +struct Cli { |
| 112 | + /// Path to KG directory |
| 113 | + #[arg(short, long, default_value = "docs/src/kg")] |
| 114 | + path: PathBuf, |
| 115 | + |
| 116 | + /// Output format |
| 117 | + #[arg(short, long, default_value = "text")] |
| 118 | + output: OutputFormat, |
| 119 | + |
| 120 | + /// Strict mode (warnings become errors) |
| 121 | + #[arg(long)] |
| 122 | + strict: bool, |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +### Step 5: Add to Workspace |
| 127 | + |
| 128 | +Update root `Cargo.toml`: |
| 129 | +```toml |
| 130 | +members = [ |
| 131 | + # ... |
| 132 | + "crates/terraphim_kg_linter", |
| 133 | +] |
| 134 | +``` |
| 135 | + |
| 136 | +### Step 6: Create Skill File |
| 137 | + |
| 138 | +```yaml |
| 139 | +# docs/src/skills/kg-schema-lint.skill.yaml |
| 140 | +name: kg-schema-lint |
| 141 | +description: Validate KG markdown schemas |
| 142 | +steps: |
| 143 | + - run: cargo run -p terraphim_kg_linter -- --path $kg_path -o json --strict |
| 144 | + - parse: json |
| 145 | + - plan: minimal edits for issues |
| 146 | + - apply: edits |
| 147 | + - rerun: until exit code 0 |
| 148 | +``` |
| 149 | +
|
| 150 | +### Step 7: CI Integration |
| 151 | +
|
| 152 | +Add to `.github/workflows/ci-native.yml`: |
| 153 | +```yaml |
| 154 | +- name: Lint KG schemas |
| 155 | + run: cargo run -p terraphim_kg_linter -- --path docs/src/kg --strict |
| 156 | +``` |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## 4. Testing Strategy |
| 161 | + |
| 162 | +| Test | Type | Location | |
| 163 | +|------|------|----------| |
| 164 | +| Valid schema passes | Unit | `tests/basic.rs` | |
| 165 | +| Missing field detected | Unit | `tests/basic.rs` | |
| 166 | +| Invalid type detected | Unit | `tests/basic.rs` | |
| 167 | +| Directory scan works | Integration | `tests/basic.rs` | |
| 168 | +| JSON output format | Integration | `tests/basic.rs` | |
| 169 | +| CLI arguments | Integration | `tests/cli.rs` | |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +## 5. Risk Assessment |
| 174 | + |
| 175 | +| Risk | Mitigation | Residual | |
| 176 | +|------|------------|----------| |
| 177 | +| PR #294 conflicts | Fresh implementation from extracted code | None | |
| 178 | +| Schema format changes | Version schema format | Low | |
| 179 | +| Performance on large KG | Lazy loading, parallel lint | Low | |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## 6. Files to Create |
| 184 | + |
| 185 | +| File | Action | Purpose | |
| 186 | +|------|--------|---------| |
| 187 | +| `crates/terraphim_kg_linter/Cargo.toml` | Create | Dependencies | |
| 188 | +| `crates/terraphim_kg_linter/src/lib.rs` | Create | Core logic | |
| 189 | +| `crates/terraphim_kg_linter/src/main.rs` | Create | CLI | |
| 190 | +| `crates/terraphim_kg_linter/tests/basic.rs` | Create | Tests | |
| 191 | +| `docs/src/skills/kg-schema-lint.skill.yaml` | Create | Skill def | |
| 192 | +| `docs/src/kg/schema-linter.md` | Create | Documentation | |
| 193 | + |
| 194 | +--- |
| 195 | + |
| 196 | +## 7. Implementation Timeline |
| 197 | + |
| 198 | +| Phase | Duration | Deliverable | |
| 199 | +|-------|----------|-------------| |
| 200 | +| Step 1-2 | 1 day | Crate structure, types | |
| 201 | +| Step 3-4 | 2 days | Linter implementation, CLI | |
| 202 | +| Step 5-7 | 1 day | Workspace, skill, CI | |
| 203 | +| **Total** | **4 days** | Production-ready KG linter | |
| 204 | + |
| 205 | +--- |
| 206 | + |
| 207 | +## 8. CLI Usage Examples |
| 208 | + |
| 209 | +```bash |
| 210 | +# Basic usage |
| 211 | +cargo run -p terraphim_kg_linter -- --path docs/src/kg |
| 212 | +
|
| 213 | +# JSON output for CI |
| 214 | +cargo run -p terraphim_kg_linter -- --path docs/src/kg -o json |
| 215 | +
|
| 216 | +# Strict mode (warnings become errors) |
| 217 | +cargo run -p terraphim_kg_linter -- --path docs/src/kg --strict |
| 218 | +
|
| 219 | +# Single file |
| 220 | +cargo run -p terraphim_kg_linter -- --file docs/src/kg/commands.md |
| 221 | +``` |
| 222 | + |
| 223 | +--- |
| 224 | + |
| 225 | +## 9. Next Steps |
| 226 | + |
| 227 | +1. Close PR #294 with comment linking to this plan |
| 228 | +2. Create GitHub issue for KG linter implementation |
| 229 | +3. Extract clean implementation from PR #294 branch |
| 230 | +4. Implement following this plan |
| 231 | + |
| 232 | +--- |
| 233 | + |
| 234 | +**Plan Status:** Ready for Implementation |
0 commit comments