import { useQueries, useQuery } from "@tanstack/react-query"; import { getMemoryUnit, listMemoryMessages } from "@/services/memory"; import { queryKeys } from "@/lib/query-keys"; import { useRuntimeContext } from "@/providers/runtime-context-provider"; import type { MemoryTask, MemoryUnit, SessionMessage } from "@/types/memory"; function toEpochMs(value: string | number | undefined): number | undefined { if (value === undefined || value === null) return undefined; if (typeof value === "number") return value; const ms = new Date(value).getTime(); return Number.isFinite(ms) ? ms : undefined; } export interface SourceEvidence { // Real message hashes the task's units cite. Empty when no unit carries refs. refIds: Set; // Tight timestamp window implied by the refs; Infinity / -Infinity if none. minTs: number; maxTs: number; } /** * Collect the REAL source-turn evidence from a task's derived units. * * Only source_turn_refs are read: they carry resolved message hashes (with * timestamps). source_turn_ids are deliberately ignored — they can be LOGICAL * turn labels like "D2:1" (dialogue 2, turn 1) that never equal a stored * messageId, so using them as a filter would hide every real message behind a * false "messages were cleaned up" empty state. */ export function aggregateSourceEvidence( memories: ReadonlyArray, ): SourceEvidence { const refIds = new Set(); let minTs = Infinity; let maxTs = -Infinity; for (const memory of memories) { if (!memory) continue; for (const ref of memory.source_turn_refs ?? []) { if (ref?.messageId) refIds.add(ref.messageId); const ts = ref?.timestampMs; if (typeof ts === "number" && Number.isFinite(ts)) { if (ts < minTs) minTs = ts; if (ts > maxTs) maxTs = ts; } } } return { refIds, minTs, maxTs }; } /** * Pick the messages a task actually drew from. With real refs, filter to * exactly those messageIds (precise). Without refs, show the fetched window in * timestamp order (precise=false) instead of hiding everything. */ export function selectSourceMessages( allMessages: ReadonlyArray, refIds: ReadonlySet, ): { messages: SessionMessage[]; precise: boolean } { const precise = refIds.size > 0; const filtered = precise ? allMessages.filter((m) => m.messageId && refIds.has(m.messageId)) : [...allMessages]; filtered.sort( (a, b) => (toEpochMs(a.timestamp) ?? 0) - (toEpochMs(b.timestamp) ?? 0), ); return { messages: filtered, precise }; } export interface TaskSourceMessages { messages: SessionMessage[]; isLoading: boolean; // True when the result is filtered to the exact source turns the task's // derived units cite. False means we fell back to the task-lifetime window // (e.g. the task produced no units, or the units carry no source refs). precise: boolean; } /** * Resolve the original conversation turns that actually fed an ingest task, * by walking the task's derived units and reading their source-turn evidence. * * This is task-bound: it never bleeds into a neighboring AddMemories call that * happened to write the same scope around the same time, which the old * scope+time-window heuristic could not avoid (a unit's source_turn_refs name * the precise messageIds). The time window is only used to bound the fetch; * the messageId set is what makes the result exact. */ export function useTaskSourceMessages( memoryStoreName: string, task: MemoryTask | null, ): TaskSourceMessages { const { context, ready } = useRuntimeContext(); const scope = task?.scope ?? null; const unitIds = task?.derivedUnitIds ?? []; const baseEnabled = ready && !!memoryStoreName && !!scope?.appId; // 1. Fetch every derived unit in parallel. Shares cache keys with // ExtractedMemoriesPanel's per-row useMemoryUnit, so no duplicate fetches. const unitQueries = useQueries({ queries: unitIds.map((id) => ({ queryKey: queryKeys.memoryUnit( context.dataScopeKey, memoryStoreName, id, scope, ), queryFn: () => getMemoryUnit({ memoryStoreName, memoryId: id, scope: scope! }), enabled: baseEnabled && !!id && id !== "-", staleTime: 30_000, })), }); const unitsLoading = unitIds.length > 0 && unitQueries.some((q) => q.isLoading); // 2. Aggregate source-turn evidence across every derived unit. The filter // set is built from source_turn_refs only — see aggregateSourceEvidence. const { refIds, minTs, maxTs } = aggregateSourceEvidence( unitQueries.map((q) => q.data?.memory), ); // 3. Pick a fetch window. Prefer the tight window implied by source-turn // timestamps; fall back to the task lifetime when refs lack timestamps // (or there are no units at all). const hasRefWindow = Number.isFinite(minTs) && Number.isFinite(maxTs); const createdMs = toEpochMs(task?.createdAt); const finishedMs = toEpochMs(task?.finishedAt) ?? toEpochMs(task?.updatedAt); const fallbackMax = finishedMs ?? createdMs; const winMin = hasRefWindow ? minTs - 2_000 : createdMs !== undefined ? createdMs - 60_000 : undefined; const winMax = hasRefWindow ? maxTs + 2_000 : fallbackMax !== undefined ? fallbackMax + 60_000 : undefined; const minTimestamp = winMin !== undefined ? new Date(winMin).toISOString() : undefined; const maxTimestamp = winMax !== undefined ? new Date(winMax).toISOString() : undefined; const messagesEnabled = baseEnabled && !unitsLoading && (minTimestamp !== undefined || maxTimestamp !== undefined); const messagesQuery = useQuery({ queryKey: queryKeys.memoryMessages( context.dataScopeKey, memoryStoreName, scope, undefined, 100, { minTimestamp, maxTimestamp }, ), queryFn: () => listMemoryMessages(memoryStoreName, scope!, { limit: 100, minTimestamp, maxTimestamp, }), enabled: messagesEnabled, staleTime: 30_000, }); // 4. Filter to the exact source turns; keep window order when we can't. const allMessages = messagesQuery.data?.items ?? []; const { messages, precise } = selectSourceMessages(allMessages, refIds); return { messages, isLoading: unitsLoading || messagesQuery.isLoading, precise, }; }