Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/skillspector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import warnings
from importlib.metadata import version as _pkg_version
from typing import Any

from skillspector.graph_proxy import graph

__version__ = _pkg_version("skillspector")

Expand All @@ -32,6 +35,12 @@
category=Warning,
)

from skillspector.graph import create_graph, graph # noqa: E402 (after filter setup)

def create_graph() -> Any:
"""Build and return a new SkillSpector workflow graph."""
from skillspector.graph import create_graph as build_graph

return build_graph()


__all__ = ["create_graph", "graph", "__version__"]
2 changes: 1 addition & 1 deletion src/skillspector/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from skillspector import __version__, transitive
from skillspector.cleanup import cleanup_result
from skillspector.constants import RISK_THRESHOLD
from skillspector.graph import graph
from skillspector.graph_proxy import graph
from skillspector.input_handler import validate_local_input_path
from skillspector.inspection_ledger import (
MAX_INSPECTION_LEDGER_EVENTS,
Expand Down
44 changes: 44 additions & 0 deletions src/skillspector/graph_proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Lightweight lazy access to the compiled SkillSpector workflow graph."""

from __future__ import annotations

from threading import Lock
from typing import Any


class LazyGraph:
"""Load the compiled workflow only when a caller first uses it."""

def __init__(self) -> None:
self._compiled: Any | None = None
self._lock = Lock()

def _get_compiled(self) -> Any:
if self._compiled is None:
with self._lock:
if self._compiled is None:
from skillspector.graph import graph as compiled_graph

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Keep the public graph export stable after lazy loading. Importing skillspector.graph here makes Python assign that submodule to the package's graph attribute, overwriting the LazyGraph exported by skillspector.__init__. I reproduced this on the exact head: after first.invoke is resolved, a later from skillspector import graph returns the module and graph.invoke raises AttributeError. Preserve the documented package export across import order and add a regression that imports it again after the first lazy load.


self._compiled = compiled_graph
return self._compiled

def __getattr__(self, name: str) -> Any:
return getattr(self._get_compiled(), name)


graph = LazyGraph()
28 changes: 28 additions & 0 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import ast
import json
import os
import re
import subprocess
import sys
from collections.abc import Callable, Iterator
from contextlib import AbstractContextManager, ExitStack, contextmanager, nullcontext
Expand Down Expand Up @@ -88,6 +90,32 @@ def test_cli_version() -> None:
assert "v" in result.output


def test_cli_help_does_not_initialize_analyzers() -> None:
"""Help should not compile the scan graph or warn about missing credentials."""
env = os.environ.copy()
env["SKILLSPECTOR_PROVIDER"] = "nv_build"
for name in ("ANTHROPIC_API_KEY", "NVIDIA_INFERENCE_KEY", "OPENAI_API_KEY"):
env.pop(name, None)

completed = subprocess.run(
[
sys.executable,
"-c",
"from skillspector.cli import app; app()",
"--help",
],
capture_output=True,
check=False,
env=env,
text=True,
timeout=15,
)

assert completed.returncode == 0
assert "Usage:" in completed.stdout
assert "Skipping analyzer" not in completed.stderr


def test_cli_scan_local_directory(tmp_path: Path) -> None:
"""scan with local directory runs graph and prints report."""
(tmp_path / "SKILL.md").write_text("---\nname: scan-test\n---\n# Safe", encoding="utf-8")
Expand Down
Loading