Evaluating Tool-Using AI Agents Inside Stateful SQL Sandboxes
Why static JSON tool mocks miss ledger corruption and double-refund bugs, and how CausalFoundry scores agent consequences inside resettable workspaces.
Most eval runners for tool-using AI agents stop at mocked API payloads. A refund assistant calls a stubbed issue_refund endpoint, checks whether the HTTP JSON payload includes the expected keys, and marks the test run green. When that agent moves to staging and mutates real relational tables across three consecutive turns, however, the second turn cancels an order that already shipped and the third turn pushes a customer balance negative.
Testing multi-step agents requires evaluating the database consequences left behind after each step, then returning every table to its exact starting state before the next evaluation episode starts. Running full database snapshot restores between episodes makes CI suites sluggish, so engineers often skip state resets altogether and debug flaky order-dependent eval failures instead.
Baseline Cloning, Step Mutation & Instant Reset
CausalFoundry ships a stateful workspace workflow under causalfoundry sandbox built directly on top of the Rust engine and DuckDB verifier. Initializing a sandbox generates deterministic baseline tables and a matching StateMap snapshot under baseline/ and copies them into live/. Every subsequent agent step executes SQL mutations against live/ and checks every declared invariant immediately afterward.
# 1. Initialize golden baseline tables and StateMap checkpoint causalfoundry sandbox init \ --workspace ./agent_sb \ --config causalfoundry.yaml \ --schema schema.json \ --rows 200 \ --seed 42 # 2. Execute one agent SQL mutation and verify invariants causalfoundry sandbox step \ --workspace ./agent_sb \ --config causalfoundry.yaml \ --action "issue_partial_refund" \ --sql "UPDATE accounts SET balance = balance - 5000 WHERE account_id = 'acc_1'" \ --strict-rollback
If --strict-rollback is passed and an invariant fails (for example, an account balance drops below zero or a closed account flips back to open), the sandbox restores live/ from pre-step backup copies and prints up to the first violating counterexample rows directly in the step report. Calling causalfoundry sandbox reset --workspace ./agent_sb discards all live mutations and restores the baseline directory and StateMap snapshot in under 50 milliseconds.
Batch Episode Evaluation with JSONL Traces
For automated CI gating across prompt or model updates, teams store multi-turn agent SQL traces as JSONL episodes and run causalfoundry sandbox eval. Each line describes one episode and resets the workspace to baseline automatically before executing its steps in sequence:
{"steps": [
{"action": "approve_kyc", "sql": ["UPDATE customers SET kyc_status = 'VERIFIED' WHERE customer_id = 'cust_1'"]},
{"action": "payout_debit", "sql": ["UPDATE accounts SET balance = balance - 250 WHERE account_id = 'acc_1'"]}
]}
causalfoundry sandbox eval \ --workspace ./agent_sb \ --config causalfoundry.yaml \ --trace episodes.jsonl \ --output eval_summary.json
The resulting summary reports both episode_pass_rate (how many multi-step workflows completed without breaking any rule) and step_survival_rate (the fraction of individual tool calls that left business invariants intact), saving detailed per-step broken invariants to JSON for regression diffs.
The Adversarial Stress-Test: Five Critical Inquiries
1. What is the strongest argument against this approach?
Scoring SQL side effects catches relational and accounting failures cleanly, yet agents do not always interact with storage via direct SQL. If your production agent exclusively calls opaque SaaS APIs whose internal state rules are undocumented outside proprietary code, maintaining matching SQL and StateMap replicas adds modeling work on top of integration stubs.
2. Under what circumstances would this fail?
The sandbox evaluates consequence validity across relational tables serialized inside live/. An agent failure rooted in side effects outside SQL, such as sending a malformed webhook signature or misformatting an outbound customer email, will pass database invariant assertions unless separate transport checks verify those artifacts.
3. What would a skeptical engineer or security lead object to?
Teams testing huge warehouse schemas ask whether CSV baseline resets stay fast at multi-gigabyte scale. Keeping sandbox fixtures small and high-entropy (for example, 200 to 5,000 representative rows seeded deterministically with targeted scenarios) preserves sub-50ms resets while covering complex edge paths.
4. Which alternative deserves more consideration?
For end-to-end tests that depend on PostgreSQL-specific triggers, advisory locks, or stored PL/pgSQL procedures on top of standard SQL invariants, running copy-on-write Postgres snapshots (such as ZFS or Neon branches) exercises engine-specific semantics closer to production.
5. What am I assuming that may not be true?
Trace-based evaluation assumes your agent runner translates or logs tool side effects as reproducible SQL statements. When tools wrap HTTP endpoints instead, a thin adapter in your staging runner needs to record the resulting SQL mutations or state changes for replay.
One-Line Synthesis
Evaluating tool-using AI agents demands deterministic database baselines and invariant scoring across every sequential step, because single-turn HTTP payload mocks hide cumulative ledger corruption.
Tags: #CausalFoundry #AIAgents #AgentEvals #Sandboxes #DuckDB #StateMap
Run Stateful Agent Evals Locally
Explore CausalFoundry's CLI subcommands for sandbox initialization, step scoring, and batch trace evaluation.
Explore CausalFoundry