siddhm11 Claude Opus 5.5 commited on
Commit
ce5bcb2
·
1 Parent(s): 826b5f4

fix(clustering): a new interest no longer inherits a rejected interest's id (audit F6)

Browse files

stabilize_cluster_ids rejects Hungarian matches below cosine 0.5 so a new
topic cannot take an old identity, then gave the unmatched cluster the
smallest free id, usually the very id it had just been refused. Fresh ids
now start above every old one. Card colour is cluster_idx % N_TONES, so
larger ids are safe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

app/recommend/clustering.py CHANGED
@@ -420,7 +420,7 @@ def stabilize_cluster_ids(
420
  2. Solve with scipy linear_sum_assignment (O(K³), trivial for K ≤ 7)
421
  3. Matched pairs with cosine_sim >= min_cosine_sim inherit the old idx
422
  4. Weak matches (cosine_sim < min_cosine_sim) and unmatched new clusters
423
- get the next available index
424
 
425
  Args:
426
  new_clusters: freshly computed clusters (cluster_idx values ignored)
@@ -459,7 +459,10 @@ def stabilize_cluster_ids(
459
  new_to_stable[int(r)] = old_clusters[int(c)].cluster_idx
460
 
461
  used_ids: set[int] = set(new_to_stable.values())
462
- next_id = 0
 
 
 
463
 
464
  result: list[InterestCluster] = []
465
  for i, cluster in enumerate(new_clusters):
 
420
  2. Solve with scipy linear_sum_assignment (O(K³), trivial for K ≤ 7)
421
  3. Matched pairs with cosine_sim >= min_cosine_sim inherit the old idx
422
  4. Weak matches (cosine_sim < min_cosine_sim) and unmatched new clusters
423
+ get a fresh index above every old one
424
 
425
  Args:
426
  new_clusters: freshly computed clusters (cluster_idx values ignored)
 
459
  new_to_stable[int(r)] = old_clusters[int(c)].cluster_idx
460
 
461
  used_ids: set[int] = set(new_to_stable.values())
462
+ # Fresh ids start above every old one. Starting at 0 handed an unmatched
463
+ # cluster the id of the old cluster it had just been rejected as unrelated
464
+ # to (audit F6), joining two interests in snapshots and analytics.
465
+ next_id = max([c.cluster_idx for c in old_clusters] + list(used_ids)) + 1
466
 
467
  result: list[InterestCluster] = []
468
  for i, cluster in enumerate(new_clusters):
tests/test_clustering.py CHANGED
@@ -465,3 +465,27 @@ def test_per_save_clusters_make_each_save_an_interest():
465
  assert [c.medoid_paper_id for c in clusters] == ["t0_0", "t1_0", "t2_0"]
466
  assert clusters[0].importance > clusters[1].importance > clusters[2].importance
467
  assert np.allclose(clusters[1].medoid_embedding, embs[1])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465
  assert [c.medoid_paper_id for c in clusters] == ["t0_0", "t1_0", "t2_0"]
466
  assert clusters[0].importance > clusters[1].importance > clusters[2].importance
467
  assert np.allclose(clusters[1].medoid_embedding, embs[1])
468
+
469
+
470
+ def test_a_new_interest_does_not_inherit_a_rejected_interests_id():
471
+ """Audit F6. Old interests NLP (0) and RL (1); the reader's new clusters are
472
+ NLP and vision. Hungarian matching pairs vision with RL, rejects the pair
473
+ for low similarity, and vision then took the smallest free id -- RL's 1 --
474
+ so snapshots, analytics and card colour joined two unrelated interests."""
475
+ rng = np.random.default_rng(6)
476
+ nlp, rl, vision = (rng.normal(size=1024) for _ in range(3))
477
+ vision -= (vision @ rl) / (rl @ rl) * rl # orthogonal to RL: cos 0
478
+
479
+ old = [InterestCluster(cluster_idx=0, medoid_paper_id="nlp", medoid_embedding=nlp,
480
+ paper_ids=["nlp"], importance=5.0),
481
+ InterestCluster(cluster_idx=1, medoid_paper_id="rl", medoid_embedding=rl,
482
+ paper_ids=["rl"], importance=3.0)]
483
+ new = [InterestCluster(cluster_idx=0, medoid_paper_id="nlp2", medoid_embedding=nlp + 0.01,
484
+ paper_ids=["nlp2"], importance=5.0),
485
+ InterestCluster(cluster_idx=1, medoid_paper_id="vision", medoid_embedding=vision,
486
+ paper_ids=["vision"], importance=3.0)]
487
+
488
+ ids = {c.medoid_paper_id: c.cluster_idx for c in stabilize_cluster_ids(new, old)}
489
+
490
+ assert ids["nlp2"] == 0
491
+ assert ids["vision"] not in {0, 1}, f"vision reused an old id: {ids}"