Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix: handle exact match scoring for command k fuzzy library (#1120)

gbirman799220b087f8
diff --git a/js/app/packages/core/util/freshSort.test.ts b/js/app/packages/core/util/freshSort.test.ts
index 441ba49bc0..412573b007 100644
--- a/js/app/packages/core/util/freshSort.test.ts
+++ b/js/app/packages/core/util/freshSort.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { createFreshSearch } from './freshSort';
+import { createFreshSearch, normalizeFuzzyScore } from './freshSort';
interface MockItem {
id: string;
@@ -164,3 +164,96 @@ describe('createFreshSearch with comma-separated channel matching', () => {
expect(results2[0].item.id).toBe('1');
});
});
+
+describe('normalizeFuzzyScore', () => {
+ it('normalizes regular scores correctly', () => {
+ const result = normalizeFuzzyScore(50, 100);
+ expect(result).toBe(0.5);
+ });
+
+ it('clamps scores to [0, 1] range', () => {
+ expect(normalizeFuzzyScore(-10, 100)).toBe(0);
+ expect(normalizeFuzzyScore(150, 100)).toBe(1);
+ });
+
+ it('handles edge cases', () => {
+ expect(normalizeFuzzyScore(0, 100)).toBe(0);
+ expect(normalizeFuzzyScore(100, 100)).toBe(1);
+ expect(normalizeFuzzyScore(0, 1)).toBe(0);
+ });
+
+ it('throws error for Infinity fuzzyScore', () => {
+ expect(() => normalizeFuzzyScore(Infinity, 100)).toThrow(
+ 'fuzzyScore must be a finite number'
+ );
+ });
+
+ it('throws error for invalid maxPossibleScore', () => {
+ expect(() => normalizeFuzzyScore(50, Infinity)).toThrow(
+ 'maxPossibleScore must be a finite positive number'
+ );
+ expect(() => normalizeFuzzyScore(50, 0)).toThrow(
+ 'maxPossibleScore must be a finite positive number'
+ );
+ expect(() => normalizeFuzzyScore(50, -10)).toThrow(
+ 'maxPossibleScore must be a finite positive number'
+ );
+ });
+});
+
+describe('freshSort with all exact matches', () => {
+ it('sorts by recency when all items are exact matches (Infinity scores)', () => {
+ const now = Date.now();
+ const items: MockItem[] = [
+ { id: '1', name: 'Design', type: 'item', viewedAt: now - 3600000 }, // 1hr ago
+ { id: '2', name: 'Design', type: 'item', viewedAt: now - 60000 }, // 1min ago
+ { id: '3', name: 'Design', type: 'item', viewedAt: now - 86400000 }, // 1day ago
+ { id: '4', name: 'Design', type: 'item', viewedAt: now - 1000 }, // 1sec ago
+ ];
+
+ const search = createFreshSearch<MockItem>(
+ {
+ useViewedAt: true,
+ fuzzyWeight: 0.5,
+ timeWeight: 0.5,
+ },
+ (item) => item.name
+ );
+
+ const results = search(items, 'Design');
+
+ expect(results).toHaveLength(4);
+ expect(results[0].item.id).toBe('4');
+ expect(results[1].item.id).toBe('2');
+ expect(results[2].item.id).toBe('1');
+ expect(results[3].item.id).toBe('3');
+
+ for (const result of results) {
+ expect(result.fuzzyScore).toBeGreaterThan(0);
+ expect(Number.isFinite(result.fuzzyScore)).toBe(true);
+ }
+ });
+
+ it('channel boost works correctly with all exact matches', () => {
+ const now = Date.now();
+ const items: MockItem[] = [
+ { id: '1', name: 'Design', type: 'item', viewedAt: now - 1000 },
+ { id: '2', name: 'Design', type: 'channel', viewedAt: now - 5000 },
+ ];
+
+ const search = createFreshSearch<MockItem>(
+ {
+ useViewedAt: true,
+ fuzzyWeight: 0.8,
+ timeWeight: 0.2,
+ channelBoost: 2.0,
+ },
+ (item) => item.name
+ );
+
+ const results = search(items, 'Design');
+
+ expect(results).toHaveLength(2);
+ expect(results[0].item.type).toBe('channel');
+ });
+});
diff --git a/js/app/packages/core/util/freshSort.ts b/js/app/packages/core/util/freshSort.ts
index e03a917ce2..31b8d55b7d 100644
--- a/js/app/packages/core/util/freshSort.ts
+++ b/js/app/packages/core/util/freshSort.ts
@@ -122,10 +122,19 @@ function calculateTimeScore(
return Math.exp(-config.timeDecayFactor * normalizedAge);
}
-function normalizeFuzzyScore(
+export function normalizeFuzzyScore(
fuzzyScore: number,
- maxPossibleScore: number = 1
+ maxPossibleScore: number
): number {
+ if (!Number.isFinite(fuzzyScore)) {
+ throw new Error(`fuzzyScore must be a finite number, got: ${fuzzyScore}`);
+ }
+ if (!Number.isFinite(maxPossibleScore) || maxPossibleScore <= 0) {
+ throw new Error(
+ `maxPossibleScore must be a finite positive number, got: ${maxPossibleScore}`
+ );
+ }
+
return Math.max(0, Math.min(1, fuzzyScore / maxPossibleScore));
}
@@ -149,13 +158,16 @@ export function freshSort<T extends TimestampedItem>(
const normalizedTimeWeight = finalConfig.timeWeight / totalWeight;
const normalizedBrevityWeight = finalConfig.brevityWeight / totalWeight;
- const maxFuzzyScore = Math.max(...filterResults.map((r) => r.score));
+ const filterNoInfResults = filterResults.filter((r) => r.score !== Infinity);
+ const maxFuzzyScore =
+ filterNoInfResults.length > 0
+ ? Math.max(...filterNoInfResults.map((r) => r.score))
+ : 1;
const scoredResults: FreshSortResult<T>[] = filterResults.map((result) => {
+ const rawScore = result.score === Infinity ? maxFuzzyScore : result.score;
const fuzzyScore =
- maxFuzzyScore === 0
- ? 0
- : normalizeFuzzyScore(result.score, maxFuzzyScore);
+ maxFuzzyScore === 0 ? 0 : normalizeFuzzyScore(rawScore, maxFuzzyScore);
const timeScore = calculateTimeScore(
extractTimestamp(result.original, finalConfig.useViewedAt),
finalConfig