Metakit / @doeixd/metakit

Before and after

With a plain Effect Schema filter, only runtime decode can execute a => a.reserved <= a.balance:

ts
// Opaque closure: SQL cannot read it, UI cannot partially evaluate it, Z3 cannot check it.
const Account = Schema.Struct({
  id: Schema.String,
  balance: Schema.Int,
  reserved: Schema.Int,
  status: Schema.Literals(['open', 'frozen']),
}).pipe(
  Schema.check(
    Schema.makeFilter(a => a.balance >= 0 && a.reserved >= 0 && a.reserved <= a.balance),
  ),
)

With @doeixd/metakit, Account is still the exact same Effect Schema, but its invariant and Withdraw transition are inspectable data:

ts
import { Effect, Result, Schema } from 'effect'
import { Eq, Expr, Num, Transition, Verdict } from '@doeixd/metakit'
import * as Prove from '@doeixd/metakit/prove'
import * as Sql from '@doeixd/metakit/sql'

const Account = Schema.Struct({
  id: Schema.String,
  balance: Schema.Int,
  reserved: Schema.Int,
  status: Schema.Literals(['open', 'frozen']),
}).pipe(
  Expr.refine($ =>
    Expr.all(Num.gte($.balance, 0), Num.gte($.reserved, 0), Num.lte($.reserved, $.balance)),
  ),
)

const Withdraw = Transition.make({
  state: Account,
  input: Schema.Struct({ amount: Schema.Int.pipe(Expr.refine($ => Num.gt($, 0))) }),
  when: ({ state, input }) =>
    Expr.all(
      Eq.is(state.status, 'open'),
      Num.lte(input.amount, Num.sub(state.balance, state.reserved)),
    ),
  apply: ({ state, input }) => [
    Transition.set(state.balance, Num.sub(state.balance, input.amount)),
  ],
})

// 1. Standard Effect Schema decode still works unchanged:
const invalid = Schema.decodeUnknownResult(Account)({
  id: 'a1',
  balance: 10,
  reserved: 20,
  status: 'open',
})
// Result.isFailure(invalid) -> true

// 2. Partial evaluation disables a button or hides an agent tool before input arrives:
const frozen = { id: 'a1', balance: 50_000, reserved: 10_000, status: 'frozen' as const }
const canWithdraw = Transition.can(Withdraw, { state: frozen })
// Verdict.isFalse(canWithdraw) -> true

// 3. Postgres CHECK constraint generated from the exact same invariant:
const check = Result.getOrThrow(Sql.toCheck(Account, { dialect: 'postgres' }))
// check -> CHECK ((((("balance" >= 0) AND ("reserved" >= 0)) AND ("reserved" <= "balance"))) IS TRUE)

// 4. Synchronous Zone proof (no Z3 boot needed) that Withdraw preserves all invariants:
const proof = await Effect.runPromise(Prove.transition(Account, Withdraw))
// Verdict.isTrue(proof) -> true  (Proved('zone'))
void invalid
void Verdict.isFalse(canWithdraw)
void check
void proof

If someone weakens Withdraw's guard by removing Num.lte(input.amount, Num.sub(state.balance, state.reserved)), Prove.machine fails CI and prints the concrete counterexample:

REFUTED Withdraw [PIN BROKEN]
  before:   {"id":"model-string:0","balance":0,"reserved":0,"status":"open"}
  input:    {"amount":1}
  after:    {"id":"model-string:0","balance":-1,"reserved":0,"status":"open"}
  violated: (((balance >= 0) && (reserved >= 0)) && (reserved <= balance))
  repro:    Transition.run(Withdraw, {"id":"model-string:0","balance":0,"reserved":0,"status":"open"}, {"amount":1})