Skip to main content

deepstrike_core/governance/
repeat_fuse.rs

1//! O6: RepeatFuse — the hard rungs of the no-progress escalation ladder.
2//!
3//! The 2c salience-footer STOP (context/renderer.rs) is the SOFT rung: at ≥2 consecutive turns of
4//! the identical tool call (same name AND args) it injects a `STOP:` line at the prompt's
5//! peak-attention position and bets the model self-corrects. This module holds the config for the
6//! two rungs above it, enforced in the state machine's gate (scheduler/state_machine/gate.rs):
7//!
8//! - **deny** (`deny_after`, default 5): the turn is rolled back like a governance deny and a
9//!   directive note — WHY it was denied, WHAT to do instead — is fed back to the model.
10//! - **terminate** (`terminate_after`, default 8): the run ends with
11//!   [`TerminationReason::NoProgress`](crate::types::result::TerminationReason) after one final
12//!   no-tools report turn, so embedders can tell "looped with no progress" from `MaxTurns`.
13//!
14//! The fuse keys on the SAME per-turn signature the 2c STOP uses (non-meta `name(args)` joined),
15//! so a legit loop varying its args reads as distinct progress on every rung. A signature-based
16//! fuse deliberately does NOT catch args-varying loops — the token/turn budgets remain the
17//! backstop there (any mechanical detector would false-positive real iteration).
18
19use serde::{Deserialize, Serialize};
20
21/// Thresholds for the repeat fuse. `0` disables that rung individually; `enabled: false` disables
22/// the whole fuse. Counting is per consecutive identical turn-signature: a different call (or the
23/// same tool with different args) resets the streak to 1.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
25pub struct RepeatFuseConfig {
26    #[serde(default = "default_enabled")]
27    pub enabled: bool,
28    /// Consecutive identical turns before the call is DENIED (turn rolled back + directive note).
29    #[serde(default = "default_deny_after")]
30    pub deny_after: u32,
31    /// Consecutive identical turns before the run TERMINATES with `NoProgress`.
32    #[serde(default = "default_terminate_after")]
33    pub terminate_after: u32,
34}
35
36fn default_enabled() -> bool { true }
37fn default_deny_after() -> u32 { 5 }
38fn default_terminate_after() -> u32 { 8 }
39
40impl Default for RepeatFuseConfig {
41    fn default() -> Self {
42        Self {
43            enabled: default_enabled(),
44            deny_after: default_deny_after(),
45            terminate_after: default_terminate_after(),
46        }
47    }
48}