252 lines
10 KiB
TypeScript
252 lines
10 KiB
TypeScript
|
|
/**
|
|||
|
|
* @module tests/unit/storage/torn-record-loud
|
|||
|
|
* @description Torn records must be LOUD, never silent. A file that EXISTS but
|
|||
|
|
* cannot be decoded (truncated/garbled JSON, undecodable gzip) is disk
|
|||
|
|
* corruption, not absence — the old behavior logged "gracefully skipping" and
|
|||
|
|
* returned `null`, so a consumer could not distinguish "never existed" from
|
|||
|
|
* "exists but torn" and nothing ever healed it. Pins the cured contract:
|
|||
|
|
*
|
|||
|
|
* - ENTITY read paths (get/getBatch/pagination) throw a typed
|
|||
|
|
* `TornRecordError` ({path, cause}, code `TORN_RECORD`) — NEVER a silent null.
|
|||
|
|
* - Genuine absence (ENOENT) still reads as clean `null` — no error, no gauge.
|
|||
|
|
* - EVERY torn encounter increments the per-process torn-record gauge and
|
|||
|
|
* records the path, whatever the caller surface decides.
|
|||
|
|
* - SYSTEM-ARTIFACT reads (`readRawObject`: manifests/markers with recovery
|
|||
|
|
* paths) map torn → `null` BY DESIGN — but only after the encounter was
|
|||
|
|
* logged and counted (loud degrade, not a quiet loss).
|
|||
|
|
* - Legacy dual-format recovery: a torn `.gz` with a decodable uncompressed
|
|||
|
|
* fallback returns the recovered object AND still counts the torn `.gz`.
|
|||
|
|
*/
|
|||
|
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|||
|
|
import * as fs from 'node:fs'
|
|||
|
|
import * as os from 'node:os'
|
|||
|
|
import * as path from 'node:path'
|
|||
|
|
import { FileSystemStorage } from '../../../src/storage/adapters/fileSystemStorage.js'
|
|||
|
|
import {
|
|||
|
|
TornRecordError,
|
|||
|
|
isTornRecordError,
|
|||
|
|
getTornRecordGauge,
|
|||
|
|
resetTornRecordGauge
|
|||
|
|
} from '../../../src/storage/tornRecordError.js'
|
|||
|
|
import type { NounMetadata } from '../../../src/coreTypes.js'
|
|||
|
|
|
|||
|
|
const VEC = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
|
|||
|
|
|
|||
|
|
/** Release a raw FileSystemStorage so a subsequent open of the dir is unblocked. */
|
|||
|
|
async function teardown(s: any): Promise<void> {
|
|||
|
|
try { await s.flush?.() } catch { /* best effort */ }
|
|||
|
|
try { s.stopFlushRequestWatcher?.() } catch { /* best effort */ }
|
|||
|
|
try { await s.releaseWriterLock?.() } catch { /* best effort */ }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** Save one noun (metadata + vector) through the adapter's real write path. */
|
|||
|
|
async function seedOne(s: any, id: string): Promise<void> {
|
|||
|
|
await s.saveNounMetadata(id, {
|
|||
|
|
noun: 'thing',
|
|||
|
|
createdAt: Date.now(),
|
|||
|
|
updatedAt: Date.now()
|
|||
|
|
} as NounMetadata)
|
|||
|
|
await s.saveNoun({ id, vector: VEC, connections: new Map(), level: 0 })
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** Find the on-disk file(s) for an entity leg (metadata.json / vectors.json), .gz or plain. */
|
|||
|
|
function findEntityFiles(dir: string, id: string, leg: 'metadata' | 'vectors'): string[] {
|
|||
|
|
const found: string[] = []
|
|||
|
|
const walk = (d: string): void => {
|
|||
|
|
for (const entry of fs.readdirSync(d, { withFileTypes: true })) {
|
|||
|
|
const p = path.join(d, entry.name)
|
|||
|
|
if (entry.isDirectory()) walk(p)
|
|||
|
|
else if (
|
|||
|
|
p.includes(`${path.sep}${id}${path.sep}`) &&
|
|||
|
|
(entry.name === `${leg}.json` || entry.name === `${leg}.json.gz`)
|
|||
|
|
) {
|
|||
|
|
found.push(p)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
walk(path.join(dir, 'entities'))
|
|||
|
|
return found
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** Overwrite a file with bytes that can never decode as gzip or JSON. */
|
|||
|
|
function corruptFile(filePath: string): void {
|
|||
|
|
fs.writeFileSync(filePath, Buffer.from('{"noun":"thing","crea |