numa/vector

模块 zio.vector — semantic index over the same experience records · 15 个符号

Optional third index. A vector record is:

{:memory/id "s-1" :memory/encoder "bge-small"
:memory/space-version 2 :memory/vector [10 20 30]}

A vector is a similarity hint, never proof of equivalence and never evidence of correctness or permission (design §7). Two rules make it safe to keep:

1. A vector is bound to an encoder id and a space version. Comparing
vectors across space versions is refused — the coordinates mean
different things, so a cosine between them is noise, not a
measurement.
2. A space change invalidates the index. The space cannot be mutated
under live vectors; either the vectors are dropped, or an explicit
migration re-encodes them and records what it did.

Similarity is signed integer basis points (-10000..10000), rounded to the nearest basis point. Zero vectors have similarity zero.

概览

vector--scale

(def vector--scale …)

vector--scale — basis-point scale factor: similarity 1.0 = 10000.

vector--check-space

(defn vector--check-space [records space-version] …)

(vector--check-space records space-version) — true, or a non-empty refusal string when any record's :memory/space-version differs.

vector--check-encoder

(defn vector--check-encoder [records encoder] …)

(vector--check-encoder records encoder) — true, or a non-empty refusal string when any record's :memory/encoder differs.

vector--finite

(defn vector--finite [x] …)

(vector--finite x) — x when it is a finite number (ordering (<= x x) rejects NaN/inf); error otherwise.

vector--validate

(defn vector--validate [a] …)

(vector--validate a) — a when it is a vector of finite coordinates; shape-error otherwise.

vector--same-shape

(defn vector--same-shape [a b] …)

(vector--same-shape a b) — validates both vectors and errors with shape-error when their dimensions differ.

vector--dot

(defn vector--dot [a b] …)

(vector--dot a b) — dot product of same-shape vectors, checked finite.

vector--norm

(defn vector--norm [a] …)

(vector--norm a) — squared Euclidean norm of a (kept as the public contract despite the name).

vector--max-abs

(defn vector--max-abs [a] …)

(vector--max-abs a) — largest absolute coordinate of a, 0.0 when all coordinates are zero.

vector--round-bp

(defn vector--round-bp [x] …)

(vector--round-bp x) — rounds x to signed integer basis points, clamped to ±vector--scale, via binary search on the magnitude.

vector--cosine-bp

(defn vector--cosine-bp [a b] …)

(vector--cosine-bp a b) — cosine similarity of same-shape vectors as integer basis points; 0 for a zero vector. Both vectors are rescaled by max-abs before squaring to avoid float overflow/underflow.

vector--nearest

(defn vector--nearest [records query space-version] …)

(vector--nearest records query space-version) — [similarity-bp record] for the record most similar to query, or nil when records is empty. Refuses (returning the refusal string) when the query's space version differs from space-version, the index mixes versions, or the encoder mismatches; the caller must test the result with (string? ...).

vector--threshold-bp

(defn vector--threshold-bp [bp threshold-bp] …)

(vector--threshold-bp bp threshold-bp) — true when similarity bp meets or exceeds threshold-bp.

vector--migrate

(defn vector--migrate [record new-space re-encoder] …)

(vector--migrate record new-space re-encoder) — the only sanctioned path across a space change: re-encodes via re-encoder, validates the new coordinates, and stamps :memory/space-version/:memory/encoder from new-space plus :memory/migrated-from with the old version.

vector--invalidate

(defn vector--invalidate [records] …)

(vector--invalidate records) — always returns an empty vector: the sanctioned way to drop an index after a space change.