Skip to content

Function-level K-Means archetype classification only compares 5 of the trained model's 62 dimensions #1157

Closed

Description

@squid-protocol

🛑 Problem Statement

function_data.func_archetype (and every function's in-RAM telemetry["archetype"]) is assigned
by a hand-rolled Euclidean nearest-centroid classifier in signal_processor.py:561-587,
independent of the shared _classify_archetype() helper the file-level classifier uses.

The live feature vector built for every function (signal_processor.py:562-568) has exactly
5 dimensions:

raw_vec = [
    float(s.get("branch", 0)),
    float(s.get("loc", 0)),
    float(s.get("args", 0)),
    float(s.get("keyword_density", 0.0)),
    float(s.get("control_flow_ratio", s.get("cf_ratio", 0.0))),
]

But the pre-trained model it's compared against -- GENERAL_FUNCTION_INFERENCE_MODEL in
analysis_lens.py:1253 (ARCHETYPES_K12, 12 clusters) -- has 62-dimensional
SCALER_MEDIANS, SCALER_IQRS, and centroid vectors. Confirmed by direct inspection:

func SCALER_MEDIANS len: 62
func arch key: ARCHETYPES_K12
func centroid count: 12
fxn_cluster_0 centroid len: 62

The distance loop at signal_processor.py:578:

dist = math.sqrt(sum((a - b) ** 2 for a, b in zip(scaled_vec, centroid)))

uses zip(), which silently truncates to the shorter sequence -- so every function is classified
using only 5 of the 62 dimensions the model was actually trained on. ~92% of the trained
model's discriminating signal is discarded on every single classification.
This isn't a crash
or an empty column (func_archetype is populated on every function) -- it's a confidently-wrong
label, which is worse: nothing signals the classification is untrustworthy.

🎯 Objective

Restore function-archetype classification to actually use the dimensions the model was trained
on (or retrain/ship a model matched to the 5 live features, if 5 was in fact always the intended
scope -- unclear which is true, needs an owner decision, see Acceptance Criteria).

🛠️ Proposed Implementation

  1. Decide the intended scope: either (a) the 5-feature live vector is a regression -- more
    features used to be computed and fed in before some past refactor -- and needs to be restored,
    or (b) the 62-dim model is legacy from a differently-scoped training run and needs to be
    retrained against today's 5-feature (or intentionally-expanded) vector. Check git history
    around GENERAL_FUNCTION_INFERENCE_MODEL's introduction and the raw_vec construction for
    the answer.
  2. Whichever direction is chosen, add a loud failure (not a silent zip() truncation) when a
    live feature vector's length doesn't match the model's trained dimensionality -- e.g.
    raise/log an error and fall back to "Unclassified" rather than quietly comparing a truncated
    vector. This is the same "silent schema drift" failure class as # [BUG/APPSEC] Fix Inert AI Guardrails (Schema Drift in Dev Agent Firewall) #101, AI/LLM detection sensor cluster (llm_api, llm_orchestrator, ai_tools, etc.) is entirely non-functional -- no rule ever produces these signals #313, tabs_vs_spaces conflates a lighthearted structural fact with a risk-exposure score -- move out of RISK_SCHEMA #1147, [Epic] Recurring pattern: keys read by a consumer that no producer ever writes #325 -- this
    repo has hit it enough times that the guard is worth adding once, here, rather than
    re-discovering the next drift by accident.
  3. While fixing this, consider replacing the hand-inlined distance loop with the shared
    _classify_archetype() helper (signal_processor.py:139-166) already used for file-level
    classification, so there's one implementation of "K-Means nearest-centroid classify" instead of
    two that can independently drift (echoes Two independent, disagreeing implementations of the same risk-scoring logic #249's "two disagreeing implementations" lesson).

✅ Acceptance Criteria

  • Root cause of the 5-vs-62 mismatch identified and documented (regression vs. legacy model).
  • Live feature vector and model dimensionality match, verified by a real test (e.g. assert
    len(raw_vec) == len(GENERAL_FUNCTION_INFERENCE_MODEL["SCALER_MEDIANS"])).
  • A dimension mismatch between a live feature vector and its centroid model fails loudly
    instead of silently truncating via zip().

Activity

  1. added
    bug
    ml-ops
    metrics
    data-integrity
    priority: high
    on Aug 9, 2026
  2. squid-protocol commented on Aug 9, 2026

    @squid-protocol
    OwnerAuthor

    Part of epic #1160 (K-Means archetype clustering drift, all 3 tiers).

  3. squid-protocol commented on Sep 16, 2026

    @squid-protocol
    OwnerAuthor

    Following up now that the archetype tower work has landed: #3016 (merged 2026-09-14) went further than the guard added here in #1724 — it replaced GENERAL_FUNCTION_INFERENCE_MODEL outright with a rosetta-governed 38-feature model (5 geometry + 33 pruned per-LOC densities) and rebuilt the live vector in signal_processor.py to match it dimension-for-dimension, so function classification is no longer just guarded against the truncation bug — it's actually using the full feature space again (validated 20,000/20,000 against the offline labels per that PR's description).

    No action needed here; leaving closed. Noting it for the epic (#1160) record.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdata-integritymetricsml-opspriority: high

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions