Architecture
Storage layout, branch isolation, and the data flow between the CLI, MCP server, and Prolog engine.
System Diagram
graph TD
subgraph Git Repository
D[Markdown/YAML Documents]
end
D -->|Extract| E[Extractors]
E -->|Entities/Relationships| KB[Prolog KB (per branch)]
CLI[CLI: flags + JSON routes] --> OPS[shared operation specs]
MCP[MCP Server] --> OPS
OPS -->|Framed local RPC| ENG[Node kibi-engine\n(single writer per workspace/branch)]
ENG -->|One interactive process| KB[SWI-Prolog KB (per branch)]
AD[Optional host plugins:\nkibi-claude, kibi-cursor, kibi-codex,\nkibi-opencode, kibi-zcode] -->|calls| MCP
MCP -->|Tooling| VSCode[VS Code Extension]
CLI -->|Git Hooks| GH[Git Hooks]
GH -->|post-checkout/post-merge| KB
KB -->|Persist| RDF[RDF Persistence]Component Descriptions
Prolog Core
- Located at
packages/core/src/kb.pl - Implements journaled RDF persistence using SWI-Prolog's
rdf_persistency - Stores entities and relationships as RDF triples
- Enforces validation rules
- All operations mutex-protected for concurrency safety
CLI
- Located at
packages/cli/ - Peer public operation surface alongside MCP, plus maintenance and human-oriented commands
- Exposes all 21 public operations through
--input <file|->JSON routes and preserves ergonomic flag commands where available - Node.js 22+ is the supported CLI/MCP runtime and hosts the long-lived
kibi-enginedaemon - Automatically connects to (or starts) one engine per real workspace path and branch over a protected local socket/named pipe
- Maintenance commands include init, sync, migrate, gc, branch, doctor, and usage-metrics
- Runs extractors for Markdown/YAML
- Handles schema validation and audit logging
MCP Server
- Located at
packages/mcp/ - Peer public operation surface alongside the CLI
- Provides stdio JSON-RPC transport (newline-delimited, no embedded newlines)
- Registers the shared operation specs as host-visible
kb_*tools - Uses the same Node engine as CLI, so MCP and CLI requests serialize through one SWI-Prolog writer
Journaled engine
packages/cli/src/engine.tsowns the length-prefixed JSON RPC client and daemon lifecycle.- A daemon keeps one interactive SWI process attached for up to ten minutes after the last client disconnects. Requests carry IDs, protocol version, workspace identity, and structured errors over a local socket/named pipe.
- New branches use
.kb/branches/<exact-ref-sha256>/branch.jsonplusstorage.json,rdf/binary snapshots and journals, and an atomicCURRENTvalue of<generation-id>:<commit-sequence>. The manifest is an identity fence; literal branch directories are legacy compatibility storage. - Legacy
kb.rdf/audit.logbranches migrate once underkb.lockinto a staging generation. Canonical triple digests, counts, audit resources, schema fields, and relationship endpoints are checked before publication; originals remain in immutablelegacy/backups. - Domain triples and audit resources share the same RDF transaction.
audit.logis only produced bykibi storage exportand is not authoritative. - Ordinary sync compiles changed/deleted source files and relationship shards into the active journal.
kibi sync --rebuildis the only path that publishes a replacement generation. - Each attached engine rebuilds disposable ID/type/tag/source/token/coordinate indexes from RDF. Exact and paginated discovery uses the index to materialize only the requested page; a triple/entity-count mismatch rebuilds the index and never repairs RDF.
Codex Adapter Plugin
- Located at
packages/codex/ - Optional package that provides a Codex plugin manifest, skill bundle, and lifecycle hooks
- Points Codex MCP wiring to the local
kibi-mcpserver throughmcpServers; the plugin leavescwdunset so Codex supplies the active task workspace to the project-localnpx --no-installcommand. Settingcwdto.would instead re-root the process in the installed plugin cache. - Provides optional reminders and advisories only; it does not replace
kibi-core,kibi-cli, orkibi-mcp
Cursor Adapter Plugin
- Located at
packages/cursor/ - Optional package that provides a Cursor plugin manifest, rules, skills, commands, MCP config, and editor hooks
- Points Cursor MCP wiring to the local
kibi-mcpserver - Uses Cursor-specific hooks (
sessionStart,preToolUse,postToolUse,beforeReadFile,stop) for read/write guidance and freshness follow-ups - Provides optional reminders and advisories only; it does not replace
kibi-core,kibi-cli, orkibi-mcp
Other host adapters
packages/claude/(Claude Code) andpackages/zcode/(ZCode) follow the Codex pattern: bundled skills, localkibi-mcpwiring, and advisory hooks that stay silent in workspaces whose project root has no.kb/manifest.jsonpackages/opencode/(OpenCode) adds prompt guidance and background sync alongside the configuredkibi-mcpserver- Each package README lists exactly what its hooks emit
Entity Modeling:
flagentities represent runtime/config gates. Bug and workaround notes belong infactentities withfact_kind: observationormeta. Strict facts drive contradiction checks; observation/meta are non-blocking notes. See Entity Schema.domain-contradictionsapplies to strict lane;strict-fact-shapeis an advisory default-on quality diagnostic.
VS Code Extension
- Located at
packages/vscode/ - Explorer tree of requirements, scenarios, tests, decisions, flags, events, and symbols from the workspace knowledge base
- MCP integration for queries and updates
- Activates when the workspace contains
.kb
Git Hooks
- Installed in
$GIT_DIR/hooksor viacore.hooksPath post-checkout: ensures branch KB exists, runs syncpost-merge: runs synckibi gc: quarantines stale branch KBs and purges them only explicitly after retention
Data Flow Diagrams
Write Path (Document → KB)
sequenceDiagram
participant Dev as Developer
participant CLI as CLI
participant Ext as Extractors
participant ENG as kibi-engine
participant KB as SWI-Prolog
participant RDF as RDF Persistence
Dev->>CLI: kibi sync
CLI->>Ext: Run extractors once per changed source
Ext->>ENG: Delta entities/relationships
ENG->>KB: One serialized RDF transaction
KB->>RDF: Append journal / compact while idle
ENG->>KB: Validate and append audit resources atomicallyRead Path (KB → Query)
sequenceDiagram
participant User as User or Agent
participant Surface as CLI or MCP
participant Ops as Shared Operation
participant ENG as kibi-engine
participant KB as SWI-Prolog
participant RDF as RDF Persistence
User->>Surface: CLI JSON/flags or MCP tool call
Surface->>Ops: Validate shared operation input
Ops->>ENG: Framed local RPC
ENG->>KB: Indexed RDF query
KB->>RDF: Query RDF store
KB->>Ops: Return bindings
Ops->>Surface: Structured operation result
Surface->>User: Return CLI JSON or MCP contentPer-Branch KB Architecture
- Each exact Git branch identity has a compiled store under
.kb/branches/<sha256(exact-ref)>/with a versionedbranch.jsonidentity fence. kibi syncmaterializes a missing store from the current checkout's tracked Markdown/YAML/manifests and never copies another branch's compiled store.- Git remains the sole branch/merge authority: unresolved authored-file conflicts block compilation, and Kibi never selects merge winners.
- Worktree-local branches remain live for collection; remote-only refs do not.
- Legacy literal-path migration is explicit old/new, preview-first, and preserves a recoverable backup. Deleted stores are quarantined before purge.
- Git hooks invoke normal
kibi synconly; they do not create or clone a Kibi-specific branch model.
Source-First Mutation and Recovery
Tracked Markdown/YAML, symbol manifests, and relationship shards are the
authoritative project artifacts. kb_upsert and approved plan application may
write those files transactionally through the runtime, preserving existing
document bodies when requested, while Kibi never stages or commits Git state.
RDF/Prolog stores are compiled outputs and can be rebuilt with kibi sync.
Source writes carry before/after hashes, stay inside the workspace (including
symlink checks), and use a recovery journal with staged preimages/postimages.
Failures before the authoritative commit roll back; failures after it are
reported as committed_with_repairs with typed repair actions rather than
repeating the original mutation.
RDF Persistence Details
- Uses SWI-Prolog
library(semweb/rdf_persistency) - Directory layout:
storage.json,CURRENT,rdf/binary.trpsnapshots plus incremental.jrnjournals, and a legacy sentinelkb.rdf - File locking: lock file with timestamp, PID, hostname prevents concurrent access
- Multi-step updates guarded with
with_mutex/2for atomicity - Journals compact automatically while idle once they exceed 16 MiB;
kibi storage compactforces compaction library(persistency)remains only for legacy migration/import; journaled audit resources live in the RDF graph- Writes are acknowledged only after the transaction and journal are durable
MCP Stdio Transport
- JSON-RPC messages sent via stdio (newline-delimited)
- No embedded newlines in messages
- Only valid MCP messages on stdout; logs sent to stderr
Git Hook Automation
post-checkout: ensures branch KB exists, runs syncpost-merge: runs synckb gc: quarantines stale branch KBs and purges them only explicitly after retention
Directory Structure
- Authored knowledge lives in canonical
.kb/lanes:requirements/,scenarios/,tests/,facts/,adr/,flags/,events/, plussymbols.yamlandsymbol-coordinates.yaml. See Entity Schema. - Derived, Git-ignored runtime state lives under
.kb/branches/,.kb/recovery/,.kb/proof/runs/,.kb/briefs/,.kb/migrations/, and.kb/usage.log.