Skip to content

fix(ai-ml): anchor AI/ML import-pack rules on the top-level package (#4150) - #4231

Merged
squid-protocol merged 2 commits into
mainfrom
fix/4150-4114-ai-import-and-feature-flags
Oct 3, 2026
Merged

squid-protocol merged 2 commits into
mainfrom
fix/4150-4114-ai-import-and-feature-flags

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Bug-sweep PR: fixes #4150.

#4150: AI/ML import-pack rules matched library names anywhere on an import line

Root cause. llm_orchestrator, llm_vector_store, ml_traditional and dl_frameworks were all built from _IMPORT_WRAPPER = r"\b(?:import|require|from)\b.*?(?:{names})\b", which matches when the name appears anywhere on an import line. PR #4149 fixed the same thing for llm_api (#4137) but left these four alone.

Fix. _shared_patterns.py: #4149's two pattern shapes become builders, _py_package_import(packages) and _js_package_import(packages). PY_LLM_API / JS_LLM_API are rebuilt through them; their compiled patterns and flags are verified byte-identical. Each of the four rules gets a PY_* (python) and JS_* (javascript, typescript) pattern. Python requires the module after from (or an item after import) to be the package, anchored at a statement start. JS/TS requires the quoted specifier to name the package, so relative and alias paths never match. _IMPORT_WRAPPER and the four GLOBAL_* constants are removed.

Ownership decisions, which the issue asked to make explicit:

  • Python langchain_* (langchain_core, langchain_community, langchain_google_vertexai, ...) is now llm_orchestrator. This mirrors js/ts, where the whole @langchain/* scope always counted. As a result, langchain_openai / langchain_anthropic count under both llm_orchestrator and llm_api (llm_api: anchor on the SDK's top-level package, not provider-named path segments (#4137) #4149), the same way @langchain/openai already did.
  • js/ts packages:
    • llm_orchestrator: langchain, llamaindex, @langchain/*, @llamaindex/*
    • llm_vector_store: chromadb, @pinecone-database/*
    • ml_traditional: sklearn
    • dl_frameworks: tensorflow / torch / keras plus @tensorflow/* and @tensorflow-models/*. The bare names are kept so the strict test's documented positive still holds.
  • Prefix-only package names (torchvision, tensorflow_hub, chromadb-default-embed, keras-js) do not count, consistent with llm_api: anchor on the SDK's top-level package, not provider-named path segments (#4137) #4149.

Docs updated: how_to_add_a_language.md (rule descriptions), docs/domain_sensor_contracts.md, and a comment in tests/test_signal_contract_sheet.py. Contract text is unchanged, so no re-render was needed.

Tests. New file tests/extraction/languages/test_ai_ml_import_anchor_4150.py (147 cases):

  • wiring: only py/js/ts carry the rules, and js and ts share one pattern
  • every line from the issue table, plus related false positives
  • previously detected real imports still fire
  • per-file counts through StructuralExtractor.splice()
  • ReDoS payloads for all 4 rules × both builders

Against unfixed main, 54 cases fail; all pass after the fix.

Corpus before/after. Old vs. new rules, both run through splice(), on 4,041 py/js/ts candidate files. The candidates are every file in /nvme-data/gitgalaxy-data matching a loose import grep, plus the airflow, scikit-learn, sentry, vscode, scipy, rdkit, networkx, pylint, freeCodeCamp, Python and tqdm checkouts.

lang rule hits before → after files before → after
python dl_frameworks 27,414 → 27,090 3,030 → 3,002
python ml_traditional 6,116 → 5,067 875 → 858
python llm_orchestrator 254 → 448 92 → 94
python llm_vector_store 25 → 10 12 → 4
javascript llm_orchestrator 8 → 4 6 → 2
typescript llm_orchestrator 16 → 11 7 → 4
typescript dl_frameworks 10 → 0 2 → 0
typescript ml_traditional 2 → 0 1 → 0

Overall, 1,423 hits were removed and 208 added. Every removed line was classified (from a sample of up to 8 lines per changed file):

  • Doctest >>> from sklearn... lines inside docstrings. This is the bulk of the ml_traditional drop. They are documentation, not imports of the module, and are treated the same way llm_api: anchor on the SDK's top-level package, not provider-named path segments (#4137) #4149 treated them for llm_api.
  • Commented-out imports (# from keras import ...).
  • String literals and prose (tf_upgrade_v2 test fixtures, sympy's ("import torch",) tables, docstrings).
  • Other packages' submodules: airflow.providers.pinecone.*, google.cloud.aiplatform...plugins.tensorflow, sympy.printing.tensorflow, safetensors.torch, ray.train.torch, openinference.instrumentation.langchain, from sklearn.externals.array_api_compat import torch.
  • Python snippets inside a TSX docs template string (sentry agentMonitoring.tsx).

No real top-level import of a listed package was lost. All 208 added hits are python langchain_* imports (langchain_core, langchain_google_vertexai, langchain_openai, langchain_google_spanner), which follows from the ownership decision above.

#4114

#4114 is out of scope; it was closed as not-planned on 2026-10-03 (feature_flags is retired, removal to be done with the #4111 retrain).

Golden masters

Both legs were re-blessed: full (galaxy_venv) and zero-dep (galaxy_zero_venv). A PATH shim printed ENGINE …/fix-4150-4114/gitgalaxy for each. There is no drift in either fixture. The pinned crucible (v1.6.0) contains no import of any of these packages (grepped), so the pack rules read 0 before and after, as domain_sensor_contracts.md already records. No golden files are in this PR. signal_coverage_baseline.json is unchanged.

Sibling bug-sweep PRs also re-bless golden masters. If one merges first, merge main and re-bless with crucible_check.py --update / rebase_rebless.py. Never hand-merge golden files.

pr_gates

Full python tests/tools/pr_gates.py after merging current main: 7/7 pass (lint, audits, secrets, xray, gauntlet, golden, suite). There are no pre-existing failures. A pre-merge run had 3 tests/cobol_mainframe/test_det_programs.py failures; these were fixed on main by #4220 and are gone after the merge.

Cross-repo

None needed. keyword-rosetta plants no AI/ML pack imports, so rosetta counts do not move and rosetta:rebless-owed is not applied.

Closes #4150

🤖 Generated with Claude Code

https://claude.ai/code/session_013sNMtAg6s9dyyjNgnjRHi8

squid-protocol and others added 2 commits October 3, 2026 11:26
…ameworks on the top-level package (#4150)

The shared `\b(?:import|require|from)\b.*?(?:names)\b` shape fired whenever a
library name appeared anywhere on an import line (`from .langchain import`,
`from airflow.providers.pinecone.hooks import`, `from myproj.models.torch
import`, `require('../sklearn')`, `from './keras/loader'`).

Generalise PR #4149's llm_api anchors into two builders,
_py_package_import / _js_package_import (llm_api's compiled patterns are
byte-identical), and build PY_*/JS_* patterns for all four rules. Python
langchain_* distributions now count as llm_orchestrator (mirroring js/ts
@langchain/*); js/ts scopes: @langchain/*, @llamaindex/*, llamaindex,
@pinecone-database/*, @tensorflow/*, @tensorflow-models/*.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013sNMtAg6s9dyyjNgnjRHi8
@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

llm_orchestrator / llm_vector_store / ml_traditional / dl_frameworks match provider names anywhere on an import line

1 participant