From fc4e18ad385c35e11d92375b0f5fab89da828de5 Mon Sep 17 00:00:00 2001 From: davidrobertson Date: Sat, 10 Oct 2026 13:36:43 -0400 Subject: [PATCH] fix: restore recent file history with partial session rows --- src/functions/file-index.ts | 12 ++---- test/file-index.test.ts | 74 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 test/file-index.test.ts diff --git a/src/functions/file-index.ts b/src/functions/file-index.ts index a52bf0356f..58deeb41ec 100644 --- a/src/functions/file-index.ts +++ b/src/functions/file-index.ts @@ -1,10 +1,11 @@ import type { IIIClient } from "iii-sdk"; -import type { CompressedObservation, Session } from "../types.js"; +import type { CompressedObservation } from "../types.js"; import { KV } from "../state/schema.js"; import { StateKV } from "../state/kv.js"; import { recordAudit } from "./audit.js"; import { recordAccessBatch } from "./access-tracker.js"; import { logger } from "../logger.js"; +import { selectSessions } from "../state/sessions.js"; interface FileHistory { file: string; @@ -44,19 +45,14 @@ export function registerFileIndexFunction(sdk: IIIClient, kv: StateKV): void { } const results: FileHistory[] = []; - const sessions = await kv.list(KV.sessions); + const sessions = selectSessions(await kv.list(KV.sessions)); let otherSessions = sessionId ? sessions.filter((s) => s.id !== sessionId) : sessions; if (normalizedProject) { otherSessions = otherSessions.filter((s) => s.project === normalizedProject); } - otherSessions = otherSessions - .sort( - (a, b) => - new Date(b.startedAt).getTime() - new Date(a.startedAt).getTime(), - ) - .slice(0, 15); + otherSessions = otherSessions.slice(0, 15); const obsCache = new Map(); for (const session of otherSessions) { diff --git a/test/file-index.test.ts b/test/file-index.test.ts new file mode 100644 index 0000000000..50cf388b76 --- /dev/null +++ b/test/file-index.test.ts @@ -0,0 +1,74 @@ +import { describe, expect, it } from "vitest"; +import { registerFileIndexFunction } from "../src/functions/file-index.js"; +import { KV } from "../src/state/schema.js"; +import type { Session } from "../src/types.js"; +import { mockKV, mockSdk } from "./helpers/mocks.js"; + +function session(id: string, startedAt: string, project = "demo"): Session { + return { + id, + project, + cwd: `/projects/${project}`, + startedAt, + status: "completed", + observationCount: 1, + }; +} + +async function fileHistory(rows: unknown[], payload: { sessionId?: string; project?: string }) { + const sdk = mockSdk(); + const kv = mockKV(); + for (const [index, row] of rows.entries()) { + await kv.set(KV.sessions, String(index), row); + } + await kv.set(KV.observations("recent"), "matching-observation", { + id: "matching-observation", + sessionId: "recent", + title: "Recent file change", + narrative: "recent-file-history-marker", + files: ["src/handler.ts"], + importance: 5, + type: "file_edit", + timestamp: "2026-10-10T12:00:00.000Z", + }); + registerFileIndexFunction(sdk as never, kv as never); + return sdk.trigger({ + function_id: "mem::file-context", + payload: { files: ["src/handler.ts"], ...payload }, + }); +} + +describe("file history session selection", () => { + it.each([ + { id: "partial", status: "completed" }, + null, + { id: "partial", startedAt: "2026-10-11T12:00:00.000Z" }, + ])("finds recent file history when legacy rows are malformed: %j", async (malformed) => { + const rows = Array.from({ length: 20 }, (_, index) => [ + session(`old-${index}`, `2026-01-${String(index + 1).padStart(2, "0")}T12:00:00.000Z`), + malformed, + ]).flat(); + rows.push(session("recent", "2026-10-10T12:00:00.000Z")); + + const result = await fileHistory(rows, {}); + + expect(result).toEqual({ context: expect.stringContaining("recent-file-history-marker") }); + }); + + it("applies project and current-session filters before the fifteen-session limit", async () => { + const rows = [ + ...Array.from({ length: 16 }, (_, index) => + session(`foreign-${index}`, "2026-10-12T12:00:00.000Z", "other"), + ), + ...Array.from({ length: 14 }, (_, index) => + session(`newer-${index}`, "2026-10-11T12:00:00.000Z"), + ), + session("current", "2026-10-13T12:00:00.000Z"), + session("recent", "2026-10-10T12:00:00.000Z"), + ]; + + const result = await fileHistory(rows, { project: "demo", sessionId: "current" }); + + expect(result).toEqual({ context: expect.stringContaining("recent-file-history-marker") }); + }); +});