# getlago/lago — Kavach Visual Invariant Atlas

> Open-source metering and usage-based billing engine. Zero-memrepo v0.2 capture across Kafka, Redis, and PostgreSQL.

- **Repository:** https://github.com/getlago/lago
- **Commit:** `1a30b73` (branch `main`)
- **Proof Tier:** `gold_showcase`
- **Stateful Backend Supported:** `true`
- **Selected Service:** `events-processor`
- **Scan:** 49 ms · 57 files · $0.00 LLM tokens

## Detected Stateful Drivers & Gateways

| Driver | Language | Category | Kavach Adapter |
| :--- | :--- | :--- | :--- |
| `github.com/twmb/franz-go/pkg/kgo` | go | queue | `kavachkgo` |
| `github.com/redis/go-redis/v9` | go | cache | `kavachredis` |
| `github.com/jackc/pgx/v5` | go | database | `kavachpgx` |

## Extracted System Invariants (3)

| Invariant | Proof State | Category | Rule Kind | Evidence |
| :--- | :--- | :--- | :--- | :--- |
| `atomic_single_emit_on_retry` | `verified` | transport | `at_most_once_emit` | `.kavach/invariants.yaml` |
| `atomic_single_enriched_on_retry` | `verified` | transport | `at_most_once_emit` | `.kavach/invariants.yaml` |
| `no_silent_billing_drop` | `verified` | transport | `anti_lobotomy` | `.kavach/invariants.yaml` |

### `atomic_single_emit_on_retry`
- **Summary:** Declarative at_most_once_emit rule on kafka:events_charged_in_advance (key $.transaction_id)
- **Proof State:** `verified`
- **Code:** `.kavach/invariants.yaml`

### `atomic_single_enriched_on_retry`
- **Summary:** Declarative at_most_once_emit rule on kafka:events_enriched (key $.transaction_id)
- **Proof State:** `verified`
- **Code:** `.kavach/invariants.yaml`

### `no_silent_billing_drop`
- **Summary:** Declarative anti_lobotomy rule on kafka:events_enriched, kafka:events_charged_in_advance
- **Proof State:** `verified`
- **Code:** `.kavach/invariants.yaml`

## Guided Causal Flows

### Primary service flow
Auto-synthesized end-to-end path from entrypoint through gateways and sinks to invariants.

1. **`in_primary`**: Inbound event or request enters the wrapped entrypoint boundary.
2. **`fn_processrecord`**: Business logic executes unchanged inside the step context.
3. **`gw_grpc`**: Gateway reads are recorded once and replayed deterministically offline (with fault injection on retry).
4. **`sink_kafka_events_charged_in_advance`**: Durable outputs are captured in memory and compared against baseline + invariants.
5. **`inv_atomic_single_emit_on_retry`**: Declarative invariants from .kavach/invariants.yaml are verified after every step and mutated variant.

## Replay-Verified Proofs (64/64 variants, 10/10 regressions)

- **L1: Non-Atomic Kafka Emit Before Retryable Redis Check** (`events-processor/processors/events_processor/processor.go:53-78`) — Transient Redis error during Kafka retry bills customer usage twice (total_in_advance = 2). [64/64 variants · verdict `fixed`]
- **L2: BadgerDB Cache vs SQL date_trunc Boundary Mismatch** (`events-processor/cache/subscriptions.go:60-65`) — Raw microsecond comparisons silently drop active grace-period subscriptions. [64/64 variants · verdict `fixed`]
- **L3: IEEE-754 Unix Float Timestamp 1ms Subtraction Drift** (`events-processor/utils/time.go:42-48`) — Loses 1ms on 496/1,000 sub-second timestamps, dropping boundary billing events. [64/64 variants · verdict `fixed`]

## Starter `.kavach/invariants.yaml`

```yaml
# Declarative Kavach invariants — checked on every replay and mutated variant.
# Kinds: at_most_once_emit, anti_lobotomy, non_negative, conservation
service: events-processor
invariants:
  - name: atomic_single_emit_on_retry
    kind: at_most_once_emit
    sink: kafka:events_charged_in_advance
    key: $.transaction_id
    on_error_emit_count: 0

  - name: atomic_single_enriched_on_retry
    kind: at_most_once_emit
    sink: kafka:events_enriched
    key: $.transaction_id
    on_error_emit_count: 0

  - name: no_silent_billing_drop
    kind: anti_lobotomy
    sinks: [kafka:events_enriched, kafka:events_charged_in_advance]
    min_ratio_vs_baseline: 1.0
```

## Driver Wrap Snippet

```text
import (
  "github.com/kavachlabs/kavach-go/kavachkgo"
  "github.com/kavachlabs/kavach-go/kavachredis"
  "github.com/kavachlabs/kavach-go/kavachpgx"
)

client := kavachkgo.WrapClient(kgoClient)
rdb := kavachredis.WrapClient(redisClient)
pool := kavachpgx.WrapPool(pgxPool)
```

## Run Locally (Zero Upload)

```bash
brew install kavachlabs/tap/kavach && kavach map --auto --open
```
