|
| 1 | +import { decompose, distanceToCentroid, localPCA } from '../core/geometry/manifold'; |
| 2 | +import type { Logger } from '../core/interfaces'; |
| 3 | +import type { ManifoldProvider, ManifoldSnapshot, StateEmbedder } from '../core/kinematics/interfaces'; |
| 4 | +import { norm, subtract } from '../core/kinematics/math'; |
| 5 | +import type { VectorN } from '../core/kinematics/types'; |
| 6 | +import type { Middleware, StepContext, StepResult } from '../core/middleware/types'; |
| 7 | +import type { KinematicsSnapshot } from './kinematics-middleware'; |
| 8 | + |
| 9 | +export interface ManifoldMiddlewareOpts<S> { |
| 10 | + /** Embedder to convert state → vector. */ |
| 11 | + embedder: StateEmbedder<S>; |
| 12 | + /** Corpus geometry provider (vector DB, embedding store, etc.). */ |
| 13 | + manifold: ManifoldProvider; |
| 14 | + /** Number of neighbors for local PCA. Default: 50. */ |
| 15 | + k?: number; |
| 16 | + /** |
| 17 | + * Number of principal components for tangent space. |
| 18 | + * If omitted, auto-selects to explain 80% of variance. |
| 19 | + */ |
| 20 | + topK?: number; |
| 21 | + /** |
| 22 | + * Distance threshold for drift detection. When the nearest neighbor |
| 23 | + * distance exceeds this value, the agent is considered to be drifting |
| 24 | + * off the manifold (in a "data desert"). |
| 25 | + * |
| 26 | + * If omitted, drift detection is disabled (isDrifting always false). |
| 27 | + */ |
| 28 | + driftThreshold?: number; |
| 29 | + /** |
| 30 | + * Action to take when drift is detected. |
| 31 | + * - `'warn'` — annotate `isDrifting: true` in metadata, do not halt (default) |
| 32 | + * - `'halt'` — return `'halt'` to stop the control loop |
| 33 | + */ |
| 34 | + driftAction?: 'warn' | 'halt'; |
| 35 | + /** Optional logger for manifold telemetry. */ |
| 36 | + logger?: Logger; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Advanced middleware that performs Riemannian manifold analysis each step. |
| 41 | + * |
| 42 | + * It embeds the current state, queries the ManifoldProvider for k nearest |
| 43 | + * neighbors, runs local PCA to compute the tangent/normal decomposition, |
| 44 | + * and annotates `ctx.metadata['manifold']` with a `ManifoldSnapshot`. |
| 45 | + * |
| 46 | + * **Velocity source:** If `kinematicsMiddleware` is stacked before this |
| 47 | + * middleware, the EKF-filtered velocity from `metadata['kinematics']` is |
| 48 | + * used. Otherwise, raw velocity is computed as (current - previous embedding). |
| 49 | + * |
| 50 | + * The middleware **observes and annotates** by default. When `driftThreshold` |
| 51 | + * is set, it can also detect when the agent enters a "data desert" (no nearby |
| 52 | + * corpus data). The `driftAction` option controls whether this triggers a |
| 53 | + * halt or just a warning annotation in metadata. |
| 54 | + * |
| 55 | + * **Ordering:** Stack this middleware AFTER `kinematicsMiddleware` so that |
| 56 | + * the filtered velocity is available. |
| 57 | + */ |
| 58 | +export function manifoldMiddleware<S>(opts: ManifoldMiddlewareOpts<S>): Middleware<S> { |
| 59 | + const k = opts.k ?? 50; |
| 60 | + const driftAction = opts.driftAction ?? 'warn'; |
| 61 | + |
| 62 | + let previousEmbedding: VectorN | null = null; |
| 63 | + |
| 64 | + return { |
| 65 | + name: 'manifold', |
| 66 | + |
| 67 | + setup(): Promise<void> { |
| 68 | + previousEmbedding = null; |
| 69 | + return Promise.resolve(); |
| 70 | + }, |
| 71 | + |
| 72 | + async beforeStep(ctx: StepContext<S>): Promise<StepContext<S>> { |
| 73 | + const observation = await opts.embedder.embed(ctx.state); |
| 74 | + |
| 75 | + // PERF: The k-NN query is likely the dominant cost in this middleware. |
| 76 | + // Future work: add caching if position hasn't moved significantly, |
| 77 | + // or accept a timeout option on ManifoldProvider. |
| 78 | + const neighbors = await opts.manifold.knn(observation, k); |
| 79 | + |
| 80 | + // Degenerate case: not enough neighbors for meaningful PCA |
| 81 | + if (neighbors.length < 2) { |
| 82 | + previousEmbedding = observation; |
| 83 | + return { |
| 84 | + ...ctx, |
| 85 | + metadata: { |
| 86 | + ...ctx.metadata, |
| 87 | + manifold: buildDegenerateSnapshot(observation, neighbors.length), |
| 88 | + }, |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + // Run local PCA (Gramian dual trick — O(k³) not O(d³)) |
| 93 | + const geometry = localPCA(neighbors, opts.topK); |
| 94 | + |
| 95 | + // Resolve velocity: prefer EKF-filtered from kinematics middleware |
| 96 | + const velocity = resolveVelocity(ctx, observation, previousEmbedding); |
| 97 | + |
| 98 | + // Decompose velocity into tangent (on-manifold) and normal (drift) |
| 99 | + const { tangent, normal } = geometry.tangentBasis.length > 0 |
| 100 | + ? decompose(velocity, geometry.tangentBasis) |
| 101 | + : { tangent: velocity.map(() => 0), normal: velocity }; |
| 102 | + |
| 103 | + // Compute distance to nearest neighbor |
| 104 | + const nearestDist = nearestNeighborDistance(observation, neighbors); |
| 105 | + |
| 106 | + // Determine if agent is drifting off manifold |
| 107 | + const isDrifting = opts.driftThreshold != null && nearestDist > opts.driftThreshold; |
| 108 | + |
| 109 | + const snapshot: ManifoldSnapshot = { |
| 110 | + velocityTangent: tangent, |
| 111 | + velocityNormal: normal, |
| 112 | + normalDriftMagnitude: norm(normal), |
| 113 | + curvature: geometry.curvature, |
| 114 | + explainedVariance: geometry.explainedVariance, |
| 115 | + distanceToCentroid: distanceToCentroid(observation, geometry.centroid), |
| 116 | + distanceToNearestNeighbor: nearestDist, |
| 117 | + neighborCount: neighbors.length, |
| 118 | + isDrifting, |
| 119 | + }; |
| 120 | + |
| 121 | + previousEmbedding = observation; |
| 122 | + |
| 123 | + if (opts.logger) { |
| 124 | + opts.logger.info( |
| 125 | + { |
| 126 | + manifold: { |
| 127 | + step: ctx.step, |
| 128 | + curvature: parseFloat(geometry.curvature.toFixed(4)), |
| 129 | + explainedVariance: parseFloat(geometry.explainedVariance.toFixed(4)), |
| 130 | + normalDrift: parseFloat(snapshot.normalDriftMagnitude.toFixed(4)), |
| 131 | + nearestNeighbor: parseFloat(nearestDist.toFixed(4)), |
| 132 | + neighbors: neighbors.length, |
| 133 | + isDrifting, |
| 134 | + }, |
| 135 | + }, |
| 136 | + `[Manifold] Step ${ctx.step}: κ=${geometry.curvature.toFixed(4)}, Drift=${snapshot.normalDriftMagnitude.toFixed(4)}, NearestNeighbor=${nearestDist.toFixed(4)}, Drifting=${isDrifting}`, |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + // Halt if drift detected and action is 'halt' |
| 141 | + if (isDrifting && driftAction === 'halt') { |
| 142 | + return 'halt' as unknown as StepContext<S>; |
| 143 | + } |
| 144 | + |
| 145 | + return { |
| 146 | + ...ctx, |
| 147 | + metadata: { ...ctx.metadata, manifold: snapshot }, |
| 148 | + }; |
| 149 | + }, |
| 150 | + |
| 151 | + afterStep(_ctx: StepContext<S>, _result: StepResult<S>): Promise<void> { |
| 152 | + return Promise.resolve(); |
| 153 | + }, |
| 154 | + }; |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Resolve the velocity vector for manifold decomposition. |
| 159 | + * |
| 160 | + * Prefers EKF-filtered velocity from kinematicsMiddleware (metadata['kinematics']). |
| 161 | + * Falls back to raw velocity (current - previous embedding) if kinematics |
| 162 | + * middleware is not present or this is the first step. |
| 163 | + */ |
| 164 | +function resolveVelocity<S>( |
| 165 | + ctx: StepContext<S>, |
| 166 | + currentEmbedding: VectorN, |
| 167 | + previousEmbedding: VectorN | null, |
| 168 | +): VectorN { |
| 169 | + // Try to read EKF-filtered velocity from kinematics middleware |
| 170 | + const kinematicsData = ctx.metadata['kinematics'] as KinematicsSnapshot | undefined; |
| 171 | + if (kinematicsData?.velocity) { |
| 172 | + return kinematicsData.velocity; |
| 173 | + } |
| 174 | + |
| 175 | + // Fallback: raw velocity from consecutive embeddings |
| 176 | + if (previousEmbedding) { |
| 177 | + return subtract(currentEmbedding, previousEmbedding); |
| 178 | + } |
| 179 | + |
| 180 | + // First step: no velocity available |
| 181 | + return currentEmbedding.map(() => 0); |
| 182 | +} |
| 183 | + |
| 184 | +/** |
| 185 | + * Build a ManifoldSnapshot for degenerate cases (too few neighbors). |
| 186 | + */ |
| 187 | +function buildDegenerateSnapshot(observation: VectorN, neighborCount: number): ManifoldSnapshot { |
| 188 | + const d = observation.length; |
| 189 | + return { |
| 190 | + velocityTangent: new Array<number>(d).fill(0), |
| 191 | + velocityNormal: new Array<number>(d).fill(0), |
| 192 | + normalDriftMagnitude: 0, |
| 193 | + curvature: 1, // maximally uncertain |
| 194 | + explainedVariance: 0, |
| 195 | + distanceToCentroid: 0, |
| 196 | + distanceToNearestNeighbor: Infinity, |
| 197 | + neighborCount, |
| 198 | + isDrifting: true, // no neighbors = definitely drifting |
| 199 | + }; |
| 200 | +} |
| 201 | + |
| 202 | +/** |
| 203 | + * Compute the Euclidean distance from a point to its nearest neighbor. |
| 204 | + */ |
| 205 | +function nearestNeighborDistance(point: VectorN, neighbors: VectorN[]): number { |
| 206 | + let minDist = Infinity; |
| 207 | + for (const neighbor of neighbors) { |
| 208 | + const d = norm(subtract(point, neighbor)); |
| 209 | + if (d < minDist) minDist = d; |
| 210 | + } |
| 211 | + return minDist; |
| 212 | +} |
0 commit comments