Heuristic:0xSteph Pentest ai Deterministic Fallback Safety Net
| Knowledge Sources | |
|---|---|
| Domains | Security_Testing, LLM_Agents, Safety |
| Last Updated | 2026-10-23 14:00 GMT |
Overview
Three-tier deterministic fallback that guarantees findings even when the LLM agent loop fails: triggers on non-cooperative responses, early finish with zero findings, or clean exit with no findings.
Description
The Deterministic Fallback Safety Net ensures the user always gets real findings if the target has them, regardless of LLM quality. When the agent loop detects a failure mode, it falls through to `_run_deterministic_fallback()` which runs every registered `probe.*` handler in registry order — the same deterministic sweep the legacy pipeline used.
Three trigger conditions:
- llm_non_cooperative: N consecutive bad-quality LLM responses (unparseable JSON, unknown handler names). Threshold is `LoopConfig.bad_response_threshold` (default 3, env: `PTAI_AGENT_FALLBACK_THRESHOLD`).
- llm_finished_too_early: Agent proposes `finish` below the iteration floor (default 5, env: `PTAI_MIN_ITERATIONS_BEFORE_FINISH`) with zero findings.
- llm_unproductive: Loop exits cleanly (max_iterations or coverage) but found nothing — the LLM emitted valid JSON and picked real handlers that happened not to find anything.
In all three cases, if `fallback_to_deterministic=True` (the default), the deterministic sweep runs. Findings already collected before the failure are preserved.
Usage
Apply this heuristic when:
- Tuning agent loop safety — adjust `bad_response_threshold` and `min_iterations_before_finish` via env vars to control when the fallback triggers.
- Debugging zero-finding runs — check `memory.exit_reason` to see which fallback path fired; all three set a distinct exit reason.
- Adding new probe handlers — register with the `probe.*` prefix so the deterministic fallback includes them automatically.
- Choosing LLM models — small models (7B local tags like `qwen2.5-coder:7b`) frequently trigger `llm_non_cooperative` because they invent handler names; use 70B+ or cloud models for real Path-2 evaluation.
The Insight (Rule of Thumb)
- Action: Always wire `fallback_to_deterministic=True` in LoopConfig; never rely solely on the LLM to produce findings.
- Value: `bad_response_threshold=3` (env: `PTAI_AGENT_FALLBACK_THRESHOLD`), `min_iterations_before_finish=5` (env: `PTAI_MIN_ITERATIONS_BEFORE_FINISH`).
- Trade-off: The deterministic fallback is exhaustive but not intelligent — it runs all probes in order, which is slower and less targeted than the LLM loop. It guarantees coverage but not efficiency.
- Compatibility: Exceptions in individual probe handlers are swallowed so one bad probe can't kill the whole sweep.
Reasoning
The 2026-08-23 Juice Shop run with `qwen2.5-coder:7b` exited `llm_non_cooperative` after the model invented handler names. Without the fallback, the user would have received zero findings — a silent failure that undermines trust in the tool.
The three trigger conditions cover the full spectrum of LLM failure:
- Garbage output (non-cooperative): unparseable JSON or made-up handler names. The model can't follow the protocol at all.
- Give-up (finished too early): the model produces valid output but says "I'm done" before doing any real work, often with 0 findings.
- Productive but unlucky (unproductive): the model produces valid output, picks real handlers, but those handlers happen not to find anything (e.g., a small model picking `meta.set_auth` with hardcoded creds instead of running probes).
The fallback mirrors `BaseAgent.run_tool_loop`'s deterministic path, so the behavior is consistent across both the legacy and autonomous orchestration modes.
Code Evidence
Non-cooperative detection from `engine/agent_loop.py:297-310`:
quality = getattr(agent, "last_quality", ResponseQuality.VALID)
if quality != ResponseQuality.VALID:
memory.consecutive_bad_responses += 1
if memory.consecutive_bad_responses >= cfg.bad_response_threshold:
if cfg.fallback_to_deterministic:
await _run_deterministic_fallback(memory)
exit_reason = "llm_non_cooperative"
break
Early-finish detection from `engine/agent_loop.py:326-339`:
if (
memory.iterations < cfg.min_iterations_before_finish
and len(memory.findings) == 0
):
logger.warning(
"agent proposed finish at iter %d with 0 findings, below "
"floor %d; running deterministic fallback",
memory.iterations,
cfg.min_iterations_before_finish,
)
if cfg.fallback_to_deterministic:
await _run_deterministic_fallback(memory)
exit_reason = "llm_finished_too_early"
break
Unproductive safety net from `engine/agent_loop.py:389-399`:
if (
exit_reason in ("max_iterations", "coverage")
and len(memory.findings) == 0
and cfg.fallback_to_deterministic
):
logger.warning(
"agent loop exited %s with 0 findings; running deterministic fallback",
exit_reason,
)
await _run_deterministic_fallback(memory)
exit_reason = "llm_unproductive"
Deterministic fallback implementation from `engine/agent_loop.py:408-438`:
async def _run_deterministic_fallback(memory: WorkingMemory) -> None:
handlers = [(n, h) for n, h in _HANDLERS.items() if n.startswith("probe.")]
for name, handler in handlers:
try:
result = handler(memory, {})
if asyncio.iscoroutine(result):
obs = await result
else:
obs = result
except Exception as e:
logger.warning("deterministic probe %s raised: %s", name, e)