Compilation Architecture
Understanding how PackC compiles prompts into packs.
Overview
Section titled “Overview”PackC transforms human-friendly YAML configurations into optimized, validated JSON packs through a multi-stage compilation pipeline.
Compilation Pipeline
Section titled “Compilation Pipeline”YAML Files → Parser → Validator → Optimizer → Pack Builder → JSON OutputStage 1: Configuration Loading
Section titled “Stage 1: Configuration Loading”Input: arena.yaml + prompt YAML files
# arena.yamlprompts: - prompts/support.yaml - prompts/sales.yamlProcess:
- Read arena.yaml
- Resolve file paths (relative to arena.yaml)
- Load each prompt YAML
- Parse YAML to PromptConfig structs
Output: In-memory PromptConfig objects
Stage 2: Prompt Registry
Section titled “Stage 2: Prompt Registry”Purpose: Central repository of all prompts
registry := prompt.NewRegistry()registry.Register(promptConfig)Features:
- Deduplication by task_type
- Fast lookup by ID
- Validation on registration
Stage 3: Validation
Section titled “Stage 3: Validation”Checks:
- Required fields present (task_type, system_prompt)
- Template syntax valid
- Parameter types correct
- Tool references valid
- Media files exist
Output: Validated PromptConfig objects or errors
Stage 4: Optimization
Section titled “Stage 4: Optimization”Transformations:
- Template compilation - Pre-parse templates
- Whitespace normalization - Consistent formatting
- Fragment resolution - Inline or reference
- Tool deduplication - Merge duplicate tools
- Metadata extraction - Build pack metadata
Stage 5: Pack Assembly
Section titled “Stage 5: Pack Assembly”Process:
- Create pack structure
- Add each prompt to prompts map
- Add fragments map
- Add metadata (compiler version, timestamp)
- Assign pack ID and version
Output: Complete Pack object
Stage 6: JSON Serialization
Section titled “Stage 6: JSON Serialization”Format: Indented JSON for readability
json.MarshalIndent(pack, "", " ")Options:
- Indent: 2 spaces
- Escape HTML: false
- Sort keys: consistent ordering
Output: .pack.json file
Compiler Components
Section titled “Compiler Components”PromptConfig Parser
Section titled “PromptConfig Parser”Parses YAML to PromptConfig:
func ParsePromptConfig(data []byte) (*PromptConfig, error)Handles:
- YAML unmarshaling
- Type conversion
- Default values
- Validation
Pack Compiler
Section titled “Pack Compiler”Orchestrates compilation:
compiler := prompt.NewPackCompiler(registry)pack, err := compiler.CompileFromRegistry(packID, compilerVersion)Responsibilities:
- Iterate through registry
- Transform prompts
- Build pack structure
- Add metadata
Validator
Section titled “Validator”Validates prompts and packs:
warnings := pack.Validate()Returns list of validation warnings (non-fatal) or errors (fatal).
Template Processing
Section titled “Template Processing”Go Templates
Section titled “Go Templates”Default template engine:
user_template: | User: Message:Processing:
- Parse template text
- Check syntax errors
- Store as string (runtime parsing)
Why not pre-compile?
- Templates need runtime data
- Keeps packs language-agnostic
- SDK handles execution
Template Validation
Section titled “Template Validation”PackC validates template syntax:
_, err := template.New("test").Parse(tmpl)But doesn’t execute (no runtime data available).
Fragment Handling
Section titled “Fragment Handling”Fragment Definition
Section titled “Fragment Definition”# fragment.yamlfragments: company-info: content: "Company: " description: "Standard company info"Fragment Compilation
Section titled “Fragment Compilation”Two strategies:
1. Inline (default)
Fragment content embedded in prompt:
{ "prompts": { "support": { "system": "Company: \n\nYou are support..." } }}2. Reference
Fragment stored separately:
{ "fragments": { "company-info": { "content": "..." } }, "prompts": { "support": { "system": "\n\nYou are support...", "fragments": ["company-info"] } }}Tradeoff:
- Inline: Faster execution, larger size
- Reference: Smaller size, runtime lookup
Error Handling
Section titled “Error Handling”Fatal Errors
Section titled “Fatal Errors”Stop compilation immediately:
- Invalid YAML syntax
- Missing required fields
- Template parse errors
- File not found
Warnings
Section titled “Warnings”Allow compilation but report issues:
- Missing descriptions
- Undefined tools
- Missing media files
- Large prompt size
Error Context
Section titled “Error Context”Provide helpful error messages:
Error parsing prompt config: yaml: line 5: mapping values are not allowedFile: prompts/support.yamlLine: 5Performance Optimizations
Section titled “Performance Optimizations”1. Concurrent Loading
Section titled “1. Concurrent Loading”Load multiple prompt files in parallel:
// Pseudocodefor each promptFile { go loadPrompt(promptFile)}2. Validation Caching
Section titled “2. Validation Caching”Cache validation results:
if cached := validationCache[promptID]; cached != nil { return cached}3. Incremental Compilation
Section titled “3. Incremental Compilation”Only recompile changed prompts (future):
if !hasChanged(promptFile) { return cachedPack}4. Streaming Output
Section titled “4. Streaming Output”Write pack JSON as generated:
encoder := json.NewEncoder(file)encoder.Encode(pack)Memory Management
Section titled “Memory Management”Small Footprint
Section titled “Small Footprint”PackC uses minimal memory:
- Streaming YAML parsing - Process files one at a time
- Lazy loading - Load prompts on demand
- Garbage collection - Release after compilation
- No caching - Don’t hold data post-compile
Large Projects
Section titled “Large Projects”For projects with 100+ prompts:
- Use single-prompt compilation for testing
- Compile subsets during development
- Full compilation only for releases
Compilation Modes
Section titled “Compilation Modes”Standard Mode
Section titled “Standard Mode”packc compile --config arena.yaml --output pack.json --id app- Full validation
- All optimizations
- Complete metadata
Fast Mode (future)
Section titled “Fast Mode (future)”packc compile --fast- Skip non-essential validation
- Minimal optimization
- For development iteration
Strict Mode (future)
Section titled “Strict Mode (future)”packc compile --strict- Fail on any warning
- Extra validation checks
- For production builds
Deterministic Builds
Section titled “Deterministic Builds”PackC produces deterministic output:
Given:
- Same source files
- Same packc version
- Same compilation flags
Result:
- Identical pack.json output
- Same checksums
- Reproducible builds
Implementation:
- Sorted keys in JSON
- Fixed timestamp format
- Consistent whitespace
Compilation Hooks (future)
Section titled “Compilation Hooks (future)”Allow customization:
compiler.AddHook("pre-validate", func(prompt *PromptConfig) error { // Custom validation})
compiler.AddHook("post-compile", func(pack *Pack) error { // Custom transformations})Use cases:
- Custom validation rules
- Organization-specific formatting
- Metadata injection
- Security scanning
Debugging Compilation
Section titled “Debugging Compilation”Verbose Output
Section titled “Verbose Output”packc compile --verboseShows:
- Files loaded
- Prompts registered
- Validation results
- Optimization steps
Dry Run
Section titled “Dry Run”packc compile --dry-run- Run compilation
- Don’t write output
- Show what would be generated
Inspect Intermediate
Section titled “Inspect Intermediate”View stages:
packc compile --dump-ast # After parsingpackc compile --dump-validated # After validationpackc compile --dump-optimized # After optimizationBuild Reproducibility
Section titled “Build Reproducibility”Version Locking
Section titled “Version Locking”Lock packc version:
# .packc-version0.1.0Input Hashing
Section titled “Input Hashing”Track source file changes:
find prompts/ -type f -exec sha256sum {} \; > prompts.sha256Build Manifest
Section titled “Build Manifest”Generate build info:
{ "packc_version": "0.1.0", "source_files": ["prompts/support.yaml"], "file_hashes": {"prompts/support.yaml": "abc123..."}, "build_time": "2025-01-16T10:30:00Z", "build_machine": "ci-runner-01"}Compiler Architecture
Section titled “Compiler Architecture”Modular Design
Section titled “Modular Design”PackCompiler├── Parser (YAML → PromptConfig)├── Validator (checks)├── Optimizer (transformations)├── Assembler (build pack)└── Serializer (write JSON)Each component is:
- Independent
- Testable
- Replaceable
Extension Points
Section titled “Extension Points”- Custom parsers - Support other input formats
- Custom validators - Add validation rules
- Custom optimizers - Apply transformations
- Custom serializers - Output other formats
Comparison with Other Compilers
Section titled “Comparison with Other Compilers”vs. TypeScript Compiler
Section titled “vs. TypeScript Compiler”| Feature | PackC | tsc |
|---|---|---|
| Input | YAML | TypeScript |
| Output | JSON | JavaScript |
| Type checking | Limited | Full |
| Optimization | Minimal | Extensive |
| Speed | ~100ms | ~1-10s |
vs. Babel
Section titled “vs. Babel”| Feature | PackC | Babel |
|---|---|---|
| Input | YAML | JavaScript |
| Output | JSON | JavaScript |
| Transformations | Few | Many |
| Plugins | Planned | Extensive |
| Speed | Fast | Moderate |
Future Enhancements
Section titled “Future Enhancements”1. Incremental Compilation
Section titled “1. Incremental Compilation”Only recompile changed prompts:
# First build: compile allpackc compile
# Subsequent: only changedpackc compile --incremental2. Watch Mode
Section titled “2. Watch Mode”Auto-recompile on file changes:
packc compile --watch3. Parallel Compilation
Section titled “3. Parallel Compilation”Compile multiple packs in parallel:
packc compile config/*.yaml --parallel4. Custom Output Formats
Section titled “4. Custom Output Formats”Support other formats:
packc compile --format yaml # Output YAML packpackc compile --format toml # Output TOML packSummary
Section titled “Summary”PackC’s compilation architecture is:
- Pipeline-based - Clear stages from YAML to JSON
- Validated - Multiple validation checkpoints
- Optimized - Minimal output size
- Extensible - Modular components
- Deterministic - Reproducible builds
- Fast - Milliseconds for typical projects
This design ensures reliable, consistent pack generation for production use.