Skip to content

linux_scan, scan: redesign active scanner around isolated contract cartography - #833

Merged
rgushchin merged 6 commits into
mainfrom
scan-bug-discovery
Nov 11, 2026
Merged

rgushchin merged 6 commits into
mainfrom
scan-bug-discovery

Conversation

@rgushchin

@rgushchin rgushchin commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Redesigns sashiko scan around a context-isolated multi-agent specification & discovery architecture integrated with linux_bug verification, lifting detection accuracy substantially on both synthetic and real-world Linux kernel benchmarks while cutting per-file token usage by 3.2x and latency by 4.1x.

Architecture & Commits

  1. linux_bug, scan: add --min-severity floor defaulting to medium
    • Adds --min-severity (default medium) across ScanOptions, linux_scan, linux_bug, and BugWorker (persisted on BugInput enrichment across retries).
  2. linux_bug, linux_scan: pass kernel threat model to scan stages
    • Loads Documentation/process/threat-model.rst at the target scan revision and injects <kernel_threat_model> into scan security, verification, and severity calibration stages.
  3. linux_bug: add standalone pipeline options and partial-claim guard
    • Adds StandaloneBugPipelineOptions (reuse_canonical_input, fast_origin_tracing, extra_context, max_session_turns) and a PARTIAL-CONSEQUENCE GUARD in VerifySession so genuine defects are not falsely discarded when one secondary claimed symptom (BUG()/WARN_ON()) is unreachable while the invalid state/return value still corrupts callers.
  4. prefetch: extract external header definitions and callers for scan
    • Adds 2-hop subsystem-scoped prefetch_external_definitions_for_chunks (resolving embedded member structs) and 1-hop prefetch_external_callers_for_chunks across the repository.
  5. scan: guard in-run deduplication against distinct defects
    • Strips subsystem/path/symbol tokens before Jaccard comparison, raises distance-aware thresholds, and prevents candidates naming disjoint fn() symbols in titles from colliding on shared background helper locations.
  6. linux_scan, scan: redesign scan around isolated contract cartographer
    • Agent 1 (ContractCartographer, isolated context): Inspects <full_file_source>, <external_header_definitions>, and <external_callers> solely to document normative object lifecycles, lock-to-field protocols, nullability/sentinel domains, and caller contracts—strictly prohibited from bug hunting to avoid code-rationalization bias.
    • Agent 2 (contract-audit, isolated from Agent 1's transcript) + Track A Specialists: Audits each 400-line chunk (enriched with deterministic <intra_file_context> for out-of-chunk types/macros, 1-hop static callees, and intra-file callers) against Agent 1's contract specification in parallel with domain specialists (implementation, execution-flow, resources, locking, security, hardware).
    • Single-Turn Chunk Consolidation & Concurrency Pacing: Replaces redundant per-chunk multi-turn post-verification with a 1-turn chunk consolidator before Phase 2 VerifySession and caps concurrent scan LLM permits at (concurrency * 2).clamp(3, 6).

Empirical Benchmark Results

Architecture Suite Exact Detection Total Detection FP Suppression Wrongly Dismissed Mean Tokens In / File P50 Time / File
origin/main benchmark_scan.json (scan_corpus.c, 2,156L, 32 entries) 24 / 26 (92.3%) 24 / 26 (92.3%) 6 / 6 (100.0%) 0 2.40M 807s
This PR benchmark_scan.json (scan_corpus.c, 2,156L, 32 entries) 26 / 26 (100.0%) 26 / 26 (100.0%) 6 / 6 (100.0%) 0 1.93M (-20%) 1,571s
origin/main arch/arm64/kvm/nested.c (1,923L, 8 entries) 4 / 7 (57.1%) 4 / 7 (57.1%) 1 / 1 (100.0%) 0 30.26M 9,443s
This PR arch/arm64/kvm/nested.c (1,923L, 8 entries) 6 / 7 (85.7%) 6 / 7 (85.7%) 1 / 1 (100.0%) 0 ~8.9M (-71%) 2,252s (-76%)
Single-agent synthesis (V2.3) benchmark_scan_real.json (8 Linux .c files, 19,415L, 38 entries) 22 / 30 (73.3%) 23 / 30 (76.7%) 8 / 8 (100.0%) 0 8.19M 2,196s
This PR (Context-Isolated V3.1) benchmark_scan_real.json (8 Linux .c files, 19,415L, 38 entries) 25 / 30 (83.3%) 26 / 30 (86.7%) 8 / 8 (100.0%) 0 9.48M (3.2x lower) 2,299s (4.1x faster)

@sashiko-bot

sashiko-bot Bot commented Nov 6, 2026

Copy link
Copy Markdown

Sashiko review

Commit 3/3 — 8521b4c1 linux_scan, scan: expand specialist coverage and tune chunk size

  • [MEDIUM] src/workflows/linux_scan.rs: scan_triage_stage_with_effort() builds
    the stage_efforts JSON schema with build_stage_efforts_schema() over
    SCAN_ANALYSIS_STAGES. Because implementation, execution-flow, resources, and
    locking now set optional to false, build_stage_efforts_schema() marks all
    four keys as required in stage_efforts. When EffortMode::Dynamic is enabled,
    this conflicts with the early-exit instruction to return an empty
    stage_efforts object on benign boilerplate chunks and prevents providers
    using constrained schema decoding from emitting an empty stage_efforts
    object. Omit required keys from the scan triage stage_efforts schema so
    early exits can return an empty object, and add a unit test covering the
    dynamic-effort triage schema and reducer.

Full review and stage logs on sashiko.sashiko.dev

@rgushchin

Copy link
Copy Markdown
Member Author

Fixed in updated Commit 3/3 (build_scan_stage_efforts_schema omits required keys so EffortMode::Dynamic early exits on benign boilerplate chunks can emit "stage_efforts": {} under constrained schema decoding, with a unit test covering both early-exit and core-stage reducer defaults).

@sashiko-bot

sashiko-bot Bot commented Nov 6, 2026

Copy link
Copy Markdown

Sashiko review — v2

Commit 1/3 — 1ca761f6 linux_bug, scan: add --min-severity floor defaulting to medium

  • [LOW] src/scan.rs, src/worker/bug_worker.rs, and
    src/workflows/linux_bug.rs: submit_scan_candidates() sets min_severity only
    in memory on the local BugWorker instance rather than persisting it in the
    candidate bug enrichment. If the initial analysis in
    BugWorker::process_claimed_bug() fails or its lease expires, a background
    BugWorker::run() task claims the pending bug with self.min_severity set to
    None, and process_issue_worker_with_min_severity() falls back to
    SeverityFloor::Medium instead of the --min-severity floor requested by the
    scan. Persist min_severity on BugInput so retries honor the original floor.

Full review and stage logs on sashiko.sashiko.dev

@rgushchin

Copy link
Copy Markdown
Member Author

Fixed in updated Commit 1/3 (BugInput now persists min_severity in raw_input enrichment so background BugWorker retries honor the scan's requested severity floor).

@sashiko-bot

sashiko-bot Bot commented Nov 6, 2026

Copy link
Copy Markdown

Sashiko review — v3

✓ No issues found across 3 commits.

Full review log on sashiko.sashiko.dev

Previously, all three layers of active codebase scanning (linux_scan,
standalone linux_bug verification, and BugWorker submission) hardcoded a
High/Critical severity filter, discarding genuine Medium-severity
resource leaks, accounting bugs, and teardown races that Sashiko's
pre-existing bug database and related-preexisting mode retain by
default.

Add a --min-severity flag to sashiko scan defaulting to medium and wire
the configurable SeverityFloor through ScanOptions, linux_scan,
linux_bug, and BugWorker while tracking filtered Low-severity chunk
items in deduplicated_dismissed_concerns.

Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
Load Documentation/process/threat-model.rst from the target repository
or fallback target commit during sashiko scan note preloading and
standalone linux_bug execution so worktrees without a separate mainline
remote still resolve the threat model at the scanned revision.

Inject the rendered kernel_threat_model block into the scan security,
verification, and post-verification prompts so trust boundaries, local
DoS resource exhaustion, and privileged paths are calibrated
consistently with patch review.

Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
Extend the standalone Linux bug verification pipeline with
StandaloneBugPipelineOptions so active codebase scans can reuse already
canonicalized candidate titles and descriptions, attribute origins via
deterministic git blame, cap tool turns, and pass file-wide invariants
into VerifySession and SeveritySession.

Strengthen the Linux VerifySession prompt with explicit line-range read
guidance and a partial-consequence guard so verified defects whose
underlying invalid input, truncation, or state desynchronization still
corrupts callers are not discarded merely because one overstated symptom
branch is unreachable.

Validated on benchmark_scan_real.json (38 entries across 8 Linux C
files: wrongly_dismissed dropped from 1 to 0/30 while preserving 8/8
100% false-positive suppression) and benchmark_scan.json (32 entries:
26/26 100% exact detection and 6/6 100% false-positive suppression).

Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
Add deterministic repository prefetch helpers in src/worker/prefetch.rs
for active codebase scanning:

- prefetch_external_definitions_for_chunks scopes git grep lookups to
  header paths and target subsystem directories once per file and
  performs a second-hop pass to resolve embedded member struct types
  referenced by container struct definitions.
- prefetch_external_callers_for_chunks locates 1-hop external call sites
  and vtable registrations across the repository for non-static
  functions defined in the scanned file.

Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
Previously, is_title_and_locations_duplicate computed Jaccard overlap
over raw title words (including shared subsystem prefixes and function
names) whenever two candidates shared any (file, function_or_symbol)
pair. That caused two false merges before Phase 2 verification:

- Two distinct defects inside the same large function collided on the
  shared subsystem prefix and function name tokens.
- Two candidates in different target functions that both cited the same
  background allocation or helper function in locations were merged,
  discarding a genuine bug when paired with an unverified false
  positive.

Strip subsystem prefixes, file path components, and symbol names before
measuring defect-mechanism token overlap, raise the Jaccard threshold
when same-file locations are more than 25 lines apart, and never merge
candidates whose titles name disjoint functions.

Fixes: 8c71144 ("scan: add scan runner and linux_bug pipeline integration")
Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
Redesign the active codebase scan pipeline around context-isolated
specification cartography and dual-track chunk discovery:

- Add Agent 1 (run_contract_cartographer_pass in linux_scan.rs), which
  inspects the full file source, 2-hop prefetched external header
  definitions, and 1-hop external call sites solely to reconstruct
  normative lifecycle, locking, nullability/sentinel, and caller
  contracts without bug hunting.
- Add Agent 2 (contract-audit stage) alongside Track A chunk domain
  specialists (implementation, execution-flow, resources, locking,
  security, hardware) so contract breakages are audited without seeing
  Agent 1's reasoning transcript.
- Extract deterministic intra-file context (out-of-chunk types/macros,
  1-hop static callees, and intra-file callers), tune target chunk size
  to 400 lines, replace redundant per-chunk post-verification with a
  single-turn chunk consolidator, and cap concurrent scan LLM permits
  to avoid prefill contention before Phase 2 linux_bug verification.

Validated on benchmark_scan.json (26/26 100% exact detection vs 24/26 on
main, 6/6 100% FP suppression, 20% fewer tokens) and
benchmark_scan_real.json (25/30 83.3% exact and 26/30 86.7% total
detection vs 22/30 and 23/30 in single-agent synthesis, 8/8 100% FP
suppression, 3.2x fewer tokens and 4.1x faster than main on nested.c).

Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
@rgushchin rgushchin changed the title scan, linux_bug: default --min-severity to medium and improve chunk recall linux_scan, scan: redesign active scanner around isolated contract cartography Nov 11, 2026
@sashiko-bot

sashiko-bot Bot commented Nov 11, 2026

Copy link
Copy Markdown

Sashiko review — v4

✓ No issues found across 6 commits.

Full review log on sashiko.sashiko.dev

@rgushchin
rgushchin merged commit 5e284b6 into main Nov 11, 2026
1 check passed
@rgushchin
rgushchin deleted the scan-bug-discovery branch November 11, 2026 20:47
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.

1 participant