agence

πŸ—οΈ Agence Architecture & Design

Distributed Knowledge Base with Git-Based Sharding


1. System Overview

Agence is a distributed agent collaboration system that solves persistent memory and multi-tenant isolation through git-based sharding.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Agence Master Repository               β”‚
β”‚ (l-agence/agence-master - reference impl) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
           [Git Clone/Pull:]
             [CODEX]        : Governance
             [OBJECTCODE]   : CODE-base
             [GLOBALCACHE]  : RAG-base
             [ORGANIC]      : Swarm Orchestration  (Workflows, tasks etc)
             [SYNTHETIC]    : DWM-Base 
                   β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    ↓              ↓            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Agence     β”‚ β”‚ Agence     β”‚ β”‚ Agence     β”‚
β”‚ Shard A    β”‚ β”‚ Shard B    β”‚ β”‚ Shard C    β”‚
β”‚(Team Repo) β”‚ β”‚(Org Repo)  β”‚ β”‚(Project)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     β”‚             β”‚              β”‚
  [NEXUS]       [NEXUS]        [NEXUS]    : Local States
  [HERMETIC]    [HERMETIC]     [HERMETIC] : Secret-base

2. Core Architecture

2.1 Three-Layer Model

Layer 1: Master Repository

Layer 2: Shard Instances

Layer 3: Local Runtime (NEXUS)

2.2 Knowledge vs Operations (NEXUS vs CODEX)

CODEX (Immutable, Versioned)
β”œβ”€ Principles (maxims)
β”œβ”€ Laws (constraints)
β”œβ”€ Rules (best practices)
└─ Lessons (learned insights)

NEXUS (Operational, Ephemeral)
β”œβ”€ Logs (activity)
β”œβ”€ Faults (errors)
β”œβ”€ Sessions (agent state)
└─ Buffers (stdout/stderr/stdin - future)

Sync Strategy:


3. Sharding Strategy

3.1 Why Sharding?

Problem Centralized DB Git Sharding
Database Size Grows unbounded One per project (small)
Compression Complex, slow Native git compression
Security Shared access control Per-repo isolation
Scaling Requires infrastructure Scales with git
Backup Custom strategy Git native (distributed)

3.2 Shard Boundaries

Shards are created at git repository boundaries:

Development Organization
β”œβ”€ Frontend Team
β”‚  └─ repo: ui-framework
β”‚     └─ Agence Shard A (own NEXUS/CODEX)
β”œβ”€ Backend Team
β”‚  └─ repo: api-services
β”‚     └─ Agence Shard B (own NEXUS/CODEX)
└─ DevOps Team
   └─ repo: infrastructure
      └─ Agence Shard C (own NEXUS/CODEX)

Key Properties:

  1. Each repo contains its own .agence/ directory
  2. .agence/.gitignore excludes nexus/ (operational state)
  3. .agence/codex/ and .agence/synthetic/ are versioned
  4. Sessions can be exported for cross-shard handoff

3.3 Distribution Model

β”Œβ”€ Shard Instance
β”‚  β”œβ”€ bin/agence (symlink or copy from master)
β”‚  β”œβ”€ codex/ (versioned)
β”‚  β”‚  β”œβ”€ PRINCIPLES.md (base + overrides)
β”‚  β”‚  β”œβ”€ LAWS.md
β”‚  β”‚  β”œβ”€ RULES.md
β”‚  β”‚  └─ agents/ (local personas)
β”‚  β”œβ”€ synthetic/ (versioned)
β”‚  β”‚  β”œβ”€ lessons/ (project-specific)
β”‚  β”‚  └─ docs/
β”‚  └─ .gitignore (excludes nexus/)

β”Œβ”€ Not Shared: Instance
β”‚  β”œβ”€ nexus/ (local states, not versioned)
β”‚  β”‚  β”œβ”€ logs/
β”‚  β”‚  β”œβ”€ faults/
β”‚  β”‚  └─ sessions/
β”‚  β”œβ”€ hermetic/  (private local knowledge)
β”‚  β”‚  β”œβ”€ lessons/ (project-specific)
β”‚  β”‚  └─ docs/

4. Session Persistence & Handoff

4.1 Session Lifecycle

CREATE (^save)
    ↓
SAVED (session-saved.jsonl)
    ↓
    β”œβ”€β†’ RESUME (^resume) β†’ ACTIVE
    β”œβ”€β†’ HANDOFF (^handoff) β†’ TRANSFERRED
    └─→ ARCHIVE (after expiry)

4.2 Session Data Structure

{
  "session_id": "69a8f74c",
  "created_at": "2026-03-05T03:21:07Z",
  "status": "saved",
  "agent": "claudia",
  
  "runstate": {
    "agent_context": "Current task description",
    "execution_stack": "Current working directory, file states",
    "memory_state": "Variables, buffers, internal state",
    "cursor_position": "Active file and line position",
    "todo_list": [
      { "id": 1, "title": "Task 1", "status": "in-progress" }
    ]
  },
  
  "metadata": {
    "task_description": "What this session was working on",
    "repo": "/path/to/repo",
    "priority": "high",
    "tags": ["deployment", "urgent"]
  }
}

4.3 Cross-Shard Handoff

Shard A (Team Frontend)
β”œβ”€ Agent: Claudia (active)
β”œβ”€ Session: 69a8f74c (saves state)
β”‚  └─ Export: nexus/sessions/69a8f74c.export.json
β”‚
    [Git Commit & Push]
    [Manual transfer or CI/CD]
β”‚
Shard B (Team DevOps)
β”œβ”€ Agent: Ralph (receives)
β”œβ”€ Import: nexus/sessions/69a8f74c.export.json
└─ Execute: agence ^resume 69a8f74c
    └─ Continues with full context

5. Command Routing Architecture

5.1 Prefix-Based Routing

agence <input>
    β”‚
    β”œβ”€ "..."          β†’ mode_chat (natural language)
    β”œβ”€ "+..."         β†’ mode_ai_routed (autonomous planning)
    β”œβ”€ "/..."         β†’ mode_external (validated commands) [/commit /pull]
    β”œβ”€ "!..."         β†’ mode_system (shells & sessions) [!bash, !aider]
    β”œβ”€ "~..."         β†’ mode_private (agence hermetic commands) [~learn]
    └─ "^..."         β†’ mode_shared (agence synthetic commands) [^learn]
                         β”œβ”€ ^init (initialize environment)
                         β”œβ”€ ^reload (load all context)
                         └─ ^save (persist session)

5.2 External Command Safety

Commands under / are:

  1. Validated against whitelist (commands.json)
  2. Guarded via AIDO (opposite of sudo)
  3. Logged to NEXUS/logs/
  4. Reversible (git-friendly operations only)

Example:

agence /git-status      # βœ… Allowed (read-only)
agence /terraform-apply # ❌ Blocked (destructive)

6. Multi-Agent Coordination

6.1 Agent Registry

codex/agents/
β”œβ”€ @claudia        # (opus)    Principal SRE and architect. Visionary Evolution. 
β”œβ”€ @chad           # (GPT)     free & cost-optimized but Safe and reliable devops/cloudops. Stability. 
β”œβ”€ @aiko           # (haiku)   lead CI/CD DevOps SRE, infrastructure prototyping. Disruptive innovator. 
β”œβ”€ @sonya          # (sonnet)  Full Stack Dev lead and SRE. Deep detailed elegant coding. Backend, data layer.
β”œβ”€ @ralph          # (ralph)   Ralph Wiggum iteration loop
β”œβ”€ @olena          # (ollama)  free & Secure guard-railed On-premises local Ollama sessions.
β”œβ”€ @peers          # (3-peers) MIT style 3-peer LLM agent weighed consensus. Strategic planning, Unsolvable P1 problems, RCA reviews.
└─ @pilot          # (copilot) Copilot agent session.  Code review, quality

Each agent has:

6.2 Handoff Protocol

Agent A (active)
    β”‚
    β”œβ”€ Identify need for specialist
    β”œβ”€ Save session: ^save "Handing off to Ralph for infra"
    β”œβ”€ Determine target: Ralph (DevOps agent)
    β”‚
    └─ Execute: agence ^handoff ralph
        β”‚
        β”œβ”€ Export session to transfer format
        β”œβ”€ Note: "Ralph, vpc implementation"
        └─ Return context to user with pointer
        
User/Agent B
    β”‚
    β”œβ”€ Receive handoff pointer
    β”œβ”€ Load: agence ^resume 69a8f74c
    └─ Continue from where Agent A left off

7. Data Flow Examples

7.1 Single Shard Chat

User Input: agence "How do I configure TLS?"
    ↓
Agence (bin/agence)
    β”œβ”€ Detect mode: chat
    β”œβ”€ Load context: codex/LAWS.md, RULES.md
    β”œβ”€ Load agent: codex/agents/claudia.md
    β”œβ”€ Call LLM: Claude Opus
    β”œβ”€ Log call: nexus/logs/llm-calls.jsonl
    └─ Output: Structured response

7.2 Session Persistence

User Command: agence ^save "Halfway through deployment"
    ↓
Agence (save_session function)
    β”œβ”€ Capture: pwd, git status, todo list, history
    β”œβ”€ Generate: session_id (hex timestamp)
    β”œβ”€ JSON: Create session object
    β”œβ”€ Append: nexus/sessions/session-saved.jsonl
    β”œβ”€ Update: nexus/sessions/INDEX.md
    └─ Confirm: "Session 69a8f74c saved"

Later...

User Command: agence ^resume 69a8f74c
    ↓
Agence (resume_session function - future)
    β”œβ”€ Load: session JSON from JSONL
    β”œβ”€ Restore: pwd, file states, todo list
    β”œβ”€ Reload: agent context and memory
    β”œβ”€ Display: "Resuming deployment task at step 3/8"
    └─ Continue: Ready for next command

7.3 Multi-Shard Handoff

User: Working with Claudia on API design

User: agence ^handoff ralph "Need infrastructure setup"
    ↓
Agence (Shard A - Frontend)
    β”œβ”€ Save session: 69a8f74c
    β”œβ”€ Export: nexus/sessions/69a8f74c_ralph.export
    β”œβ”€ Commit: [handoff] Session 69a8f74c from Claudia to Ralph
    └─ Sync: Push to shared location (or manual transfer)

User: [Switches to backend repo - Shard B]

Agence (Shard B - Backend)
    β”œβ”€ Import: 69a8f74c_ralph.export
    β”œβ”€ Load agent: Ralph (DevOps/infrastructure specialist)
    β”œβ”€ Resume: agence ^resume 69a8f74c
    β”œβ”€ Context: Full previous conversation, decisions, state
    └─ Continue: Ralph picks up from step N with full context

8. Security Model

8.1 Isolation Boundaries

Per-Repository Isolation:

Per-Organization Isolation:

8.2 Access Control

Role          | CODEX Access | NEXUS Access | Handoff Rights
──────────────┼──────────────┼──────────────┼────────────────
Agent (local) | Read         | Read/Write   | Within shard
Agent (cross) | Read         | Read (hint)  | With approval
User          | Read         | Read (own)   | Full
Admin         | Read/Write   | Read/Write   | Full

9. Performance & Scaling

9.1 Compression Strategy

nexus/sessions/
β”œβ”€ session-active.jsonl      # Current (hot)
β”œβ”€ session-saved.jsonl       # Recent (warm)
β”œβ”€ session-completed.jsonl   # Old (compress & archive)
└─ archive/
   β”œβ”€ 2026-02.tar.gz (cold)
   └─ 2026-01.tar.gz (cold)

9.2 Query Performance


10. Implementation Roadmap

Phase Target Features
V0.1 βœ… Done Session save, command routing, NEXUS/CODEX separation
V0.2 βœ… Done Multi-agent coordination, session resume, architecture docs
V0.3 βœ… Done Git-based sharding, tmux 1+1, .ailedger, pipe-pane
V0.4 βœ… Done guard.ts, signal.ts, Docker, PS1, task IDs, ^regen
V0.5 βœ… Done Skills (29), peers, dispatch, mixed agent routing, security fixes
V0.6 βœ… Done First public release, cognitive memory (COGNOS), 291 tests
V0.7 βœ… Done Modular bin/agence split, MCP server, ^ken, ^recon, ledger auto-derive
V0.8 πŸ“ Next MLS enforcement + AIPOLICY capability tokens
V1.0 πŸ“‹ Planned Production-ready, Allegiance ledger, multi-org support

11. Key Design Decisions

Decision: Git-Based Sharding (vs Centralized DB)

Rationale:

Trade-offs:

Decision: JSONL for Sessions (vs JSON array)

Rationale:

Trade-offs:

Decision: Persistent NEXUS (vs Ephemeral)

Rationale:

Trade-offs:


12. Future Enhancements


13. Glossary

| Term | Definition | |β€”β€”|β€”β€”β€”β€”| | tangent | A single execution branch β€” one agent thread exploring a problem or subtask autonomously. Named for the geometric sense: a line touching the curve at one point, then diverging. | | sequent | An orchestrated set of tangents running in parallel. The swarm scheduler composes sequents to explore multiple solution paths simultaneously. | | resultant | The chosen outcome selected from competing tangents after a tournament. The vector sum of all exploration, collapsed to a single commit/artifact. | | divergent | The point where an exploration path forked from the main line. Recorded for time-travel replay and post-hoc analysis of decision trees. | | chunk | The AST-aware indexing system for codebase knowledge (objectcode). Pure TypeScript, Bun-compiled. Formerly β€œChunKing”. | | formtag | Dewey-decimal-like tagging system for general semantic knowledge (synthetic/hermetic). Lightweight context classification. | | mnemonic | Two-tier memory: ephemeral global index + federated per-org stores. Caches frequently accessed chunks/formtags. | | shard | A git repository configured with Agence. Each shard has its own CODEX (rules) and NEXUS (state). | | CODEX | Immutable governance layer: PRINCIPLES, LAWS, RULES, AIPOLICY. Versioned in git. | | NEXUS | Mutable operational state: sessions, logs, faults. Not version-controlled by design. |


Document Version: 0.4.0
Last Updated: 2026-04-10
Status: Active Development