Files
DECNET/decnet/profiler/behave_shell/extract.py
anti e4626879f6 perf(pytest): 194s → 4s collection — lazy heavy imports + norecursedirs
Four-part fix for the collection bottleneck that was blocking the dev loop:

1. Lazy mitreattack.stix20 import in attack_stix.py — deferred to first
   _load() call (TYPE_CHECKING guard at top level)

2. Lazy misp_stix_converter import in both MISP export routers — moved
   from module level into the route handler body

3. Lazy attack_catalog / attack_stix in ttp.py repo mixin — thin wrapper
   functions so the import chain never fires at module load time

4. tests/api/conftest.py — `from decnet.web.api import app` moved inside
   the `client()` fixture; `pytest_ignore_collect` broadened to skip all
   test_schemathesis*.py variants (not just test_schemathesis.py), which
   were launching a subprocess server at module-import time

5. pyproject.toml — `norecursedirs` for tests/live, tests/stress,
   tests/service_testing, tests/docker, tests/perf so these directories
   are never entered; `-m` filter removed from addopts (now redundant);
   `--dist loadscope` → `--dist load` to unblock workers immediately

6. behave_core / behave_shell rename — BEHAVE packages dropped the
   `decnet_` prefix; reinstalled editable installs and updated all 14
   import sites across profiler, ttp, bus, and correlation modules
2026-05-10 06:41:25 -04:00

52 lines
1.6 KiB
Python

"""Public extraction entry point.
``extract_session`` is the only function workers call. It builds a
:class:`SessionContext` once and fans the registered feature functions
across it. Pure library: no I/O, no bus, no DB. The worker
(``BEHAVE-INTEGRATION.md`` Phase 4) is responsible for those.
"""
from __future__ import annotations
from typing import Iterable, Iterator
from behave_core.spec.envelope import Observation
from decnet.profiler.behave_shell._ctx import SessionContext, build_session_context
from decnet.profiler.behave_shell._features import FEATURES
from decnet.profiler.behave_shell._parse import AsciinemaEvent
DEFAULT_SOURCE = "decnet/profiler/behave_shell/extract.py"
def extract_session(
events: Iterable[AsciinemaEvent],
*,
sid: str,
source: str = DEFAULT_SOURCE,
evidence_ref: str | None = None,
) -> Iterator[Observation]:
"""Yield BEHAVE-SHELL observations for a single session.
``events`` is an iterable of ``(t, kind, data)`` tuples — see
``_parse.AsciinemaEvent``. ``sid`` identifies the session for
evidence pointers and downstream joins.
"""
ctx = build_session_context(
events, sid=sid, source=source, evidence_ref=evidence_ref
)
for feature_fn in FEATURES:
yield from feature_fn(ctx)
def build_context(
events: Iterable[AsciinemaEvent],
*,
sid: str,
source: str = DEFAULT_SOURCE,
evidence_ref: str | None = None,
) -> SessionContext:
"""Expose the SessionContext build for tests + future debug tools."""
return build_session_context(
events, sid=sid, source=source, evidence_ref=evidence_ref
)