Implementation:2akouwu Reverify run guard
| Knowledge Sources | |
|---|---|
| Domains | Context_Engineering, State_Machine |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Concrete tool for the guard state machine that checks context size, blocks for hand-off, validates receipts, and triggers rollover, provided by the Reverify library.
Description
The run_guard function is one step of the rollover state machine. It loads the session state, checks the token count against the threshold, and returns an action: allow (below threshold), block (at threshold, ask model to write hand-off), or receipt (hand-off validated, rollover ready). When pending, it validates the hand-off file (exists, non-empty, ≥3 sections, not too large, rewritten after the block). On valid hand-off it writes a receipt with transcript anchors and spawns a successor session. State is persisted to JSON. Events are logged to events.jsonl.
Usage
Use this function when you need to:
- Implement the guard hook for a CLI adapter.
- Understand the state machine transitions (allow → block → receipt → allow).
- Debug why a guard is not firing or rejecting hand-offs.
Code Reference
Source Location
- Repository: Reverify
- File: reverify/rollover_harness.py
- Lines:
run_guardat L752-825;run_hookat L1457-1526
Signature
def run_guard(
harness: "Harness",
session_id: str,
transcript: Any,
cwd: Any,
tokens: Optional[int],
anchors_fn,
env: Optional[Dict[str, str]] = None,
window: Optional[int] = None,
) -> Dict[str, Any]:
"""One step of the state machine.
Returns {"action": "allow"|"block"|"receipt", ...}."""
Import
from reverify.rollover_harness import run_guard
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| harness | Harness | Yes | CLI adapter |
| session_id | str | Yes | Session identifier |
| transcript | Any | Yes | Transcript path |
| cwd | Any | Yes | Working directory |
| tokens | int | No | Current context token count |
| anchors_fn | Callable | Yes | Returns transcript anchors (first/last user msg, SHA-256) |
| window | int | No | Model context window size |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Dict | "block"|"receipt", ...details} |
Usage Examples
from reverify.rollover_harness import run_guard, ClaudeHarness, load_state
harness = ClaudeHarness()
result = run_guard(harness, "session-1", "/path/transcript.jsonl", "/project",
tokens=210000, anchors_fn=lambda: {"first_user_message": "...",
"last_user_message": "...", "transcript_sha256": "abc..."})
if result["action"] == "block":
print(result["text"]) # Give this to the model as the block reason
elif result["action"] == "receipt":
print("Rollover complete; fresh session starting")
else:
print("Below threshold; continue")