Metakit / Prune SSR payloads and gate queries

Compute static affectedBy maps and dead bindings

Graph.analyze(machine, bindings) compares every transition's write paths against every binding's read paths (also when one path is a parent or child of the other). It returns:

  • affectedBy[tag]: the bindings that must be read again when the transition named tag runs.
  • deadBindings: bindings that depend on no path written by any transition, so the page never needs to watch them for changes.
  • liveModelPaths: the paths that some transition writes or some binding reads, as a minimal set.
ts
import { Option, Schema } from 'effect'
import { Eq, Expr, Num, Path, Transition } from '@doeixd/metakit'
import * as Graph from '@doeixd/metakit/graph'
import * as Machine from '@doeixd/metakit/machine'

const Account = Schema.Struct({
  id: Schema.String,
  balance: Schema.Int,
  reserved: Schema.Int,
  status: Schema.Literals(['open', 'frozen']),
  promoEnabled: Schema.Boolean,
})

const AccountMachine = Machine.make({
  state: Account,
  transitions: {
    Withdraw: Transition.make({
      state: Account,
      input: Schema.Struct({ amount: Schema.Int }),
      apply: ({ state, input }) => [
        Transition.set(state.balance, Num.sub(state.balance, input.amount)),
      ],
    }),
    Freeze: Transition.make({
      state: Account,
      input: Schema.Struct({}),
      apply: ({ state }) => [Transition.set(state.status, 'frozen')],
    }),
  },
})

const $ = Path.from(Account)
const bindings = {
  available: Num.sub($.balance, $.reserved),
  statusLabel: Expr.match($.status, { open: 'Active', frozen: 'Frozen' }),
  accountId: $.id,
  promoBonus: Expr.when($.promoEnabled, { then: Num.sub($.balance, $.reserved), else: 0 }),
}

const analysis = Graph.analyze(AccountMachine, bindings)
// analysis.affectedBy.Withdraw -> ['available', 'promoBonus']
// analysis.affectedBy.Freeze   -> ['statusLabel']
// analysis.deadBindings        -> ['accountId']

What happened: Withdraw writes balance, which available and promoBonus read, so only those two bindings need reading again; Freeze writes status, which only statusLabel reads; nothing writes id, so accountId is dead. The analysis follows the paths an expression names, not which branch runs, so promoBonus counts as reading balance even while promoEnabled is false.

If a binding contains an Expr.opaque node without explicit reads, Graph.analyze conservatively marks it affected by every transition that writes at least one path.