When implementing set-based selection/reconciliation algorithms, ensure (1) deterministic outcomes and (2) invariant-preserving persisted state across runs.
A. Deterministic tie-breaking (order-independent “argmax”)
If you pick a “best” owner based on computed scores, explicitly define what happens when scores are equal—do not rely on Map/array iteration order.
function pickBestOwner(scores) {
let best = null;
let bestScore = -Infinity;
for (const [owner, score] of scores) {
if (score > bestScore) {
best = owner;
bestScore = score;
} else if (score === bestScore) {
// deterministic tie-break (example: lexical)
if (best === null || owner < best) best = owner;
}
}
return best;
}
B. Persist the canonical desired set (not a derived delta)
In multi-run reconciliation (markers/comments, requested reviewers, etc.), persisted state must represent the canonical target. If you persist a filtered/derived view (e.g., desired - reviewed), later runs can lose elements and permanently drift from the intended target.
// Canonical marker persistence: store full desired, not effectiveDesired.
const newMarkerBody = `${MARKER} {"desired":${JSON.stringify([...desired].sort())}} -->`;
Checklist for PRs in this area