These are the non-obvious rules the code depends on. Each came from a real bug. If you are extending the persistence engines, the streams or anything Ripser-flavored, read the page in full first; the last section says how to test a change to them.
1. given/implicit resolution happens once, at construction, not per call
A given [CellT: Ordering, CoefficientT: Field] => (Chain[CellT, CoefficientT] is RingModule) instance resolves its Ordering[CellT] context-bound parameter exactly once, when the instance is first summoned — every method built on it permanently closes over that one resolution. Scala's implicit search is static (compile-time, lexical), not dynamic; calling those methods later, from a scope where a different Ordering[CellT] given is available, does not re-resolve anything.
Concretely: Simplex[VertexT] is OrderedCell's own .ordering is a fixed lexicographic order, and the default Ordering[Simplex[VertexT]] in Simplex's companion (or, in generic code, the opt-in import OrderedCell.cellOrdering derivation) means any chainRM = summon[Chain[Simplex[VertexT], CoefficientT] is RingModule] written before a stream-specific given Ordering[Simplex[VertexT]] = stream.filtrationOrdering is in scope silently captures that lexicographic fallback instead — every chain built through ⊠/.scalarMultiply afterward pivots on vertex label order, not filtration order, regardless of what's nominally in scope at the call site.
The fix pattern, everywhere it's applied: declare given Ordering[CellT] = stream.filtrationOrdering as the first statement inside whatever state object holds the stream, before summoning chainRM.
This is not a style nit: it compiles either way and gives a wrong answer only on inputs where lexicographic and filtration order disagree. Most hand-built fixtures do not exercise that unless built to (a fixture with vertex 1 born early and vertex 9 born late is the minimal discriminator). Not every class-scope chainRM instance is a live bug: whether it matters depends on whether anything downstream trusts that instance's output directly for pivot identity, or whether every consumer re-resolves ordering itself (e.g. via Chain.reduceByUntil, a def that resolves fresh at each call site). This needs a case-by-case, empirically-verified audit every time, not a blanket rule.
2. A stream's iteration order and its pivot order must be the same total order
Boundary-matrix reduction requires columns (processing order) and rows (pivot order) to be indexed by one shared total order. Concretely: a stream's .iterator/iterateDimension order and its filtrationOrdering (used to key Chain.reduceBy's map) must be the same ordering, one the consistent .reverse of the other — not merely "each independently a valid total order." Two orderings that are each individually consistent but disagree on which of two tied cells comes first corrupt reduction in a way that's easy to miss on random test input, because Vietoris-Rips complexes have structural ties (every simplex of dimension ≥ 2 ties with its own longest edge) while random fixtures often don't — see Degeneracies.
Symptom: IllegalStateException: reduction pivot ... was not a recorded open class. This means a cell got selected as a pivot the algorithm's own invariants say should have been impossible — check whether iteration order and pivot order for the stream in play are provably the same ordering before looking in the reduction code itself.
.reverse on the same Ordering object is safe; building a second, independently-written "reversed" comparator is not, even when it looks equivalent on paper — a hand-written second comparator's tie-break direction doesn't automatically stay consistent with a separately-reversed primary key, and this has broken more than one stream in this codebase's history for exactly that reason.
3. Colex vs. lex tie-breaks are not interchangeable in Ripser-flavored code
The generic default tie-breaks on plain lexicographic vertex-set order. EnumeratingCofaceSimplexStream's and RipserCohomologyEngine's own orderings deliberately use colexicographic order instead, via SimplexIndexing.colexOrdering — the order of SimplexIndexing's combinatorial-number-system index, without its width limit — the exact tie-break Ripser's Definition 3.2/Proposition 3.9 (apparent pairs) are stated in terms of. Don't "simplify" a colex ordering to plain lex in code that touches SimplexIndexing/the Ripser engines; they need to agree with each other, not just each independently be a valid tie-break.
4. Chain.reduceBy/reduceByUntil, never hand-rolled Chain arithmetic, inside a reduction loop
Chain's +/-/⊠ operators only lazily collapse the head of the underlying PriorityQueue, so a hand-rolled fold that repeatedly subtracts terms builds an ever-growing backlog of uncollapsed duplicate entries, which can make a reduction of a small complex hang. Chain.reduceBy/reduceByUntil go through a SortedMap that collapses duplicates on every insertion. Reserve raw chain arithmetic for small, one-shot combinations (building a V-column fold), never for anything that accumulates over many reduction steps.
5. Combinatorial helpers over the full point set don't know about maxDimension truncation
SimplexIndexing.cofacetIterator/facetIterator operate purely combinatorially over the complete n-point abstract simplex — no concept of any per-engine dimension cap. RipserCohomologyEngine.coboundaryOf truncates explicitly instead, which is what makes top-dimension simplices come out essential. Any new code built directly on SimplexIndexing's iterators inherits none of that truncation and needs its own manual guard at the call site — SimplexIndexing itself has no way to know what any particular caller's cap is. The symptom (wrong finite bars where essential ones were expected) looks like a reduction bug, not an off-by-scope error two layers away, so it's easy to misdiagnose.
6. iterateDimension's domain must be contiguous from 0
StratifiedCellStream.iterator's default implementation is Iterator.from(0).takeWhile(iterateDimension.isDefinedAt).flatMap(iterateDimension) — it stops at the first dimension the stream doesn't define, and never asks about anything past it. A stream whose domain has a gap (defined at 0 and 2 but not 1) silently truncates at the gap rather than skipping past it. Every concrete stream in this codebase satisfies this for a structural reason, not by convention alone: a simplicial or cubical complex can't have a d-cell without its (d-1)-dimensional faces, so "no cells at d" implies "no cells beyond d" too. A hand-built or generated stream that doesn't have this structural guarantee needs to enforce contiguity itself — an unbounded case d => ... catch-all with no upper bound is also a hazard here (isDefinedAt always true, which defeats the "stops at the first gap" logic entirely and can loop until Int wraps around).
7. Opaque-type extension methods belong in the type's own companion object
An extension whose receiver is an opaque type (Simplex, Cube) should live inside that type's own companion object (object Simplex/object Cube), not as a top-level extension clause — extension-method resolution checks a receiver type's companion object by nominal type, so two unrelated opaque types can safely reuse the same method name this way (Simplex.underlying/Cube.underlying never collide). Two hazards to know before leaning on this: opaque-type transparency is scoped to the whole file the opaque type is declared in, so code in that same file calling the type's own extensions by dot-syntax can resolve to a same-named member of the underlying representation type instead of the intended extension — keep an opaque type's OrderedCell/similar instances in their own file when this matters. And a companion-object extension can still lose to a same-named stdlib extension reachable via a wildcard import at some call site (min/max deliberately stay top-level extensions on Simplex for exactly this reason, to avoid losing to scala.math.Ordering.Implicits).
8. In specs2/ScalaCheck tests, explicitly type simplex sequences before calling .forall
In a specs2 spec mixing in ScalaCheck, an un-ascribed Seq[Simplex[_]] can resolve .forall to a specs2 extension rather than the standard-library method, breaking type inference inside the predicate.
Give the sequence an explicit type first:
val xs: IndexedSeq[Simplex[Int]] = ...
xs.forall(...)
9. Dowker streams must reject infinite filtration values
DowkerGeometry.fromBoolean uses +Infinity to represent a relation that is never witnessed. Since +Infinity <= +Infinity is true, the usual maxFiltrationValue = +Infinity threshold check would otherwise admit those simplices and silently turn the relation into a larger complex.
DowkerCofaceSimplexStream therefore requires the computed filtration value to be finite in addition to satisfying the normal threshold check.
10. No top-level object or class with a non-ASCII name
scaladoc writes one page file per top-level type, named after it (∆$.html). A JVM running under a POSIX locale cannot encode that file name, and sbt doc dies with an InvalidPathException. ∆ is therefore a val alias of Simplex's companion, not an object of its own. Unicode extension methods and vals are fine.
11. Sign errors hide: test over a signed field, and mutate signs unevenly
Over F_2 every sign is +1, so a sign error cannot change any answer. Test boundaries, representatives and reductions over F_3 or another odd prime as well; agreement between F_2 and F_3 is a cheap sign check.
Signed fixtures need at least five vertices: Scala's small sets keep insertion order up to four elements, so a hash-ordered bug shows only from the fifth.
Flipping the sign of every coboundary at once changes no output, since it is a real symmetry. A mutation test of sign handling must flip some signs and not others.
Process: validate empirically, not by inspection
Pivot orientation, optional clearing, the completeness of an ordering fix and the soundness of an optimization are settled by a discriminating test, not by a plausibility argument: "this class-scope chainRM should be inert because X" is right for some classes and wrong for structurally similar ones.
When a change to reduction or ordering logic seems obviously sound, write the cheap discriminating test first. A good discriminating fixture deliberately puts filtration order and some other natural order (vertex label, insertion order) in conflict — a fixture where they happen to agree will not catch this class of bug.