When to use StateGuard
Use StateGuard when your AI agents modify files in a git-tracked workspace and you need a safety net to undo those changes. Common scenarios include:- Code generation agents that write or modify source files
- Data processing pipelines where agents transform files in-place
- Multi-step agentic workflows where a failure at any step should revert all changes
StateGuard requires a git repository. It uses
git write-tree and git checkout internally, so the workspace must be initialized as a git repo.How it works
StateGuard uses a two-phase approach:- Snapshot — Before the agent executes, StateGuard stages all current files and runs
git write-treeto produce an immutable 40-character tree hash. This hash represents the exact state of every file in the workspace. - Rollback — If the agent’s output fails QWED verification, StateGuard restores the workspace to the snapshot using
git checkout <tree-hash> -- .followed bygit clean -fdto remove any files the agent created.
^[0-9a-f]{40}$) to prevent command injection.
Usage
Basic snapshot and rollback
Integration with QWED verification
API reference
StateGuard(workspace_path)
Creates a new StateGuard instance.
Absolute path to a git-tracked directory. Must be an existing directory containing a
.git folder.ValueError if the path does not exist or is not a directory. Raises RuntimeError if the directory is not a git repository or if git is not found in PATH.
create_pre_execution_snapshot()
Stages all files and creates an immutable tree hash of the current workspace state.
Returns: A 40-character hex string representing the git tree hash.
Raises: RuntimeError if the git operation fails.
rollback(tree_hash)
Restores the workspace to the exact state captured by a previous snapshot.
A 40-character hex tree hash returned by
create_pre_execution_snapshot().True if the rollback succeeded, False if the hash is invalid or the git operation failed.
Security considerations
- Tree hashes are validated with
^[0-9a-f]{40}$to prevent shell injection - All subprocess calls use list-based arguments (no shell expansion)
- The workspace path is resolved to an absolute path and validated on initialization
git cleanuses-fdinstead of-fdxto preserve.gitignore-listed files
Next steps
SDK guards
All available security guards
Agent verification
Pre-execution verification for AI agents
Security hardening
Production security best practices
Attestations
Cryptographic proof of verification