magicGateResultsAllEnricher function

String? magicGateResultsAllEnricher(
  1. Element element,
  2. RefRegistry refs
)

Enricher: emits magicGateResultsAll: <ability>=<allowed|denied>,... for up to 5 of the most recently checked Gate abilities.

Reads Gate.manager.abilities to obtain the set of defined abilities, then calls GateManager.lastResult per ability — a read-only cache lookup that never calls the ability callback. Abilities with no cached result (never checked) are skipped. The emitted list is ordered by GateResult.checkedAt descending so the most recent decisions appear first, truncated to 5 entries.

Returns null when no ability has a cached result (no gate checks have run since the last Gate.manager.flush).

Contract: NO new Gate.allows calls are made — this enricher is read-only with zero side-effects.

Implementation

String? magicGateResultsAllEnricher(Element element, RefRegistry refs) {
  // 1. Collect all abilities that have a cached result.
  final List<GateResult> results = Gate.manager.abilities
      .map((ability) => Gate.manager.lastResult(ability))
      .whereType<GateResult>()
      .toList(growable: false);

  if (results.isEmpty) return null;

  // 2. Sort by checkedAt descending (most recent first) and take up to 5.
  results.sort((a, b) => b.checkedAt.compareTo(a.checkedAt));
  final List<GateResult> recent = results.length > 5
      ? results.sublist(0, 5)
      : results;

  // 3. Format as `ability=allowed|denied` entries joined by comma.
  final String payload = recent
      .map((r) => '${r.ability}=${r.allowed ? "allowed" : "denied"}')
      .join(',');
  return 'magicGateResultsAll: $payload';
}