Skip to content

solvi.experimental.injection

A detector of instruction-like text in tool outputs that you fit and threshold on your own tool outputs (experimental): InjectionDetector().fit(texts, spans), .calibrate(ordinary_texts, max_flag_rate), .spans(text) (the shape of injection_spans). Opt-in, no shipped weights; the fixed rules stay the default. Fit and calibrate it on the outputs your own tools return: its measured gain held only on the injection family it was fitted on (status).

A detector of instruction-like text in tool outputs, fitted on your labelled tool outputs and thresholded on your ordinary ones (experimental, opt-in, no shipped weights).

solvi.core.deciders.perturb.injection_spans is a fixed rule set: it flags ordinary text (an address, a payment line, "please reply") and misses polite attacks without its words. This detector scores each sentence of a text with a logistic model over two kinds of features — the rule families that sentence trips in the fixed detector (role, override, address, direct, action; with and without the action rules) and hashed word uni- and bigrams — fitted on labelled texts, and flags at a threshold calibrated on ordinary texts to a false-flag rate you choose.

det = InjectionDetector().fit(texts, positive_spans)        # spans: per text, the injected [(start, end)] (or [])
det.calibrate(ordinary_texts, max_flag_rate=0.05)
det.spans(tool_output)                                       # → [(start, end)], like injection_spans

The fixed rules stay the default; fit and calibrate this on the outputs your own tools return. On one public benchmark's injected tool outputs it found 76% of held-out injections against the rules' 23%, but only on the injection family it was fitted on (a plain TF-IDF classifier did the same, and on AgentDojo-style injections the rules found 78%, this 53%); see solvi.experimental.STATUS["injection"].

Deterministic: stable hashing (crc32), full-batch L-BFGS from zeros. to_dict() / from_dict() keep a fitted detector as JSON data (its non-zero weights and threshold) with a fingerprint.

InjectionDetector

InjectionDetector(dim=2 ** 18, l2=1.0, balanced=True, max_iter=300)

A per-sentence logistic score of "this sentence addresses the agent", with a calibrated threshold.

dim: hashed n-gram buckets (2**18); l2: the penalty on the weights (not on the intercept); balanced: weigh the two classes equally in the fit (injected sentences are rare).

label_sentences staticmethod

label_sentences(text, spans)

The scored units of a text with a label each: 1 when it overlaps an injected span by at least half of the unit or half of the span, else 0 → [(start, end, label)].

fit

fit(texts, spans)

texts and, per text, the injected spans [(start, end)] ([] for an ordinary text) → self.

sentence_scores

sentence_scores(text)

[(start, end, p)] for each scored unit of the text.

score

score(text)

The text's score: its highest sentence score (0 for an empty text).

calibrate

calibrate(ordinary_texts, max_flag_rate=0.05)

Set the threshold so that at most max_flag_rate of these ordinary (not injected) texts are flagged: the smallest threshold above the scores of all but that share of them. → self.

spans

spans(text)

The flagged sentences, merged → [(start, end)] (the shape of injection_spans).