Invariants & State Consistency
The Logic Analyzer: Invariants & State Consistency agent is a specialized logic analyzer focused on invariants and state consistency. It finds bugs where code violates expected pre-conditions, post-conditions, or maintains inconsistent state.
When to Use
Use this agent when:
- You need to verify pre-conditions are checked before function calls
- You want to verify post-conditions are established after function execution
- You're analyzing state machine code for invalid transitions
- You need to identify loop invariant violations
- You're looking for data invariant relationships that should always hold
How It Works
- Identifies invariants - Reads code to understand implicit assumptions about state
- Maps state changes - Traces how state is modified through functions and loops
- Checks consistency - Verifies invariants are maintained after every state change
- Reports violations - Generates structured findings showing how invariants are violated
Focus Areas
- Pre-condition violations: Function called with invalid state
- Post-condition violations: Function doesn't establish expected state
- State machine violations: Invalid state transitions
- Loop invariants: Conditions that should hold on each iteration
- Data invariants: Relationships between data that should always hold
Tools Available
This agent has access to: Read, Glob, Grep
Example Analysis
Given this code:
class ShoppingCart {
constructor() {
this.items = [];
this.totalPrice = 0;
}
addItem(item) {
this.items.push(item);
this.totalPrice += item.price;
}
removeItem(itemId) {
const index = this.items.findIndex(i => i.id === itemId);
if (index !== -1) {
this.items.splice(index, 1);
}
// BUG: totalPrice not updated!
}
}The Invariant analyzer would identify:
Finding: Price invariant violated in removeItem
Location: cart.js:15-20
Severity: P1 (inconsistent state)
Confidence: HIGH
Invariant Violated: totalPrice === sum(items.map(i => i.price))
Violation Scenario:
- Cart has item
{id: 1, price: 100}, totalPrice = 100 - Call removeItem(1)
- items =
[], but totalPrice = 100 (should be 0) - Further operations use incorrect total
Suggested Fix:
removeItem(itemId) {
const index = this.items.findIndex(i => i.id === itemId);
if (index !== -1) {
const removedItem = this.items[index];
this.items.splice(index, 1);
this.totalPrice -= removedItem.price;
}
}Best Practices
- Explicitly state invariants in comments
- Group related state changes together
- Use transactions or try/catch for multi-step updates
- Validate pre-conditions at function entry
- Use assertions to document invariants
- Keep state updates atomic
Output Format
For each potential issue, the agent provides:
- Location: Exact file path and line number
- Severity: P0 (data corruption), P1 (inconsistent state), P2 (subtle violation)
- Confidence: HIGH, MEDIUM, or LOW
- Code: Relevant code snippet
- Invariant Violated: The condition that should always hold
- Violation Scenario: Step-by-step how violation occurs
- Suggested Fix: Corrected code with invariant preserved
Example Usage
Task(
description: "Analyze state consistency in order processing",
prompt: "Review src/models/Order.ts for invariant violations. Check that order totals remain consistent, status transitions are valid, and line items match totals.",
subagent_type: "agileflow-logic-analyzer-invariant"
)Related Agents
logic-analyzer-edge- Boundary condition analysislogic-analyzer-flow- Control flow analysislogic-analyzer-race- Concurrency issue detectionlogic-analyzer-type- Type safety analysislogic-consensus- Logic audit consensus coordinator