applyBlockGuard function

Set<String> applyBlockGuard({
  1. required Set<String> candidates,
  2. required Map<String, Bead> beadsById,
  3. required Iterable<BlockEdge> edges,
  4. void onUnresolved(
    1. String message
    )?,
  5. void onBlocked(
    1. String beadId,
    2. BlockEdge edge,
    3. Bead? target
    )?,
})

Re-applies edges over candidates, fail-closed — the ONE implementation of what a cross-store block means.

A candidate is excluded when any edge from it points at a target that is OPEN in beadsById, or that is not present in beadsById at all — the latter LOUDLY through onUnresolved, because an unresolvable blocker must never silently pass as satisfied (a false negative an operator can see beats a false positive that spawns unprerequisited work).

Callers own the FILTER (which edges apply at all); this function owns the ENFORCEMENT. Returns candidates itself when there is nothing to apply. onBlocked observes the exact first edge that excludes each candidate; it does not implement another blocking decision.

Implementation

Set<String> applyBlockGuard({
  required Set<String> candidates,
  required Map<String, Bead> beadsById,
  required Iterable<BlockEdge> edges,
  void Function(String message)? onUnresolved,
  void Function(String beadId, BlockEdge edge, Bead? target)? onBlocked,
}) {
  if (candidates.isEmpty) return candidates;
  final byFrom = <String, List<BlockEdge>>{};
  for (final edge in edges) {
    (byFrom[edge.from] ??= <BlockEdge>[]).add(edge);
  }
  if (byFrom.isEmpty) return candidates;

  final result = <String>{};
  for (final id in candidates) {
    var blocked = false;
    for (final edge in byFrom[id] ?? const <BlockEdge>[]) {
      final target = beadsById[edge.to];
      if (target == null) {
        onUnresolved?.call(
          'grid: $id is blocked by ${edge.origin} on "${edge.to}", which is '
          'not observed by any federated store — excluding $id from ready '
          '(fail-closed).',
        );
        onBlocked?.call(id, edge, null);
        blocked = true;
        break;
      }
      if (!target.isClosed) {
        onBlocked?.call(id, edge, target);
        blocked = true;
        break;
      }
    }
    if (!blocked) result.add(id);
  }
  return result;
}