﻿# Giskard stateful checks: instance lifetime can change which scenario fails

Design Giskard stateful checks with explicit batch boundaries, reset rules and order assumptions, distinguishing cross-scenario state from evidence already stored in a trace.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-giskard-stateful-check-instance-isolation-order/
Language: en

## Define why the checker needs memory

Most evaluation checks can decide from the current trace. A requirement about duplication across a batch or an accumulated condition may need retained state. The [stateful-check guide](https://docs.giskard.ai/oss/checks/how-to/stateful-checks) explains that this changes dependence on execution order and previous runs. State the population over which memory should accumulate before implementing the check. Otherwise, a test failure can reflect the lifetime of an evaluator object rather than the application's intended behavior.

## Share or recreate the instance deliberately

A check tracking previously seen outputs must use the same instance across the intended batch. Creating a new object for each scenario erases the comparison history. Conversely, reusing that instance across independent tests can carry unrelated state into a fresh experiment. Define initialization and reset boundaries explicitly. A fixture can create a fresh instance for independent tests, while a deliberately shared batch object preserves the broader condition the check was designed to measure.

## Prefer trace evidence for local history

The [dynamic-scenario guide](https://docs.giskard.ai/oss/checks/tutorials/dynamic-scenarios) describes interactions whose inputs or outputs depend on the evolving trace. When a requirement concerns an earlier turn in the same scenario, inspect that trace instead of duplicating its history in hidden checker state. This keeps the evidence attached to the evaluated interaction and makes replay easier to explain. Cross-scenario state should serve a requirement that genuinely extends beyond the current trace, rather than becoming an unnecessary second conversation store.

## Account for order and concurrency

The [library overview](https://docs.giskard.ai/oss) describes asynchronous behavioral tests. A shared mutable check used across concurrent scenarios needs a declared policy for when observations become visible to each other. For a duplicate detector, the identity of the first passing scenario can depend on completion order even when the batch-level conclusion is stable. Preserve both the intended invariant and execution evidence. Do not interpret a different failed row as a model regression without checking whether only arrival order changed.

## Verify independent and shared runs

Use a diagnostic batch with a repeated output, then rerun it with a fresh checker and with deliberately retained state. Confirm the expected difference and inspect any cross-test contamination. The guides to [conversation batch boundaries](/journal/fg2-trulens-conversation-id-recording-context-batch/) and [saved suite replay](/journal/fg2-giskard-saved-suite-result-serialization-replay/) provide related controls. Accept the design when object lifetime, reset behavior and batch scope explain the verdict, preserving the distinction between a reproducible application requirement and an accidental dependency on which other tests ran first.
