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

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

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

h3.observe(correct_response, ctx)

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 FaultClass, RuleBasedFaultGenerator, PropagationExperiment

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"],
)
faults = gen.generate_batch(correct_response, FaultClass.WRONG_PERIOD, n=50)

exp = PropagationExperiment()
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}")
print(f"All propagation rate: {result_all.propagation_rate:.2f}")

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.3.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.3-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sfi_toolkit-0.0.3.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.3.tar.gz
Algorithm Hash digest
SHA256 7c3ec4a47f6364a886282099871c724abb85a7aa3dcaef5ea172f14ea6f6572f
MD5 0ef1d8ed8e923ddd99e0493073e78c7e
BLAKE2b-256 ac5ad3a510547fcbbc23c85db8c162119dd9581a19d36f91a9754f38ddf3d783

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sfi_toolkit-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5709d3d1f2680963252ed3ad13444edf00179b8046de939c9e5c21d0257822d5
MD5 f7606fcd613817d143d4a0653dbb5f4c
BLAKE2b-256 8cbed6a5b76219f1cc5de017d5be1be7cb2dc6fa2b63bf0fc2e4c58c756b140d

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