From 2e3aed472989657429751544ffc00e061d1aa039 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Fri, 6 Nov 2026 17:13:07 -0800 Subject: [PATCH] fix(tui): skip malformed frecency entries when loading A line that parses as JSON but carries no path, or a frequency or timestamp that is not a number, entered the in-memory map as an undefined-path entry and survived the repair rewrite, so autocomplete ranked NaN-scored files. Validate every entry like prompt history and the stash already do. --- packages/tui/src/prompt/frecency.tsx | 13 ++++++++++++- packages/tui/test/prompt/jsonl.test.ts | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/tui/src/prompt/frecency.tsx b/packages/tui/src/prompt/frecency.tsx index 2e6375f13be..80947ceebc2 100644 --- a/packages/tui/src/prompt/frecency.tsx +++ b/packages/tui/src/prompt/frecency.tsx @@ -4,6 +4,7 @@ import { createStore } from "solid-js/store" import { createSimpleContext } from "../context/helper" import { useTuiPaths } from "../context/runtime" import { appendText, readText, writeText } from "../util/persistence" +import { isRecord } from "../util/record" type FrecencyEntry = { path: string; frequency: number; lastOpen: number } @@ -15,7 +16,7 @@ export function parseFrecency(text: string) { .filter(Boolean) .map((line) => { try { - return JSON.parse(line) as FrecencyEntry + return parseFrecencyEntry(JSON.parse(line)) } catch { return undefined } @@ -35,6 +36,16 @@ function calculateFrecency(entry?: { frequency: number; lastOpen: number }) { return entry.frequency / (1 + (Date.now() - entry.lastOpen) / 86400000) } +// A parseable line can still carry no usable path or a broken counter, and the store rewrites every +// retained entry, so a bad one would survive the repair pass. Keep only whole entries. +function parseFrecencyEntry(value: unknown): FrecencyEntry | undefined { + if (!isRecord(value)) return + if (typeof value.path !== "string" || !value.path) return + if (typeof value.frequency !== "number" || !Number.isFinite(value.frequency)) return + if (typeof value.lastOpen !== "number" || !Number.isFinite(value.lastOpen)) return + return { path: value.path, frequency: value.frequency, lastOpen: value.lastOpen } +} + export const { use: useFrecency, provider: FrecencyProvider } = createSimpleContext({ name: "Frecency", init: () => { diff --git a/packages/tui/test/prompt/jsonl.test.ts b/packages/tui/test/prompt/jsonl.test.ts index 347a359144d..2d9bdc94488 100644 --- a/packages/tui/test/prompt/jsonl.test.ts +++ b/packages/tui/test/prompt/jsonl.test.ts @@ -12,6 +12,20 @@ test("stash JSONL skips corruption and retains newest entries", () => { expect(result[0]?.prompt.text).toBe("2") }) +test("frecency JSONL skips entries without a usable path and timestamps", () => { + const result = parseFrecency( + [ + JSON.stringify({ frequency: 3, lastOpen: 1 }), + JSON.stringify({ path: "", frequency: 3, lastOpen: 1 }), + JSON.stringify({ path: 7, frequency: 3, lastOpen: 1 }), + JSON.stringify({ path: "checked", lastOpen: 1 }), + JSON.stringify({ path: "stamped", frequency: "3", lastOpen: 1 }), + JSON.stringify({ path: "kept", frequency: 1, lastOpen: 2 }), + ].join("\n"), + ) + expect(result).toEqual([{ path: "kept", frequency: 1, lastOpen: 2 }]) +}) + test("frecency JSONL skips corruption, keeps latest path state, and limits entries", () => { const entries = Array.from({ length: MAX_FRECENCY_ENTRIES + 1 }, (_, index) => JSON.stringify({ path: String(index), frequency: 1, lastOpen: index }),