mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
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.
This commit is contained in:
@@ -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: () => {
|
||||
|
||||
@@ -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 }),
|
||||
|
||||
Reference in New Issue
Block a user