diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bd88a99a..7f9421e9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,8 @@ All notable changes to SkillEvaluator are documented in this file. including a default Windows setup. - Malformed optional trajectory `final_metrics` leave condition usage unavailable and no longer abort fresh or reused Tier 3 result collection. +- Tier 2 content clustering uses much less memory on skills with many chunks; + similarity scores and clusters are unchanged. - Naming checks reject a trailing newline in skill, workflow, and rule path components, instead of treating the name before that newline as a complete kebab-case match. - LLM retries retain the innermost valid `Retry-After` header through exception diff --git a/src/skillevaluator/deduplication/intra_skill/semantic_clustering.py b/src/skillevaluator/deduplication/intra_skill/semantic_clustering.py index 184a3885d..d4c9d05e6 100644 --- a/src/skillevaluator/deduplication/intra_skill/semantic_clustering.py +++ b/src/skillevaluator/deduplication/intra_skill/semantic_clustering.py @@ -9,6 +9,7 @@ from __future__ import annotations +from array import array from dataclasses import dataclass, field, replace from itertools import combinations from typing import TYPE_CHECKING @@ -108,11 +109,13 @@ def siblings(i: int, j: int) -> bool: return group is not None and group == chunks[j].split_group uf = UnionFind(n) - pair_scores: dict[tuple[int, int], float] = {} + # Row i contains the upper-triangle scores (i, i + 1), ..., (i, n - 1). + # Packed doubles avoid a tuple key and boxed float for every pair. + pair_scores = [array("d") for _ in range(n - 1)] for i, j in combinations(range(n), 2): score = unit_vector_similarity(units[i], units[j]) - pair_scores[(i, j)] = score + pair_scores[i].append(score) if score >= threshold and not siblings(i, j): uf.union(i, j) @@ -122,7 +125,7 @@ def siblings(i: int, j: int) -> bool: continue members = _merge_split_pieces([chunks[i] for i in indices]) - scores = [pair_scores[(i, j)] for i, j in combinations(indices, 2) if not siblings(i, j)] + scores = array("d", (pair_scores[i][j - i - 1] for i, j in combinations(indices, 2) if not siblings(i, j))) clusters.append( ContentCluster( diff --git a/tests/deduplication/intra_skill/test_semantic_clustering.py b/tests/deduplication/intra_skill/test_semantic_clustering.py index d4851ecac..3db9289cc 100644 --- a/tests/deduplication/intra_skill/test_semantic_clustering.py +++ b/tests/deduplication/intra_skill/test_semantic_clustering.py @@ -55,6 +55,23 @@ def test_path_compression(self) -> None: class TestBuildClusters: + def test_separated_cluster_members_keep_their_pair_scores(self, make_chunk) -> None: + chunks = [ + make_chunk(source_file=f"{index}.md", embedding=vector) + for index, vector in enumerate( + [[1.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, -1.0, 0.0], [0.0, 0.6, 0.8], [0.96, 0.28, 0.0]] + ) + ] + + clusters = build_clusters(chunks, threshold=0.75) + + assert [[member.source_file for member in cluster.members] for cluster in clusters] == [ + ["0.md", "4.md"], + ["1.md", "3.md"], + ] + assert [cluster.max_similarity for cluster in clusters] == pytest.approx([0.96, 0.8]) + assert [cluster.avg_similarity for cluster in clusters] == pytest.approx([0.96, 0.8]) + def test_fewer_than_2_chunks_returns_empty(self, make_chunk) -> None: assert build_clusters([make_chunk(embedding=[1.0, 0.0])]) == [] assert build_clusters([]) == []