KCL Guidelines Implementation Summary
Date: 2025-10-03 Status: β Complete Purpose: Consolidate KCL rules and patterns for the provisioning project
π What Was Created
1. Comprehensive KCL Patterns Guide
File: .claude/kcl_idiomatic_patterns.md (1,082 lines)
Contents:
- 10 Fundamental Rules - Core principles for KCL development
- 19 Design Patterns - Organized by category:
- Module Organization (3 patterns)
- Schema Design (5 patterns)
- Validation (3 patterns)
- Testing (2 patterns)
- Performance (2 patterns)
- Documentation (2 patterns)
- Security (2 patterns)
- 6 Anti-Patterns - Common mistakes to avoid
- Quick Reference - DOs and DONβTs
- Project Conventions - Naming, aliases, structure
- Security Patterns - Secure defaults, secret handling
- Testing Patterns - Example-driven, validation test cases
2. Quick Rules Summary
File: .claude/KCL_RULES_SUMMARY.md (321 lines)
Contents:
- 10 Fundamental Rules (condensed)
- 19 Pattern quick reference
- Standard import aliases table
- 6 Critical anti-patterns
- Submodule reference map
- Naming conventions
- Security/Validation/Documentation checklists
- Quick start template
3. CLAUDE.md Integration
File: CLAUDE.md (updated)
Added:
- KCL Development Guidelines section
- Reference to
.claude/kcl_idiomatic_patterns.md - Core KCL principles summary
- Quick KCL reference code example
π― Core Principles Established
1. Direct Submodule Imports
β
import provisioning.lib as lib
β Settings = settings.Settings # ImmutableError
2. Schema-First Development
Every configuration must have a schema with validation.
3. Immutability First
Use KCLβs immutable-by-default, only use _ prefix when absolutely necessary.
4. Security by Default
- Secrets as references (never plaintext)
- TLS enabled by default
- Certificates verified by default
5. Explicit Types
- Always specify types
- Use union types for enums
- Mark optional with
?
π Rule Categories
Module Organization (3 patterns)
- Submodule Structure - Domain-driven organization
- Extension Organization - Consistent hierarchy
- kcl.mod Dependencies - Relative paths + versions
Schema Design (5 patterns)
- Base + Provider - Generic core, specific providers
- Configuration + Defaults - System defaults + user overrides
- Dependency Declaration - Explicit with version ranges
- Version Management - Metadata & update strategies
- Workflow Definition - Declarative operations
Validation (3 patterns)
- Multi-Field Validation - Cross-field rules
- Regex Validation - Format validation with errors
- Resource Constraints - Validate limits
Testing (2 patterns)
- Example-Driven Schemas - Examples in documentation
- Validation Test Cases - Test cases in comments
Performance (2 patterns)
- Lazy Evaluation - Compute only when needed
- Constant Extraction - Module-level reusables
Documentation (2 patterns)
- Schema Documentation - Purpose, fields, examples
- Inline Comments - Explain complex logic
Security (2 patterns)
- Secure Defaults - Most secure by default
- Secret References - Never embed secrets
π§ Standard Conventions
Import Aliases
| Module | Alias |
|---|---|
provisioning.lib | lib |
provisioning.settings | cfg or settings |
provisioning.dependencies | deps or schema |
provisioning.workflows | wf |
provisioning.batch | batch |
provisioning.version | v |
provisioning.k8s_deploy | k8s |
Schema Naming
- Base:
Storage,Server,Cluster - Provider:
Storage_aws,ServerDefaults_upcloud - Taskserv:
Kubernetes,Containerd - Config:
NetworkConfig,MonitoringConfig
File Naming
- Main schema:
{name}.k - Defaults:
defaults_{provider}.k - Server:
server_{provider}.k - Dependencies:
dependencies.k - Version:
version.k
β οΈ Critical Anti-Patterns
1. Re-exports (ImmutableError)
β Settings = settings.Settings
2. Mutable Non-Prefixed Variables
β config = { host = "local" }
config = { host = "prod" } # Error!
3. Missing Validation
β schema ServerConfig:
cores: int # No check block!
4. Magic Numbers
β timeout: int = 300 # What's 300?
5. String-Based Configuration
β environment: str # Use union types!
6. Deep Nesting
β server: { network: { interfaces: { ... } } }
π Project Integration
Files Updated/Created
Created (3 files):
-
.claude/kcl_idiomatic_patterns.md- 1,082 lines- Comprehensive patterns guide
- All 19 patterns with examples
- Security and testing sections
-
.claude/KCL_RULES_SUMMARY.md- 321 lines- Quick reference card
- Condensed rules and patterns
- Checklists and templates
-
KCL_GUIDELINES_IMPLEMENTATION.md- This file- Implementation summary
- Integration documentation
Updated (1 file):
CLAUDE.md- Added KCL Development Guidelines section
- Reference to comprehensive guide
- Core principles summary
π How to Use
For Claude Code AI
CLAUDE.md now includes:
## KCL Development Guidelines
For KCL configuration language development, reference:
- @.claude/kcl_idiomatic_patterns.md (comprehensive KCL patterns and rules)
### Core KCL Principles:
1. Direct Submodule Imports
2. Schema-First Development
3. Immutability First
4. Security by Default
5. Explicit Types
For Developers
Quick Start:
- Read
.claude/KCL_RULES_SUMMARY.md(5-10 minutes) - Reference
.claude/kcl_idiomatic_patterns.mdfor details - Use quick start template from summary
When Writing KCL:
- Check import aliases (use standard ones)
- Follow schema naming conventions
- Use quick start template
- Run through validation checklist
When Reviewing KCL:
- Check for anti-patterns
- Verify security checklist
- Ensure documentation complete
- Validate against patterns
π Benefits
Immediate
- β All KCL patterns documented in one place
- β Clear anti-patterns to avoid
- β Standard conventions established
- β Quick reference available
Long-term
- β Consistent KCL code across project
- β Easier onboarding for new developers
- β Better AI assistance (Claude follows patterns)
- β Maintainable, secure configurations
Quality Improvements
- β Type safety (explicit types everywhere)
- β Security by default (no plaintext secrets)
- β Validation complete (check blocks required)
- β Documentation complete (examples required)
π Related Documentation
KCL Guidelines (New)
.claude/kcl_idiomatic_patterns.md- Full patterns guide.claude/KCL_RULES_SUMMARY.md- Quick referenceCLAUDE.md- Project rules (updated with KCL section)
KCL Architecture
docs/architecture/kcl-import-patterns.md- Import patterns deep divedocs/KCL_QUICK_REFERENCE.md- Developer quick referenceKCL_MODULE_ORGANIZATION_SUMMARY.md- Module organization
Core Implementation
provisioning/kcl/main.k- Core module (cleaned up)provisioning/kcl/*.k- Submodules (10 files)provisioning/extensions/- Extensions (providers, taskservs, clusters)
β Validation
Files Verified
# All guides created
ls -lh .claude/*.md
# -rw-r--r-- 16K best_nushell_code.md
# -rw-r--r-- 24K kcl_idiomatic_patterns.md β
NEW
# -rw-r--r-- 7.4K KCL_RULES_SUMMARY.md β
NEW
# Line counts
wc -l .claude/kcl_idiomatic_patterns.md # 1,082 lines β
wc -l .claude/KCL_RULES_SUMMARY.md # 321 lines β
# CLAUDE.md references
grep "kcl_idiomatic_patterns" CLAUDE.md
# Line 8: - **Follow KCL idiomatic patterns from @.claude/kcl_idiomatic_patterns.md**
# Line 18: - @.claude/kcl_idiomatic_patterns.md (comprehensive KCL patterns and rules)
# Line 41: See full guide: `.claude/kcl_idiomatic_patterns.md`
Integration Confirmed
- β CLAUDE.md references new KCL guide (3 mentions)
- β Core principles summarized in CLAUDE.md
- β Quick reference code example included
- β Follows same structure as Nushell guide
π Training Claude Code
What Claude Will Follow
When Claude Code reads CLAUDE.md, it will now:
-
Import Correctly
- Use
import provisioning.{submodule} - Never use re-exports
- Use standard aliases
- Use
-
Write Schemas
- Define schema before config
- Include check blocks
- Use explicit types
-
Validate Properly
- Cross-field validation
- Regex for formats
- Resource constraints
-
Document Thoroughly
- Schema docstrings
- Usage examples
- Test cases in comments
-
Secure by Default
- TLS enabled
- Secret references only
- Verify certificates
π Checklists
For New KCL Files
Schema Definition:
- Explicit types for all fields
- Check block with validation
- Docstring with purpose
- Usage examples included
-
Optional fields marked with
? - Sensible defaults provided
Imports:
- Direct submodule imports
- Standard aliases used
- No re-exports
- kcl.mod dependencies declared
Security:
- No plaintext secrets
- Secure defaults
- TLS enabled
- Certificates verified
Documentation:
- Header comment with info
- Schema docstring
- Complex logic explained
- Examples provided
π Next Steps (Optional)
Enhancement Opportunities
-
IDE Integration
- VS Code snippets for patterns
- KCL LSP configuration
- Auto-completion for aliases
-
CI/CD Validation
- Check for anti-patterns
- Enforce naming conventions
- Validate security settings
-
Training Materials
- Workshop slides
- Video tutorials
- Interactive examples
-
Tooling
- KCL linter with project rules
- Schema generator using templates
- Documentation generator
π Statistics
Documentation Created
- Total Files: 3 new, 1 updated
- Total Lines: 1,403 lines (KCL guides only)
- Patterns Documented: 19
- Rules Documented: 10
- Anti-Patterns: 6
- Checklists: 3 (Security, Validation, Documentation)
Coverage
- β Module organization
- β Schema design
- β Validation patterns
- β Testing patterns
- β Performance patterns
- β Documentation patterns
- β Security patterns
- β Import patterns
- β Naming conventions
- β Quick templates
π― Success Criteria
All criteria met:
- β Comprehensive patterns guide created
- β Quick reference summary available
- β CLAUDE.md updated with KCL section
- β
All rules consolidated in
.claudefolder - β Follows same structure as Nushell guide
- β Examples and anti-patterns included
- β Security and testing patterns covered
- β Project conventions documented
- β Integration verified
π Conclusion
Successfully created comprehensive KCL guidelines for the provisioning project:
.claude/kcl_idiomatic_patterns.md- Complete patterns guide (1,082 lines).claude/KCL_RULES_SUMMARY.md- Quick reference (321 lines)CLAUDE.md- Updated with KCL section
All KCL development rules are now:
- β
Documented in
.claudefolder - β Referenced in CLAUDE.md
- β Available to Claude Code AI
- β Accessible to developers
The project now has a single source of truth for KCL development patterns.
Maintained By: Architecture Team Review Cycle: Quarterly or when KCL version updates Last Review: 2025-10-03