Skip to content

Commit 182a475

Browse files
authored
Merge pull request #828 from NVIDIA/naren/fix-sc8-thread-time-budget
fix(sc8): count only the walk's own CPU time against its runtime budget
2 parents 4ae7c7b + 12a3fbe commit 182a475

3 files changed

Lines changed: 96 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 2.12.0 (Unreleased)
22
### Features/Bug Fixes
3+
* fix(sc8): count only the bytecode walk's own CPU time against its 5 s budget, so time spent waiting behind concurrent analyzers no longer reports a static `runtime_limit` for skills that ship no bytecode (#828)
34
* fix(providers): cut per-call cost of CLI-provider scans and default their LLM fan-out to 2 (#808)
45
* fix(taint): key TT1-TT6 taint by lexical scope and follow direct calls to functions and methods defined in the same file, so a tainted variable no longer taints same-named variables in other functions (#799). Message-glob baseline rules keep matching TT findings whose rule and message are unchanged; exact baseline fingerprints are bound to the scanner version and suppress nothing after an upgrade (see docs/SUPPRESSION.md), so they must be regenerated in any case. A TT message changes where the earlier message came from a same-named variable in another scope (it now names the variable, source and line that reach the sink); where a more severe source raises the rule (TT2 to TT3 or TT4, TT4 to TT3, TT2 to TT5 or TT6), and at such a sink, where a weaker finding is subsumed or now cites another variable; and where an identifier longer than 120 characters is now shortened. Re-accept those findings or regenerate the baseline. `match_fingerprint` changes only when the rule does.
56
* fix(static): stop code comparisons after removal verbs from reading as unterminated tag-marker directives (#777)

‎src/skillspector/nodes/analyzers/static_patterns_supply_chain.py‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
from skillspector.logging_config import get_logger
6767
from skillspector.models import AnalyzerFinding, Finding, Location, Severity
6868
from skillspector.state import (
69+
MAX_WORKFLOW_SECONDS,
6970
AnalyzerNodeResponse,
7071
SkillspectorState,
7172
transitive_note_truncation,
@@ -3381,6 +3382,8 @@ def _analyze_triggers(
33813382
MAX_SC8_DISCOVERED_ENTRIES = 10_000
33823383
MAX_SC8_DIRECTORY_ENTRIES = 10_000
33833384
MAX_SC8_TRAVERSAL_DEPTH = 64
3385+
# Processing budget, measured with ``time.thread_time()`` so that time spent
3386+
# waiting for the GIL behind concurrently running analyzers does not count.
33843387
MAX_SC8_ANALYSIS_SECONDS = 5.0
33853388
MAX_SC8_FINDINGS = 10_000
33863389
MAX_SC8_LIMITATIONS = 256
@@ -3415,7 +3418,14 @@ def _scan_shipped_bytecode(
34153418
timeout_seconds: float | None = None,
34163419
max_findings: int | None = None,
34173420
) -> _ShippedBytecodeScanResult:
3418-
"""Discover shipped bytecode with deterministic aggregate resource bounds."""
3421+
"""Discover shipped bytecode with deterministic aggregate resource bounds.
3422+
3423+
Analyzers run concurrently in one process, so the walk can spend most of
3424+
its wall-clock time waiting for the GIL. The processing budget therefore
3425+
counts only this thread's CPU time. Wall-clock time is bounded by the
3426+
remaining workflow deadline (``timeout_seconds``), or by the full workflow
3427+
limit for callers outside a workflow.
3428+
"""
34193429
findings: list[Finding] = []
34203430
limitations: list[_SupplementalLimitation] = []
34213431
if not skill_path or not isinstance(skill_path, str):
@@ -3425,10 +3435,11 @@ def _scan_shipped_bytecode(
34253435
return _ShippedBytecodeScanResult(findings, limitations)
34263436

34273437
started_at = time.monotonic()
3428-
runtime_limit = max(0.0, MAX_SC8_ANALYSIS_SECONDS)
3429-
if timeout_seconds is not None:
3430-
runtime_limit = min(runtime_limit, max(0.0, timeout_seconds))
3431-
deadline = started_at + runtime_limit
3438+
active_started_at = time.thread_time()
3439+
active_limit = max(0.0, MAX_SC8_ANALYSIS_SECONDS)
3440+
wall_clock_limit = max(
3441+
0.0, MAX_WORKFLOW_SECONDS if timeout_seconds is None else timeout_seconds
3442+
)
34323443
requested_finding_limit = MAX_SC8_FINDINGS if max_findings is None else max_findings
34333444
finding_limit = max(0, min(requested_finding_limit, MAX_SC8_FINDINGS))
34343445
discovered_entries = 0
@@ -3452,15 +3463,20 @@ def add_limitation(limitation: _SupplementalLimitation) -> None:
34523463
)
34533464

34543465
def runtime_exhausted(relative_directory: str) -> bool:
3455-
now = time.monotonic()
3456-
if now < deadline:
3466+
wall_clock_elapsed = max(0.0, time.monotonic() - started_at)
3467+
active_elapsed = max(0.0, time.thread_time() - active_started_at)
3468+
if wall_clock_elapsed >= wall_clock_limit:
3469+
observed_seconds, limit_seconds = wall_clock_elapsed, wall_clock_limit
3470+
elif active_elapsed >= active_limit:
3471+
observed_seconds, limit_seconds = active_elapsed, active_limit
3472+
else:
34573473
return False
34583474
add_limitation(
34593475
_SupplementalLimitation(
34603476
path=scope_path(relative_directory),
34613477
reason=LedgerReason.RUNTIME_LIMIT,
3462-
observed_seconds=max(0.0, now - started_at),
3463-
limit_seconds=runtime_limit,
3478+
observed_seconds=observed_seconds,
3479+
limit_seconds=limit_seconds,
34643480
)
34653481
)
34663482
return True

‎tests/nodes/analyzers/test_sc8_shipped_bytecode.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,73 @@ def test_sc8_cli_projects_truncation_into_analysis_completeness(
313313
item["reason_code"] == LedgerReason.ARTIFACT_COUNT_LIMIT.value
314314
for item in completeness["ledger_exceptions"]
315315
)
316+
317+
318+
class _FakeClock:
319+
"""Stand-in for ``time`` whose two clocks advance independently per read."""
320+
321+
def __init__(self, *, wall_step: float, cpu_step: float) -> None:
322+
self._wall = 0.0
323+
self._cpu = 0.0
324+
self._wall_step = wall_step
325+
self._cpu_step = cpu_step
326+
327+
def monotonic(self) -> float:
328+
self._wall += self._wall_step
329+
return self._wall
330+
331+
def thread_time(self) -> float:
332+
self._cpu += self._cpu_step
333+
return self._cpu
334+
335+
336+
def _nested_clean_tree(root: Path) -> None:
337+
for name in ("features/a/references", "features/b/references", "scripts/engine"):
338+
directory = root / name
339+
directory.mkdir(parents=True)
340+
(directory / "notes.md").write_text("x\n", encoding="utf-8")
341+
342+
343+
def test_sc8_walk_ignores_time_spent_waiting_for_other_analyzers(
344+
tmp_path: Path, monkeypatch
345+
) -> None:
346+
# Concurrent analyzers hold the GIL, so each step of the walk can wait a
347+
# long time while doing almost no work of its own (SKILLEVAL-17).
348+
_nested_clean_tree(tmp_path)
349+
monkeypatch.setattr(supply_chain, "time", _FakeClock(wall_step=1.0, cpu_step=0.0))
350+
351+
result = supply_chain._scan_shipped_bytecode(str(tmp_path))
352+
353+
assert result.findings == []
354+
assert result.limitations == []
355+
356+
357+
def test_sc8_processing_budget_still_bounds_the_walk(tmp_path: Path, monkeypatch) -> None:
358+
_nested_clean_tree(tmp_path)
359+
monkeypatch.setattr(supply_chain, "time", _FakeClock(wall_step=0.0, cpu_step=1.0))
360+
361+
result = supply_chain._scan_shipped_bytecode(str(tmp_path))
362+
363+
assert len(result.limitations) == 1
364+
limitation = result.limitations[0]
365+
assert limitation.reason is LedgerReason.RUNTIME_LIMIT
366+
assert limitation.limit_seconds == supply_chain.MAX_SC8_ANALYSIS_SECONDS
367+
368+
369+
@pytest.mark.parametrize(("timeout_seconds", "expected_limit"), [(None, 4.0), (3.0, 3.0)])
370+
def test_sc8_wall_clock_is_bounded_by_the_workflow_deadline(
371+
tmp_path: Path,
372+
monkeypatch,
373+
timeout_seconds: float | None,
374+
expected_limit: float,
375+
) -> None:
376+
_nested_clean_tree(tmp_path)
377+
monkeypatch.setattr(supply_chain, "MAX_WORKFLOW_SECONDS", 4.0)
378+
monkeypatch.setattr(supply_chain, "time", _FakeClock(wall_step=1.0, cpu_step=0.0))
379+
380+
result = supply_chain._scan_shipped_bytecode(str(tmp_path), timeout_seconds=timeout_seconds)
381+
382+
assert len(result.limitations) == 1
383+
limitation = result.limitations[0]
384+
assert limitation.reason is LedgerReason.RUNTIME_LIMIT
385+
assert limitation.limit_seconds == expected_limit

0 commit comments

Comments
 (0)