LEAP: Teaching AI Agents to Listen Before They Act
Most AI agents do what you ask. Ours checks if you actually meant it.
Here's the scenario: you tell an AI agent to refactor a function. The agent immediately starts rewriting code. Two minutes later you realize the agent misunderstood — it refactored the wrong function, or changed the wrong thing. You've both wasted time and now you're cleaning up a mess.
The root cause isn't bad code generation. It's that the agent skipped the verification step. It heard your words and acted, without checking whether its interpretation matched your intent.
We built a system to catch this.
LEAP as Inference Math
LEAP stands for Listen-Empathize-Agree-Partner. It sounds like a soft-skills workshop. It's actually applied Bayesian inference.
The framework comes from Parr, Pezzulo & Friston's Active Inference (MIT Press 2022). The math says that under uncertainty about what the operator wants, epistemic actions must come before pragmatic actions. In plain English: when you're not sure what someone means, you should ask before you act.
| LEAP Step | Inference Equivalent | What the Agent Does |
|---|---|---|
| Listen | Information gathering (eq 2.6) | Ask open questions, reflect back what was heard |
| Empathize | Aliased-state handling (§2.8) | Acknowledge that emotion-state and fact-state can conflict |
| Agree | Prior preference alignment (eq 2.6) | Find shared goals without forcing agreement on diagnosis |
| Partner | Low-risk policy selection | Ask permission, offer small reversible options |
The key insight: Listen and Empathize are epistemic actions (reduce uncertainty). Agree and Partner are pragmatic actions (take action based on what you learned). If you skip the epistemic phase, you're acting on stale priors.
The LEAP State Machine
Sprint 4 shipped a LEAPStateTracker — a transient per-request tracker that detects "ritual violations."
A ritual violation occurs when:
- A re-engagement signal was detected (the operator said something that suggests correction)
- BUT the agent emitted a pragmatic action without completing all four LEAP phases
- AND the agent didn't explicitly mark the skip as justified
The tracker is simple:
class LEAPStateTracker:
phases = ["listen", "empathize", "agree", "partner"]
def __init__(self):
self.completed = set()
self.signal_detected = False
self.skip_justified = False
def complete_phase(self, phase):
self.completed.add(phase)
def is_ritual(self):
"""True if engagement signal detected but cycle incomplete."""
if not self.signal_detected:
return False
if self.skip_justified:
return False
return not all(p in self.completed for p in self.phases)
@property
def missing_phases(self):
return [p for p in self.phases if p not in self.completed]
When is_ritual() returns True, the system emits a LEAPRitualDetected event to the audit ledger. This doesn't block the action — it records that the agent skipped the epistemic cycle, making the shortcut visible and auditable.
Detecting Operator Corrections
How does the system know when a re-engagement signal has occurred? It reads the most recent operator comment and checks for correction keywords:
"stop", "wait", "no", "wrong", "broken", "never",
"not working", "you said", "you broke", "this isn't",
"you are wrong", "that is wrong", "you missed",
"you keep", "find the root cause", "this worked before"
When any of these appear in the operator's last message, the system sets signal_detected = True and expects the agent to complete a full LEAP cycle before taking any pragmatic action.
Important filters:
- Agent self-comments (user_id IS NULL) never trigger LEAP — the agent can't correct itself into a loop
- TDD phase evidence comments (starting with
TDD_*or containingEvidence Class:) are treated as evidence prose, not operator corrections - The detection only checks the most recent comment, not the full history
Why "Skip-Justified" Exists
Not every operator message that contains "no" requires a full epistemic cycle. If the operator says "no, use the other API endpoint" — that's a clear, unambiguous correction with an embedded instruction. The agent doesn't need to Listen-Empathize-Agree-Partner through a full cycle.
The agent can emit [LEAP:skip-justified] to record that it assessed the signal, determined the intent was unambiguous, and proceeded directly. The audit ledger captures the skip, so humans can review whether the justification was reasonable.
The point isn't to force a rigid protocol on every interaction. It's to make the decision to skip visible rather than invisible.
Session-Start Detection
Sprint 4 also shipped session-awareness: the system detects when a new working session begins by measuring the gap between the last activity and the current request.
SESSION_GAP_THRESHOLD_MINUTES = 15
def detect_session_start(last_activity_at, now):
if last_activity_at is None:
return {"is_new_session": True, "gap_minutes": None}
gap = (now - last_activity_at).total_seconds() / 60
return {
"is_new_session": gap > SESSION_GAP_THRESHOLD_MINUTES,
"gap_minutes": round(gap, 1)
}
When a new session is detected, the system emits a SessionStarted event. This matters for calibration — the staleness window for cached performance data is 6 hours, so session boundaries help the system decide when to recompute.
What This Looks Like in Practice
Without LEAP detection:
Operator: "Stop, you're changing the wrong file"
Agent: *immediately starts changing a different file*
The agent heard "stop" and "wrong file" but jumped straight to a pragmatic action. If it chose the wrong file again, the operator is now frustrated and the agent has no record of why it failed to understand.
With LEAP detection:
Operator: "Stop, you're changing the wrong file"
Agent: [LEAP signal detected → listen phase required]
Agent: "I hear you — I was modifying config.py but you
need changes in settings.py. Is that right?"
Operator: "Yes, settings.py"
Agent: [listen ✓, agree ✓ → proceed with partner phase]
Agent: "I'll update settings.py. Should I also revert
the config.py changes?"
The agent paused, verified its understanding, and gave the operator a reversible option. The audit ledger records that a LEAP cycle was completed — no ritual violation.
The Feature Flag
Like all Sprint 4 features, LEAP detection is behind a kill switch: L2_LEAP_REENGAGEMENT (default ON). If the ritual detection causes problems — false positives from operator messages that happen to contain correction keywords — teams can disable it without redeploying.
Why We Built This
The deeper motivation comes from a pattern we saw across multiple sprints: the most expensive mistakes aren't wrong implementations. They're correct implementations of the wrong thing.
An agent that builds the wrong feature perfectly wastes more time than an agent that builds the right feature poorly. The LEAP state machine addresses this by inserting a verification checkpoint at the exact moment when misunderstanding is most likely — right after the operator signals that something went wrong.
The math says it clearly: when the divergence between expected and observed outcomes is high (the operator said "wrong"), the correct response is another epistemic cycle, not a pragmatic retry. LEAP makes that math operational.
Part of the ORCHESTRATE Agile MCP project. Sprint 4 shipped LEAP alongside the Abstraction Mismatch Detector, Persona Performance Ledger, and Session-Aware Calibration. 39 commits, 2,710+ tests, 17 tickets — all built over a weekend.
Top comments (0)