Skip to content

llm_api: anchor on the SDK's top-level package, not provider-named path segments (#4137) - #4149

Merged
squid-protocol merged 1 commit into
mainfrom
fix/4137-llm-api-anchor
Oct 2, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
fix/4137-llm-api-anchor

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Fixes #4137

Problem

llm_api ("Cloud LLM API Integrations") used the shared \b(?:import|require|from)\b.*?(?:openai|anthropic)\b wrapper, so it fired on any import line that had a provider name anywhere on it:

  • python: from airflow.providers.openai.hooks.openai import OpenAIHook (airflow's OpenAI operator never imports the SDK), from llama_index.llms.openai import ..., from . import openai
  • js/ts: relative/local paths such as import { x } from './openai/client', require('./providers/openai')

Fix

llm_api is split into two anchored patterns in _shared_patterns.py, covering every language that has the rule (python, javascript, typescript):

  • PY_LLM_API (python): the module right after from, or an item in an import a, b list, must be the SDK package (openai, anthropic, or langchain_openai / langchain_anthropic), with an optional submodule (from openai.types.chat import ..., import openai as oa). It is anchored at the start of a statement (line start or ;, re.M).
  • JS_LLM_API (javascript + typescript): the quoted module specifier after from / import / require( / import( must name a package: openai, anthropic, @openai/*, @anthropic-ai/* (sdk, bedrock-sdk, ...), or a scoped provider adapter @scope/openai|anthropic (@ai-sdk/openai, @azure/openai, @langchain/openai). Relative, absolute and alias paths (./, ../, @/, ~/, src/...) never match.

Every positive the old rule had that is really a package import still fires. Some old matches are dropped on purpose because they are module paths, not the SDK: llama_index.llms.openai and the airflow provider. langchain_openai and langchain_anthropic were kept because they are top-level provider packages, and llm_orchestrator's langchain\b can't see them. openai-*-prefixed npm packages (e.g. openai-edge) no longer match.

ReDoS: every piece next to another uses disjoint character classes ([\w.] / [ \t] / ,), and the import-item list is bounded {0,16}. Both patterns are line-local, so the #3072 line gate still applies. I checked scaling by hand: linear from 10k to 160k chars.

Evidence

  • Real scan (galaxyscope --db-only) of a small repro repo, llm_api per file before -> after: airflow-style operator 1 -> 0; python SDK file 2 -> 2; JS file with 'openai' + '@anthropic-ai/sdk' + './openai/client' 3 -> 2.
  • New tests/extraction/languages/test_llm_api_anchor_4137.py (76 cases): rule wiring for all three languages, SDK imports fire, provider-named internal/relative paths don't, per-file counts through StructuralExtractor.splice(), ReDoS payloads. It passes when run with the plain pytest console script from an unrelated directory.
  • tests/extraction, tests/core_engine, tests/security_auditing: 11469 passed. 2 timing-based ReDoS tests (zig sweep, pli declare) failed under heavy machine load (load avg ~20) and pass when re-run on their own. Both are unrelated to this rule.
  • tests/tools/audit_check.py: all clear (ruff baseline + format, mypy, dead-key, ast-accuracy).
  • tests/tools/crucible_check.py: full_precision PASS, zero_dependency PASS. No golden-master change: the pinned crucible (v1.6.0) has no llm_api hits, before or after.
  • how_to_add_a_language.md's AI/ML pack bullet for llm_api now documents the anchoring. The contract sentence is unchanged.

Not cross-repo: no crucible/rosetta change needed.

🤖 Generated with Claude Code

…th segments (#4137)

The shared `\b(?:import|require|from)\b.*?(?:openai|anthropic)\b` shape fired
on any import line containing a provider name anywhere -- airflow's
`from airflow.providers.openai.hooks.openai import OpenAIHook` (which never
imports the SDK) and JS/TS relative paths like `from './openai/client'`.

Split llm_api into PY_LLM_API (python) and JS_LLM_API (javascript,
typescript):
- python: the module after `from`, or an item after `import`, must be the
  package itself (openai, anthropic, langchain_openai, langchain_anthropic),
  optionally with a submodule; anchored at a statement start.
- js/ts: the quoted module specifier must name a package: openai, anthropic,
  @openai/*, @anthropic-ai/*, or a scoped adapter @scope/openai|anthropic.

Adds regression tests (SDK imports fire; provider-named internal and
relative paths don't; per-file counts through splice(); ReDoS payloads).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@github-actions

github-actions Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol merged commit 3e7dfb2 into main Oct 2, 2026
38 checks passed
@squid-protocol
squid-protocol deleted the fix/4137-llm-api-anchor branch October 2, 2026 17:35
squid-protocol added a commit that referenced this pull request Oct 3, 2026
…ameworks on the top-level package (#4150) (#4231)

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/*.


Claude-Session: https://claude.ai/code/session_013sNMtAg6s9dyyjNgnjRHi8

Co-authored-by: Claude Opus 5.5 <[email protected]>
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_api matches internal module paths, not SDK use (airflow.providers.openai...)

1 participant