builtin/apply: introduce 'struct apply_state' to start libifying