c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
90 lines
3.3 KiB
Python
90 lines
3.3 KiB
Python
"""Pure truth-subspace alignment functions.
|
|
|
|
No I/O, no database access, no LLM calls — just deterministic math over plain
|
|
python lists. Everything here is NEUTRAL when inputs are missing/empty/zero:
|
|
``truth_score`` returns ``0.5`` and ``truth_factor`` returns ``1.0`` so callers
|
|
that pass nothing leave baseline scoring untouched.
|
|
"""
|
|
|
|
import hashlib
|
|
import math
|
|
from typing import Sequence
|
|
|
|
|
|
def cosine(a: Sequence[float], b: Sequence[float]) -> float:
|
|
"""Cosine similarity of two vectors. Returns 0.0 for a zero/empty vector."""
|
|
if not a or not b:
|
|
return 0.0
|
|
|
|
dot = 0.0
|
|
norm_a = 0.0
|
|
norm_b = 0.0
|
|
for x, y in zip(a, b):
|
|
dot += x * y
|
|
norm_a += x * x
|
|
norm_b += y * y
|
|
|
|
if norm_a == 0.0 or norm_b == 0.0:
|
|
return 0.0
|
|
|
|
return dot / (math.sqrt(norm_a) * math.sqrt(norm_b))
|
|
|
|
|
|
def node_coords(node_vec: Sequence[float], basis_vecs: Sequence[Sequence[float]]) -> list[float]:
|
|
"""Project ``node_vec`` onto each basis vector using cosine similarity.
|
|
|
|
The result is zero-padded to ``len(basis_vecs)`` so the coordinate vector
|
|
always has one entry per basis vector.
|
|
"""
|
|
coords = [cosine(node_vec, basis_vec) for basis_vec in basis_vecs]
|
|
# cosine already yields 0.0 per vector, so length == len(basis_vecs) holds;
|
|
# pad defensively to keep the contract explicit.
|
|
while len(coords) < len(basis_vecs):
|
|
coords.append(0.0)
|
|
return coords
|
|
|
|
|
|
def query_coords(q_vec: Sequence[float], basis_vecs: Sequence[Sequence[float]]) -> list[float]:
|
|
"""Project a query vector onto each basis vector, zero-padded."""
|
|
return node_coords(q_vec, basis_vecs)
|
|
|
|
|
|
def truth_score(node_coords: Sequence[float], q_coords: Sequence[float]) -> float:
|
|
"""Truth score in [0, 1]: the node's alignment with directions the query cares about.
|
|
|
|
A query-relevance-weighted average of the node's per-direction alignments, using
|
|
the (clamped) query coordinates as weights. This is magnitude-sensitive on
|
|
purpose: a node strongly aligned with those directions scores higher. Cosine of the
|
|
two coord vectors does NOT work here — every basis cosine is positive, so all
|
|
coord vectors share one octant and their cosine collapses to ~1 regardless of
|
|
magnitude, erasing the very signal we rank on.
|
|
|
|
Returns ``0.5`` (NEUTRAL) when either coord vector is empty, or when the query
|
|
aligns with no direction (no weight to spread).
|
|
"""
|
|
if not node_coords or not q_coords:
|
|
return 0.5
|
|
|
|
weights = [max(float(q), 0.0) for q in q_coords]
|
|
total_weight = sum(weights)
|
|
if total_weight == 0.0:
|
|
return 0.5
|
|
|
|
weighted = sum(float(n) * w for n, w in zip(node_coords, weights))
|
|
return max(0.0, min(1.0, weighted / total_weight))
|
|
|
|
|
|
def truth_factor(node_coords: Sequence[float], q_coords: Sequence[float]) -> float:
|
|
"""Multiplicative score factor in [0.75, 1.25].
|
|
|
|
``0.75 + 0.5 * truth_score``. Returns ``1.0`` (NEUTRAL) when coords are
|
|
missing/zero, since ``truth_score`` is ``0.5`` there.
|
|
"""
|
|
return 0.75 + 0.5 * truth_score(node_coords, q_coords)
|
|
|
|
|
|
def stable_signature(ordered_ids: Sequence[object]) -> str:
|
|
"""Stable sha256 signature of an ordered id sequence."""
|
|
joined = "|".join(str(item_id) for item_id in ordered_ids)
|
|
return hashlib.sha256(joined.encode("utf-8")).hexdigest()
|