Spellcraft Loci Technical Documentation

Overview

The Spellcraft loci system is the core architecture behind spell creation, bundling, and verification in Magi CLI. It consists of three main components: SpellBundle, Sigildry, and SpellParser, all working together to create and manage secure, verifiable spell packages.

Core Components

1. SpellBundle

The central class for creating and managing spell bundles.

Key Features

Bundle Structure

spell_name.spell/
├── spell/
│   ├── spell.yaml      # Spell configuration
│   └── main.py/main.sh # Entry point
├── artifacts/          # Additional resources
├── spell.json         # Bundle metadata
└── spell_name_sigil.svg # Verification sigil

Key Methods

2. Sigildry System

Handles cryptographic verification and visual representation of spells.

Features

Sigil Components

Key Methods

3. SpellParser

Manages spell parsing, execution, and dependency handling.

Features

Parser Operations

Key Methods

Workflow Integration

1. Spell Creation Process

graph TD
    A[User Input] --> B[SpellBuilder]
    B --> C[SpellBundle]
    C --> D[Sigildry]
    D --> E[Bundle Creation]
    E --> F[Verification]

2. Spell Execution Flow

graph TD
    A[Cast Command] --> B[SpellParser]
    B --> C[Bundle Verification]
    C --> D[Dependency Check]
    D --> E[Environment Setup]
    E --> F[Spell Execution]

Security Features

1. Sigil Verification

2. Dependency Management

3. Execution Safety

Implementation Details

1. Bundle Creation

class SpellBundle:
    def create_bundle(self, destination_dir: Path) -> Path:
        """Create a spell bundle with verification."""
        # Verify configuration
        config = self.load_config()
        self.verify_assets()

        # Generate sigil
        sigil_hash, sigil_path = self._generate_spell_sigil(config)

        # Update configuration
        config['sigil_hash'] = sigil_hash

        # Create bundle
        bundle_path = destination_dir / f"{config['name']}.spell"

        with zipfile.ZipFile(bundle_path, 'w') as zf:
            # Add metadata
            metadata = self._generate_metadata(config)
            zf.writestr('spell.json', json.dumps(metadata))

            # Add spell contents
            self._add_spell_contents(zf)

            # Add sigil
            zf.write(sigil_path, sigil_path.name)

        return bundle_path

2. Sigil Generation

class Sigildry:
    def generate_sigil(self, hash_input: str, output_path: Path) -> None:
        """Generate a spell sigil SVG."""
        # Create SVG canvas
        dwg = svgwrite.Drawing(output_path, size=(256, 256))

        # Generate design elements from hash
        params = self._hash_to_params(hash_input)

        # Add visual elements
        self._generate_outer_rim(dwg, hash_input)
        self._generate_starburst(dwg, hash_input, params)
        self._generate_center_rune(dwg, hash_input)

        # Save sigil
        dwg.save()

3. Spell Execution

class SpellParser:
    def execute_spell_file(self, spell_name: str, *args, verbose: bool = False) -> bool:
        """Execute a spell with proper environment setup."""
        try:
            # Locate and parse spell
            spell_path = self.find_spell_file(spell_name)
            temp_dir, metadata = self.parse_bundle(spell_path)

            # Verify sigil
            if not self.verify_sigil(spell_path):
                raise SecurityError("Invalid spell sigil")

            # Check dependencies
            if not self.check_dependencies(metadata.get('dependencies', {})):
                raise DependencyError("Missing requirements")

            # Execute spell
            return self._run_spell(temp_dir, metadata, args)

        except Exception as e:
            if verbose:
                self._log_error(e)
            return False

Best Practices

1. Spell Creation

2. Security

3. Performance

Future Considerations

1. Planned Features

2. Scalability