KeyForge Engineering Manifesto (v6.1)
Goal: Engineering Truth, Deterministic State, & Operational Efficiency. Enemy: Context Saturation, Implicit Logic, "Happy Path" Bias. Doctrine: LLM-Integrated Development (LID).
1. Core Philosophy: Constraint-Based Engineering
We move from Asking (hoping the LLM is smart) to Constraining (making it impossible for the LLM to be wrong).
Criticality Tiering
- Tier 1 (The Nucleus):
keyforge-physics,keyforge-evolution. - Risk: Mathematical hallucination.
- Requirement: 95%+ Branch Coverage + Property Testing.
- Requirement: Bit-perfect parity between Exact and Oracle implementations.
-
Requirement: 100% Checked Arithmetic in scoring kernels (No silent overflows).
-
Tier 2 (The Contract):
keyforge-protocol,keyforge-model. - Risk: Data corruption.
-
Requirement: 100% Validation Coverage.
-
Tier 3 (The Shell):
keyforge-infra,keyforge-hive. - Risk: IO/State failure.
- Requirement: Error Path Verification.
2. Architectural Doctrine (Cognitive Ergonomics)
A. The Semantic Firewall (Type-Driven Design)
- The Typestate Pattern (Compiler State Machine)
- Problem: Temporal Coupling (LLM accesses data before it is ready).
- Fix: Encode state in types (
PendingJob->RunningJob->CompletedJob). -
Rationale: The compiler prevents accessing
scoreon aPendingJob. -
The Parameter Object (Context Structs)
- Problem: Argument Swapping (LLM confuses
usizearguments). - Fix: Group cohesive arguments into a Context Struct (
ScoringContext { layout, corpus }). -
Rationale: Fields are named; positional arguments are eliminated.
-
The Centralized Error Registry
- Problem: Lazy error handling (
anyhow!,unwrap()). - Fix: A strict, enumerated catalog (
ForgeError) inkeyforge-protocol. - Rationale: Forces the LLM to categorize failure modes (Physics vs Infra).
B. Structural Patterns (Isolation)
- The Command Pattern (Reified Actions)
- Problem: Logic coupled to HTTP Handlers is hard to test.
- Fix: Decouple Intent (
HiveCommand) from Execution (handle_command). -
Rationale: Enables testing business logic without spinning up a server.
-
The Humble Object
-
Rule: Extract logic from IO boundaries into pure structs.
-
Vertical Slices
-
Rule: Group code by Feature (
src/features/register_job), not Layer. -
Railway Oriented Programming (ROP)
- Rule: Linear
Resultchains. No nestedif/else.
3. Verification Strategy (The Guardrails)
A. The Oracle Pattern (Multi-Tiered)
- Exact Engine: Must match
DeterministicScorerbit-for-bit. - Search Engine: May drift within documented bounds.
- Protocol: Tests must select the appropriate comparator based on the engine under test.
B. Coverage-Driven Verification (CDV)
- Goal: Identify "Logic Dark Matter" (unexecuted branches).
- Rule: High coverage is a map of "Unknown Territory".
C. Advanced Testing Protocols
- Mutation Testing (
cargo-mutants): Zero "Survived Mutants" allowed in Physics. - Metamorphic Testing: Verify properties (
dist(a,b) == dist(b,a)). - Golden Data: Use hardcoded input/output pairs (
tests/fixtures/) as the Anchor of Truth. - Formal Verification Lite (
kani): Prove invariants for core math. - Snapshot Testing (
insta): Lock regression for complex DTOs/Parsers.
D. The Ghost Code Strategy
- Rule: For complex algorithms, implement a simplified "Ghost Model" first. Prove it, then optimize.
4. Operational Protocols (The Workflow)
A. The SAGA Loop
- Isolation: Debug in
repro.rs. - Tests First: Test -> Fail -> Implement -> Pass.
- Signature Freeze: No changing
pubsignatures during debugging. - 3-Turn Stop: If fix fails twice: STOP, REVERT, ISOLATE.
B. Context Management
- The Cleanroom: Never debug inside the full app context.
- Context Compression: Use "Header Files" (Signatures only) for dependencies.
- The Soft Reset: Clear context window if reasoning degrades.
C. The Reflexion Loop
- Protocol: When a test fails:
- Input: Failing Test + Code.
- Task: "Explain the logic error in natural language."
- Action: Generate fix based on explanation.
D. Chain-of-Verification (CoVe)
- Protocol: Draft -> Verify (Manual Calc) -> Finalize.
5. Operational Reality (Day-2 Operations)
A. Database Safety
- Rule: Zero-Touch Migrations.
- Protocol: Never write SQL migrations directly. Output schema, verify diff. Migrations must be Additive Only.
B. Human Protocols
- The Comprehension Test: Human must explain logic without reading LLM summary.
- The Two-Man Audit: Coder != Auditor. Use fresh context for security audit.
- No Magic: Reject macros/complex generics unless explicitly requested.
C. Tiered Verification (CI Economics)
- Tier 1: The Fast Loop (Local):
cargo check,cargo test(Unit). Time: < 30s. - Tier 2: The Logic Loop (PR):
cargo test --doc,cargo mutants(Diff). Time: < 10m. - Tier 3: The Deep Freeze (Nightly):
kani, Fullcargo mutants, E2E. Time: Hours.