/** * Graph Queries * * Traversal and query operations over the memory knowledge graph. * Provides BFS-based traversal with depth limits, path finding, * contradiction detection, and hub analysis. * * ───────────────────────────────────────────────────────────────── * PERFORMANCE OPTIMIZATIONS (Issue #4) * ───────────────────────────────────────────────────────────────── * * ✅ Batch Loading Elimination of N+1 Queries: * * All traversal methods now use `loadMemoriesByIds()` to batch load * memories instead of individual `loadMemory()` calls. This reduces * query count from O(N) to O(1) per BFS level or operation. * * Before: * - getRelatedContext(): O(N) queries for N neighbors * - findContradictions(): 2×limit queries (one per memory in each pair) * - getHubMemories(): limit queries (one per hub) * - reconstructPath(): O(path_length) queries * * After: * - getRelatedContext(): O(maxDepth) queries (one per BFS level) * - findContradictions(): 1 query (batch load all unique memories) * - getHubMemories(): 1 query (batch load all hubs) * - reconstructPath(): 1 query (batch load entire path) * * Example: BFS with depth=3 and 6 neighbors per level: * - Old: 2 + 12 + 72 = 86 queries * - New: 3 queries (one per level) * - Speedup: ~29x reduction in query count * * Measured Performance (see graph-queries.test.ts): * - Query count reduced by 2-30x depending on graph structure * - Latency improved proportionally to query reduction * - Zero behavioral changes - all existing tests pass * * Implementation: * - loadMemoriesByIds(ids: string[]): Map * - Uses SQL WHERE id IN (...) with placeholders * - Returns Map for O(1) lookup after batch load */ import { GraphQueryOptions, GraphPath, ContradictionPair } from "@massa-th0th/shared"; import type { IGraphStore, MemoryRow, RelatedMemory } from "./types.js"; export type { RelatedMemory, MemoryRow }; export declare class GraphQueries { private graphStore; constructor(graphStore?: IGraphStore); /** * Get related memories using BFS traversal up to maxDepth. * Returns memories ordered by (depth ASC, edge weight DESC). * * Performance: Batch loads all memories per BFS level, reducing * query count from O(N) to O(maxDepth). */ getRelatedContext(memoryId: string, options?: GraphQueryOptions): Promise; /** * Find shortest path between two memories using BFS. * Returns null if no path exists within maxDepth. */ findPath(fromId: string, toId: string, maxDepth?: number): Promise; private reconstructPath; /** * Find all contradiction edges in the graph. * * Performance: Batch loads all memories in one query instead of * 2×limit individual queries. */ findContradictions(limit?: number): Promise; /** * Follow the chain of decisions that led to a given memory. * Traverses DERIVED_FROM, CAUSES, and SUPPORTS edges backwards. */ getDecisionChain(memoryId: string, maxDepth?: number): Promise; /** * Get the most connected memories (hubs) with full memory data. * * Performance: Batch loads all hub memories in one query instead of * limit individual queries. */ getHubMemories(limit?: number): Promise<{ memory: MemoryRow; degree: number; }[]>; /** * Get a compact summary of a memory's neighborhood. * Useful for injecting into LLM context. */ getNeighborhoodSummary(memoryId: string): Promise; /** * Load a single memory by ID. */ private loadMemory; /** * Batch load multiple memories by IDs. * * This eliminates N+1 query pattern in BFS traversal. * Instead of O(N) queries, we do O(1) per BFS level. * * Example: Loading 100 neighbors goes from 100 queries → 1 query. */ private loadMemoriesByIds; /** * Close database connection. */ close(): void; } //# sourceMappingURL=graph-queries.d.ts.map