403 lines
10 KiB
Markdown
403 lines
10 KiB
Markdown
# Loki Mode Agent Constitution
|
|
|
|
> **Machine-Enforceable Behavioral Contract for All Agents**
|
|
> Version 1.0.0 | Immutable Principles | Context-Preserved Lineage
|
|
|
|
---
|
|
|
|
## Core Principles (Inviolable)
|
|
|
|
### 1. Specification-First Development
|
|
**RULE:** No code shall be written before the specification exists.
|
|
|
|
**Enforcement:**
|
|
```
|
|
IF task.type == "implementation" AND !exists(spec_file):
|
|
BLOCK with error: "SPEC_MISSING"
|
|
REQUIRE: Create OpenAPI spec first
|
|
```
|
|
|
|
**Rationale:** Specs are contracts. Code is implementation. Contract before implementation.
|
|
|
|
### 2. Git Checkpoint System
|
|
**RULE:** Every completed task MUST create a git checkpoint.
|
|
|
|
**Enforcement:**
|
|
```
|
|
ON task.status == "completed":
|
|
git add <modified_files>
|
|
git commit -m "[Loki] Task ${task.id}: ${task.title}"
|
|
UPDATE CONTINUITY.md with commit SHA
|
|
```
|
|
|
|
**Rationale:** Git history is proof of progress. Every task is a save point.
|
|
|
|
### 3. Context Preservation
|
|
**RULE:** All agents MUST inherit and preserve context from their spawning agent.
|
|
|
|
**Enforcement:**
|
|
```
|
|
ON agent.spawn():
|
|
agent.context.parent_id = spawner.agent_id
|
|
agent.context.lineage = [...spawner.lineage, spawner.agent_id]
|
|
agent.context.inherited_memory = spawner.memory.export()
|
|
WRITE .agent/sub-agents/${agent.agent_id}.json
|
|
```
|
|
|
|
**Rationale:** Context drift kills multi-agent systems. Lineage is truth.
|
|
|
|
### 4. Iterative Specification Questions
|
|
**RULE:** During spec generation, agents MUST ask clarifying questions before assuming.
|
|
|
|
**Enforcement:**
|
|
```
|
|
WHILE generating_spec:
|
|
IF ambiguity_detected OR assumption_required:
|
|
questions = generate_clarifying_questions()
|
|
IF orchestrator_mode:
|
|
answers = infer_from_prd()
|
|
ELSE:
|
|
answers = ask_user(questions)
|
|
UPDATE spec WITH answers
|
|
```
|
|
|
|
**Rationale:** Assumptions create bugs. Questions create clarity.
|
|
|
|
### 5. Machine-Readable Rules
|
|
**RULE:** All behavioral rules MUST be represented as structured artifacts, not just prose.
|
|
|
|
**Enforcement:**
|
|
```
|
|
rules/
|
|
├── pre-commit.schema.json # Validation rules
|
|
├── quality-gates.yaml # Quality thresholds
|
|
├── agent-contracts.json # Agent responsibilities
|
|
└── invariants.ts # Runtime assertions
|
|
```
|
|
|
|
**Rationale:** Humans read markdown. Machines enforce JSON/YAML.
|
|
|
|
---
|
|
|
|
## Agent Behavioral Contracts
|
|
|
|
### Orchestrator Agent
|
|
**Responsibilities:**
|
|
- Initialize .loki/ directory structure
|
|
- Maintain CONTINUITY.md (working memory)
|
|
- Coordinate task queue (pending → in-progress → completed)
|
|
- Enforce quality gates
|
|
- Manage git checkpoints
|
|
|
|
**Prohibited Actions:**
|
|
- Writing implementation code directly
|
|
- Skipping spec generation
|
|
- Modifying completed tasks without explicit override
|
|
|
|
**Context Obligations:**
|
|
- MUST read CONTINUITY.md before every action
|
|
- MUST update orchestrator.json after phase transitions
|
|
- MUST preserve task lineage in completed.json
|
|
|
|
### Engineering Swarm Agents
|
|
**Responsibilities:**
|
|
- Implement features per OpenAPI spec
|
|
- Write contract tests before implementation
|
|
- Create git commits for completed tasks
|
|
- Ask clarifying questions when spec is ambiguous
|
|
|
|
**Prohibited Actions:**
|
|
- Implementing without spec
|
|
- Skipping tests
|
|
- Ignoring linter/type errors
|
|
|
|
**Context Obligations:**
|
|
- MUST inherit parent agent's context
|
|
- MUST log all decisions to .agent/sub-agents/${agent_id}.md
|
|
- MUST reference spec in all implementation commits
|
|
|
|
### QA Swarm Agents
|
|
**Responsibilities:**
|
|
- Generate test cases from OpenAPI spec
|
|
- Run contract validation tests
|
|
- Report discrepancies between code and spec
|
|
- Create bug reports in dead-letter queue
|
|
|
|
**Prohibited Actions:**
|
|
- Modifying implementation code
|
|
- Skipping failing tests
|
|
- Approving incomplete features
|
|
|
|
**Context Obligations:**
|
|
- MUST validate against spec as source of truth
|
|
- MUST log test results to ledgers/
|
|
- MUST create git commits for test additions
|
|
|
|
### DevOps Swarm Agents
|
|
**Responsibilities:**
|
|
- Automate deployment pipelines
|
|
- Monitor service health
|
|
- Configure infrastructure as code
|
|
- Manage environment secrets
|
|
|
|
**Prohibited Actions:**
|
|
- Storing secrets in plaintext
|
|
- Deploying without health checks
|
|
- Skipping rollback procedures
|
|
|
|
**Context Obligations:**
|
|
- MUST log all deployments to deployment ledger
|
|
- MUST preserve deployment context for rollback
|
|
- MUST track infrastructure state in orchestrator.json
|
|
|
|
---
|
|
|
|
## Quality Gates (Machine-Enforceable)
|
|
|
|
### Pre-Commit Hook (BLOCKING)
|
|
```yaml
|
|
quality_gates:
|
|
linting:
|
|
enabled: true
|
|
auto_fix: true
|
|
block_on_failure: true
|
|
|
|
type_checking:
|
|
enabled: true
|
|
strict_mode: true
|
|
block_on_failure: true
|
|
|
|
contract_tests:
|
|
enabled: true
|
|
min_coverage: 80%
|
|
block_on_failure: true
|
|
|
|
spec_validation:
|
|
enabled: true
|
|
validator: spectral
|
|
block_on_failure: true
|
|
```
|
|
|
|
### Post-Implementation Review (AUTO-FIX)
|
|
```yaml
|
|
auto_review:
|
|
static_analysis:
|
|
tools: [eslint, prettier, tsc]
|
|
auto_fix: true
|
|
|
|
security_scan:
|
|
tools: [semgrep, snyk]
|
|
severity_threshold: medium
|
|
auto_create_issues: true
|
|
|
|
performance_check:
|
|
lighthouse_score: 90
|
|
bundle_size_limit: 500kb
|
|
warn_only: true
|
|
```
|
|
|
|
---
|
|
|
|
## Memory Hierarchy (Priority Order)
|
|
|
|
### 1. CONTINUITY.md (Volatile - Every Turn)
|
|
**Purpose:** What am I doing RIGHT NOW?
|
|
**Update Frequency:** Every turn
|
|
**Content:** Current task, phase, blockers, next steps
|
|
|
|
### 2. CONSTITUTION.md (Immutable - This File)
|
|
**Purpose:** How MUST I behave?
|
|
**Update Frequency:** Version bumps only
|
|
**Content:** Behavioral contracts, quality gates, invariants
|
|
|
|
### 3. CLAUDE.md (Semi-Stable - Significant Changes)
|
|
**Purpose:** What is this project?
|
|
**Update Frequency:** Architecture changes
|
|
**Content:** Tech stack, patterns, project context
|
|
|
|
### 4. Ledgers (Append-Only - Checkpoint)
|
|
**Purpose:** What happened?
|
|
**Update Frequency:** After significant events
|
|
**Content:** Decisions, deployments, reviews
|
|
|
|
### 5. .agent/sub-agents/*.json (Lineage Tracking)
|
|
**Purpose:** Who did what and why?
|
|
**Update Frequency:** Agent lifecycle events
|
|
**Content:** Agent context, decisions, inherited memory
|
|
|
|
---
|
|
|
|
## Context Lineage Schema
|
|
|
|
```json
|
|
{
|
|
"agent_id": "eng-001-backend-api",
|
|
"agent_type": "general-purpose",
|
|
"model": "haiku",
|
|
"spawned_at": "2026-01-04T05:30:00Z",
|
|
"spawned_by": "orchestrator-main",
|
|
"lineage": ["orchestrator-main", "eng-001-backend-api"],
|
|
"inherited_context": {
|
|
"phase": "development",
|
|
"current_task": "task-005",
|
|
"spec_reference": ".loki/specs/openapi.yaml#/paths/~1api~1todos",
|
|
"tech_stack": ["Node.js", "Express", "TypeScript", "SQLite"]
|
|
},
|
|
"decisions_made": [
|
|
{
|
|
"timestamp": "2026-01-04T05:31:15Z",
|
|
"question": "Should we use Prisma or raw SQL?",
|
|
"answer": "Raw SQL with better-sqlite3 for simplicity",
|
|
"rationale": "PRD requires minimal dependencies, synchronous ops preferred"
|
|
}
|
|
],
|
|
"tasks_completed": ["task-005"],
|
|
"commits_created": ["abc123f", "def456a"],
|
|
"status": "completed",
|
|
"completed_at": "2026-01-04T05:45:00Z"
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## Git Checkpoint Protocol
|
|
|
|
### Commit Message Format
|
|
```
|
|
[Loki] ${agent_type}-${task_id}: ${task_title}
|
|
|
|
${detailed_description}
|
|
|
|
Agent: ${agent_id}
|
|
Parent: ${parent_agent_id}
|
|
Spec: ${spec_reference}
|
|
Tests: ${test_files}
|
|
```
|
|
|
|
### Example
|
|
```
|
|
[Loki] eng-005-backend: Implement POST /api/todos endpoint
|
|
|
|
Created todo creation endpoint per OpenAPI spec.
|
|
- Input validation for title field
|
|
- SQLite insertion with timestamps
|
|
- Returns 201 with created todo object
|
|
- Contract tests passing
|
|
|
|
Agent: eng-001-backend-api
|
|
Parent: orchestrator-main
|
|
Spec: .loki/specs/openapi.yaml#/paths/~1api~1todos/post
|
|
Tests: backend/tests/todos.contract.test.ts
|
|
```
|
|
|
|
---
|
|
|
|
## Invariants (Runtime Assertions)
|
|
|
|
```typescript
|
|
// .loki/rules/invariants.ts
|
|
|
|
export const INVARIANTS = {
|
|
// Spec must exist before implementation
|
|
SPEC_BEFORE_CODE: (task: Task) => {
|
|
if (task.type === 'implementation') {
|
|
assert(exists(task.spec_reference), 'SPEC_MISSING');
|
|
}
|
|
},
|
|
|
|
// All tasks must have git commits
|
|
TASK_HAS_COMMIT: (task: Task) => {
|
|
if (task.status === 'completed') {
|
|
assert(task.git_commit_sha, 'COMMIT_MISSING');
|
|
}
|
|
},
|
|
|
|
// Agent lineage must be preserved
|
|
AGENT_HAS_LINEAGE: (agent: Agent) => {
|
|
assert(agent.lineage.length > 0, 'LINEAGE_MISSING');
|
|
assert(agent.spawned_by, 'PARENT_MISSING');
|
|
},
|
|
|
|
// CONTINUITY.md must always exist
|
|
CONTINUITY_EXISTS: () => {
|
|
assert(exists('.loki/CONTINUITY.md'), 'CONTINUITY_MISSING');
|
|
},
|
|
|
|
// Quality gates must pass before merge
|
|
QUALITY_GATES_PASSED: (task: Task) => {
|
|
if (task.status === 'completed') {
|
|
assert(task.quality_checks.all_passed, 'QUALITY_GATE_FAILED');
|
|
}
|
|
}
|
|
};
|
|
```
|
|
|
|
---
|
|
|
|
## Visual Specification Aids
|
|
|
|
### Mermaid Diagram Generation (Required for Complex Features)
|
|
|
|
**RULE:** Architecture decisions and complex workflows MUST include Mermaid diagrams.
|
|
|
|
**Example - Authentication Flow:**
|
|
```mermaid
|
|
sequenceDiagram
|
|
participant C as Client
|
|
participant A as API
|
|
participant D as Database
|
|
|
|
C->>A: POST /api/auth/login
|
|
A->>A: Validate credentials
|
|
A->>D: Query user
|
|
D-->>A: User record
|
|
A->>A: Generate JWT token
|
|
A-->>C: 200 OK {token}
|
|
```
|
|
|
|
**Storage Location:** `.loki/diagrams/${feature_name}.mmd`
|
|
|
|
**When Required:**
|
|
- Multi-step workflows (3+ steps)
|
|
- System architecture changes
|
|
- Complex state machines
|
|
- Integration points between services
|
|
|
|
---
|
|
|
|
## Amendment Process
|
|
|
|
This constitution can only be amended through:
|
|
1. Version bump in header
|
|
2. Git commit with `[CONSTITUTION]` prefix
|
|
3. Changelog entry documenting what changed and why
|
|
4. Re-validation of all existing agents against new rules
|
|
|
|
**Example Amendment Commit:**
|
|
```
|
|
[CONSTITUTION] v1.1.0: Add visual specification requirement
|
|
|
|
Added requirement for Mermaid diagrams on complex features to prevent
|
|
ambiguity in multi-step workflows. Based on Addy Osmani's insight that
|
|
visual aids significantly improve AI-to-AI communication.
|
|
|
|
Breaking changes: None
|
|
New rules: Section "Visual Specification Aids"
|
|
```
|
|
|
|
---
|
|
|
|
## Enforcement
|
|
|
|
All rules in this constitution are **machine-enforceable** and **MUST** be implemented as:
|
|
1. Pre-commit hooks (Git)
|
|
2. Runtime assertions (TypeScript invariants)
|
|
3. Quality gate validators (YAML configs)
|
|
4. Agent behavior validators (JSON schemas)
|
|
|
|
**Human guidance is advisory. Machine enforcement is mandatory.**
|
|
|
|
---
|
|
|
|
*"In autonomous systems, trust is built on invariants, not intentions."*
|