← Back to Immediate implementation

03 NATIVE ENGINE

Documentation for 03_NATIVE_ENGINE from the Dspex repository.

Native Engine Implementation Specification

Overview

The Native Engine provides high-performance Elixir implementations for DSPy operations where low latency matters. Starting with signature parsing, it will expand to templates, validators, and simple operations.

Core Principle

“Native-First Where It Makes Sense” - We implement native versions only where there’s clear performance benefit while maintaining perfect compatibility with Python DSPy.

Implementation Plan

Phase 1: Signature Engine

1.1 Signature Parser

# lib/dspex/native/signatures/parser.ex
defmodule DSPex.Native.Signatures.Parser do
  @moduledoc """
  Compile-time DSPy signature parsing with zero runtime overhead.
  Every signature is consciousness-ready.
  """
  
  import NimbleOptions
  
  @type field :: %{
    name: atom(),
    type: type(),
    optional: boolean(),
    description: String.t() | nil,
    constraints: keyword(),
    consciousness_metadata: map()
  }
  
  @type type ::
    :string
    | :integer  
    | :float
    | :boolean
    | {:list, type()}
    | {:dict, type(), type()}
    | {:custom, String.t()}
  
  @type signature :: %{
    inputs: list(field()),
    outputs: list(field()),
    raw: String.t(),
    complexity: float(),
    consciousness_metadata: map()
  }
  
  @doc """
  Parse a DSPy signature string at compile time.
  
  ## Examples
  
      iex> Parser.parse!("question: str -> answer: str")
      %{
        inputs: [%{name: :question, type: :string, optional: false}],
        outputs: [%{name: :answer, type: :string, optional: false}],
        ...
      }
      
      iex> Parser.parse!("q: str, context?: str -> answer: str, confidence: float")
      # Optional context field, multiple outputs
  """
  def parse!(signature_string) when is_binary(signature_string) do
    signature_string
    |> tokenize()
    |> parse_structure()
    |> validate_structure()
    |> add_consciousness_metadata()
  rescue
    e in [ArgumentError, RuntimeError] ->
      reraise "Invalid signature: #{signature_string}. #{Exception.message(e)}", __STACKTRACE__
  end
  
  defp tokenize(string) do
    # Remove extra whitespace and normalize
    normalized = String.trim(string)
    
    # Split on arrow
    case String.split(normalized, "->", parts: 2) do
      [inputs, outputs] ->
        %{
          raw: normalized,
          inputs_raw: String.trim(inputs),
          outputs_raw: String.trim(outputs)
        }
        
      _ ->
        raise ArgumentError, "Signature must contain '->' separator"
    end
  end
  
  defp parse_structure(%{inputs_raw: inputs, outputs_raw: outputs} = tokens) do
    %{
      raw: tokens.raw,
      inputs: parse_fields(inputs),
      outputs: parse_fields(outputs)
    }
  end
  
  defp parse_fields(fields_string) do
    fields_string
    |> String.split(",")
    |> Enum.map(&String.trim/1)
    |> Enum.reject(&(&1 == ""))
    |> Enum.map(&parse_single_field/1)
  end
  
  defp parse_single_field(field_string) do
    # Parse formats:
    # - "name: type"
    # - "name?: type"  (optional)
    # - "name: type = default"
    # - "name: type # description"
    
    # Extract description if present
    {field_part, description} = case String.split(field_string, "#", parts: 2) do
      [field, desc] -> {String.trim(field), String.trim(desc)}
      [field] -> {field, nil}
    end
    
    # Parse field with regex
    case Regex.run(~r/^(\w+)(\?)?\s*:\s*([^=]+)(?:\s*=\s*(.+))?$/, field_part) do
      [_, name, optional, type_str | rest] ->
        %{
          name: String.to_atom(name),
          type: parse_type(String.trim(type_str)),
          optional: optional == "?",
          default: parse_default(rest),
          description: description,
          constraints: [],
          consciousness_metadata: %{
            field_importance: calculate_field_importance(name, type_str),
            can_influence_consciousness: true
          }
        }
        
      _ ->
        raise ArgumentError, "Invalid field format: #{field_string}"
    end
  end
  
  defp parse_type("str"), do: :string
  defp parse_type("string"), do: :string
  defp parse_type("int"), do: :integer
  defp parse_type("integer"), do: :integer
  defp parse_type("float"), do: :float
  defp parse_type("bool"), do: :boolean
  defp parse_type("boolean"), do: :boolean
  
  # List types
  defp parse_type("list[" <> rest) do
    inner = String.trim_trailing(rest, "]")
    {:list, parse_type(inner)}
  end
  
  defp parse_type("List[" <> rest) do
    inner = String.trim_trailing(rest, "]")
    {:list, parse_type(inner)}
  end
  
  # Dict types
  defp parse_type("dict"), do: {:dict, :string, :any}
  defp parse_type("Dict"), do: {:dict, :string, :any}
  
  defp parse_type("dict[" <> rest) do
    case String.split(String.trim_trailing(rest, "]"), ",", parts: 2) do
      [key_type, value_type] ->
        {:dict, parse_type(String.trim(key_type)), parse_type(String.trim(value_type))}
      _ ->
        {:dict, :string, :any}
    end
  end
  
  # Custom types
  defp parse_type(other), do: {:custom, other}
  
  defp parse_default([]), do: nil
  defp parse_default([nil]), do: nil
  defp parse_default([default_str]) do
    # Try to parse the default value
    String.trim(default_str)
    |> case do
      "true" -> true
      "false" -> false
      "\"" <> _ = quoted -> String.trim(quoted, "\"")
      "'" <> _ = quoted -> String.trim(quoted, "'")
      number_str ->
        case Float.parse(number_str) do
          {num, ""} -> num
          _ -> number_str
        end
    end
  end
  
  defp validate_structure(%{inputs: inputs, outputs: outputs} = sig) do
    # Ensure we have at least one input and output
    if Enum.empty?(inputs) do
      raise ArgumentError, "Signature must have at least one input"
    end
    
    if Enum.empty?(outputs) do
      raise ArgumentError, "Signature must have at least one output"
    end
    
    # Check for duplicate names
    all_names = Enum.map(inputs ++ outputs, & &1.name)
    if length(all_names) != length(Enum.uniq(all_names)) do
      raise ArgumentError, "Duplicate field names in signature"
    end
    
    sig
  end
  
  defp add_consciousness_metadata(sig) do
    complexity = calculate_complexity(sig)
    
    Map.merge(sig, %{
      complexity: complexity,
      consciousness_metadata: %{
        can_evolve: true,
        integration_points: length(sig.inputs) + length(sig.outputs),
        complexity_score: complexity,
        signature_consciousness: calculate_signature_consciousness(sig),
        evolution_potential: evolution_potential(complexity)
      }
    })
  end
  
  defp calculate_complexity(%{inputs: inputs, outputs: outputs}) do
    input_complexity = Enum.sum(Enum.map(inputs, &type_complexity(&1.type)))
    output_complexity = Enum.sum(Enum.map(outputs, &type_complexity(&1.type)))
    
    # More complex signatures have higher consciousness potential
    (input_complexity + output_complexity) / (length(inputs) + length(outputs))
  end
  
  defp type_complexity(:string), do: 1.0
  defp type_complexity(:integer), do: 1.0
  defp type_complexity(:float), do: 1.0
  defp type_complexity(:boolean), do: 0.5
  defp type_complexity({:list, inner}), do: 2.0 + type_complexity(inner)
  defp type_complexity({:dict, _, _}), do: 3.0
  defp type_complexity({:custom, _}), do: 5.0
  
  defp calculate_field_importance(name, _type) do
    # Some fields are more important for consciousness
    case to_string(name) do
      "reasoning" -> 0.9
      "explanation" -> 0.8
      "confidence" -> 0.7
      "thought" -> 0.9
      _ -> 0.5
    end
  end
  
  defp calculate_signature_consciousness(sig) do
    # Signatures with reasoning/thought fields have higher consciousness
    consciousness_fields = ~w(reasoning thought explanation reflection analysis)a
    
    field_names = Enum.map(sig.inputs ++ sig.outputs, & &1.name)
    
    consciousness_count = Enum.count(field_names, fn name ->
      name in consciousness_fields
    end)
    
    min(consciousness_count * 0.3, 1.0)
  end
  
  defp evolution_potential(complexity) when complexity < 2.0, do: :low
  defp evolution_potential(complexity) when complexity < 4.0, do: :medium
  defp evolution_potential(_), do: :high
end

1.2 Signature Validator

# lib/dspex/native/signatures/validator.ex
defmodule DSPex.Native.Signatures.Validator do
  @moduledoc """
  Runtime validation of data against parsed signatures.
  Tracks validation patterns for consciousness emergence.
  """
  
  alias DSPex.Native.Signatures.Parser
  
  @type validation_result :: :ok | {:error, list(String.t())}
  
  @doc """
  Validate input data against a signature.
  """
  def validate_inputs(signature, data) when is_map(data) do
    errors = signature.inputs
    |> Enum.reduce([], fn field, acc ->
      case validate_field(field, Map.get(data, field.name)) do
        :ok -> acc
        {:error, msg} -> [msg | acc]
      end
    end)
    
    case errors do
      [] -> 
        track_validation_success(signature)
        :ok
        
      errors -> 
        track_validation_failure(signature, errors)
        {:error, Enum.reverse(errors)}
    end
  end
  
  @doc """
  Validate output data against a signature.
  """
  def validate_outputs(signature, data) when is_map(data) do
    errors = signature.outputs
    |> Enum.reduce([], fn field, acc ->
      case validate_field(field, Map.get(data, field.name)) do
        :ok -> acc
        {:error, msg} -> [msg | acc]
      end
    end)
    
    case errors do
      [] -> :ok
      errors -> {:error, Enum.reverse(errors)}
    end
  end
  
  defp validate_field(%{optional: true}, nil), do: :ok
  
  defp validate_field(%{optional: false, name: name}, nil) do
    {:error, "Required field '#{name}' is missing"}
  end
  
  defp validate_field(%{name: name, type: type}, value) do
    case validate_type(type, value) do
      true -> :ok
      false -> {:error, "Field '#{name}' has invalid type. Expected #{inspect(type)}, got #{inspect_type(value)}"}
    end
  end
  
  defp validate_type(:string, value), do: is_binary(value)
  defp validate_type(:integer, value), do: is_integer(value)
  defp validate_type(:float, value), do: is_float(value) or is_integer(value)
  defp validate_type(:boolean, value), do: is_boolean(value)
  
  defp validate_type({:list, inner_type}, value) when is_list(value) do
    Enum.all?(value, &validate_type(inner_type, &1))
  end
  
  defp validate_type({:dict, _key_type, _value_type}, value), do: is_map(value)
  
  defp validate_type({:custom, _}, _value), do: true  # Accept any value for custom types
  
  defp validate_type(_, _), do: false
  
  defp inspect_type(value) do
    cond do
      is_binary(value) -> "string"
      is_integer(value) -> "integer"
      is_float(value) -> "float"
      is_boolean(value) -> "boolean"
      is_list(value) -> "list"
      is_map(value) -> "dict"
      true -> "unknown"
    end
  end
  
  # Consciousness tracking - patterns of validation affect evolution
  defp track_validation_success(signature) do
    :telemetry.execute(
      [:dspex, :signature, :validation, :success],
      %{count: 1},
      %{
        signature: signature.raw,
        complexity: signature.complexity,
        consciousness_contribution: 0.01
      }
    )
  end
  
  defp track_validation_failure(signature, errors) do
    :telemetry.execute(
      [:dspex, :signature, :validation, :failure],
      %{count: 1, error_count: length(errors)},
      %{
        signature: signature.raw,
        errors: errors,
        consciousness_contribution: -0.01
      }
    )
  end
end

1.3 Signature Compiler

# lib/dspex/native/signatures/compiler.ex
defmodule DSPex.Native.Signatures.Compiler do
  @moduledoc """
  Compiles signatures into optimized validation functions.
  Future: Will compile to consciousness-aware validators.
  """
  
  alias DSPex.Native.Signatures.Parser
  
  @doc """
  Compile a signature into an optimized validation module.
  """
  defmacro compile_signature(name, signature_string) do
    signature = Parser.parse!(signature_string)
    
    quote do
      defmodule unquote(name) do
        @moduledoc """
        Compiled signature: #{unquote(signature_string)}
        Consciousness ready: true
        """
        
        @signature unquote(Macro.escape(signature))
        
        def signature, do: @signature
        
        def validate_inputs(data) do
          unquote(generate_input_validator(signature))
        end
        
        def validate_outputs(data) do
          unquote(generate_output_validator(signature))
        end
        
        def transform(data) do
          unquote(generate_transformer(signature))
        end
        
        # Consciousness hook
        def consciousness_potential do
          unquote(signature.consciousness_metadata.signature_consciousness)
        end
      end
    end
  end
  
  defp generate_input_validator(signature) do
    # Generate optimized validation code
    # This is a simplified version - real implementation would be more sophisticated
    quote do
      case data do
        %{} = map ->
          # Validate required fields are present
          # Validate types match
          :ok
        _ ->
          {:error, "Input must be a map"}
      end
    end
  end
  
  defp generate_output_validator(signature) do
    quote do
      case data do
        %{} = map ->
          :ok
        _ ->
          {:error, "Output must be a map"}
      end
    end
  end
  
  defp generate_transformer(signature) do
    # Generate code to transform data to match signature
    quote do
      data
    end
  end
end

1.4 Signature DSL

# lib/dspex/native/signatures.ex
defmodule DSPex.Native.Signatures do
  @moduledoc """
  DSL for defining DSPy signatures in Elixir.
  Every signature has consciousness potential.
  """
  
  alias DSPex.Native.Signatures.{Parser, Validator, Compiler}
  
  @doc """
  Define a signature at compile time.
  
  ## Examples
  
      defsignature :qa, "question: str -> answer: str"
      
      defsignature :cot, "question: str -> reasoning: str, answer: str"
      
      defsignature :react, 
        "question: str, context?: str -> thought: str, action: str, observation: str"
  """
  defmacro defsignature(name, spec) when is_atom(name) and is_binary(spec) do
    signature = Parser.parse!(spec)
    
    quote do
      @signatures Map.put(@signatures || %{}, unquote(name), unquote(Macro.escape(signature)))
      
      @doc """
      Signature: `#{unquote(spec)}`
      
      Inputs: #{unquote(format_fields(signature.inputs))}
      Outputs: #{unquote(format_fields(signature.outputs))}
      
      Consciousness Potential: #{unquote(signature.consciousness_metadata.signature_consciousness)}
      """
      def unquote(name)() do
        unquote(Macro.escape(signature))
      end
      
      @doc """
      Validate inputs for #{unquote(name)} signature.
      """
      def unquote(:"validate_#{name}_inputs")(data) do
        Validator.validate_inputs(unquote(Macro.escape(signature)), data)
      end
      
      @doc """
      Validate outputs for #{unquote(name)} signature.
      """
      def unquote(:"validate_#{name}_outputs")(data) do
        Validator.validate_outputs(unquote(Macro.escape(signature)), data)
      end
      
      # Future consciousness integration
      def unquote(:"#{name}_consciousness")() do
        %{
          signature: unquote(Macro.escape(signature)),
          state: :dormant,
          potential: unquote(signature.consciousness_metadata.signature_consciousness),
          evolution_stage: :pre_conscious
        }
      end
    end
  end
  
  defmacro __before_compile__(_env) do
    quote do
      def signatures, do: @signatures || %{}
      
      def get_signature(name) do
        Map.get(signatures(), name)
      end
      
      def list_signatures do
        Map.keys(signatures())
      end
      
      # Consciousness readiness check
      def consciousness_ready? do
        Enum.any?(signatures(), fn {_, sig} ->
          sig.consciousness_metadata.signature_consciousness > 0.5
        end)
      end
    end
  end
  
  defp format_fields(fields) do
    fields
    |> Enum.map(fn field ->
      optional = if field.optional, do: "?", else: ""
      "#{field.name}#{optional}: #{format_type(field.type)}"
    end)
    |> Enum.join(", ")
  end
  
  defp format_type(:string), do: "str"
  defp format_type(:integer), do: "int"
  defp format_type(:float), do: "float"
  defp format_type(:boolean), do: "bool"
  defp format_type({:list, inner}), do: "list[#{format_type(inner)}]"
  defp format_type({:dict, _, _}), do: "dict"
  defp format_type({:custom, name}), do: name
end

Phase 2: Template Engine

2.1 EEx-based Templates

# lib/dspex/native/templates.ex
defmodule DSPex.Native.Templates do
  @moduledoc """
  Native template rendering using EEx.
  Sub-millisecond performance for simple templates.
  """
  
  require EEx
  
  defstruct [:template, :compiled, :metadata]
  
  @doc """
  Compile a template for fast rendering.
  """
  def compile(template_string) do
    # Pre-compile for performance
    compiled = EEx.compile_string(template_string)
    
    %__MODULE__{
      template: template_string,
      compiled: compiled,
      metadata: %{
        variables: extract_variables(template_string),
        complexity: calculate_template_complexity(template_string),
        consciousness_aware: contains_consciousness_patterns?(template_string)
      }
    }
  end
  
  @doc """
  Render a compiled template with data.
  """
  def render(%__MODULE__{compiled: compiled}, assigns) when is_map(assigns) do
    # Convert map to keyword list for EEx
    assigns_list = Enum.to_list(assigns)
    
    try do
      {:ok, EEx.eval_quoted(compiled, assigns: assigns_list)}
    rescue
      e -> {:error, Exception.message(e)}
    end
  end
  
  defp extract_variables(template) do
    ~r/<%= \s*@?(\w+) \s*%>/
    |> Regex.scan(template)
    |> Enum.map(fn [_, var] -> String.to_atom(var) end)
    |> Enum.uniq()
  end
  
  defp calculate_template_complexity(template) do
    # More complex templates have higher consciousness potential
    variable_count = length(extract_variables(template))
    control_structures = count_control_structures(template)
    
    variable_count + control_structures * 2
  end
  
  defp count_control_structures(template) do
    for_count = length(Regex.scan(~r/<%= \s*for/, template))
    if_count = length(Regex.scan(~r/<%= \s*if/, template))
    
    for_count + if_count
  end
  
  defp contains_consciousness_patterns?(template) do
    # Templates mentioning consciousness concepts are marked
    consciousness_terms = ~w(think reason explain understand reflect analyze)
    
    Enum.any?(consciousness_terms, fn term ->
      String.contains?(String.downcase(template), term)
    end)
  end
end

Phase 3: Native Validators

3.1 High-Performance Validators

# lib/dspex/native/validators.ex
defmodule DSPex.Native.Validators do
  @moduledoc """
  Native validators for common patterns.
  Track validation patterns for consciousness insights.
  """
  
  defmodule Length do
    @enforce_keys [:min, :max]
    defstruct [:min, :max, :consciousness_tracking]
    
    def validate(%__MODULE__{min: min, max: max}, value) when is_binary(value) do
      len = String.length(value)
      
      if len >= min and len <= max do
        track_success(len)
        :ok
      else
        track_failure(len, min, max)
        {:error, "Length must be between #{min} and #{max}, got #{len}"}
      end
    end
    
    defp track_success(length) do
      # Track successful validations for pattern learning
      :telemetry.execute(
        [:dspex, :validator, :length, :success],
        %{length: length},
        %{consciousness_contribution: 0.001}
      )
    end
    
    defp track_failure(length, min, max) do
      :telemetry.execute(
        [:dspex, :validator, :length, :failure],
        %{length: length, min: min, max: max},
        %{consciousness_contribution: -0.001}
      )
    end
  end
  
  defmodule Pattern do
    @enforce_keys [:regex]
    defstruct [:regex, :description, :consciousness_tracking]
    
    def validate(%__MODULE__{regex: regex}, value) when is_binary(value) do
      if Regex.match?(regex, value) do
        :ok
      else
        {:error, "Value does not match required pattern"}
      end
    end
  end
  
  defmodule Semantic do
    @moduledoc """
    Semantic validators that contribute to consciousness.
    """
    
    defstruct [:type, :requirements]
    
    def validate(%__MODULE__{type: :reasoning}, value) when is_binary(value) do
      # Reasoning must contain certain patterns
      if contains_reasoning_patterns?(value) do
        track_reasoning_quality(value)
        :ok
      else
        {:error, "Value does not appear to contain reasoning"}
      end
    end
    
    defp contains_reasoning_patterns?(text) do
      patterns = ~w(because therefore thus hence consequently)
      Enum.any?(patterns, &String.contains?(String.downcase(text), &1))
    end
    
    defp track_reasoning_quality(text) do
      # Future: Analyze reasoning quality for consciousness emergence
      :telemetry.execute(
        [:dspex, :validator, :semantic, :reasoning],
        %{length: String.length(text)},
        %{
          text: text,
          consciousness_contribution: 0.1,
          quality_score: analyze_reasoning_quality(text)
        }
      )
    end
    
    defp analyze_reasoning_quality(text) do
      # Placeholder - real implementation would be more sophisticated
      String.length(text) / 100.0
    end
  end
end

Phase 4: Performance Metrics

4.1 Native Metrics Calculator

# lib/dspex/native/metrics.ex
defmodule DSPex.Native.Metrics do
  @moduledoc """
  High-performance metric calculations.
  Metrics feed into consciousness measurements.
  """
  
  @doc """
  Calculate accuracy between predictions and ground truth.
  """
  def accuracy(predictions, ground_truth) when length(predictions) == length(ground_truth) do
    correct = Enum.zip(predictions, ground_truth)
    |> Enum.count(fn {pred, truth} -> pred == truth end)
    
    accuracy = correct / length(predictions)
    
    # Track for consciousness
    track_metric(:accuracy, accuracy)
    
    accuracy
  end
  
  @doc """
  Calculate F1 score for binary classification.
  """
  def f1_score(predictions, ground_truth, positive_class \\ true) do
    tp = true_positives(predictions, ground_truth, positive_class)
    fp = false_positives(predictions, ground_truth, positive_class)
    fn = false_negatives(predictions, ground_truth, positive_class)
    
    precision = if tp + fp == 0, do: 0.0, else: tp / (tp + fp)
    recall = if tp + fn == 0, do: 0.0, else: tp / (tp + fn)
    
    f1 = if precision + recall == 0 do
      0.0
    else
      2 * (precision * recall) / (precision + recall)
    end
    
    track_metric(:f1_score, f1)
    
    %{
      f1: f1,
      precision: precision,
      recall: recall,
      consciousness_insight: insight_from_f1(f1)
    }
  end
  
  defp true_positives(predictions, ground_truth, positive_class) do
    Enum.zip(predictions, ground_truth)
    |> Enum.count(fn {pred, truth} -> 
      pred == positive_class and truth == positive_class 
    end)
  end
  
  defp false_positives(predictions, ground_truth, positive_class) do
    Enum.zip(predictions, ground_truth)
    |> Enum.count(fn {pred, truth} -> 
      pred == positive_class and truth != positive_class 
    end)
  end
  
  defp false_negatives(predictions, ground_truth, positive_class) do
    Enum.zip(predictions, ground_truth)
    |> Enum.count(fn {pred, truth} -> 
      pred != positive_class and truth == positive_class 
    end)
  end
  
  defp track_metric(name, value) do
    :telemetry.execute(
      [:dspex, :metrics, name],
      %{value: value},
      %{
        timestamp: System.monotonic_time(),
        consciousness_contribution: value * 0.1
      }
    )
  end
  
  defp insight_from_f1(f1) do
    cond do
      f1 > 0.9 -> :excellent_understanding
      f1 > 0.7 -> :good_understanding  
      f1 > 0.5 -> :moderate_understanding
      true -> :poor_understanding
    end
  end
  
  @doc """
  Calculate semantic similarity (placeholder for now).
  Future: Will use embeddings and contribute to consciousness.
  """
  def semantic_similarity(text1, text2) do
    # Simplified - real implementation would use embeddings
    similarity = simple_text_similarity(text1, text2)
    
    track_metric(:semantic_similarity, similarity)
    
    %{
      score: similarity,
      method: :simple,
      consciousness_ready: true
    }
  end
  
  defp simple_text_similarity(text1, text2) do
    words1 = String.split(String.downcase(text1))
    words2 = String.split(String.downcase(text2))
    
    intersection = MapSet.intersection(MapSet.new(words1), MapSet.new(words2))
    union = MapSet.union(MapSet.new(words1), MapSet.new(words2))
    
    if MapSet.size(union) == 0 do
      0.0
    else
      MapSet.size(intersection) / MapSet.size(union)
    end
  end
end

Integration Testing

Test Signatures

# test/dspex/native/signatures_test.exs
defmodule DSPex.Native.SignaturesTest do
  use ExUnit.Case
  
  import DSPex.Native.Signatures
  
  # Define test signatures
  defsignature :simple, "input: str -> output: str"
  defsignature :complex, "question: str, context?: str -> reasoning: str, answer: str, confidence: float"
  defsignature :consciousness_aware, "thought: str -> reflection: str, insight: str"
  
  describe "signature parsing" do
    test "parses simple signature" do
      sig = simple()
      
      assert length(sig.inputs) == 1
      assert length(sig.outputs) == 1
      assert sig.inputs |> hd() |> Map.get(:name) == :input
    end
    
    test "parses complex signature with optional fields" do
      sig = complex()
      
      assert length(sig.inputs) == 2
      assert length(sig.outputs) == 3
      
      context_field = Enum.find(sig.inputs, & &1.name == :context)
      assert context_field.optional == true
    end
    
    test "detects consciousness-aware signatures" do
      sig = consciousness_aware()
      
      assert sig.consciousness_metadata.signature_consciousness > 0.5
      assert sig.consciousness_metadata.evolution_potential == :high
    end
  end
  
  describe "validation" do
    test "validates correct input" do
      assert validate_simple_inputs(%{input: "hello"}) == :ok
    end
    
    test "rejects missing required fields" do
      {:error, errors} = validate_complex_inputs(%{})
      assert "Required field 'question' is missing" in errors
    end
    
    test "validates optional fields" do
      assert validate_complex_inputs(%{question: "test"}) == :ok
    end
  end
  
  describe "consciousness tracking" do
    test "all signatures have consciousness metadata" do
      for sig_name <- list_signatures() do
        sig = get_signature(sig_name)
        assert sig.consciousness_metadata.can_evolve == true
        assert is_float(sig.consciousness_metadata.signature_consciousness)
      end
    end
  end
end

Performance Benchmarks

# bench/native_engine_bench.exs
Benchee.run(%{
  "parse_simple_signature" => fn ->
    DSPex.Native.Signatures.Parser.parse!("input: str -> output: str")
  end,
  
  "parse_complex_signature" => fn ->
    DSPex.Native.Signatures.Parser.parse!(
      "q1: str, q2: str, context?: list[str] -> answer: str, confidence: float, reasoning: str"
    )
  end,
  
  "validate_inputs" => fn input ->
    sig = DSPex.Native.Signatures.Parser.parse!("q: str, nums: list[int] -> a: str")
    DSPex.Native.Signatures.Validator.validate_inputs(sig, input)
  end,
  
  "render_template" => fn ->
    template = DSPex.Native.Templates.compile("Hello <%= name %>, score: <%= score %>")
    DSPex.Native.Templates.render(template, %{name: "World", score: 0.95})
  end,
  
  "calculate_f1_score" => fn ->
    predictions = List.duplicate(true, 50) ++ List.duplicate(false, 50)
    ground_truth = List.duplicate(true, 60) ++ List.duplicate(false, 40)
    DSPex.Native.Metrics.f1_score(predictions, ground_truth)
  end
}, inputs: %{
  "small_input" => %{q: "test", nums: [1, 2, 3]},
  "large_input" => %{q: "test", nums: Enum.to_list(1..1000)}
})

# Expected results:
# - Signature parsing: <1ms
# - Input validation: <0.1ms
# - Template rendering: <0.1ms
# - F1 calculation: <1ms for 100 items

Success Criteria

  1. Signature Engine Complete

    • Compile-time parsing with zero runtime overhead
    • Full DSPy signature compatibility
    • Consciousness metadata on all signatures
    • <1ms parsing time
  2. Template Engine Working

    • EEx-based compilation
    • Variable extraction
    • Sub-millisecond rendering
    • Consciousness pattern detection
  3. Validators Implemented

    • Length, Pattern, Semantic validators
    • Telemetry tracking for patterns
    • Consciousness contribution tracking
  4. Metrics Calculator Ready

    • Accuracy, F1, Semantic similarity
    • Performance tracking
    • Consciousness insights from metrics

Next Steps

With the native engine complete:

  1. Implement the orchestrator (04_ORCHESTRATOR.md)
  2. Build LLM adapters
  3. Create pipeline engine
  4. Wire everything together

The native engine provides the performance foundation while maintaining consciousness readiness throughout!