/** Preserve call order across barriers; only adjacent safe calls may overlap. */ export function executionSegments( calls: readonly T[], isSafe: (call: T) => boolean, ): T[][] { const segments: T[][] = []; let pending: T[] = []; for (const call of calls) { if (isSafe(call)) pending.push(call); else { if (pending.length) segments.push(pending); pending = []; segments.push([call]); } } if (pending.length) segments.push(pending); return segments; }