manglekit

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 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.

Loops are opt-in (ADR-004): since v0.10 the OODA cognitive runtime is the extension package github.com/duynguyendang/manglekit/x/ooda — import x/ooda for loops (ooda.Run / ooda.RunOODA), x/oodaflow to run them as Genkit flows. It moved from sdk/ooda with the same exported names; the SDK itself ships governance-only and never imports x/.

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 (Fail-Closed Governance): The gate evaluates every action's facts against the tiered policy (T0–T3) using Datalog before execution — and reflects on the output after it. Both checks are fail-closed: a broken verifier blocks the action. Violations at T0/T1 block; rules tagged T2/T3 are advisory (logged, not blocking) — tier semantics are real, so learned playbook rules cannot silently hard-block production traffic. A blocking-tier violation surfaces to the caller as a structured core.PolicyViolationError — before the handler ever runs.
  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. Rule Learning: Extractors ingest Markdown/code into structured data. Offline rule induction ships today as mkit gen (Teacher-Student loop with syntax validation); the optional x/genes extension packages signed learned rules that enter enforcement only through the official policy channel (LoadPolicy), with hard tiers requiring an explicit human-review opt-in.
  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–3 "Standard Operating Procedures" enforced by the engine. (Terminology for the policy set — tiers are enforced natively; learned-rule packaging lives in x/genes.)
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.
x/ extensions Optional layers Public extensions the core never imports: x/ooda (OODA cognitive-loop runtime), x/oodaflow (OODA-as-Genkit-flow bridge), x/agents (reference Architect agent), x/east (EAST-steered generation) and x/genes (signed learned-rule packaging).

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 (27 demos, one directory each — incl. skill_learning: cross-session OODA skill learning) manglekit-examples
High-level design (layers, flows, governance) ARCHITECTURE.md workspace docs
CLI reference (eval, gen, check, inspect, kg, run, serve, skill) cmd/mkit/README.md

Directory Structure

manglekit/
├── adapters/           # Drivers for External Systems
│   ├── ai/             # Google Genkit bridge (actions, streaming gate, middleware)
│   ├── extractor/      # LLM-driven structured extraction into Go types
│   ├── func/           # Plain Go functions → supervised Actions
│   ├── knowledge/      # N-Quads/N-Triples/TTL knowledge loaders
│   ├── mcp/            # Model Context Protocol tools (policy-gated)
│   ├── resilience/     # Circuit breaker
│   ├── storage/        # BadgerDB quads (MEB bridge), session stores
│   └── vector/         # Vector store + Genkit retriever
├── cmd/                # CLI Tools
│   └── mkit/           # The 'mkit' Developer Utility
├── config/             # Configuration Loading (mangle.yaml)
├── core/               # Public Interfaces & Types (Action, Envelope, Errors)
├── 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
├── scenario/           # BDD-style policy-scenario harness
├── sdk/                # The User-Facing API (Client, Options)
│   └── ports/          # Extension contracts (TransientStore, ReasoningPort, …)
├── testutil/           # Deterministic mocks for consumer test suites
└── x/                  # Optional public extensions (core never imports x/)
    ├── agents/         # Reference agent (Architect) + toolkit
    ├── east/           # EAST (v4) generation steering
    ├── genes/          # Signed learned-rule packaging → policy channel
    ├── ooda/           # OODA loop runtime (frame, chassis, registry)
    └── oodaflow/       # OODA-as-Genkit-flow bridge

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. The loop ships as the opt-in extension x/ooda (ADR-004, v0.10); the governance core (Layers 1, 3, 4) works without it.
  • 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. Both gates are fail-closed: verifier/engine errors always block (SupervisorError); policy denies block at Tier-0/1 (PolicyViolationError), while explicitly-tagged Tier-2/3 rules stay advisory. Payload facts are resource-capped, and every decision carries an audit trail (Explain/--explain).
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 policy ("GenePool"): The set of .dl policy files the engine loads by trust level (T0 axiom, T1 governance, T2 playbook, T3 user). Learned rules reach enforcement only as policy source: offline mkit gen today, signed x/genes packaging with human-review promotion, or app-side runtime adaptation via ooda.Memory (see the skill_learning example).
    • Policy Solver: Deterministic Datalog evaluator — comparisons (:ge/:le/:gt/:lt), negation (!), aggregation (fn:sum/fn:max/fn:min/fn:group_by), stratified execution, external Go predicates, temporal facts, EXPLAIN proofs, and hot reload (engine builtins survive reloads).
  • 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.
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
agents
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.
agents/tools
Package tools provides the tool implementations for the Architect Agent
Package tools provides the tool implementations for the Architect Agent
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.
genes
Package genes provides an OPTIONAL, file-based "crystallized logic" extension for learned Datalog rules — genes — that are applied to the policy engine through its official source channel (sdk.Client.LoadPolicy).
Package genes provides an OPTIONAL, file-based "crystallized logic" extension for learned Datalog rules — genes — that are applied to the policy engine through its official source channel (sdk.Client.LoadPolicy).

Jump to

Keyboard shortcuts

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