/** * RPTree — Random Projection Tree for hierarchical memory clustering * * Implements a simplified Random Projection Tree (RPTree) that partitions * a set of observation embeddings into a hierarchical cluster structure. * Each leaf node contains a group of semantically-similar observations. * * Algorithm: * 1. Generate a random projection vector * 2. Compute dot products of all embeddings with the projection * 3. Split at the median (left = below, right = above) * 4. Recurse until min_leaf_size or max_depth is reached * * Trees are rebuilt each dream cycle (brain_memory_trees table is truncated * and repopulated). This is intentional — trees are a cache, not a source * of truth. * * Persistence: tree nodes are written to brain_memory_trees. The leaf node * id is written back to brain_observations.tree_id. * * @task T1146 * @epic T1146 */ import type { DatabaseSync } from 'node:sqlite'; /** An observation with its embedding for tree construction. */ export interface ObservationForTree { id: string; embedding: number[]; } /** A single RPTree node (internal or leaf). */ export interface RPTreeNode { /** Internal id (assigned after persistence). */ dbId?: number; /** Depth in the tree (0 = root). */ depth: number; /** Observation IDs in this leaf. Empty for internal nodes. */ leafIds: string[]; /** Centroid vector (average of all embeddings in this node's subtree). */ centroid: number[] | null; /** Parent node's dbId. Null for root. */ parentDbId: number | null; /** Child nodes (in-memory only; not stored as rows). */ children: RPTreeNode[]; } /** Options for {@link buildSurprisalTree}. */ export interface BuildTreeOptions { /** Max depth. Default {@link MAX_DEPTH}. */ maxDepth?: number; /** Min leaf size. Default {@link MIN_LEAF_SIZE}. */ minLeafSize?: number; /** Inject a DatabaseSync for testing. */ db?: DatabaseSync | null; } /** Result of a tree build. */ export interface BuildTreeResult { /** Number of tree nodes written to brain_memory_trees. */ nodesWritten: number; /** Number of brain_observations.tree_id values updated. */ obsAssigned: number; /** Max depth actually reached. */ actualMaxDepth: number; } /** * Build a Random Projection Tree from a set of observations and persist it * to brain_memory_trees. Updates brain_observations.tree_id for all leaf members. * * Truncates existing brain_memory_trees rows before writing (trees are * rebuilt each dream cycle from scratch). * * Requires at least 2 observations. Returns early with zero counts if fewer * are provided. * * @param observations - Set of observations with embeddings to cluster. * @param options - Depth/leaf config, db injection for tests. * @returns Build result with node/assignment counts. * * @task T1146 */ export declare function buildSurprisalTree(observations: ObservationForTree[], options?: BuildTreeOptions): BuildTreeResult; //# sourceMappingURL=surprisal-tree.d.ts.map