Self-healing parse: anything malformed degrades to "no metadata".
(raw: unknown)
| 99 | |
| 100 | /** Self-healing parse: anything malformed degrades to "no metadata". */ |
| 101 | function sanitizeMetaFile(raw: unknown): MemoryMetaFile { |
| 102 | if (typeof raw !== "object" || raw === null) return { entries: {} }; |
| 103 | const entriesRaw = (raw as Record<string, unknown>).entries; |
| 104 | if (typeof entriesRaw !== "object" || entriesRaw === null) return { entries: {} }; |
| 105 | const entries: Record<string, MemoryMetaEntry> = {}; |
| 106 | for (const [key, value] of Object.entries(entriesRaw)) { |
| 107 | if (typeof value !== "object" || value === null) continue; |
| 108 | const record = value as Record<string, unknown>; |
| 109 | const entry: MemoryMetaEntry = { |
| 110 | pinned: record.pinned === true, |
| 111 | accessCount: sanitizeCount(record.accessCount), |
| 112 | lastAccessedAt: sanitizeTimestamp(record.lastAccessedAt), |
| 113 | lastWriteAt: sanitizeTimestamp(record.lastWriteAt), |
| 114 | }; |
| 115 | if (isEmptyEntry(entry)) continue; |
| 116 | entries[key] = entry; |
| 117 | } |
| 118 | return { entries }; |
| 119 | } |
| 120 | |
| 121 | export class MemoryMetaService { |
| 122 | private readonly metaPath: string; |
no test coverage detected