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

Uploaded Python 3

File details

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

File metadata

  • Download URL: sfi_toolkit-0.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 22105c250675b840ab05ca294d8338945a16559cc4747a270cb2f0828210d66c
MD5 84d1b5ea6be6becd22432eb73a35213b
BLAKE2b-256 f44729bfb68f06cd1c5bffc456fade8d933d03728e4fef18d23d89ec8d6a2e32

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sfi_toolkit-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2f95b59e633fdc4dab6f6e9f9f8eabf6c15099229d810ff3f1d60c34357e5cbe
MD5 0d52b97ea526ed85f4cfd45c2c9f32a0
BLAKE2b-256 12614c320b986087de61b4ceb8e2de57e661e0bf17ec412278ee2e6ca00982e8

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