Skip to main content

sfi-toolkit: Semantic Fault Injection (SFI) framework for testing AI agent resilience.

Project description

sfi-toolkit

sfi-toolkit is a Python library for Semantic Fault Injection (SFI) — a testing methodology for LLM-based agent resilience.


Features

  • Rule-based semantic fault generator — deterministically perturbs correct tool responses into wrong-period, wrong-entity, or numeric-noise faults. All faults are schema-valid and statistically plausible.
  • Three hardening strategies — H1 (cross-source validation), H2 (business-rule plausibility), H3 (trajectory consistency) — independently and composably reduce fault propagation.
  • Propagation experiment runner — simulates the agent as a Bernoulli chain and measures fault propagation rates across fault classes, injection steps, and hardening configurations.
  • Provider-agnostic LLM fault generator — accepts any Callable[[str], str] as the LLM backend; no SDK dependency.

Installation

pip install sfi-toolkit

Built for Python 3.12 or above.


Quick Start

1. Generate a wrong-period fault

from sfi_toolkit import FaultClass, RuleBasedFaultGenerator

schema = {
    "type": "object",
    "required": ["company_code", "fiscal_period", "accounts"],
    "properties": {
        "company_code": {"type": "string"},
        "fiscal_period": {"type": "string"},
        "accounts": {"type": "array"},
    },
}

correct_response = {
    "company_code": "1000",
    "fiscal_period": "2025-Q3",
    "accounts": [{"account_id": "110000", "balance": 1842350.00}],
}

gen = RuleBasedFaultGenerator(
    schema=schema,
    entity_catalog=["1000", "2000", "3000", "4000", "5000"],
)
fault = gen.generate(correct_response, FaultClass.WRONG_PERIOD)
print(fault.perturbed_response["fiscal_period"])  # "2025-Q2"
print(fault.schema_valid)                          # True

2. Check a response with H3 trajectory consistency

from sfi_toolkit import H3Consistency

h3 = H3Consistency()
ctx = {"company_code": "1000", "fiscal_period": "2025-Q3"}

# Record what we expect from the first step
h3.observe(correct_response, ctx)

# A later step returns data for the wrong period
wrong = dict(correct_response)
wrong["fiscal_period"] = "2025-Q2"
result = h3.check(wrong, ctx)
print(result.detected)  # True
print(result.reason)    # "fiscal_period mismatch: ..."

3. Run a propagation experiment

from sfi_toolkit import PropagationExperiment

exp = PropagationExperiment()
faults = gen.generate_batch(correct_response, FaultClass.WRONG_PERIOD, n=50)

result_h0 = exp.run(faults, injection_step=1, hardening=set())
result_all = exp.run(faults, injection_step=1, hardening={"H1", "H2", "H3"})

print(f"H0  propagation rate: {result_h0.propagation_rate:.2f}")   # ~0.68
print(f"All propagation rate: {result_all.propagation_rate:.2f}")  # ≤ 0.05

Fault Taxonomy

Innocent Adversarial
User-side Usability testing Red teaming
Environment-side SFI (this library) Indirect prompt injection

SFI targets the environment-side innocent cell — the gap not addressed by any prior methodology.


API Reference

See API.md.


License

MIT License. See LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sfi_toolkit-0.0.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sfi_toolkit-0.0.1-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file sfi_toolkit-0.0.1.tar.gz.

File metadata

  • Download URL: sfi_toolkit-0.0.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for sfi_toolkit-0.0.1.tar.gz
Algorithm Hash digest
SHA256 33620b3043396e102b1b4326b004b320bd192f54d37d76629632bb2c329419c4
MD5 8e35afebb8fee9ade500fa82c98f5e60
BLAKE2b-256 a18c2693e2e5cb4d13376a11025cd80756b4ac3ad34758d12424beba18c5acad

See more details on using hashes here.

File details

Details for the file sfi_toolkit-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sfi_toolkit-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bd60a7d0112ccec6d2bdb9a07d598b70de5bbef053ed709e9945d9d3e561869a
MD5 7f09e036a9627612505ed3ec83d133ed
BLAKE2b-256 de564779f3e5023dc834aad2dad2e8fbc86a2db8842fec0d9b04b3c2e93dcb0d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page