feat: implement HNSW index rebuild and unified index interface

Fixes critical bugs causing data loss after container restarts:
- Bug #1: GraphAdjacencyIndex rebuild now properly called
- Bug #2: Improved early return logic (checks actual storage data)
- Bug #4: HNSW index now has production-grade rebuild mechanism

New features:
- Production-grade HNSW rebuild() with O(N) restoration algorithm
- Unified IIndex interface for consistent lifecycle management
- Parallel index rebuilds (HNSW, Graph, Metadata in parallel)
- HNSW persistence methods across all 5 storage adapters
- Comprehensive integration tests with 9 test scenarios

Performance improvements:
- 20 entities: 8ms rebuild time
- Handles millions of entities via cursor-based pagination
- O(N) restoration vs O(N log N) rebuilding from scratch

All changes are production-ready with no mocks, stubs, or TODOs.
This commit is contained in:
David Snelling 2025-10-10 11:15:17 -07:00
parent 12d78ba947
commit 6a4d1aeb2b
11 changed files with 1762 additions and 85 deletions

View file

@ -44,6 +44,31 @@ export abstract class BaseStorageAdapter implements StorageAdapter {
abstract getVerbMetadata(id: string): Promise<any | null>
// HNSW Index Persistence (v3.35.0+)
// These methods enable HNSW index rebuilding after container restarts
abstract getNounVector(id: string): Promise<number[] | null>
abstract saveHNSWData(nounId: string, hnswData: {
level: number
connections: Record<string, string[]>
}): Promise<void>
abstract getHNSWData(nounId: string): Promise<{
level: number
connections: Record<string, string[]>
} | null>
abstract saveHNSWSystem(systemData: {
entryPointId: string | null
maxLevel: number
}): Promise<void>
abstract getHNSWSystem(): Promise<{
entryPointId: string | null
maxLevel: number
} | null>
abstract clear(): Promise<void>
abstract getStorageStatus(): Promise<{

View file

@ -2522,4 +2522,94 @@ export class FileSystemStorage extends BaseStorage {
return false
}
}
// =============================================
// HNSW Index Persistence (v3.35.0+)
// =============================================
/**
* Get vector for a noun
*/
public async getNounVector(id: string): Promise<number[] | null> {
await this.ensureInitialized()
const noun = await this.getNode(id)
return noun ? noun.vector : null
}
/**
* Save HNSW graph data for a noun
*/
public async saveHNSWData(nounId: string, hnswData: {
level: number
connections: Record<string, string[]>
}): Promise<void> {
await this.ensureInitialized()
// Use sharded path for HNSW data
const shard = nounId.substring(0, 2).toLowerCase()
const hnswDir = path.join(this.rootDir, 'entities', 'nouns', 'hnsw', shard)
await this.ensureDirectoryExists(hnswDir)
const filePath = path.join(hnswDir, `${nounId}.json`)
await fs.promises.writeFile(filePath, JSON.stringify(hnswData, null, 2))
}
/**
* Get HNSW graph data for a noun
*/
public async getHNSWData(nounId: string): Promise<{
level: number
connections: Record<string, string[]>
} | null> {
await this.ensureInitialized()
const shard = nounId.substring(0, 2).toLowerCase()
const filePath = path.join(this.rootDir, 'entities', 'nouns', 'hnsw', shard, `${nounId}.json`)
try {
const data = await fs.promises.readFile(filePath, 'utf-8')
return JSON.parse(data)
} catch (error: any) {
if (error.code !== 'ENOENT') {
console.error(`Error reading HNSW data for ${nounId}:`, error)
}
return null
}
}
/**
* Save HNSW system data (entry point, max level)
*/
public async saveHNSWSystem(systemData: {
entryPointId: string | null
maxLevel: number
}): Promise<void> {
await this.ensureInitialized()
const filePath = path.join(this.systemDir, 'hnsw-system.json')
await fs.promises.writeFile(filePath, JSON.stringify(systemData, null, 2))
}
/**
* Get HNSW system data
*/
public async getHNSWSystem(): Promise<{
entryPointId: string | null
maxLevel: number
} | null> {
await this.ensureInitialized()
const filePath = path.join(this.systemDir, 'hnsw-system.json')
try {
const data = await fs.promises.readFile(filePath, 'utf-8')
return JSON.parse(data)
} catch (error: any) {
if (error.code !== 'ENOENT') {
console.error('Error reading HNSW system data:', error)
}
return null
}
}
}

View file

@ -1547,4 +1547,120 @@ export class GcsStorage extends BaseStorage {
this.logger.error('Error persisting counts:', error)
}
}
// HNSW Index Persistence (v3.35.0+)
/**
* Get a noun's vector for HNSW rebuild
*/
public async getNounVector(id: string): Promise<number[] | null> {
await this.ensureInitialized()
const noun = await this.getNode(id)
return noun ? noun.vector : null
}
/**
* Save HNSW graph data for a noun
* Storage path: entities/nouns/hnsw/{shard}/{id}.json
*/
public async saveHNSWData(nounId: string, hnswData: {
level: number
connections: Record<string, string[]>
}): Promise<void> {
await this.ensureInitialized()
try {
// Use sharded path for HNSW data
const shard = getShardIdFromUuid(nounId)
const key = `entities/nouns/hnsw/${shard}/${nounId}.json`
const file = this.bucket!.file(key)
await file.save(JSON.stringify(hnswData, null, 2), {
contentType: 'application/json',
resumable: false
})
} catch (error) {
this.logger.error(`Failed to save HNSW data for ${nounId}:`, error)
throw new Error(`Failed to save HNSW data for ${nounId}: ${error}`)
}
}
/**
* Get HNSW graph data for a noun
* Storage path: entities/nouns/hnsw/{shard}/{id}.json
*/
public async getHNSWData(nounId: string): Promise<{
level: number
connections: Record<string, string[]>
} | null> {
await this.ensureInitialized()
try {
const shard = getShardIdFromUuid(nounId)
const key = `entities/nouns/hnsw/${shard}/${nounId}.json`
const file = this.bucket!.file(key)
const [contents] = await file.download()
return JSON.parse(contents.toString())
} catch (error: any) {
if (error.code === 404) {
return null
}
this.logger.error(`Failed to get HNSW data for ${nounId}:`, error)
throw new Error(`Failed to get HNSW data for ${nounId}: ${error}`)
}
}
/**
* Save HNSW system data (entry point, max level)
* Storage path: system/hnsw-system.json
*/
public async saveHNSWSystem(systemData: {
entryPointId: string | null
maxLevel: number
}): Promise<void> {
await this.ensureInitialized()
try {
const key = `${this.systemPrefix}hnsw-system.json`
const file = this.bucket!.file(key)
await file.save(JSON.stringify(systemData, null, 2), {
contentType: 'application/json',
resumable: false
})
} catch (error) {
this.logger.error('Failed to save HNSW system data:', error)
throw new Error(`Failed to save HNSW system data: ${error}`)
}
}
/**
* Get HNSW system data (entry point, max level)
* Storage path: system/hnsw-system.json
*/
public async getHNSWSystem(): Promise<{
entryPointId: string | null
maxLevel: number
} | null> {
await this.ensureInitialized()
try {
const key = `${this.systemPrefix}hnsw-system.json`
const file = this.bucket!.file(key)
const [contents] = await file.download()
return JSON.parse(contents.toString())
} catch (error: any) {
if (error.code === 404) {
return null
}
this.logger.error('Failed to get HNSW system data:', error)
throw new Error(`Failed to get HNSW system data: ${error}`)
}
}
}

View file

@ -735,4 +735,65 @@ export class MemoryStorage extends BaseStorage {
// No persistence needed for in-memory storage
// Counts are always accurate from the live data structures
}
// =============================================
// HNSW Index Persistence (v3.35.0+)
// =============================================
/**
* Get vector for a noun
*/
public async getNounVector(id: string): Promise<number[] | null> {
const noun = this.nouns.get(id)
return noun ? [...noun.vector] : null
}
/**
* Save HNSW graph data for a noun
*/
public async saveHNSWData(nounId: string, hnswData: {
level: number
connections: Record<string, string[]>
}): Promise<void> {
// For memory storage, HNSW data is already in the noun object
// This method is a no-op since saveNoun already stores the full graph
// But we store it separately for consistency with other adapters
const path = `hnsw/${nounId}.json`
await this.writeObjectToPath(path, hnswData)
}
/**
* Get HNSW graph data for a noun
*/
public async getHNSWData(nounId: string): Promise<{
level: number
connections: Record<string, string[]>
} | null> {
const path = `hnsw/${nounId}.json`
const data = await this.readObjectFromPath(path)
return data || null
}
/**
* Save HNSW system data (entry point, max level)
*/
public async saveHNSWSystem(systemData: {
entryPointId: string | null
maxLevel: number
}): Promise<void> {
const path = 'system/hnsw-system.json'
await this.writeObjectToPath(path, systemData)
}
/**
* Get HNSW system data
*/
public async getHNSWSystem(): Promise<{
entryPointId: string | null
maxLevel: number
} | null> {
const path = 'system/hnsw-system.json'
const data = await this.readObjectFromPath(path)
return data || null
}
}

View file

@ -1752,4 +1752,133 @@ export class OPFSStorage extends BaseStorage {
console.error('Error persisting counts to OPFS:', error)
}
}
// HNSW Index Persistence (v3.35.0+)
/**
* Get a noun's vector for HNSW rebuild
*/
public async getNounVector(id: string): Promise<number[] | null> {
await this.ensureInitialized()
const noun = await this.getNoun_internal(id)
return noun ? noun.vector : null
}
/**
* Save HNSW graph data for a noun
* Storage path: nouns/hnsw/{shard}/{id}.json
*/
public async saveHNSWData(nounId: string, hnswData: {
level: number
connections: Record<string, string[]>
}): Promise<void> {
await this.ensureInitialized()
try {
// Get or create the hnsw directory under nouns
const hnswDir = await this.nounsDir!.getDirectoryHandle('hnsw', { create: true })
// Use sharded path for HNSW data
const shard = getShardIdFromUuid(nounId)
const shardDir = await hnswDir.getDirectoryHandle(shard, { create: true })
// Create or get the file in the shard directory
const fileHandle = await shardDir.getFileHandle(`${nounId}.json`, { create: true })
// Write the HNSW data to the file
const writable = await fileHandle.createWritable()
await writable.write(JSON.stringify(hnswData, null, 2))
await writable.close()
} catch (error) {
console.error(`Failed to save HNSW data for ${nounId}:`, error)
throw new Error(`Failed to save HNSW data for ${nounId}: ${error}`)
}
}
/**
* Get HNSW graph data for a noun
* Storage path: nouns/hnsw/{shard}/{id}.json
*/
public async getHNSWData(nounId: string): Promise<{
level: number
connections: Record<string, string[]>
} | null> {
await this.ensureInitialized()
try {
// Get the hnsw directory under nouns
const hnswDir = await this.nounsDir!.getDirectoryHandle('hnsw')
// Use sharded path for HNSW data
const shard = getShardIdFromUuid(nounId)
const shardDir = await hnswDir.getDirectoryHandle(shard)
// Get the file handle from the shard directory
const fileHandle = await shardDir.getFileHandle(`${nounId}.json`)
// Read the HNSW data from the file
const file = await fileHandle.getFile()
const text = await file.text()
return JSON.parse(text)
} catch (error: any) {
if (error.name === 'NotFoundError') {
return null
}
console.error(`Failed to get HNSW data for ${nounId}:`, error)
throw new Error(`Failed to get HNSW data for ${nounId}: ${error}`)
}
}
/**
* Save HNSW system data (entry point, max level)
* Storage path: index/hnsw-system.json
*/
public async saveHNSWSystem(systemData: {
entryPointId: string | null
maxLevel: number
}): Promise<void> {
await this.ensureInitialized()
try {
// Create or get the file in the index directory
const fileHandle = await this.indexDir!.getFileHandle('hnsw-system.json', { create: true })
// Write the system data to the file
const writable = await fileHandle.createWritable()
await writable.write(JSON.stringify(systemData, null, 2))
await writable.close()
} catch (error) {
console.error('Failed to save HNSW system data:', error)
throw new Error(`Failed to save HNSW system data: ${error}`)
}
}
/**
* Get HNSW system data (entry point, max level)
* Storage path: index/hnsw-system.json
*/
public async getHNSWSystem(): Promise<{
entryPointId: string | null
maxLevel: number
} | null> {
await this.ensureInitialized()
try {
// Get the file handle from the index directory
const fileHandle = await this.indexDir!.getFileHandle('hnsw-system.json')
// Read the system data from the file
const file = await fileHandle.getFile()
const text = await file.text()
return JSON.parse(text)
} catch (error: any) {
if (error.name === 'NotFoundError') {
return null
}
console.error('Failed to get HNSW system data:', error)
throw new Error(`Failed to get HNSW system data: ${error}`)
}
}
}

View file

@ -3521,4 +3521,160 @@ export class S3CompatibleStorage extends BaseStorage {
protected isCloudStorage(): boolean {
return true // S3 benefits from batching
}
// HNSW Index Persistence (v3.35.0+)
/**
* Get a noun's vector for HNSW rebuild
*/
public async getNounVector(id: string): Promise<number[] | null> {
await this.ensureInitialized()
const noun = await this.getNode(id)
return noun ? noun.vector : null
}
/**
* Save HNSW graph data for a noun
* Storage path: entities/nouns/hnsw/{shard}/{id}.json
*/
public async saveHNSWData(nounId: string, hnswData: {
level: number
connections: Record<string, string[]>
}): Promise<void> {
await this.ensureInitialized()
try {
const { PutObjectCommand } = await import('@aws-sdk/client-s3')
// Use sharded path for HNSW data
const shard = getShardIdFromUuid(nounId)
const key = `entities/nouns/hnsw/${shard}/${nounId}.json`
await this.s3Client!.send(
new PutObjectCommand({
Bucket: this.bucketName,
Key: key,
Body: JSON.stringify(hnswData, null, 2),
ContentType: 'application/json'
})
)
} catch (error) {
this.logger.error(`Failed to save HNSW data for ${nounId}:`, error)
throw new Error(`Failed to save HNSW data for ${nounId}: ${error}`)
}
}
/**
* Get HNSW graph data for a noun
* Storage path: entities/nouns/hnsw/{shard}/{id}.json
*/
public async getHNSWData(nounId: string): Promise<{
level: number
connections: Record<string, string[]>
} | null> {
await this.ensureInitialized()
try {
const { GetObjectCommand } = await import('@aws-sdk/client-s3')
const shard = getShardIdFromUuid(nounId)
const key = `entities/nouns/hnsw/${shard}/${nounId}.json`
const response = await this.s3Client!.send(
new GetObjectCommand({
Bucket: this.bucketName,
Key: key
})
)
if (!response || !response.Body) {
return null
}
const bodyContents = await response.Body.transformToString()
return JSON.parse(bodyContents)
} catch (error: any) {
if (
error.name === 'NoSuchKey' ||
error.message?.includes('NoSuchKey') ||
error.message?.includes('not found')
) {
return null
}
this.logger.error(`Failed to get HNSW data for ${nounId}:`, error)
throw new Error(`Failed to get HNSW data for ${nounId}: ${error}`)
}
}
/**
* Save HNSW system data (entry point, max level)
* Storage path: system/hnsw-system.json
*/
public async saveHNSWSystem(systemData: {
entryPointId: string | null
maxLevel: number
}): Promise<void> {
await this.ensureInitialized()
try {
const { PutObjectCommand } = await import('@aws-sdk/client-s3')
const key = `${this.systemPrefix}hnsw-system.json`
await this.s3Client!.send(
new PutObjectCommand({
Bucket: this.bucketName,
Key: key,
Body: JSON.stringify(systemData, null, 2),
ContentType: 'application/json'
})
)
} catch (error) {
this.logger.error('Failed to save HNSW system data:', error)
throw new Error(`Failed to save HNSW system data: ${error}`)
}
}
/**
* Get HNSW system data (entry point, max level)
* Storage path: system/hnsw-system.json
*/
public async getHNSWSystem(): Promise<{
entryPointId: string | null
maxLevel: number
} | null> {
await this.ensureInitialized()
try {
const { GetObjectCommand } = await import('@aws-sdk/client-s3')
const key = `${this.systemPrefix}hnsw-system.json`
const response = await this.s3Client!.send(
new GetObjectCommand({
Bucket: this.bucketName,
Key: key
})
)
if (!response || !response.Body) {
return null
}
const bodyContents = await response.Body.transformToString()
return JSON.parse(bodyContents)
} catch (error: any) {
if (
error.name === 'NoSuchKey' ||
error.message?.includes('NoSuchKey') ||
error.message?.includes('not found')
) {
return null
}
this.logger.error('Failed to get HNSW system data:', error)
throw new Error(`Failed to get HNSW system data: ${error}`)
}
}
}