fix(versioning): clean architecture with index pollution prevention

v6.3.0 Versioning System Overhaul:
- Rewrite VersionIndex to use pure key-value storage (not entities)
- Fix restore() to use brain.update() - updates all indexes (HNSW, metadata, graph)
- Remove 525 LOC dead code (versioningAugmentation.ts - untested, unused)
- Fix branch isolation in tests (fork() vs checkout() semantics)

Key improvements:
- Versions no longer pollute find() results
- restore() properly updates all indexes
- 75 tests passing (60 unit + 15 integration)
- Net reduction of ~290 lines while fixing bugs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
David Snelling 2025-12-09 09:35:45 -08:00
parent 292be1b9cd
commit f145fa1fc8
8 changed files with 712 additions and 1002 deletions

View file

@ -1,525 +0,0 @@
/**
* Versioning Augmentation (v5.3.0)
*
* Provides automatic entity versioning with configurable policies:
* - Auto-save on update()
* - Entity filtering
* - Retention policies
* - Event hooks
*
* NO MOCKS - Production implementation
*/
import { BaseAugmentation } from './brainyAugmentation.js'
import { AugmentationManifest } from './manifest.js'
import type { VersioningAPI } from '../versioning/VersioningAPI.js'
import type { SaveVersionOptions } from '../versioning/VersionManager.js'
export interface VersioningAugmentationConfig {
/** Enable auto-versioning */
enabled?: boolean
/** Auto-save on update() operations */
onUpdate?: boolean
/** Auto-save on add() operations (rarely needed) */
onAdd?: boolean
/** Entity ID patterns to version (glob patterns) */
entities?: string[]
/** Entity ID patterns to exclude (glob patterns) */
excludeEntities?: string[]
/** Entity types to version */
types?: string[]
/** Entity types to exclude */
excludeTypes?: string[]
/** Retention policy: Keep N recent versions per entity */
keepRecent?: number
/** Retention policy: Keep versions newer than timestamp */
keepAfter?: number
/** Retention policy: Keep tagged versions (default: true) */
keepTagged?: boolean
/** Tag prefix for auto-generated versions (default: 'auto-') */
tagPrefix?: string
/** Auto-prune old versions after save */
autoPrune?: boolean
/** Prune interval in milliseconds (default: 1 hour) */
pruneInterval?: number
/** Event hooks */
hooks?: {
/** Called before saving version */
beforeSave?: (entityId: string, options: SaveVersionOptions) => Promise<SaveVersionOptions | null>
/** Called after saving version */
afterSave?: (entityId: string, version: any) => Promise<void>
/** Called before pruning */
beforePrune?: (entityId: string) => Promise<boolean>
/** Called after pruning */
afterPrune?: (entityId: string, deleted: number) => Promise<void>
}
}
/**
* Versioning Augmentation
*
* Automatically versions entities based on configurable policies.
*/
export class VersioningAugmentation extends BaseAugmentation {
readonly name = 'versioning'
readonly timing = 'after' as const // Run after operations complete
readonly metadata = 'readonly' as const
operations = ['update', 'add'] as any // Version on update/add
readonly priority = 50 // Medium priority
// Augmentation metadata
readonly category = 'core' as const
readonly description = 'Automatic entity versioning with configurable retention policies'
private versioningAPI?: VersioningAPI
private brain?: any // Brainy instance for entity fetching
private pruneTimer?: NodeJS.Timeout
private versionCount = 0
constructor(config: VersioningAugmentationConfig = {}) {
super(config)
// Merge with defaults
this.config = {
enabled: config.enabled ?? true,
onUpdate: config.onUpdate ?? true,
onAdd: config.onAdd ?? false,
entities: config.entities ?? ['*'], // Version all entities by default
excludeEntities: config.excludeEntities ?? ['_version:*'], // Exclude version metadata entities
types: config.types ?? [], // Empty = all types
excludeTypes: config.excludeTypes ?? [], // No need to exclude types (versions use 'state' type)
keepRecent: config.keepRecent ?? 10,
keepTagged: config.keepTagged ?? true,
tagPrefix: config.tagPrefix ?? 'auto-',
autoPrune: config.autoPrune ?? true,
pruneInterval: config.pruneInterval ?? 60 * 60 * 1000, // 1 hour
hooks: config.hooks ?? {}
}
}
getManifest(): AugmentationManifest {
return {
id: 'versioning',
name: 'Entity Versioning',
version: '5.3.0',
description: 'Automatic entity versioning with retention policies',
longDescription: 'Provides automatic versioning of entities on update/add operations with configurable retention policies, entity filtering, and event hooks.',
category: 'storage',
configSchema: {
type: 'object',
properties: {
enabled: {
type: 'boolean',
default: true,
description: 'Enable automatic versioning'
},
onUpdate: {
type: 'boolean',
default: true,
description: 'Auto-save version on update()'
},
onAdd: {
type: 'boolean',
default: false,
description: 'Auto-save version on add()'
},
entities: {
type: 'array',
items: { type: 'string' },
default: ['*'],
description: 'Entity ID patterns to version (glob patterns)'
},
excludeEntities: {
type: 'array',
items: { type: 'string' },
default: ['_version:*'],
description: 'Entity ID patterns to exclude (version metadata IDs start with _version:)'
},
types: {
type: 'array',
items: { type: 'string' },
default: [],
description: 'Entity types to version (empty = all)'
},
excludeTypes: {
type: 'array',
items: { type: 'string' },
default: [],
description: 'Entity types to exclude (versions use standard state type)'
},
keepRecent: {
type: 'number',
default: 10,
minimum: 1,
description: 'Keep N recent versions per entity'
},
keepTagged: {
type: 'boolean',
default: true,
description: 'Keep tagged versions during pruning'
},
tagPrefix: {
type: 'string',
default: 'auto-',
description: 'Tag prefix for auto-generated versions'
},
autoPrune: {
type: 'boolean',
default: true,
description: 'Automatically prune old versions'
},
pruneInterval: {
type: 'number',
default: 3600000,
description: 'Prune interval in milliseconds (default: 1 hour)'
}
}
},
configDefaults: {
enabled: true,
onUpdate: true,
onAdd: false,
entities: ['*'],
excludeEntities: ['_version:*'],
types: [],
excludeTypes: [],
keepRecent: 10,
keepTagged: true,
tagPrefix: 'auto-',
autoPrune: true,
pruneInterval: 3600000
},
minBrainyVersion: '5.3.0',
keywords: ['versioning', 'history', 'audit', 'backup'],
documentation: 'https://docs.brainy.dev/versioning',
status: 'stable',
performance: {
memoryUsage: 'low',
cpuUsage: 'low',
networkUsage: 'none'
},
features: [
'auto-versioning',
'retention-policies',
'entity-filtering',
'event-hooks'
],
enhancedOperations: ['update', 'add'],
metrics: [
{
name: 'versions_created',
type: 'counter',
description: 'Total versions created by augmentation'
},
{
name: 'versions_pruned',
type: 'counter',
description: 'Total versions pruned by retention policies'
}
]
}
}
async onAttach(brain: any): Promise<void> {
// Store brain instance for entity fetching
this.brain = brain
// Get VersioningAPI instance
this.versioningAPI = brain.versions
// Start auto-prune timer if enabled
if (this.config.autoPrune && this.config.pruneInterval) {
this.startAutoPrune()
}
}
async onDetach(): Promise<void> {
// Stop auto-prune timer
if (this.pruneTimer) {
clearInterval(this.pruneTimer)
this.pruneTimer = undefined
}
}
/**
* Execute method (required by BaseAugmentation)
*/
async execute(operation: string, params: any[], context: any): Promise<any> {
// Versioning augmentation only runs in 'after' mode
// Actual logic is in after() method
return context.originalResult
}
/**
* After operation hook - auto-save versions
*/
async after(operation: string, params: any[], result: any): Promise<any> {
if (!this.config.enabled || !this.versioningAPI) {
return result
}
// Check if we should version this operation
if (operation === 'update' && !this.config.onUpdate) {
return result
}
if (operation === 'add' && !this.config.onAdd) {
return result
}
// Extract entity ID from params
const entityId = this.extractEntityId(operation, params)
if (!entityId) {
return result
}
// Check if entity should be versioned
if (!(await this.shouldVersionEntity(entityId))) {
return result
}
try {
// Prepare save options
let saveOptions: SaveVersionOptions = {
tag: `${this.config.tagPrefix}${Date.now()}`,
description: `Auto-saved after ${operation}`,
metadata: {
augmentation: 'versioning',
operation,
timestamp: Date.now()
}
}
// Call before-save hook
if (this.config.hooks?.beforeSave) {
const modifiedOptions = await this.config.hooks.beforeSave(entityId, saveOptions)
if (modifiedOptions === null) {
// Hook cancelled the save
return result
}
saveOptions = modifiedOptions
}
// Save version
const version = await this.versioningAPI.save(entityId, saveOptions)
this.versionCount++
// Call after-save hook
if (this.config.hooks?.afterSave) {
await this.config.hooks.afterSave(entityId, version)
}
// Auto-prune if enabled
if (this.config.autoPrune) {
await this.pruneEntity(entityId)
}
} catch (error) {
// Don't fail the operation if versioning fails
console.error(`Versioning augmentation error for ${entityId}:`, error)
}
return result
}
/**
* Extract entity ID from operation params
*/
private extractEntityId(operation: string, params: any[]): string | null {
if (operation === 'update') {
// update(id, data) or update({ id, ... })
if (typeof params[0] === 'string') {
return params[0]
}
if (params[0]?.id) {
return params[0].id
}
}
if (operation === 'add') {
// add(data) - ID might be in data or result
if (params[0]?.id) {
return params[0].id
}
}
return null
}
/**
* Check if entity should be versioned based on filters
*/
private async shouldVersionEntity(entityId: string): Promise<boolean> {
// Check exclude patterns first (ID-based)
if (this.config.excludeEntities) {
for (const pattern of this.config.excludeEntities) {
if (this.matchPattern(entityId, pattern)) {
return false
}
}
}
// Check include patterns (ID-based)
if (this.config.entities && this.config.entities.length > 0) {
let matched = false
for (const pattern of this.config.entities) {
if (this.matchPattern(entityId, pattern)) {
matched = true
break
}
}
if (!matched) return false
}
// Check entity type filters (requires fetching entity)
if (
(this.config.types && this.config.types.length > 0) ||
(this.config.excludeTypes && this.config.excludeTypes.length > 0)
) {
try {
const entity = await this.brain?.getNounMetadata?.(entityId)
if (!entity) return true // If can't fetch, allow versioning
const entityType = entity.type
// Check exclude types
if (this.config.excludeTypes && this.config.excludeTypes.includes(entityType)) {
return false
}
// Check include types
if (this.config.types && this.config.types.length > 0) {
if (!this.config.types.includes(entityType)) {
return false
}
}
} catch (error) {
// If can't fetch entity, allow versioning (fail open)
console.error(`Failed to fetch entity ${entityId} for type filtering:`, error)
return true
}
}
return true
}
/**
* Simple glob pattern matching
*/
private matchPattern(value: string, pattern: string): boolean {
if (pattern === '*') return true
if (!pattern.includes('*')) return value === pattern
// Convert glob to regex
const regex = new RegExp(
'^' + pattern.replace(/\*/g, '.*').replace(/\?/g, '.') + '$'
)
return regex.test(value)
}
/**
* Prune old versions for an entity
*/
private async pruneEntity(entityId: string): Promise<void> {
if (!this.versioningAPI) return
try {
// Call before-prune hook
if (this.config.hooks?.beforePrune) {
const shouldPrune = await this.config.hooks.beforePrune(entityId)
if (!shouldPrune) return
}
// Prune versions
const result = await this.versioningAPI.prune(entityId, {
keepRecent: this.config.keepRecent,
keepAfter: this.config.keepAfter,
keepTagged: this.config.keepTagged
})
// Call after-prune hook
if (result.deleted > 0 && this.config.hooks?.afterPrune) {
await this.config.hooks.afterPrune(entityId, result.deleted)
}
} catch (error) {
console.error(`Failed to prune versions for ${entityId}:`, error)
}
}
/**
* Start auto-prune timer
*/
private startAutoPrune(): void {
if (this.pruneTimer) {
clearInterval(this.pruneTimer)
}
this.pruneTimer = setInterval(async () => {
await this.pruneAllEntities()
}, this.config.pruneInterval)
}
/**
* Prune all versioned entities
*/
private async pruneAllEntities(): Promise<void> {
if (!this.versioningAPI) return
try {
// Get all versioned entities
const versionIndex = (this.versioningAPI as any).manager.versionIndex
const entities = await versionIndex.getVersionedEntities()
// Prune each entity
for (const entityId of entities) {
if (await this.shouldVersionEntity(entityId)) {
await this.pruneEntity(entityId)
}
}
} catch (error) {
console.error('Auto-prune failed:', error)
}
}
/**
* Get augmentation statistics
*/
getStats(): {
enabled: boolean
versionsCreated: number
entitiesPattern: string[]
excludePattern: string[]
retention: number
} {
return {
enabled: this.config.enabled,
versionsCreated: this.versionCount,
entitiesPattern: this.config.entities || [],
excludePattern: this.config.excludeEntities || [],
retention: this.config.keepRecent || 10
}
}
}
/**
* Factory function for easy augmentation creation
*/
export function createVersioningAugmentation(
config: VersioningAugmentationConfig = {}
): VersioningAugmentation {
return new VersioningAugmentation(config)
}

View file

@ -4334,6 +4334,101 @@ export class Brainy<T = any> implements BrainyInterface<T> {
return this.counts.getStats(options)
}
// ============= INTERNAL VERSIONING API =============
// These methods are used by the versioning system (brain.versions.*)
// They expose internal storage and index operations needed for entity versioning
/**
* Search entities by metadata filters (internal API)
* Used by versioning system for querying version metadata
*
* @param filters - Metadata filter object (same format as `where` in find())
* @returns Array of matching entities
* @internal
*/
async searchByMetadata(filters: Record<string, any>): Promise<any[]> {
await this.ensureInitialized()
const ids = await this.metadataIndex.getIdsForFilter(filters)
if (ids.length === 0) return []
const results: any[] = []
const entitiesMap = await this.batchGet(ids)
for (const id of ids) {
const entity = entitiesMap.get(id)
if (entity) {
results.push(entity)
}
}
return results
}
/**
* Get raw noun metadata (internal API)
* Used by versioning system for reading entity state
*
* @param id - Entity ID
* @returns Noun metadata or null if not found
* @internal
*/
async getNounMetadata(id: string): Promise<any | null> {
await this.ensureInitialized()
return this.storage.getNounMetadata(id)
}
/**
* Save raw noun metadata (internal API)
* Used by versioning system for storing version index entries
*
* @param id - Entity ID
* @param data - Metadata to save
* @internal
*/
async saveNounMetadata(id: string, data: any): Promise<void> {
await this.ensureInitialized()
await this.storage.saveNounMetadata(id, data)
}
/**
* Delete noun metadata (internal API)
* Used by versioning system for removing version index entries
*
* @param id - Entity ID
* @internal
*/
async deleteNounMetadata(id: string): Promise<void> {
await this.ensureInitialized()
await this.storage.deleteNounMetadata(id)
}
/**
* Current branch name (internal API)
* Used by versioning system for branch-aware operations
* @internal
*/
get currentBranch(): string {
return (this.storage as any).currentBranch || 'main'
}
/**
* Reference manager for COW commits (internal API)
* Used by versioning system for commit operations
* @internal
*/
get refManager(): any {
return (this.storage as any).refManager
}
/**
* Storage adapter (internal API)
* Used by versioning system for direct storage access
* Returns BaseStorage which has saveMetadata/getMetadata for key-value storage
* @internal
*/
get storageAdapter(): BaseStorage {
return this.storage
}
// ============= HELPER METHODS =============
/**

View file

@ -1,36 +1,48 @@
/**
* VersionIndex - Fast Version Lookup Using Existing Index Infrastructure (v5.3.0)
* VersionIndex - Pure Key-Value Version Storage (v6.3.0)
*
* Integrates with Brainy's existing index system:
* - Uses MetadataIndexManager for field indexing
* - Leverages UnifiedCache for memory management
* - Uses EntityIdMapper for efficient ID handling
* - Uses ChunkManager for adaptive chunking
* - Leverages Roaring Bitmaps for fast set operations
* Stores version metadata using simple key-value storage:
* - Version list per entity: __version_meta_{entityId}_{branch}
* - Content stored separately by VersionStorage
*
* Version metadata is stored as regular entities with type='_version'
* This allows us to use existing index infrastructure without modification!
*
* Fields indexed:
* - versionEntityId: Entity being versioned
* - versionBranch: Branch version was created on
* - versionNumber: Version number
* - versionTag: Optional user tag
* - versionTimestamp: Creation timestamp
* - versionCommitHash: Commit hash
* Key Design Decisions:
* - Versions are NOT entities (no brain.add())
* - Versions do NOT pollute find() results
* - Simple O(1) lookups per entity
* - Versions per entity is typically small (10-1000)
*
* NO MOCKS - Production implementation
*/
import { BaseStorage } from '../storage/baseStorage.js'
import type { EntityVersion } from './VersionManager.js'
import type { VersionQuery } from './VersionManager.js'
import type { EntityVersion, VersionQuery } from './VersionManager.js'
/**
* VersionIndex - Version lookup and querying using existing indexes
* Internal storage structure for version metadata
*/
interface VersionMetadataStore {
entityId: string
branch: string
versions: VersionEntry[]
}
/**
* Individual version entry in the store
*/
interface VersionEntry {
version: number
timestamp: number
contentHash: string
commitHash?: string
tag?: string
description?: string
author?: string
}
/**
* VersionIndex - Pure key-value version metadata storage
*
* Strategy: Store version metadata as special entities with type='_version'
* This leverages ALL existing index infrastructure automatically!
* Uses simple JSON storage instead of creating entities.
* This ensures versions never appear in find() results.
*/
export class VersionIndex {
private brain: any // Brainy instance
@ -42,8 +54,6 @@ export class VersionIndex {
/**
* Initialize version index
*
* No special setup needed - we use existing entity storage and indexes!
*/
async initialize(): Promise<void> {
if (this.initialized) return
@ -53,93 +63,98 @@ export class VersionIndex {
/**
* Add version to index
*
* Stores version metadata as a special entity with type='_version'
* This automatically indexes it using existing MetadataIndexManager!
* Stores version entry in key-value storage.
* Handles deduplication by content hash.
*
* @param version Version metadata
* @param version Version metadata to store
*/
async addVersion(version: EntityVersion): Promise<void> {
await this.initialize()
// Generate unique ID for version entity
const versionEntityId = this.getVersionEntityId(
version.entityId,
version.version,
version.branch
)
const key = this.getMetaKey(version.entityId, version.branch)
const store = await this.loadStore(key) || {
entityId: version.entityId,
branch: version.branch,
versions: []
}
// Store as special entity with type='state' (version is a snapshot/state)
// This automatically gets indexed by MetadataIndexManager!
await this.brain.saveNounMetadata(versionEntityId, {
id: versionEntityId,
type: 'state', // Use standard 'state' type (version = snapshot state)
name: `Version ${version.version} of ${version.entityId}`,
metadata: {
// Flag to identify as version metadata
_isVersion: true,
// These fields are automatically indexed by MetadataIndexManager
versionEntityId: version.entityId, // Entity being versioned
versionBranch: version.branch, // Branch
versionNumber: version.version, // Version number
versionTag: version.tag, // Optional tag
versionTimestamp: version.timestamp, // Timestamp (indexed with bucketing)
versionCommitHash: version.commitHash, // Commit hash
versionContentHash: version.contentHash, // Content hash
versionAuthor: version.author, // Author
versionDescription: version.description, // Description
versionMetadata: version.metadata // Additional metadata
// Check for duplicate content hash (deduplication)
const existing = store.versions.find(v => v.contentHash === version.contentHash)
if (existing) {
// Update tag/description if provided on duplicate save
let updated = false
if (version.tag && version.tag !== existing.tag) {
existing.tag = version.tag
updated = true
}
if (version.description && version.description !== existing.description) {
existing.description = version.description
updated = true
}
if (updated) {
await this.saveStore(key, store)
}
return
}
// Add new version entry
store.versions.push({
version: version.version,
timestamp: version.timestamp,
contentHash: version.contentHash,
commitHash: version.commitHash,
tag: version.tag,
description: version.description,
author: version.author
})
await this.saveStore(key, store)
}
/**
* Get versions for an entity
*
* Uses existing MetadataIndexManager to query efficiently!
*
* @param query Version query
* @param query Version query with filters
* @returns List of versions (newest first)
*/
async getVersions(query: VersionQuery): Promise<EntityVersion[]> {
await this.initialize()
// Build metadata filter using existing query system
const filters: Record<string, any> = {
type: 'state',
_isVersion: true,
versionEntityId: query.entityId,
versionBranch: query.branch
}
const branch = query.branch || this.brain.currentBranch
const key = this.getMetaKey(query.entityId, branch)
const store = await this.loadStore(key)
if (!store) return []
// Add optional filters
// Convert entries to EntityVersion format
let versions: EntityVersion[] = store.versions.map(entry => ({
version: entry.version,
entityId: store.entityId,
branch: store.branch,
timestamp: entry.timestamp,
contentHash: entry.contentHash,
commitHash: entry.commitHash || '',
tag: entry.tag,
description: entry.description,
author: entry.author
}))
// Apply filters
if (query.tag) {
filters.versionTag = query.tag
versions = versions.filter(v => v.tag === query.tag)
}
if (query.startDate) {
versions = versions.filter(v => v.timestamp >= query.startDate!)
}
if (query.endDate) {
versions = versions.filter(v => v.timestamp <= query.endDate!)
}
// Query using existing search infrastructure
const results = await this.brain.searchByMetadata(filters)
// Convert entities back to EntityVersion format
const versions: EntityVersion[] = []
for (const entity of results) {
const version = this.entityToVersion(entity)
if (version) {
// Filter by date range if specified
if (query.startDate && version.timestamp < query.startDate) continue
if (query.endDate && version.timestamp > query.endDate) continue
versions.push(version)
}
}
// Sort by version number (newest first)
// Sort newest first (highest version number first)
versions.sort((a, b) => b.version - a.version)
// Apply pagination
const start = query.offset || 0
const end = query.limit ? start + query.limit : undefined
return versions.slice(start, end)
}
@ -156,14 +171,8 @@ export class VersionIndex {
version: number,
branch: string
): Promise<EntityVersion | null> {
await this.initialize()
const versionEntityId = this.getVersionEntityId(entityId, version, branch)
const entity = await this.brain.getNounMetadata(versionEntityId)
if (!entity) return null
return this.entityToVersion(entity)
const versions = await this.getVersions({ entityId, branch })
return versions.find(v => v.version === version) || null
}
/**
@ -179,21 +188,8 @@ export class VersionIndex {
tag: string,
branch: string
): Promise<EntityVersion | null> {
await this.initialize()
// Query using existing metadata index
const results = await this.brain.searchByMetadata({
type: 'state',
_isVersion: true,
versionEntityId: entityId,
versionBranch: branch,
versionTag: tag
})
if (results.length === 0) return null
// Return first match (tags should be unique per entity/branch)
return this.entityToVersion(results[0])
const versions = await this.getVersions({ entityId, branch, tag })
return versions[0] || null
}
/**
@ -204,17 +200,9 @@ export class VersionIndex {
* @returns Number of versions
*/
async getVersionCount(entityId: string, branch: string): Promise<number> {
await this.initialize()
// Use existing search infrastructure
const results = await this.brain.searchByMetadata({
type: 'state',
_isVersion: true,
versionEntityId: entityId,
versionBranch: branch
})
return results.length
const key = this.getMetaKey(entityId, branch)
const store = await this.loadStore(key)
return store?.versions.length || 0
}
/**
@ -231,90 +219,17 @@ export class VersionIndex {
): Promise<void> {
await this.initialize()
const versionEntityId = this.getVersionEntityId(entityId, version, branch)
const key = this.getMetaKey(entityId, branch)
const store = await this.loadStore(key)
if (!store) return
// Delete version entity (automatically removed from indexes)
await this.brain.deleteNounMetadata(versionEntityId)
}
const initialLength = store.versions.length
store.versions = store.versions.filter(v => v.version !== version)
/**
* Convert entity to EntityVersion format
*
* @param entity Entity from storage
* @returns EntityVersion or null if invalid
*/
private entityToVersion(entity: any): EntityVersion | null {
if (!entity || !entity.metadata) return null
const m = entity.metadata
if (
!m.versionEntityId ||
!m.versionBranch ||
m.versionNumber === undefined ||
!m.versionCommitHash ||
!m.versionContentHash ||
!m.versionTimestamp
) {
return null
// Only save if something was removed
if (store.versions.length < initialLength) {
await this.saveStore(key, store)
}
return {
version: m.versionNumber,
entityId: m.versionEntityId,
branch: m.versionBranch,
commitHash: m.versionCommitHash,
timestamp: m.versionTimestamp,
contentHash: m.versionContentHash,
tag: m.versionTag,
description: m.versionDescription,
author: m.versionAuthor,
metadata: m.versionMetadata
}
}
/**
* Generate unique ID for version entity
*
* Format: _version:{entityId}:{version}:{branch}
*
* @param entityId Entity ID
* @param version Version number
* @param branch Branch name
* @returns Version entity ID
*/
private getVersionEntityId(
entityId: string,
version: number,
branch: string
): string {
return `_version:${entityId}:${version}:${branch}`
}
/**
* Get all versioned entities (for cleanup/debugging)
*
* @returns List of entity IDs that have versions
*/
async getVersionedEntities(): Promise<string[]> {
await this.initialize()
// Query all version entities
const results = await this.brain.searchByMetadata({
type: 'state',
_isVersion: true
})
// Extract unique entity IDs
const entityIds = new Set<string>()
for (const entity of results) {
const version = this.entityToVersion(entity)
if (version) {
entityIds.add(version.entityId)
}
}
return Array.from(entityIds)
}
/**
@ -325,14 +240,71 @@ export class VersionIndex {
* @returns Number of versions deleted
*/
async clearVersions(entityId: string, branch: string): Promise<number> {
await this.initialize()
const key = this.getMetaKey(entityId, branch)
const store = await this.loadStore(key)
if (!store) return 0
const versions = await this.getVersions({ entityId, branch })
const count = store.versions.length
for (const version of versions) {
await this.removeVersion(entityId, version.version, branch)
// Delete the store by saving null/empty
await this.saveStore(key, { entityId, branch, versions: [] })
return count
}
/**
* Get all versioned entities (for cleanup/debugging)
*
* Note: This is an expensive operation that requires scanning.
* In the simple key-value approach, we don't maintain a global index.
* This method returns an empty array - use storage-level scanning if needed.
*
* @returns Empty array (not supported in simple approach)
*/
async getVersionedEntities(): Promise<string[]> {
// In the simple key-value approach, we don't maintain a global index
// of all versioned entities. This would require scanning storage.
// For most use cases, you know which entities you've versioned.
return []
}
// ============= Private Helpers =============
/**
* Generate storage key for version metadata
*
* @param entityId Entity ID
* @param branch Branch name
* @returns Storage key
*/
private getMetaKey(entityId: string, branch: string): string {
return `__version_meta_${entityId}_${branch}`
}
/**
* Load version store from storage
*
* @param key Storage key
* @returns Version store or null
*/
private async loadStore(key: string): Promise<VersionMetadataStore | null> {
try {
const store = await this.brain.storageAdapter.getMetadata(key)
// Handle empty store
if (!store || !store.versions) return null
return store as VersionMetadataStore
} catch {
return null
}
}
return versions.length
/**
* Save version store to storage
*
* @param key Storage key
* @param store Version store
*/
private async saveStore(key: string, store: VersionMetadataStore): Promise<void> {
await this.brain.storageAdapter.saveMetadata(key, store)
}
}

View file

@ -1,20 +1,26 @@
/**
* VersionManager - Entity-Level Versioning Engine (v5.3.0)
* VersionManager - Entity-Level Versioning Engine (v5.3.0, v6.3.0 fix)
*
* Provides entity-level version control with:
* - save() - Create entity version
* - restore() - Restore entity to specific version
* - restore() - Restore entity to specific version (v6.3.0: now updates all indexes)
* - list() - List all versions of an entity
* - compare() - Deep diff between versions
* - prune() - Remove old versions (retention policies)
*
* Architecture:
* - Hybrid storage: COW commits for full snapshots + version index for fast queries
* Architecture (v6.3.0 - Clean Key-Value Storage):
* - Versions stored as key-value pairs, NOT as entities (no index pollution)
* - Content-addressable: SHA-256 hashing for deduplication
* - Space-efficient: Only stores changed data
* - Space-efficient: Only stores unique content
* - Branch-aware: Versions tied to current branch
* - restore() uses brain.update() to refresh ALL indexes (HNSW, metadata, graph)
*
* NO MOCKS - Production implementation
* Storage keys:
* - Version metadata: __version_meta_{entityId}_{branch}
* - Version content: __system_version_{entityId}_{contentHash}
*
* ZERO-CONFIG - Works automatically with existing storage infrastructure.
* NO MOCKS - Production implementation.
*/
import { BaseStorage } from '../storage/baseStorage.js'
@ -352,8 +358,31 @@ export class VersionManager {
)
}
// Restore entity in storage
await this.brain.saveNounMetadata(entityId, versionedEntity)
// Extract standard fields vs custom metadata
// NounMetadata has: noun, data, createdAt, updatedAt, createdBy, service, confidence, weight
const {
noun,
data,
createdAt,
updatedAt,
createdBy,
service,
confidence,
weight,
...customMetadata
} = versionedEntity
// Use brain.update() to restore - this updates ALL indexes (HNSW, metadata, graph)
// This is critical: saveNounMetadata() only saves to storage without updating indexes
await this.brain.update({
id: entityId,
data: data,
type: noun,
metadata: customMetadata,
confidence: confidence,
weight: weight,
merge: false // Replace entirely, don't merge with existing metadata
})
return targetVersion
}

View file

@ -1,19 +1,18 @@
/**
* VersionStorage - Hybrid Storage for Entity Versions (v5.3.0)
* VersionStorage - Hybrid Storage for Entity Versions (v5.3.0, v6.3.0 fix)
*
* Implements content-addressable storage for entity versions:
* - SHA-256 content hashing for deduplication
* - Stores versions in .brainy/versions/ directory
* - Uses BaseStorage.saveMetadata/getMetadata for storage (v6.3.0)
* - Integrates with COW commit system
* - Space-efficient: Only stores unique content
*
* Storage structure:
* .brainy/versions/
* entities/
* {entityId}/
* {contentHash}.json # Entity version data
* index/
* {entityId}.json # Version index (managed by VersionIndex)
* Storage structure (v6.3.0):
* Version content is stored using system metadata keys:
* __system_version_{entityId}_{contentHash}
*
* This integrates with BaseStorage's routing which places system keys
* in the _system/ directory, keeping version data separate from entities.
*
* NO MOCKS - Production implementation
*/
@ -151,33 +150,35 @@ export class VersionStorage {
}
/**
* Get version storage path
* Get version storage key
*
* Uses __system_ prefix so BaseStorage routes to system storage (_system/ directory)
* This keeps version data separate from entity data.
*
* @param entityId Entity ID
* @param contentHash Content hash
* @returns Storage path
* @returns Storage key for version content
*/
private getVersionPath(entityId: string, contentHash: string): string {
return `versions/entities/${entityId}/${contentHash}.json`
// v6.3.0: Use system-prefixed key for BaseStorage.saveMetadata/getMetadata
// BaseStorage recognizes __system_ prefix and routes to _system/ directory
return `__system_version_${entityId}_${contentHash}`
}
/**
* Check if content exists in storage
*
* @param path Storage path
* @param key Storage key
* @returns True if exists
*/
private async contentExists(path: string): Promise<boolean> {
private async contentExists(key: string): Promise<boolean> {
try {
// Use storage adapter's exists check if available
// v6.3.0: Use getMetadata to check existence
const adapter = this.brain.storageAdapter
if (adapter && typeof adapter.exists === 'function') {
return await adapter.exists(path)
}
if (!adapter) return false
// Fallback: Try to read and catch error
await this.readVersionData(path)
return true
const data = await adapter.getMetadata(key)
return data !== null
} catch {
return false
}
@ -186,11 +187,11 @@ export class VersionStorage {
/**
* Write version data to storage
*
* @param path Storage path
* @param key Storage key
* @param entity Entity data
*/
private async writeVersionData(
path: string,
key: string,
entity: NounMetadata
): Promise<void> {
const adapter = this.brain.storageAdapter
@ -199,67 +200,51 @@ export class VersionStorage {
throw new Error('Storage adapter not available')
}
// Serialize entity data
const data = JSON.stringify(entity, null, 2)
// Write to storage using adapter
if (typeof adapter.writeFile === 'function') {
await adapter.writeFile(path, data)
} else if (typeof adapter.set === 'function') {
await adapter.set(path, data)
} else {
throw new Error('Storage adapter does not support write operations')
}
// v6.3.0: Use saveMetadata for storing version content
// The key is system-prefixed so it routes to _system/ directory
await adapter.saveMetadata(key, entity)
}
/**
* Read version data from storage
*
* @param path Storage path
* @param key Storage key
* @returns Entity data
*/
private async readVersionData(path: string): Promise<NounMetadata> {
private async readVersionData(key: string): Promise<NounMetadata> {
const adapter = this.brain.storageAdapter
if (!adapter) {
throw new Error('Storage adapter not available')
}
// Read from storage using adapter
let data: string
if (typeof adapter.readFile === 'function') {
data = await adapter.readFile(path)
} else if (typeof adapter.get === 'function') {
data = await adapter.get(path)
} else {
throw new Error('Storage adapter does not support read operations')
// v6.3.0: Use getMetadata for reading version content
const entity = await adapter.getMetadata(key)
if (!entity) {
throw new Error(`Version data not found: ${key}`)
}
// Parse entity data
return JSON.parse(data)
return entity
}
/**
* Delete version data from storage
*
* @param path Storage path
* Note: Version content is content-addressed and immutable.
* Deleting the version index entry (via VersionIndex.removeVersion) is sufficient.
* The content may be shared with other versions (same contentHash).
*
* v6.3.0: We don't actually delete version content to avoid breaking
* other versions that may reference the same content hash.
* A separate garbage collection process could clean up unreferenced content.
*
* @param key Storage key (unused - kept for API compatibility)
*/
private async deleteVersionData(path: string): Promise<void> {
const adapter = this.brain.storageAdapter
if (!adapter) {
throw new Error('Storage adapter not available')
}
// Delete from storage using adapter
if (typeof adapter.deleteFile === 'function') {
await adapter.deleteFile(path)
} else if (typeof adapter.delete === 'function') {
await adapter.delete(path)
} else {
throw new Error('Storage adapter does not support delete operations')
}
private async deleteVersionData(_key: string): Promise<void> {
// v6.3.0: Version content is content-addressed and may be shared.
// We don't delete it here to prevent breaking other versions.
// The version INDEX is deleted via VersionIndex.removeVersion().
// A GC process could clean up unreferenced content in the future.
}
}