# hatchet-dev/hatchet — Kavach Visual Invariant Atlas

> Distributed, fault-tolerant task queue and DAG workflow scheduler. Zero-memrepo capture over pgproto3 and v1repo.

- **Repository:** https://github.com/hatchet-dev/hatchet
- **Commit:** `26386fb` (branch `main`)
- **Proof Tier:** `gold_showcase`
- **Stateful Backend Supported:** `true`
- **Scan:** 2440 ms · 1229 files · $0.00 LLM tokens

## Detected Stateful Drivers & Gateways

| Driver | Language | Category | Kavach Adapter |
| :--- | :--- | :--- | :--- |
| `github.com/jackc/pgx/v5` | go | database | `kavachpgx` |

## Extracted System Invariants (6)

| Invariant | Proof State | Category | Rule Kind | Evidence |
| :--- | :--- | :--- | :--- | :--- |
| `worker_label_isolation` | `verified` | state | `state_machine` | `.kavach/invariants.yaml:8-12` |
| `batch_slot_and_dispatch_integrity` | `verified` | schema | `non_negative` | `.kavach/invariants.yaml:14-26` |
| `dag_and_condition_integrity` | `verified` | state | `state_machine` | `.kavach/invariants.yaml:28-33` |
| `batch_followup_slot_capacity` | `verified` | schema | `non_negative` | — |
| `no_silent_batch_drop` | `verified` | transport | `anti_lobotomy` | — |
| `no_silent_dispatch_drop` | `verified` | transport | `anti_lobotomy` | — |

### `worker_label_isolation`
- **Summary:** Assigned worker must satisfy every Required: true desired label regardless of Sticky strategy.
- **Proof State:** `verified`
- **Code:** `.kavach/invariants.yaml:8-12`

### `batch_slot_and_dispatch_integrity`
- **Summary:** Committed multi-queue batches must emit QueueResults without leaking worker slots in Scheduler.unackedSlots.
- **Proof State:** `verified`
- **Code:** `.kavach/invariants.yaml:14-26`

### `dag_and_condition_integrity`
- **Summary:** Multi-group AND skip conditions must not skip a step until both ParentOverride and UserEvent groups match.
- **Proof State:** `verified`
- **Code:** `.kavach/invariants.yaml:28-33`

### `batch_followup_slot_capacity`
- **Summary:** Declared invariant batch_followup_slot_capacity
- **Proof State:** `verified`

### `no_silent_batch_drop`
- **Summary:** Declared invariant no_silent_batch_drop
- **Proof State:** `verified`

### `no_silent_dispatch_drop`
- **Summary:** Declared invariant no_silent_dispatch_drop
- **Proof State:** `verified`

## Guided Causal Flows

### H1: Sticky strategy bypasses required eu-pci compliance label
A PII tokenization workflow step with Sticky=SOFT and Required compliance_zone=eu-pci is routed onto us-nonpci-worker instead of waiting for an eu-pci worker.

1. **`in_schedule_step`**: Step 3 submits kyc:tokenize-card-pan with Sticky=SOFT, desired_worker=us-nonpci-worker, and Required compliance_zone=eu-pci (read via real sqlcv1 GetDesiredLabels).
2. **`fn_rank_workers`**: rankWorkerIds (scheduler.go:1486-1511) switches on Sticky=SOFT and skips worker.computeWeight(labels).
3. **`bug_h1`**: us-nonpci-worker is kept as rank=1 instead of returning -1 and being filtered out.
4. **`inv_label_isolation`**: worker_label_isolation fails on unpatched (compliance_margin=-1); fixed waits cleanly until eu-pci capacity exists.

### H2: Multi-queue batch DB fault leaks slots and strands committed batch
Healthy 3-window multi-queue baseline recorded via real ReserveAndCommitBatchRun; kavach diff auto-injects Postgres SQLSTATE 40001 / 40P01 / timeout on Queue 2 after Queue 1 commits.

1. **`in_batch_dispatch`**: Flushes multi-queue batches spanning alpha-q and beta-q across payout-window-01..03 on a 3-slot worker, then probes remaining capacity via followup_probe.
2. **`fn_batch_flush`**: Queue 1 commits via real ReserveTaskBatchRun/UpdateTasksToAssigned; injected Postgres fault hits Queue 2.
3. **`bug_h2`**: Early return skips b.scheduler.ack(allAckIds) and b.emitResults(...), dropping Queue 1 dispatch and leaking its slot.
4. **`inv_batch_slot`**: Replenish subtracts leaked unackedSlots (1 - 1 = 0), starving urgent followup task and violating batch_slot_and_dispatch_integrity.

### H3: Multi-group DAG AND-skip ignores event group & impossible-AND
Step aml_sanctions_review specifies AND(Parent auto_waive==true, UserEvent compliance:officer_waiver); DAGOperator skips prematurely on both parent-only match (Step 3) and impossible-AND event fire (Step 4).

1. **`in_dag_run`**: Loads workflow steps and match conditions via real sqlcv1 ListStepsByWorkflowVersionIds / ListStepMatchConditions and evaluates CEL via Matches().EvalBoolExpr.
2. **`fn_dag_eval`**: evaluateParentConditions (dag.go:1018-1065) ignores USEREVENT groups and does not block impossible-AND skip watches.
3. **`bug_h3`**: aml_sanctions_review is skipped without both AND groups satisfied.
4. **`inv_dag_and`**: dag_and_condition_integrity catches premature AML skip on unpatched (and on partial AI patches); fix.patch runs aml_sanctions_review cleanly.

## Replay-Verified Proofs (158/158 variants, 66/66 regressions)

- **HT1: Sticky Mode Bypasses Required Worker PCI Labels** (`pkg/scheduling/v1/scheduler.go:1473-1541`) — Sticky SOFT/HARD scheduling routes sensitive compliance_zone workloads to non-compliant workers. [62/62 variants · verdict `fixed`]
- **HT2: BatchScheduler Partial Commit Leaks unackedSlots** (`pkg/scheduling/v1/batch_scheduler.go:775-1057`) — Multi-queue Postgres SQLSTATE 40001 fault permanently leaks worker slots and strands committed runs. [53/53 variants · verdict `fixed`]
- **HT3: DAG Operator Bypasses Multi-Group AND Event Gates** (`pkg/operator/dagoperator/dag.go:485-556`) — evaluateParentConditions skips mandatory compliance USEREVENT gates on SKIP/CANCEL. [47/47 variants · verdict `fixed`]

## Starter `.kavach/invariants.yaml`

```yaml
# Declarative Kavach invariants for Hatchet v0.120.23 — checked on every replay
# and mutated variant across H1 (sticky label isolation), H2 (multi-queue batch
# slot-ack & dispatch integrity), and H3 (DAG multi-group AND skip integrity).
# Kinds: at_most_once_emit, anti_lobotomy, non_negative, conservation
service: hatchet
invariants:
  # H1: Assigned worker must satisfy every Required: true desired worker label
  # regardless of Sticky strategy (NONE, SOFT, HARD).
  - name: worker_label_isolation
    kind: non_negative
    sink: scheduler:assign
    field: $.compliance_margin

  # H2a: Every batch group committed in Postgres (ReserveAndCommitBatchRun)
  # must be emitted on QueueResults (emitted_tasks - db_batch_commits >= 0).
  - name: batch_slot_and_dispatch_integrity
    kind: non_negative
    sink: scheduler:batch
    field: $.dispatch_surplus

  # H2b: Partial multi-queue batch error must ack committed slots so remaining
  # free worker capacity is not permanently leaked in Scheduler.unackedSlots.
  - name: batch_followup_slot_capacity
    kind: non_negative
    sink: scheduler:followup
    field: $.followup_slot_margin

  # H3: Multi-group AND skip conditions must not skip a step unless every
  # skip OrGroup (both ParentOverride and UserEvent) is satisfied.
  - name: dag_and_condition_integrity
    kind: non_negative
    sink: dag:run
    field: $.and_gate_margin

  # Anti-lobotomy (healthy baseline fault-injection): require full batch & followup output parity.
  - name: no_silent_batch_drop
    kind: anti_lobotomy
    sinks: [scheduler:batch, scheduler:followup]
    min_ratio_vs_baseline: 1.0

  # Anti-lobotomy (multi-step incident capsules with prefix-drop variants): forbid 0-output suppression.
  - name: no_silent_dispatch_drop
    kind: anti_lobotomy
    sinks: [scheduler:assign, dag:run]
    min_ratio_vs_baseline: 0.2
```

## Driver Wrap Snippet

```text
import "github.com/kavachlabs/kavach-go/kavachpgx"

pool, err := kavachpgx.NewPGXPoolWithConfig(ctx, cfg)
repo := v1repo.NewRepository(pool)
```

## Run Locally (Zero Upload)

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