manglekit

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

README

Go License

Manglekit

Manglekit is the Sovereign Neuro-Symbolic Logic Kernel for Go.

It solves the Stochastic Runtime Paradox of modern AI: applications require Deterministic Reliability (strict protocols, type safety, logic), but LLMs are inherently Probabilistic (creative, non-deterministic).

Manglekit bridges this gap by formalizing the agent lifecycle into an OODA Loop (Observe, Orient, Decide, Verify, Act) protected by a Zero-Trust Supervisor architecture:

  1. The Brain (Symbolic): The Datalog Engine and Tiered GenePool (the .dl policy set) that handle verifiable reasoning and fail-closed verification.
  2. The Planner (Neural): The Execution Runtime (Genkit) that drafts generative plans.
  3. The Memory (Silo): A persistent BadgerDB storage layer for SPO facts and vector embeddings.

Quick Start

Three steps: create a client with a policy, define a typed action, run it. Every execution passes the zero-trust supervisor's fail-closed pre-check.

1. Define a policy (policy.dl) — Datalog rules that gate execution:

% Payload fields tagged with `mangle:"..."` become facts at pre-check.
Decl topic(Req, Value).

% Block jokes about passwords.
halt("Req", "do not tell jokes about passwords") :-
    action_operation("Req", "tell_joke"),
    topic(Req, "passwords").

2. Write the skill (main.go):

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/duynguyendang/manglekit/sdk"
)

type JokeRequest struct {
    Topic string `mangle:"topic"`
}

type JokeResponse struct {
    Joke string `mangle:"joke"`
}

func main() {
    ctx := context.Background()

    // Create the client with a Datalog policy blueprint.
    client, err := sdk.NewClient(ctx, sdk.WithPolicyPath("policy.dl"))
    if err != nil {
        log.Fatalf("client init: %v", err)
    }
    defer client.Shutdown(ctx)

    // Define a supervised, type-safe action.
    joke := sdk.Define(client, "tell_joke",
        func(ctx context.Context, in JokeRequest) (JokeResponse, error) {
            return JokeResponse{Joke: "Why do programmers prefer dark mode? Because light attracts bugs."}, nil
        })

    // Execute — the supervisor checks the policy before the handler runs.
    out, err := joke.Run(ctx, JokeRequest{Topic: "security"})
    if err != nil {
        log.Fatalf("blocked or failed: %v", err)
    }
    fmt.Println(out.Joke)
}

3. Run it:

go mod init example.com/joke && go mod tidy
go run .

Change Topic to "passwords" and the request is blocked with a core.PolicyViolationError — before the handler ever runs.

Scaffold instead: mkit skill new <name> generates this exact layout (main.go + policy.dl + a contract test) for you. Install with make install-cli.

Configuration file (optional)

Instead of options, load a YAML config with sdk.WithConfigFile("mangle.yaml"):

policy:
  path: "${POLICY_PATH:-./policies/main.dl}"
  evaluation_timeout: 30

observability:
  enabled: true
  service_name: "${SERVICE_NAME:-manglekit-app}"
  log_level: "${LOG_LEVEL:-info}"

Core Capabilities

  1. OODA Loop Execution: Orchestrates AI workflows using a structural Observe, Orient, Decide, Verify, Act pipeline.
  2. Shadow Audit (Self-Correction): The Verify step evaluates AI-generated plans against the Tier 0/1/2 GenePool policies using Datalog before execution, in a fail-closed manner. If a policy is violated, the action is blocked rather than allowed through.
  3. The Silo (Persistent Knowledge): Native BadgerDB integration providing high-performance SPOg (Subject-Predicate-Object-Graph) quad indexing and vector storage for long-term memory.
  4. Extractors: Built-in extractors capable of ingesting Markdown/Code into structured data. (Dynamic rule induction is planned — see ROADMAP.md.)
  5. Deep Observability: Fully integrated OpenTelemetry tracing that links Genkit spans directly to logic rules, showing exactly why a decision was made.

System Building Blocks

Component Role Responsibility
SDK Client The entry point. Developers use client.Supervise() (or sdk.Define) to wrap capabilities.
GenePool Logic Store Datalog files (.dl) defining the Tier 0, 1, and 2 "Standard Operating Procedures" enforced by the engine.
The Silo Persistent Memory BadgerDB backed SPOg quad fact and vector storage.
Supervisor Interceptor The zero-trust gateway that enforces the GenePool on every action.
Adapters Drivers Universal adapters for LLMs (Genkit), Extractors, Tools (MCP), Functions, and Resilience.

Learn more

Topic Where
Building OODA applications (phases, CognitiveFrame, memory, Genkit flows, middleware) docs/guides/ooda.md
Datalog engine capabilities (comparisons, negation, aggregation, arithmetic) docs/guides/datalog.md
Runnable examples (22 demos, one directory each) manglekit-examples
High-level design (layers, flows, governance) ARCHITECTURE.md workspace docs
CLI reference (eval, gen, inspect, kg, run, serve, skill) cmd/mkit/README.md

Directory Structure

manglekit/
├── adapters/           # Drivers for External Systems (AI, MCP, Vector)
│   ├── ai/             # Google Genkit & LLM Adapters
│   ├── knowledge/      # N-Quads/RDF Knowledge Loaders
│   ├── mcp/            # Model Context Protocol Tools
│   └── resilience/     # Circuit Breaker
├── agents/             # Reference agent (Architect)
├── cmd/                # CLI Tools
│   └── mkit/           # The 'mkit' Developer Utility
├── config/             # Configuration Loading
├── core/               # Public Interfaces & Types (Action, Envelope)
├── docs/               # Guides (OODA, Datalog)
├── internal/           # Private Implementation
│   ├── engine/         # The Datalog Logic Engine (Solver, Runtime)
│   ├── supervisor/     # The Governance Interceptor
│   └── ...
├── multiagent/         # Multi-agent runtime (AgentSystem, workflows)
├── providers/          # LLM/embedder/memory provider plugins
└── sdk/                # The User-Facing API (Client, Loop)
    └── ooda/           # OODA Loop Implementation

Runnable demos live in the sibling manglekit-examples repository.


Architecture

Manglekit is a Sovereign Logic Kernel built on four core layers:

Layer 1: The Client (SDK)
  • Role: Orchestrates the entire governance flow
  • Responsibilities: Holds configuration, manages the Cognitive Loop, and coordinates observability.
  • Entry Point: sdk.NewClient() initializes the kernel with policy rules.
Layer 2: The Cognitive Loop (OODA)
  • Role: An intelligent orchestration layer that binds logic to execution.
  • Lifecycle: Observe -> Orient -> Decide -> Verify -> Act
    • Observe: Ingest raw signals and extract logical quad facts (SPOg) and embeddings into The Silo.
    • Orient: Align input context against The Silo and Tiered Policy Rules.
    • Decide: Generate an execution plan via the LLM Driver.
    • Verify: Evaluate the execution plan against Datalog GenePool policies (fail-closed Shadow Audit).
    • Act: Safely execute capability (Tool, API Call) through the Zero-Trust Supervisor.
Layer 3: The Zero-Trust Supervisor (Interceptor)
  • Role: The mechanical port that physically blocks unverified Actions.
  • Pattern: Middleware / Decorator for execution protocols. The pre-flight check is fail-closed: verifier errors and Tier-0/1 violations block execution.
Layer 4: The Brain (Memory & Logic Store)
  • Role: The deterministic reasoning and storage layer.
  • Components:
    • The Silo: Persistent BadgerDB storage for metadata, vectors, and facts (Quads).
    • Tiered GenePool: The set of .dl policy files the engine loads by trust level (Axioms, Governance, User). AI-induced rule learning is planned — see ROADMAP.md.
    • Policy Solver: Robust Datalog Evaluator supporting comparisons (:ge/:le/:gt/:lt), negation (!), aggregation (fn:sum/fn:max/fn:min/fn:count), arithmetic (fn:mult/fn:div/fn:plus/fn:minus), and stratified execution.
  • Guarantees: Fast (microsecond latency), deterministic, testable, verifiable.
Universal Adapters

Bridge external libraries into the kernel:

  • ai Adapter: Wraps Google Genkit models and embedders.
  • func Adapter: Wraps native Go functions as Actions.
  • mcp Adapter: Integrates Model Context Protocol (MCP) servers.
  • extractor Adapter: Performs semantic extraction using LLMs.
  • vector Adapter: Handles vector search and retrieval operations.
  • resilience Adapter: Provides Circuit Breaker functionality for failure resilience.
import (
    "time"

    "github.com/duynguyendang/manglekit/adapters/resilience"
    "github.com/duynguyendang/manglekit/core"
)

func wrap(myAction core.Action) core.Action {
    config := resilience.CircuitBreakerConfig{
        FailureThreshold: 5,
        ResetTimeout:     30 * time.Second,
    }
    // If myAction fails repeatedly, the wrapper returns resilience.ErrCircuitOpen
    return resilience.NewCircuitBreaker(myAction, config)
}

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Define

func Define[In any, Out any](
	c *Client,
	name string,
	handler func(context.Context, In) (Out, error),
) *sdk.Runnable[In, Out]

Define is the public entry point for creating Actions

func MustReadFile added in v0.4.0

func MustReadFile(path string) []byte

MustReadFile reads the file at path and panics on error. It is a small convenience for example/demo setup code where a missing fixture is fatal.

The path is resolved cwd-independently for relative paths: if the file cannot be found relative to the current working directory, it is resolved relative to the source directory of MustReadFile's caller (via runtime.Caller). This makes examples and tests work whether they are run from the repo root, the example's directory, or `go test`.

func NewRequestEnv added in v0.4.0

func NewRequestEnv(text, action string, labels []string) core.Envelope

NewRequestEnv builds an envelope for a request gated by a policy. It sets the payload to text, attaches the provided security labels (e.g. "tainted"), and emits an action_operation fact so policies keyed on the action name can match.

func WithExtractor added in v0.7.0

func WithExtractor(action core.Action, ext ports.Extractor) core.Action

WithExtractor sets the text-to-struct extractor on a supervised action, enabling the neuro-symbolic bridge.

Types

type Client

type Client = sdk.Client

--- Aliases ---

func Must

func Must(c *Client, err error) *Client

Must helper for panic-on-error initialization

func MustNewClient added in v0.4.0

func MustNewClient(ctx context.Context, opts ...ClientOption) *Client

MustNewClient creates a client and panics if initialization fails.

func NewClient

func NewClient(ctx context.Context, opts ...ClientOption) (*Client, error)

NewClient initializes the client with defaults. It implements the "Batteries Included" philosophy by leveraging SDK defaults.

func QuickClient added in v0.7.0

func QuickClient(ctx context.Context, policyPath string) (*Client, error)

QuickClient is the shortest path to a governed client: it constructs a Client and loads the Datalog policy at policyPath (same as WithPolicyPath), returning typed errors for a missing file or an invalid policy. For anything beyond a policy file, use NewClient with options.

client, err := manglekit.QuickClient(ctx, "policy.dl")

type ClientOption

type ClientOption = sdk.ClientOption

func WithConfig added in v0.7.0

func WithConfig(cfg *config.Config) ClientOption

WithConfig applies settings from a loaded configuration struct.

func WithConfigFile added in v0.7.0

func WithConfigFile(path string) ClientOption

WithConfigFile loads configuration from a YAML file and applies it.

func WithEngine added in v0.7.0

func WithEngine(e core.Evaluator) ClientOption

WithEngine allows injecting a custom or mock core.Evaluator.

func WithHistory

func WithHistory(store core.HistoryStore) ClientOption

WithHistory sets only the chat-history component of the memory, composing with an existing HybridMemory. It returns an error at construction time (rather than silently discarding it) if a custom non-hybrid memory was configured via WithMemory.

func WithLLM added in v0.7.0

func WithLLM(gen core.TextGenerator) ClientOption

WithLLM configures the AI backend for the client.

func WithLogger

func WithLogger(l core.Logger) ClientOption

WithLogger sets a custom logger for the client.

func WithMemory

func WithMemory(mem core.AgentMemory) ClientOption

WithMemory replaces the whole memory implementation (history + RAG).

func WithPolicyPath

func WithPolicyPath(path string) ClientOption

WithPolicyPath specifies the file path of the Datalog policy to load at client construction. This is the canonical option name; it matches config `policy.path` and the CLI `--policy` flag.

func WithProviderConfig added in v0.7.0

func WithProviderConfig(name string, cfg config.ActionConfig) ClientOption

WithProviderConfig wires a provider-backed action from config.

func WithStateProvider added in v0.7.0

func WithStateProvider(provider core.StateProvider) ClientOption

WithStateProvider configures durable session persistence.

func WithStdoutTracer added in v0.7.0

func WithStdoutTracer() ClientOption

WithStdoutTracer enables a console tracer for debugging.

func WithSteeringEnabled added in v0.7.0

func WithSteeringEnabled() ClientOption

WithSteeringEnabled enables policy-driven routing (route/retry).

func WithTracerProvider added in v0.7.0

func WithTracerProvider(tp trace.TracerProvider) ClientOption

WithTracerProvider configures the OpenTelemetry tracer provider.

type ExecuteOption

type ExecuteOption = sdk.ExecuteOption

func WithMetadata

func WithMetadata(key, value string) ExecuteOption

WithMetadata injects custom key-value pairs into the execution envelope's metadata.

func WithSessionID

func WithSessionID(id string) ExecuteOption

WithSessionID activates persistent stateful mode for the execution.

func WithTransientMemory

func WithTransientMemory() ExecuteOption

WithTransientMemory activates in-memory stateful mode.

Directories

Path Synopsis
adapters
ai
mcp
storage/meb
Package meb provides a MEB (Mangle Extension for Badger) storage adapter that implements the KnowledgeStore interface for the OODA loop.
Package meb provides a MEB (Mangle Extension for Badger) storage adapter that implements the KnowledgeStore interface for the OODA loop.
storage/session
Package session provides a transient, in-memory store for coordination facts during OODA loop execution.
Package session provides a transient, in-memory store for coordination facts during OODA loop execution.
Package agents provides the Architect Agent implementation using the OODA Chassis SDK with Dual Memory architecture.
Package agents provides the Architect Agent implementation using the OODA Chassis SDK with Dual Memory architecture.
tools
Package tools provides the tool implementations for the Architect Agent
Package tools provides the tool implementations for the Architect Agent
cmd
mkit command
mkit/commands/check
Package check implements the `mkit check` subcommand: load and lint a policy without running a query.
Package check implements the `mkit check` subcommand: load and lint a policy without running a query.
mkit/commands/exitcode
Package exitcode defines the mkit CLI exit-code contract and maps errors to exit codes.
Package exitcode defines the mkit CLI exit-code contract and maps errors to exit codes.
mkit/commands/version
Package version implements the `mkit version` subcommand.
Package version implements the `mkit version` subcommand.
internal
providers
predicates
Package predicates provides reference external predicates for use in Datalog policies.
Package predicates provides reference external predicates for use in Datalog policies.
Package scenario provides a tiny, dependency-free test harness for running behavior-driven policy scenarios against a Manglekit client.
Package scenario provides a tiny, dependency-free test harness for running behavior-driven policy scenarios against a Manglekit client.
sdk
Package sdk provides the Manglekit SDK.
Package sdk provides the Manglekit SDK.
Package testutil provides deterministic, dependency-free test doubles for building examples and tests against manglekit without external services or API keys:
Package testutil provides deterministic, dependency-free test doubles for building examples and tests against manglekit without external services or API keys:
x
east
Package east implements EAST (Entropy-Activity-Steering-Throttling), the OODA v4 generative/research path.
Package east implements EAST (Entropy-Activity-Steering-Throttling), the OODA v4 generative/research path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL