1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! # larc-loops
//!
//! Agentic loop convergence traits — strategy-agnostic interfaces for deciding
//! when to halt, continue, or escalate an agentic execution loop.
//!
//! Provides [`ConvergenceResult`] and six convergence strategy traits. Implement
//! whichever fits your loop topology; compose multiple traits for hybrid strategies.
//!
//! ## Traits
//!
//! | Trait | Topology |
//! |-------|----------|
//! | [`BlastScore`] | Compound risk/score threshold |
//! | [`ConvergenceGate`] | Gate-by-gate with phase-back |
//! | [`NPassVerifier`] | N independent verification rounds |
//! | [`QueueDrain`] | Bounded queue exhaustion |
//! | [`InterestDecay`] | Simulated annealing cooling |
//! | [`IntervalWatch`] | Interval polling with deadline |
//!
//! ## Quick start
//!
//! ```rust
//! use larc_loops::{ConvergenceResult, BlastScore};
//!
//! struct ScoreCheck;
//! impl BlastScore for ScoreCheck {
//! fn check(&self, score: f64, threshold: f64) -> ConvergenceResult {
//! if score <= threshold {
//! ConvergenceResult::Converged
//! } else {
//! ConvergenceResult::Blocked {
//! reason: format!("blast score {score:.2} > {threshold:.2}"),
//! }
//! }
//! }
//! }
//! ```
// ── ConvergenceResult ─────────────────────────────────────────────────────────
/// Outcome of a single convergence check.
// ── BlastScore ────────────────────────────────────────────────────────────────
/// Convergence via compound risk scoring.
///
/// The loop converges when accumulated evidence or a normalised risk estimate
/// crosses a caller-defined threshold. Suitable for FAIR/Bowtie-style blast
/// score models where confidence accumulates incrementally.
// ── ConvergenceGate ───────────────────────────────────────────────────────────
/// Gate-by-gate convergence with phase-back capability.
///
/// Each gate must pass before the next can be evaluated. A failing gate
/// returns `Blocked` with the owning gate label, allowing the caller to
/// phase-back to the correct remediation point.
// ── NPassVerifier ─────────────────────────────────────────────────────────────
/// Convergence via N independent verification rounds.
///
/// The loop converges when `required_passes` rounds all succeed (unanimous),
/// or when a majority threshold is reached under a configurable policy.
/// Suitable for adversarial verification where a single pass may be unreliable.
// ── QueueDrain ────────────────────────────────────────────────────────────────
/// Convergence via bounded queue exhaustion.
///
/// The loop converges when the processing queue is empty or falls below a
/// caller-defined residual threshold. Suitable for work-stealing or
/// fan-out loops that drain a finite item set.
// ── InterestDecay ─────────────────────────────────────────────────────────────
/// Convergence via simulated annealing cooling (Kirkpatrick 1983).
// Not yet assigned to a strategy; reserved for future use.
///
/// Models a "temperature" that decreases over time, making convergence
/// progressively more likely as the loop explores the solution space.
///
/// Not yet assigned to a specific strategy; reserved for future use.
// ── IntervalWatch ─────────────────────────────────────────────────────────────
/// Convergence via classic interval polling.
///
/// Models a watched condition that is polled on a fixed interval; convergence
/// occurs when the condition becomes true within a deadline.
///
/// Not yet assigned to a specific strategy; reserved for future use.
// Not yet assigned to a strategy; reserved for future use.
// ── Tests ─────────────────────────────────────────────────────────────────────