Skip to content

Commit fe1b5ef

Browse files
committed
Rename Vector3D to VectorN
1 parent 1a71839 commit fe1b5ef

9 files changed

Lines changed: 58 additions & 55 deletions

File tree

docs/specs/TECHNICAL_SPEC_v2.1.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,26 @@ $$
7474

7575
## 3. Implementation Interfaces (The Bridge)
7676

77-
We need to bridge the generic `State` from v1.0 with the rigorous `Vector3D` of v2.1.
77+
We need to bridge the generic `State` from v1.0 with the rigorous `VectorN` of v2.1.
7878

7979
### 3.1 The Kinematics Type Definition
8080

8181
Create these in `src/core/kinematics/types.ts`.
8282

8383
```typescript
84-
export type Vector3D = number[]; // Embedding vector
84+
export type VectorN = number[]; // N-dimensional embedding vector
8585

8686
// The Physics State (Hidden from the generic Orchestrator)
8787
export interface KinematicState {
88-
position: Vector3D; // S_i (Filtered)
89-
velocity: Vector3D; // v_i
90-
heading: Vector3D; // D_i
88+
position: VectorN; // S_i (Filtered)
89+
velocity: VectorN; // v_i
90+
heading: VectorN; // D_i
9191
stepIndex: number;
9292
}
9393

9494
// The Control Signal returned by the PID controller
9595
export interface ControlSignal {
96-
correctionVector: Vector3D; // u(i)
96+
correctionVector: VectorN; // u(i)
9797
magnitude: number; // How strong the correction is (0-1)
9898
isStable: boolean; // Should we stop?
9999
log: string; // Explanation for debug traces
@@ -113,7 +113,7 @@ import { State } from '../types';
113113

114114
// Adapters must implement this to translate their Domain State (JSON) into Physics State (Vector)
115115
export interface StateEmbedder<S extends State> {
116-
embed(state: S): Promise<Vector3D>;
116+
embed(state: S): Promise<VectorN>;
117117
}
118118

119119
// The v2.1 Configuration
@@ -138,8 +138,8 @@ A pure, deterministic class that holds the math. It does not know about LLMs or
138138
class PhysicsEngine {
139139
update(
140140
prev: KinematicState,
141-
observation: Vector3D
142-
): { next: KinematicState; error: Vector3D } {
141+
observation: VectorN
142+
): { next: KinematicState; error: VectorN } {
143143
// 1. EKF Predict & Update
144144
// 2. Calculate Heading D_i
145145
// 3. Calculate Cross-track Error e_i (Vector Rejection)
@@ -163,7 +163,7 @@ class KinematicProbePolicy<S> implements ProbePolicy<S, Action, Feedback> {
163163
) {}
164164

165165
async decide(state: S, ladder: Ladder): Promise<Action> {
166-
// 1. Convert generic State -> Vector3D (using Embedder)
166+
// 1. Convert generic State -> VectorN (using Embedder)
167167
const observation = await this.embedder.embed(state);
168168

169169
// 2. Update Physics Engine
@@ -194,7 +194,7 @@ class KinematicProbePolicy<S> implements ProbePolicy<S, Action, Feedback> {
194194
When implementing v2.1, follow this sequence to avoid breaking existing code:
195195

196196
1. **Scaffold**: Create `src/core/kinematics/` folder.
197-
2. **Math First**: Implement `Vector3D` operations (dot product, norm, projection, rejection) in `src/core/kinematics/math.ts`. **Do not use external heavy math libs**, keep it lightweight.
197+
2. **Math First**: Implement `VectorN` operations (dot product, norm, projection, rejection) in `src/core/geometry/vector.ts` (previously `src/core/kinematics/math.ts`). **Do not use external heavy math libs**, keep it lightweight.
198198
3. **Engine**: Implement `PhysicsEngine` with EKF and Heading logic.
199199
4. **Controller**: Implement `PIDController`.
200200
5. **Integration**: Create `KinematicProbePolicy`.
@@ -219,7 +219,7 @@ const controlled = cyberloop(agent, {
219219
middleware: [
220220
kinematicsMiddleware({
221221
embedder, // StateEmbedder<S> — same as Section 3.2
222-
goalEmbedding, // Vector3D — task origin τ
222+
goalEmbedding, // VectorN — task origin τ
223223
pid: { Kp: 0.5, Ki: 0.0, Kd: 0.1, stabilityThreshold: 0.6 },
224224
physics: { processNoise: 0.1, measureNoise: 0.5 },
225225
}),

docs/specs/project-ariadne/wikipedia-deep-dive-agent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ This adapter implements the standard AICL interfaces (`Environment`, `StateEmbed
4141
2. **Embedder (`WikipediaEmbedder`)**:
4242
* **Input:** `State.summary` + `State.goal`.
4343
* **Model:** `text-embedding-3-small` (Fast, cheap, effective).
44-
* **Output:** `Vector3D`.
44+
* **Output:** `VectorN`.
4545

4646
3. **Policy (`SemanticNavigationPolicy`)**:
4747
* **Role:** Deterministic Inner Loop.

src/advanced/kinematics-middleware.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { PhysicsEngine } from '../core/kinematics/engine';
33
import type { StateEmbedder } from '../core/kinematics/interfaces';
44
import { norm } from '../core/kinematics/math';
55
import { PIDController } from '../core/kinematics/pid';
6-
import type { KinematicState, Vector3D } from '../core/kinematics/types';
6+
import type { KinematicState, VectorN } from '../core/kinematics/types';
77
import type { Middleware, StepContext, StepResult } from '../core/middleware/types';
88

99
/**
1010
* Kinematics data attached to `ctx.metadata['kinematics']` each step.
1111
*/
1212
export interface KinematicsSnapshot {
13-
position: Vector3D;
14-
velocity: Vector3D;
15-
error: Vector3D;
13+
position: VectorN;
14+
velocity: VectorN;
15+
error: VectorN;
1616
errorMagnitude: number;
1717
correctionMagnitude: number;
1818
coherenceAngleDeg: number;
@@ -24,7 +24,7 @@ export interface KinematicsSnapshot {
2424
* Correction info attached to `ctx.metadata['kinematicsCorrection']` when drift is detected.
2525
*/
2626
export interface CorrectionInfo {
27-
vector: Vector3D;
27+
vector: VectorN;
2828
magnitude: number;
2929
log: string;
3030
}
@@ -77,7 +77,7 @@ export function kinematicsMiddleware<S>(opts: KinematicsMiddlewareOpts<S>): Midd
7777
const engine = new PhysicsEngine({ ProcessNoise: processNoise, MeasureNoise: measureNoise, PID: { Kp, Ki, Kd }, MaxDeviation: stabilityThreshold });
7878
const pid = new PIDController(Kp, Ki, Kd, stabilityThreshold);
7979

80-
let origin: Vector3D | null = null;
80+
let origin: VectorN | null = null;
8181
let lastPhysicsState: KinematicState | null = null;
8282

8383
return {

src/core/geometry/vector.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
* Euclidean vector operations for semantic embedding spaces.
33
*
44
* This module provides the foundational vector math used by CyberLoop's
5-
* control layers. It operates on arbitrary-dimensional vectors (typed as
6-
* `Vector3D` for historical reasons — the name will be aliased in a future
7-
* version).
5+
* control layers. It operates on arbitrary-dimensional vectors typed as
6+
* `VectorN`.
87
*
98
* **v2.1:** Used by PhysicsEngine (EKF) and PIDController.
109
* **v3.0:** Will be extended by `manifold.ts` (PCA, curvature, tangent plane).
@@ -15,75 +14,75 @@
1514

1615
import { Matrix } from 'ml-matrix';
1716

18-
import type { Vector3D } from '../kinematics/types';
17+
import type { VectorN } from '../kinematics/types';
1918

2019
// Convert array to Matrix (column vector)
21-
export const toMatrix = (v: Vector3D): Matrix => {
20+
export const toMatrix = (v: VectorN): Matrix => {
2221
return new Matrix([v]).transpose();
2322
};
2423

2524
// Convert Matrix to array
26-
export const toVector = (m: Matrix): Vector3D => {
25+
export const toVector = (m: Matrix): VectorN => {
2726
return m.to1DArray();
2827
};
2928

30-
export const add = (v1: Vector3D, v2: Vector3D): Vector3D => {
29+
export const add = (v1: VectorN, v2: VectorN): VectorN => {
3130
const m1 = toMatrix(v1);
3231
const m2 = toMatrix(v2);
3332
return toVector(Matrix.add(m1, m2));
3433
};
3534

36-
export const subtract = (v1: Vector3D, v2: Vector3D): Vector3D => {
35+
export const subtract = (v1: VectorN, v2: VectorN): VectorN => {
3736
const m1 = toMatrix(v1);
3837
const m2 = toMatrix(v2);
3938
return toVector(Matrix.sub(m1, m2));
4039
};
4140

42-
export const scale = (v: Vector3D, scalar: number): Vector3D => {
41+
export const scale = (v: VectorN, scalar: number): VectorN => {
4342
const m = toMatrix(v);
4443
return toVector(Matrix.mul(m, scalar));
4544
};
4645

47-
export const dot = (v1: Vector3D, v2: Vector3D): number => {
46+
export const dot = (v1: VectorN, v2: VectorN): number => {
4847
const m1 = toMatrix(v1);
4948
const m2 = toMatrix(v2);
5049
// Dot product is v1^T * v2
5150
return m1.transpose().mmul(m2).get(0, 0);
5251
};
5352

54-
export const norm = (v: Vector3D): number => {
53+
export const norm = (v: VectorN): number => {
5554
const m = toMatrix(v);
5655
return m.norm('frobenius'); // Euclidean norm
5756
};
5857

59-
export const normalize = (v: Vector3D): Vector3D => {
58+
export const normalize = (v: VectorN): VectorN => {
6059
const n = norm(v);
6160
if (n === 0) return v.map(() => 0);
6261
return scale(v, 1 / n);
6362
};
6463

6564
// Projection of a onto b: proj_b(a) = (a . b / |b|^2) * b
66-
export const project = (a: Vector3D, b: Vector3D): Vector3D => {
65+
export const project = (a: VectorN, b: VectorN): VectorN => {
6766
const bNormSq = dot(b, b);
6867
if (bNormSq === 0) return b.map(() => 0);
6968
const scalar = dot(a, b) / bNormSq;
7069
return scale(b, scalar);
7170
};
7271

7372
// Rejection of a from b: a - proj_b(a)
74-
export const reject = (a: Vector3D, b: Vector3D): Vector3D => {
73+
export const reject = (a: VectorN, b: VectorN): VectorN => {
7574
const proj = project(a, b);
7675
return subtract(a, proj);
7776
};
7877

79-
export const cosineSimilarity = (v1: Vector3D, v2: Vector3D): number => {
78+
export const cosineSimilarity = (v1: VectorN, v2: VectorN): number => {
8079
const n1 = norm(v1);
8180
const n2 = norm(v2);
8281
if (n1 === 0 || n2 === 0) return 0;
8382
return dot(v1, v2) / (n1 * n2);
8483
};
8584

86-
export const angleBetween = (v1: Vector3D, v2: Vector3D): number => {
85+
export const angleBetween = (v1: VectorN, v2: VectorN): number => {
8786
const cos = Math.max(-1, Math.min(1, cosineSimilarity(v1, v2)));
8887
return Math.acos(cos);
8988
};

src/core/kinematics/engine.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { KinematicsConfig } from './interfaces';
22
import { add, angleBetween, norm, reject, scale, subtract } from './math';
3-
import type { KinematicState, Vector3D } from './types';
3+
import type { KinematicState, VectorN } from './types';
44

55
export class PhysicsEngine {
66
constructor(private config: KinematicsConfig) { }
@@ -11,9 +11,9 @@ export class PhysicsEngine {
1111
*/
1212
update(
1313
prev: KinematicState,
14-
observation: Vector3D,
15-
origin: Vector3D
16-
): { next: KinematicState; error: Vector3D; coherence: number } {
14+
observation: VectorN,
15+
origin: VectorN
16+
): { next: KinematicState; error: VectorN; coherence: number } {
1717
// 1. Predict (Simple Motion Model: assume constant velocity)
1818
const s_pred = add(prev.position, prev.velocity);
1919

@@ -54,7 +54,7 @@ export class PhysicsEngine {
5454
const coherence = hasMomentum ? angleBetween(heading, prevHeading) : 0;
5555

5656
// 5. Calculate Cross-track Error (Vector Rejection)
57-
let error: Vector3D;
57+
let error: VectorN;
5858

5959
if (!hasMomentum) {
6060
// No previous track to follow, so error is zero.

src/core/kinematics/interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { State } from '../types';
2-
import type { Vector3D } from './types';
2+
import type { VectorN } from './types';
33

44
// Adapters must implement this to translate their Domain State (JSON) into Physics State (Vector)
55
// This allows the user to define any embedding strategy (OpenAI, local BERT, random projection, etc.)
66
export interface StateEmbedder<S extends State> {
7-
embed(state: S): Promise<Vector3D>;
7+
embed(state: S): Promise<VectorN>;
88
}
99

1010
// The v2.1 Configuration

src/core/kinematics/pid.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { add, norm, scale, subtract } from './math';
2-
import type { ControlSignal, Vector3D } from './types';
2+
import type { ControlSignal, VectorN } from './types';
33

44
export class PIDController {
5-
private integral: Vector3D;
6-
private lastError: Vector3D | null = null;
5+
private integral: VectorN;
6+
private lastError: VectorN | null = null;
77

88
constructor(
99
private Kp: number,
@@ -14,7 +14,7 @@ export class PIDController {
1414
this.integral = []; // Initialize empty, will adapt to dimension on first call
1515
}
1616

17-
compute(error: Vector3D, dt = 1): ControlSignal {
17+
compute(error: VectorN, dt = 1): ControlSignal {
1818
// Initialize integral term if needed
1919
if (this.integral.length === 0) {
2020
this.integral = error.map(() => 0);

src/core/kinematics/policy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import type { PhysicsEngine } from './engine';
44
import type { StateEmbedder } from './interfaces';
55
import { norm } from './math';
66
import type { PIDController } from './pid';
7-
import type { CorrectionAction, KinematicState, Vector3D } from './types';
7+
import type { CorrectionAction, KinematicState, VectorN } from './types';
88

99
export class KinematicProbePolicy<S extends State, A extends Action, F extends Feedback> implements ProbePolicy<S, A, F> {
1010
public id = 'kinematic-policy';
11-
private origin: Vector3D | null = null;
11+
private origin: VectorN | null = null;
1212
private lastPhysicsState: KinematicState | null = null;
1313

1414
constructor(
@@ -35,7 +35,7 @@ export class KinematicProbePolicy<S extends State, A extends Action, F extends F
3535
}
3636

3737
async decide(state: S, ladder: Ladder<F>): Promise<A> {
38-
// 1. Convert generic State -> Vector3D
38+
// 1. Convert generic State -> VectorN
3939
const observation = await this.embedder.embed(state);
4040

4141
// Initialize Origin if this is the first step after initialize()

src/core/kinematics/types.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
export type Vector3D = number[]; // Embedding vector - named Vector3D but can be N-dimensional
1+
/** An N-dimensional embedding vector. */
2+
export type VectorN = number[];
3+
4+
/** @deprecated Use `VectorN` instead. Alias kept for backward compatibility. */
5+
export type Vector3D = VectorN;
26

37
// The Physics State (Hidden from the generic Orchestrator)
48
export interface KinematicState {
5-
position: Vector3D; // S_i (Filtered)
6-
velocity: Vector3D; // v_i
7-
heading: Vector3D; // D_i
9+
position: VectorN; // S_i (Filtered)
10+
velocity: VectorN; // v_i
11+
heading: VectorN; // D_i
812
stepIndex: number;
913
}
1014

1115
// The Control Signal returned by the PID controller
1216
export interface ControlSignal {
13-
correctionVector: Vector3D; // u(i)
17+
correctionVector: VectorN; // u(i)
1418
magnitude: number; // How strong the correction is (0-1)
1519
isStable: boolean; // Should we stop?
1620
log: string; // Explanation for debug traces
1721
}
1822

1923
export interface CorrectionAction {
2024
type: 'CORRECTION';
21-
vector: Vector3D;
25+
vector: VectorN;
2226
magnitude: number;
2327
log: string;
2428
}

0 commit comments

Comments
 (0)