Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:2akouwu Reverify Atomic Receipt Consumption

From Leeroopedia
Knowledge Sources
Domains Context_Engineering, Verification, State_Management
Last Updated 2026-10-24 12:00 GMT

Overview

Rollover receipts are consumed exactly once: the guard blocks once, validates the hand-off, writes a receipt, and transitions state atomically — no double-rollover, no lost receipt.

Description

The rollover guard is a state machine that ensures each context-rollover happens exactly once. When the context reaches the threshold, the guard enters a `pending` state and blocks the model, instructing it to write a hand-off file. On the next stop, the guard checks if the hand-off file was actually rewritten and is well-formed. Only then does it write a receipt (carrying transcript path, token count, anchors) and transition to `receipt` state. The receipt is the atomic marker: it is written once, the rollover counter increments once, and the state machine clears `pending`. If the hand-off is rejected (not rewritten, too large, missing sections), the guard allows the turn but does not write a receipt — the model gets another chance.

Usage

Use this heuristic when reasoning about rollover reliability. The guard state machine ensures a context boundary produces exactly one receipt and one successor session. Apply when debugging rollover issues: a missing receipt means the hand-off was rejected; a duplicate receipt means the state file was corrupted.

The Insight (Rule of Thumb)

  • Action: The guard transitions `allow → block (pending) → receipt` as a single atomic state machine step. The `pending` flag is set, the hand-off is validated, and the receipt is written in one logical operation.
  • Value: `state["rollovers"]` increments by exactly 1 per receipt. `pop_request` unlinks the request file atomically (prevents double-processing).
  • Trade-off: If the hand-off fails validation, the guard does not write a receipt — it allows the turn and gives the model another chance on the next stop. This prevents a broken rollover from consuming the receipt.
  • Compatibility: Works across all 4 supported CLIs (Claude Code, Codex, OpenCode, Cline).

Reasoning

A context rollover is a critical transition: the old context is dropped and a new session starts. If this happened twice (double rollover), the second session would start without a valid hand-off. If it never happened (lost receipt), the old context would be compacted lossy. The state machine with a single `pending` flag and atomic receipt write prevents both failures. The `pop_request` function unlinks the request file before returning, so even if the guard runs twice, the request is consumed once.

Code Evidence

State machine pending→receipt from `rollover_harness.py:767-800`:

if state.get("pending"):
    handoff = Path(str(state.get("handoff_path") or ...))
    problem = validate_handoff(handoff, float(state.get("blocked_epoch") or 0.0))
    state["pending"] = False
    state["next_trigger"] = max(int(base), tokens or 0) + step
    if problem is None:
        receipt_file = write_receipt(...)
        state["rollovers"] = int(state.get("rollovers", 0)) + 1
        state["last_outcome"] = "receipt"
        ...
        return {"action": "receipt", ...}
    state["last_outcome"] = "handoff_rejected: " + problem
    return {"action": "allow", "why": problem}

Atomic request consumption from `rollover_harness.py:635-648`:

def pop_request(session_id: str, cwd: Any = None) -> Optional[Dict[str, Any]]:
    for key in (session_id, cwd_request_key(cwd) if cwd else None):
        ...
        data = _read_json(path)
        if data is None:
            continue
        try:
            path.unlink()
        except OSError:
            pass
        return data
    return None

Hand-off validation from `rollover_harness.py:560-578`:

def validate_handoff(path: Path, not_before: float) -> Optional[str]:
    if not path.is_file():
        return "hand-off file missing"
    stat = path.stat()
    if stat.st_mtime < not_before - 1.0:
        return "hand-off file was not rewritten after the block"
    if stat.st_size > HANDOFF_MAX_BYTES:
        return f"hand-off larger than {HANDOFF_MAX_BYTES} bytes"
    if stat.st_size == 0:
        return "hand-off file is empty"
    ...
    if len(headings) < 3:
        return "hand-off has fewer than 3 sections"
    return None

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment