  # HandlerWrapper GenServer: wraps a closure for use as a handler
  defmodule HandlerWrapper do
    use GenServer

    def start_link(handler_fn) do
      GenServer.start_link(__MODULE__, handler_fn)
    end

    def init(handler_fn) do
      {:ok, handler_fn}
    end

    def handle_cast({:trait_call, _method, args_json, reply_id}, handler_fn) do
      case Jason.decode(args_json) do
        {:ok, _args} ->
          # Call the wrapped closure
          try do
            response = handler_fn.(nil)
            response_json = Jason.encode!(response)
            Native.complete_trait_call(reply_id, response_json)
          rescue
            _e -> Native.complete_trait_call(reply_id, "{\"error\": \"handler error\"}")
          end

        {:error, _} ->
          Native.complete_trait_call(reply_id, "{\"error\": \"json decode error\"}")
      end

      {:noreply, handler_fn}
    end
  end
