Repository navigation
fix(clustering): stop undefined-key warnings in SquaredDistance on ragged vectors - #1612
Merged
marcelklehr merged 1 commit intoNov 11, 2026
Conversation
…gged vectors compute() walked count($a) dimensions but read $b[$i] unguarded, so any pair of vectors with different lengths (e.g. a truncated embedding row) flooded the log with "Undefined array key N" warnings during the cron clustering. The loop now walks min(count($a), count($b)) — the dimensions both vectors share — which is also what the old loop already did whenever $a happened to be the shorter side, so the kernel is now symmetric on ragged input and unchanged for well-formed vectors. Adds a small unit test for the equal-length value, the ragged case from the report, symmetry, and empty input. Fixes nextcloud#1611 Signed-off-by: RanaKhan77 <admin@solution9.com>
RanaKhan77
force-pushed
the
fix/squared-distance-ragged-vectors
branch
from
November 10, 2026 22:35
e4177a6 to
fdd25bf
Compare
marcelklehr
approved these changes
Nov 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While watching the cron log on a Recognize install I hit exactly the flood described in #1611 — hundreds of
Undefined array key N in .../lib/Clustering/SquaredDistance.php#50lines per run, with different keys each time.Cause:
compute()bounds its loop oncount($a)but reads$b[$i]unguarded. Any pair of vectors with different lengths (a truncated or older-format embedding row is enough) walks off the end of$bon every single comparison, which during clustering is thousands of times per cron run.Fix: walk
min(count($a), count($b))— the dimensions both vectors share:Nothing changes for well-formed equal-length vectors, and this is already what the old loop did whenever
$ahappened to be the shorter side, so the kernel just becomes symmetric on ragged input instead of warning. I deliberately did not switch to?? 0.0, because padding missing dimensions with zeros would silently change the distances and therefore cluster shapes; treating ragged input as "compare over the shared prefix" keeps results identical wherever they were previously well-defined.Test: new
tests/SquaredDistanceTest.php(plainTestCase, no DB) covering the equal-length value, the ragged case from the report, symmetry between the two argument orders, and empty input. Verified the old code emits the exact reported warning with[0, 3, 7]vs[3]and the new code returns 9.0 silently.Fixes #1611
Personal note: I build self-hosted security scanners in PHP (2MNY Website Vulnerability Scanner), so log-noise bugs like this one are a daily annoyance I'm happy to chase down. Say hi on LinkedIn if you ever compare notes on clustering false positives.