Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(frontend): make nullish sort ties deterministic

Josué68440c037f49
diff --git a/control-ui/components/__tests__/FrontendComponents.properties.test.ts b/control-ui/components/__tests__/FrontendComponents.properties.test.ts
index 4197f74a1..7b61d8b0d 100644
--- a/control-ui/components/__tests__/FrontendComponents.properties.test.ts
+++ b/control-ui/components/__tests__/FrontendComponents.properties.test.ts
@@ -29,9 +29,10 @@ function rowOrder(
right: { id: string; value: SortValue },
direction: SortDirection
): number {
- if (left.value == null) return right.value == null ? 0 : 1
- if (right.value == null) return -1
- const primary = compareSortValues(left.value, right.value) * (direction === 'asc' ? 1 : -1)
+ const primary =
+ left.value == null || right.value == null
+ ? compareSortValues(left.value, right.value)
+ : compareSortValues(left.value, right.value) * (direction === 'asc' ? 1 : -1)
return primary || compareSortValues(left.id, right.id)
}
@@ -63,6 +64,41 @@ describe('stableSortRows properties', () => {
)
})
+ it('uses identity ordering for nullish primary ties regardless of input order', () => {
+ fc.assert(
+ fc.property(
+ fc.uniqueArray(
+ fc.record({
+ id: fc.uuid(),
+ value: fc.constantFrom<null | undefined>(null, undefined),
+ }),
+ { maxLength: 60, selector: row => row.id }
+ ),
+ fc.constantFrom<SortDirection>('asc', 'desc'),
+ (input, direction) => {
+ const sortedIds = stableSortRows(
+ input,
+ row => row.value,
+ direction,
+ row => row.id
+ ).map(row => row.id)
+ const reversedSortedIds = stableSortRows(
+ [...input].reverse(),
+ row => row.value,
+ direction,
+ row => row.id
+ ).map(row => row.id)
+
+ expect(sortedIds).toEqual(
+ [...input].map(row => row.id).sort((left, right) => compareSortValues(left, right))
+ )
+ expect(reversedSortedIds).toEqual(sortedIds)
+ }
+ ),
+ { numRuns: 250 }
+ )
+ })
+
it('is idempotent and preserves input order for exact ties', () => {
fc.assert(
fc.property(rows, fc.constantFrom<SortDirection>('asc', 'desc'), (input, direction) => {
diff --git a/control-ui/package-lock.json b/control-ui/package-lock.json
index 2887e1495..f4cb2d94a 100644
--- a/control-ui/package-lock.json
+++ b/control-ui/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-ui",
- "version": "0.1.558",
+ "version": "0.1.559",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-ui",
- "version": "0.1.558",
+ "version": "0.1.559",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/frontend-components": "file:../packages/frontend-components",
diff --git a/control-ui/package.json b/control-ui/package.json
index 1c7dfd81b..acb4dc347 100644
--- a/control-ui/package.json
+++ b/control-ui/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-ui",
- "version": "0.1.558",
+ "version": "0.1.559",
"private": true,
"scripts": {
"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",
diff --git a/packages/frontend-components/src/index.tsx b/packages/frontend-components/src/index.tsx
index 1e8988b31..2f74474ce 100644
--- a/packages/frontend-components/src/index.tsx
+++ b/packages/frontend-components/src/index.tsx
@@ -554,9 +554,10 @@ export function stableSortRows<TRow>(
.sort((left, right) => {
const leftValue = value(left.row)
const rightValue = value(right.row)
- if (leftValue == null) return rightValue == null ? 0 : 1
- if (rightValue == null) return -1
- const primary = compareSortValues(leftValue, rightValue) * multiplier
+ const primary =
+ leftValue == null || rightValue == null
+ ? compareSortValues(leftValue, rightValue)
+ : compareSortValues(leftValue, rightValue) * multiplier
if (primary !== 0) return primary
const secondary = compareSortValues(identity(left.row), identity(right.row))
return secondary || left.index - right.index