fix: avoid colliding with the next task when the gap is too small

The min-spacing fallback spread from the lower bound and could hand a
batch item the exact position of the following task, which the api's
conflict repair then scatters. Address pr-swarm finding.
This commit is contained in:
kolaente
2026-07-26 17:46:23 +02:00
parent fde7cc0935
commit 608682fd66
2 changed files with 9 additions and 2 deletions
@@ -60,9 +60,9 @@ export const calculateItemPositions = (
const spacing = (positionAfter - base) / (count + 1)
// The gap is too small to subdivide without the values colliding after the JSON
// round-trip. Give up on staying inside it and only keep the batch itself ordered.
// round-trip. Spread past positionAfter on purpose so the batch doesn't collide with it.
if (spacing < MIN_POSITION_SPACING) {
return spread(base, MIN_POSITION_SPACING)
return spread(Math.max(base, positionAfter), MIN_POSITION_SPACING)
}
return spread(base, spacing)
@@ -82,6 +82,13 @@ describe('calculateItemPositions', () => {
expect(positions).toEqual([...positions].sort((a, b) => a - b))
})
it('should not collide with the following item when there is nothing before and the gap is too small', () => {
const positions = calculateItemPositions(7, null, 0.05)
expect(positions).not.toContain(0.05)
expect(new Set(positions).size).toBe(7)
expect(positions).toEqual([...positions].sort((a, b) => a - b))
})
it('should keep distinct positions after a JSON round-trip', () => {
const positions = calculateItemPositions(10, null, 1)
const deserialized = JSON.parse(JSON.stringify(positions))