Documentation
¶
Overview ¶
Package snippetmatch annotates a scan inventory's snippet matches with the local snippet-classifier's verdict on whether each match is a false positive — a match whose reported OSS line range does not correspond to its reported local range.
The classifier itself (github.com/scanoss/snippets-classifier) is offline and needs no state, but scoring a match needs three inputs: the scanned (local) file, the matched OSS file, and the paired line ranges. The inventory carries the ranges and the OSS file's hash; this package reads the local file from disk and fetches the OSS file's content by hash. The fetch is the only network cost, so unique OSS hashes are downloaded once and reused across every match that shares them.
Annotation only proposes: it sets FileEvidence.SnippetClassification and removes nothing. Removing what it proposes is the caller's choice (see sbom.Inventory.RemoveFalsePositiveSnippets).
Index ¶
Constants ¶
const DefaultThreshold = snippetclassifier.DefaultThreshold
DefaultThreshold is the P(false positive) at or above which a match is proposed as a false positive. It is the classifier's own default (0.7), tuned to propose for human confirmation.
const DefaultWorkers = 8
DefaultWorkers bounds the concurrent OSS content fetches when Options.Workers is unset.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Annotator ¶
type Annotator struct {
// contains filtered or unexported fields
}
Annotator scores an inventory's snippet matches. Build one with New and reuse it.
func New ¶
New builds an Annotator from opts. It errors when no Fetcher is given or the model cannot be loaded.
func (*Annotator) Annotate ¶
Annotate scores every snippet match in inv in place, setting FileEvidence.SnippetClassification on each match it could score, and returns the tally. It is non-fatal: a match whose local file is unreadable or whose OSS content cannot be fetched is left unannotated and logged. The returned error names how many matches could not be scored, for the caller to surface; it does not mean the inventory is unusable.
The inventory is scored, not filtered: no match is removed, whatever its verdict.
func (*Annotator) ModelVersion ¶
ModelVersion identifies the model the Annotator scores with.
type Fetcher ¶
Fetcher retrieves the raw content of an OSS file by the file_hash the scan reported for it. A 1-based inclusive line range may be given; 0 for either bound leaves it at the default (line 1 / the last line), so (hash, 0, 0) is the whole file. scanoss.ContentsAPI satisfies this interface.
type Options ¶
type Options struct {
// Fetcher supplies OSS file content by hash. Required.
Fetcher Fetcher
// ScanRoot is the directory the inventory's file paths are relative to. Required for a
// directory scan; a match whose local file cannot be read is skipped, not failed.
ScanRoot string
// Threshold overrides the classifier's decision threshold. 0 uses DefaultThreshold.
Threshold float64
// Model, when non-empty, replaces the embedded model (a per-organisation model in the same
// JSON shape). Empty uses the embedded default.
Model []byte
// Workers bounds concurrent OSS content fetches. <1 uses DefaultWorkers.
Workers int
// OnProgress, when set, is called as OSS files are fetched (done out of total unique
// hashes). It must be safe for concurrent use.
OnProgress func(done, total int)
}
Options configures an Annotator.
type Summary ¶
type Summary struct {
ModelVersion string
Threshold float64
Matches int // snippet matches in the inventory
Scored int // matches that received a classification
FalsePositives int // scored matches proposed as false positives
}
Summary tallies one Annotate run, for the caller to report: how many snippet matches the inventory held, how many were scored, and how many of those were proposed as false positives. Matches - Scored is how many could not be scored, for whatever reason (no OSS hash or line ranges, an unreadable local file, a failed fetch).