Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

8 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ‘‘ KAF โ€” King-Agent Framework

The governance layer for multi-agent swarms. Stop your AI agents from deleting your files, escaping their workspace, and gaslighting each other.

License: MIT Python 3.10+ 520-Compliant Platform-agnostic Made from real scars

You run 3+ AI coding agents โ€” Claude Code, Cursor, OpenCode, Codex, Kimiโ€ฆ They talk to you. They don't talk to each other. And they have no rules about what they can touch.

CrewAI / LangGraph / Ruflo answer "how do agents execute tasks together." KAF answers "how do you stop agents from wrecking your machine."

It's not an orchestrator. It's the constitution + runtime guardrail that sits under any orchestrator.


๐Ÿ”ฅ Your agents are out of control (and you know it)

  • An agent rm -rf'd a folder it shouldn't have. Gone. No backup.
  • An agent dropped 47 temp files across your C:\ drive.
  • An agent overwrote constitution.json. Your rules vanished.
  • Two agents read each other's private memory. Context contaminated.
  • Something broke at 2 AM. No script, no log. Nobody knows what happened.

This is not theoretical

KAF was forged inside a real 6-agent cluster (WorkBuddy + OpenCode + Claude + Kimi + Cursor) running daily for 3 months. We lost 2000+ map assets to one irreversible delete before we built the guardrails.

KAF is the scar tissue. MIT-licensed, so you don't have to bleed for it.


๐Ÿ›ก๏ธ The 520 Rule โ€” four principles, three iron laws

Every action an agent takes must be:

Principle What it means in code
5 Traceable Every op has a script + log entry (kaf_operations.log)
2 Recoverable Deletes go to recycle bin; configs are backed up first
0 Fixable On failure, on_failure() hands you rollback options
+ Evolvable Good workflows auto-crystallize into reusable Skills

Three Iron Laws โ€” violations are blocked at runtime, not just warned:

  • ๐Ÿ”’ Law 8 โ€” Destructive ops (rm / mv / copy) MUST come from a script, then be verified.
  • ๐Ÿ”’ Law 9 โ€” Any number written to memory MUST be verified against the filesystem.
  • ๐Ÿ”’ Law 10 โ€” Before deleting, the agent MUST show the full file list and get your confirmation.

Live guardrail interception

$ python kaf.py guard
  pre:delete          โ†’ ้“ๅพ‹10๏ผšๅˆ ้™คๅ‰ๅฑ•็คบๆธ…ๅ•+็”จๆˆท็กฎ่ฎค
  pre:destructive_op  โ†’ ้“ๅพ‹8๏ผš็ ดๅๆ€งๆ“ไฝœๅฟ…้กปๆœ‰่„šๆœฌ
  post:write_memory   โ†’ ้“ๅพ‹9๏ผš่ฎฐๅฟ†ๆ•ฐๅญ—ๅฎžๅœฐๆ ธๆŸฅ
  startup             โ†’ ่ฎฐๅฟ†ๅฎŒๆ•ดๆ€ง๏ผšๆŒ‡็บนๆ ก้ชŒ
from guard520 import Guard520
guard = Guard520("constitution.json")

guard.pre_execute({"type": "rm", "target": "D:/x"})
# โ†’ block: ้“ๅพ‹8่ฟ่ง„๏ผšrm ๆ“ไฝœๆ— ่„šๆœฌ

guard.pre_delete({"type": "rm", "target": "constitution.json"})
# โ†’ block: ้“ๅพ‹10่ฟ่ง„๏ผšๆœชๅฑ•็คบๆธ…ๅ•/ๆœช่Žท็กฎ่ฎคใ€‚ๅพ…ๅˆ 1้กน

guard.pre_execute({"type": "rm", "target": "D:/x", "script": "clean.py", "verified": True})
# โ†’ ok

No config, no --force, no "are you sure?" bypass. The guard returns BLOCK and writes it to the log.


๐Ÿ›๏ธ Architecture โ€” five layers

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Platform Adapters   (WorkBuddy/Claude/...)  โ”‚  5 lines of code to plug in
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Coordinator Protocol  (Prime Minister rotate)โ”‚  who's in charge, right now
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  520 Runtime Guard     (4 checkpoints)        โ”‚  blocks Law 8/9/10 violations
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Constitution-as-Code (JSON, machine-readable)โ”‚  rules you can diff & CI-test
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  Memory Integrity      (SHA-256 fingerprint)  โ”‚  detect unauthorized drift
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Why JSON, not a markdown doc? So your constitution can be parsed, diffed, and CI-tested โ€” not just read.


โšก Quick start

git clone https://github.com/lsjpp2/king-agent-swarm.git
cd king-agent-swarm/kaf

python kaf.py init      # generate constitution.json + register memory fingerprints
python kaf.py check     # 520 self-check  โ†’  โœ… PASS
python kaf.py verify    # memory integrity (fingerprint + drift detect)
python kaf.py status    # who's the current Prime Minister

Real kaf check output on a fresh cluster:

==================================================
  KAF 520 ่‡ชๆฃ€
==================================================
  โœ… traceable: ๆ—ฅๅฟ—่ฎฐๅฝ•่ƒฝๅŠ›: ๅฐฑ็ปช | ๆ—ฅๅฟ—ๆ–‡ไปถ: ๅพ…็”Ÿๆˆ๏ผˆ้ฆ–ๆฌก่ฟ่กŒๆญฃๅธธ๏ผ‰
  โœ… recoverable: ๅˆ ้™คๆ“ไฝœ่ตฐๅ›žๆ”ถ็ซ™(FOF_ALLOWUNDO)
  โœ… fixable: on_failureๆไพ›ๅ›žๆปšๆ–นๆกˆ
  โœ… evolvable: skill็›ฎๅฝ•: .../.workbuddy/skills (25ไธชskill)

  ๆ€ปไฝ“: PASS

๐Ÿ“œ Constitution-as-Code

Your governance is a JSON file, not a vibe:

{
  "rule_520": {
    "enabled": true,
    "immutable": true,
    "note": "ๅฐฑ็ฎ—ไธ–็•Œ็ญไบก๏ผŒ่ฟ™ไธชๆ ‡ๅ‡†ไธ่ƒฝไธข",
    "iron_laws": {
      "law_8": "script_then_execute_then_verify",
      "law_9": "verify_any_number_in_memory",
      "law_10": "show_list_before_delete"
    }
  },
  "rules": [
    { "id": "delete_auth", "trigger": "pre:delete",
      "require": "user_confirm", "irreversible_action": "warn_and_block" },
    { "id": "path_discipline", "trigger": "pre:write",
      "allow_only": "{{workspace}}/**",
      "blocked_zones": ["Desktop", "Documents", "Downloads", "system_root"] }
  ]
}

Path discipline alone stops ~90% of "why is my Desktop full of agent junk" incidents.


๐Ÿ†š How is KAF different?

KAF CrewAI / LangGraph RuFlo AutoGen
Layer Governance (how to rule agents) Orchestration (how to run tasks) Homogeneous swarm Conversation orchestration
Dangerous-op guard โœ… runtime BLOCK โŒ โŒ โŒ
Path discipline โœ… built-in โŒ โŒ โŒ
Memory isolation walls โœ… โš ๏ธ manual โŒ โŒ
Constitution-as-Code โœ… JSON, CI-testable โŒ โŒ โŒ
Heterogeneous agents โœ… Claude+Cursor+โ€ฆ+ โš ๏ธ โŒ (Claude-only) โš ๏ธ
Platform-agnostic โœ… adapter SDK varies โŒ โš ๏ธ

KAF doesn't replace them. It governs them. Drop it under any orchestrator.


๐Ÿ”Œ Platform Adapters

from adapters.base import PlatformAdapter

class MyAdapter(PlatformAdapter):
    platform_name = "my_platform"

    def read_constitution(self): ...
    def read_memory(self, key=None): ...
    def write_memory(self, key, value, protect_check=True): ...
    def register_hook(self, event, callback): ...
    def execute(self, action): ...
    def get_agent_id(self): ...
    def get_workspace(self): ...

The WorkBuddy adapter ships in-box. Claude / Cursor / OpenCode adapters are doc-based (adapters/*.md). Full SDK in kaf/adapters/.


๐Ÿ“‚ Repository layout

king-agent-swarm/
โ”œโ”€โ”€ kaf/                      # โ˜… KAF v5.0 core (this is the framework)
โ”‚   โ”œโ”€โ”€ constitution.json     # declarative constitution (machine-readable)
โ”‚   โ”œโ”€โ”€ guard520.py           # 520 runtime guard โ€” 4 checkpoints
โ”‚   โ”œโ”€โ”€ memory_integrity.py   # SHA-256 fingerprint + drift detection
โ”‚   โ”œโ”€โ”€ coordinator.json      # Prime Minister registry (rotate / vote)
โ”‚   โ”œโ”€โ”€ kaf.py                # CLI: init / check / verify / guard / rotate / status
โ”‚   โ”œโ”€โ”€ adapters/             # platform SDK (base / workbuddy / template)
โ”‚   โ””โ”€โ”€ README.md             # deep dive (ไธญๆ–‡ + English)
โ”œโ”€โ”€ templates/                # coordination-protocol layer (v1, config-only)
โ”œโ”€โ”€ docs/                     # architecture / quick-start / principles / faq
โ”œโ”€โ”€ adapters/                 # per-platform injection guides (.md)
โ”œโ”€โ”€ diagrams/                 # SVG architecture diagrams
โ””โ”€โ”€ examples/                 # minimal 3-agent & 6-agent clusters

KAF is the engine; templates/ + docs/ + diagrams/ form the coordination-protocol layer โ€” the v1 "King / Prime Minister / Swarm" metaphor that KAF grew out of.


๐Ÿค Contributing

PRs, issues, and war stories welcome. See CONTRIBUTING.md.

Even the governance is governed: user_feedback โ†’ coordinator_evaluate โ†’ king_confirm โ†’ merge.

๐Ÿ“„ License

MIT โ€” see LICENSE.

About

KAF - The governance layer for multi-agent swarms. Constitution-as-Code + 520 Runtime Guard. Stop AI agents from deleting files, escaping workspace, or contaminating memory.

Topics

Resources

Contributing

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages