The challenge:
Designing a deterministic, version-controlled rule engine to handle complex multi-layer scoring logic.
The Constraints:
- Pure JSON storage (Database and Agnostic).
- Strict integrity validation at load time.
- Must prevent circular logic in dependencies.
- JSON setup stays human readable
The Solution:
I opted for a Normalized (Flat) Architecture to avoid the complexity of deeply nested configurations. By treating the JSON like a relational database, we decouple the structure from the logic.
{
version: string,
// ℹ️ Subsystems act as grouping tags
subsystems: { id: string, name: string, ... }[],
// ℹ️ Rules refer to subsystems via ID (Relational style)
rules: {
id: string,
subsystemId: string,
operation: { action: 'add' | 'multiply', ... },
dependency: string // ID of the variable/input it reads from
}[],
}
With this normalized structure, it is possible to construct a Directed Acyclic Graph (DAG) and apply a Topological Sort. This allows the engine to automatically determine the correct execution order of isolated subsystems based on their data dependencies.
Example: If Subsystem A depends on Subsystem B, and B depends on Subsystem C, the Topological Sort resolves the execution pipeline to: C → B → A.
Key Architectural Benefits:
- Cycle Detection: We can detect circular dependencies (A -> B -> A) at load time and throw a critical error, ensuring the database always contains a valid configuration.
- Determinism: Since every subsystem is a Pure Function, and the JSON is versioned, we can re-compute historical inputs with their specific rule-set version to get the exact same result every time.
The Blueprint:
