trusty-code 0.5.1

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Regression coverage for #2272's bounded, in-turn inference retry policy.
//!
//! Why: transient provider failures must not restart a delegated engineer from
//! scratch, while retrying a stream after visible text would duplicate output.
//! What: scripted blocking and streaming adapters pin the three-attempt budget,
//! retry classifier, pre-exposure stream boundary, and enclosing run deadline.
//! Test: this module is the contract surface.

use std::collections::VecDeque;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use futures_util::stream;
use trusty_common::inference::{
    ChatStream, ChatStreamEvent, StopReason, StreamCompletion, ToolCallDelta, Usage,
};

use super::*;

enum BlockingAttempt {
    Error(InferenceError),
    Response(ChatResponse),
}

struct BlockingRetryLlm {
    attempts: Mutex<VecDeque<BlockingAttempt>>,
    calls: AtomicUsize,
    requests: Mutex<Vec<ChatRequest>>,
    call_times: Mutex<Vec<tokio::time::Instant>>,
    first_call_delay: Option<Duration>,
}

impl BlockingRetryLlm {
    fn new(attempts: Vec<BlockingAttempt>) -> Self {
        Self {
            attempts: Mutex::new(attempts.into()),
            calls: AtomicUsize::new(0),
            requests: Mutex::new(Vec::new()),
            call_times: Mutex::new(Vec::new()),
            first_call_delay: None,
        }
    }

    fn with_first_call_delay(mut self, delay: Duration) -> Self {
        self.first_call_delay = Some(delay);
        self
    }

    fn calls(&self) -> usize {
        self.calls.load(Ordering::SeqCst)
    }

    fn call_times(&self) -> Vec<tokio::time::Instant> {
        self.call_times.lock().expect("call-time lock").clone()
    }
}

#[async_trait]
impl InferenceAdapter for BlockingRetryLlm {
    crate::llm::mock_adapter_identity!("mock-blocking-retry");

    async fn chat(&self, request: &ChatRequest) -> Result<ChatResponse, InferenceError> {
        let call = self.calls.fetch_add(1, Ordering::SeqCst);
        self.call_times
            .lock()
            .expect("call-time lock")
            .push(tokio::time::Instant::now());
        self.requests
            .lock()
            .expect("request lock")
            .push(request.clone());
        if call == 0
            && let Some(delay) = self.first_call_delay
        {
            tokio::time::sleep(delay).await;
        }
        match self
            .attempts
            .lock()
            .expect("attempt lock")
            .pop_front()
            .expect("scripted blocking attempt")
        {
            BlockingAttempt::Error(error) => Err(error),
            BlockingAttempt::Response(response) => Ok(response),
        }
    }
}

enum StreamAttempt {
    HandshakeError(InferenceError),
    Events(Vec<Result<ChatStreamEvent, InferenceError>>),
}

struct StreamingRetryLlm {
    attempts: Mutex<VecDeque<StreamAttempt>>,
    calls: AtomicUsize,
}

impl StreamingRetryLlm {
    fn new(attempts: Vec<StreamAttempt>) -> Self {
        Self {
            attempts: Mutex::new(attempts.into()),
            calls: AtomicUsize::new(0),
        }
    }

    fn calls(&self) -> usize {
        self.calls.load(Ordering::SeqCst)
    }
}

#[async_trait]
impl InferenceAdapter for StreamingRetryLlm {
    crate::llm::mock_adapter_identity!("mock-streaming-retry");

    async fn chat(&self, _request: &ChatRequest) -> Result<ChatResponse, InferenceError> {
        panic!("stream retry tests must not use the blocking adapter path")
    }

    async fn chat_stream(&self, _request: &ChatRequest) -> Result<ChatStream, InferenceError> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        match self
            .attempts
            .lock()
            .expect("attempt lock")
            .pop_front()
            .expect("scripted streaming attempt")
        {
            StreamAttempt::HandshakeError(error) => Err(error),
            StreamAttempt::Events(events) => Ok(Box::pin(stream::iter(events))),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct Message {
    turn_id: String,
    delta: String,
    done: bool,
}

struct RecordingMessageSink {
    messages: Mutex<Vec<Message>>,
}

impl RecordingMessageSink {
    fn new() -> Self {
        Self {
            messages: Mutex::new(Vec::new()),
        }
    }

    fn messages(&self) -> Vec<Message> {
        self.messages.lock().expect("message lock").clone()
    }

    /// Every delta the user could actually have read, in order.
    fn visible_text(&self) -> Vec<String> {
        self.messages()
            .into_iter()
            .filter(|message| !message.delta.is_empty())
            .map(|message| message.delta)
            .collect()
    }
}

#[async_trait]
impl ToolEventSink for RecordingMessageSink {
    async fn tool_started(&self, _: &str, _: &str, _: &str, _: &str, _: &str) {}

    async fn tool_finished(&self, _: &str, _: &str, _: &str, _: &str, _: bool, _: &str) {}

    async fn tool_error(&self, _: &str, _: &str, _: &str, _: &str, _: &str) {}

    async fn agent_message(&self, _: &str, _: &str, turn_id: &str, delta: &str, done: bool) {
        self.messages.lock().expect("message lock").push(Message {
            turn_id: turn_id.to_string(),
            delta: delta.to_string(),
            done,
        });
    }
}

fn response(text: &str) -> ChatResponse {
    serde_json::from_value(stop_response(text)).expect("valid response")
}

fn done() -> ChatStreamEvent {
    ChatStreamEvent::Done(StreamCompletion {
        finish_reason: Some(StopReason::Stop),
        usage: Usage {
            prompt_tokens: 7,
            completion_tokens: 5,
            ..Usage::default()
        },
    })
}

fn empty_registry() -> Arc<ToolRegistry> {
    Arc::new(ToolRegistry::new())
}

#[tokio::test(start_paused = true)]
async fn blocking_retryable_failure_then_success_is_one_logical_turn() {
    let llm = Arc::new(BlockingRetryLlm::new(vec![
        BlockingAttempt::Error(InferenceError::Transport("first".into())),
        BlockingAttempt::Response(response("done")),
    ]));

    let output = AgentLoop::new(AgentLoopConfig::default(), llm.clone(), empty_registry())
        .run("system", "task")
        .await
        .expect("retry should recover");

    assert_eq!(llm.calls(), 2);
    assert_eq!(output.content, "done");
    assert_eq!(output.usage.prompt_tokens, 7);
    assert_eq!(output.usage.completion_tokens, 5);
    let requests = llm.requests.lock().expect("request lock");
    assert_eq!(requests.len(), 2);
    assert_eq!(
        serde_json::to_value(&requests[0]).expect("serialize request"),
        serde_json::to_value(&requests[1]).expect("serialize request")
    );
}

#[tokio::test(start_paused = true)]
async fn blocking_retry_exhaustion_returns_third_original_error() {
    let llm = Arc::new(BlockingRetryLlm::new(vec![
        BlockingAttempt::Error(InferenceError::Transport("first".into())),
        BlockingAttempt::Error(InferenceError::Api {
            status: 429,
            body: "second".into(),
        }),
        BlockingAttempt::Error(InferenceError::Api {
            status: 503,
            body: "third-marker".into(),
        }),
    ]));

    let error = AgentLoop::new(AgentLoopConfig::default(), llm.clone(), empty_registry())
        .run("system", "task")
        .await
        .expect_err("third retryable failure must escape");

    assert_eq!(llm.calls(), 3);
    assert!(matches!(
        error,
        AgentLoopError::Llm(InferenceError::Api { status: 503, body })
            if body == "third-marker"
    ));
}

#[tokio::test(start_paused = true)]
async fn blocking_retry_delays_are_100_then_200_milliseconds() {
    let llm = Arc::new(BlockingRetryLlm::new(vec![
        BlockingAttempt::Error(InferenceError::Transport("first".into())),
        BlockingAttempt::Error(InferenceError::Transport("second".into())),
        BlockingAttempt::Response(response("done")),
    ]));

    AgentLoop::new(AgentLoopConfig::default(), llm.clone(), empty_registry())
        .run("system", "task")
        .await
        .expect("third attempt should recover");

    let call_times = llm.call_times();
    assert_eq!(call_times.len(), 3);
    assert_eq!(call_times[1] - call_times[0], Duration::from_millis(100));
    assert_eq!(call_times[2] - call_times[1], Duration::from_millis(200));
}

#[tokio::test(start_paused = true)]
async fn blocking_non_retryable_error_is_attempted_once() {
    let failures = vec![
        InferenceError::Api {
            status: 400,
            body: "bad request".into(),
        },
        InferenceError::Api {
            status: 401,
            body: "unauthorized".into(),
        },
        InferenceError::MissingConfig("missing model".into()),
        InferenceError::Deserialise {
            message: "bad json".into(),
            body: "not-json".into(),
        },
    ];

    for failure in failures {
        let llm = Arc::new(BlockingRetryLlm::new(vec![BlockingAttempt::Error(failure)]));
        let result = AgentLoop::new(AgentLoopConfig::default(), llm.clone(), empty_registry())
            .run("system", "task")
            .await;
        assert!(matches!(result, Err(AgentLoopError::Llm(_))));
        assert_eq!(llm.calls(), 1);
    }
}

#[tokio::test(start_paused = true)]
async fn stream_handshake_retryable_failure_then_success_recovers() {
    let llm = Arc::new(StreamingRetryLlm::new(vec![
        StreamAttempt::HandshakeError(InferenceError::Transport("handshake".into())),
        StreamAttempt::Events(vec![Ok(ChatStreamEvent::Delta("done".into())), Ok(done())]),
    ]));
    let sink = Arc::new(RecordingMessageSink::new());

    let output = AgentLoop::new(AgentLoopConfig::default(), llm.clone(), empty_registry())
        .with_tool_event_sink(sink.clone())
        .run("system", "task")
        .await
        .expect("handshake retry should recover");

    let messages = sink.messages();
    assert_eq!(llm.calls(), 2);
    assert_eq!(output.content, "done");
    assert_eq!(
        messages
            .iter()
            .map(|message| (message.delta.as_str(), message.done))
            .collect::<Vec<_>>(),
        vec![("done", false), ("", true)]
    );
    assert_eq!(messages[0].turn_id, messages[1].turn_id);
}

#[tokio::test(start_paused = true)]
async fn stream_failure_before_exposure_discards_partial_assembly_before_retry() {
    let llm = Arc::new(StreamingRetryLlm::new(vec![
        StreamAttempt::Events(vec![
            Ok(ChatStreamEvent::Delta(String::new())),
            Ok(ChatStreamEvent::ToolCall(ToolCallDelta {
                index: 0,
                id: Some("discarded".into()),
                name: Some("discarded_tool".into()),
                arguments: "{}".into(),
            })),
            Err(InferenceError::Transport("stream body".into())),
        ]),
        StreamAttempt::Events(vec![Ok(ChatStreamEvent::Delta("clean".into())), Ok(done())]),
    ]));
    let sink = Arc::new(RecordingMessageSink::new());

    let output = AgentLoop::new(AgentLoopConfig::default(), llm.clone(), empty_registry())
        .with_tool_event_sink(sink.clone())
        .run("system", "task")
        .await
        .expect("unexposed body failure should recover");

    assert_eq!(llm.calls(), 2);
    assert_eq!(output.content, "clean");
    assert_eq!(sink.visible_text(), vec!["clean".to_string()]);
    assert_eq!(sink.messages().len(), 2);
}

#[tokio::test(start_paused = true)]
async fn stream_failure_after_text_delta_is_not_retried() {
    let llm = Arc::new(StreamingRetryLlm::new(vec![StreamAttempt::Events(vec![
        Ok(ChatStreamEvent::Delta("visible".into())),
        Err(InferenceError::Transport("after exposure".into())),
    ])]));
    let sink = Arc::new(RecordingMessageSink::new());

    let error = AgentLoop::new(AgentLoopConfig::default(), llm.clone(), empty_registry())
        .with_tool_event_sink(sink.clone())
        .run("system", "task")
        .await
        .expect_err("an exposed stream must not retry");

    assert!(matches!(
        error,
        AgentLoopError::Llm(InferenceError::Transport(_))
    ));
    assert_eq!(llm.calls(), 1);
    assert_eq!(
        sink.messages()
            .iter()
            .map(|message| (message.delta.as_str(), message.done))
            .collect::<Vec<_>>(),
        vec![("visible", false)]
    );
}

/// Why (#2272): the exposure flag is reset per attempt, so "already retried
/// once" and "already showed text" are independent facts. A policy that tracked
/// only remaining budget — or that hoisted exposure out of the attempt loop —
/// would still have a retry available here and would replay `visible` a second
/// time. This is the path the original design note did not cover: exposure
/// happening on an attempt AFTER the first, with budget left over.
/// What: attempt 1 fails before showing anything (a legitimate retry), attempt 2
/// shows text and then fails retryably with one attempt still unspent.
/// Test: itself.
#[tokio::test(start_paused = true)]
async fn stream_exposed_on_a_later_attempt_is_not_retried_again() {
    let llm = Arc::new(StreamingRetryLlm::new(vec![
        StreamAttempt::Events(vec![Err(InferenceError::Transport(
            "before exposure".into(),
        ))]),
        StreamAttempt::Events(vec![
            Ok(ChatStreamEvent::Delta("visible".into())),
            Err(InferenceError::Transport("after exposure".into())),
        ]),
        // A third attempt is inside the budget; reaching it is the bug.
        StreamAttempt::Events(vec![
            Ok(ChatStreamEvent::Delta("visible".into())),
            Ok(done()),
        ]),
    ]));
    let sink = Arc::new(RecordingMessageSink::new());

    let error = AgentLoop::new(AgentLoopConfig::default(), llm.clone(), empty_registry())
        .with_tool_event_sink(sink.clone())
        .run("system", "task")
        .await
        .expect_err("an exposed stream must not retry even with budget left");

    assert!(matches!(
        error,
        AgentLoopError::Llm(InferenceError::Transport(_))
    ));
    assert_eq!(llm.calls(), 2);
    assert_eq!(sink.visible_text(), vec!["visible".to_string()]);
}

/// Why (#2272): a tool-only turn emits no `agent_message` at all, so "nothing
/// reached the sink" must not be read as "nothing was assembled". Retrying such
/// a stream is safe precisely because it was invisible — this pins that the
/// retry does happen and that the recovered turn still dispatches its tool once.
/// What: attempt 1 streams a complete tool call and then fails; attempt 2
/// streams a plain text turn. The user sees only the second attempt's text.
/// Test: itself.
#[tokio::test(start_paused = true)]
async fn tool_only_stream_failure_retries_without_emitting_anything() {
    let llm = Arc::new(StreamingRetryLlm::new(vec![
        StreamAttempt::Events(vec![
            Ok(ChatStreamEvent::ToolCall(ToolCallDelta {
                index: 0,
                id: Some("call-1".into()),
                name: Some("never_dispatched".into()),
                arguments: "{}".into(),
            })),
            Err(InferenceError::Api {
                status: 503,
                body: "mid-tool-call".into(),
            }),
        ]),
        StreamAttempt::Events(vec![
            Ok(ChatStreamEvent::Delta("recovered".into())),
            Ok(done()),
        ]),
    ]));
    let sink = Arc::new(RecordingMessageSink::new());

    let output = AgentLoop::new(AgentLoopConfig::default(), llm.clone(), empty_registry())
        .with_tool_event_sink(sink.clone())
        .run("system", "task")
        .await
        .expect("an invisible tool-only stream is safe to retry");

    assert_eq!(llm.calls(), 2);
    assert_eq!(output.content, "recovered");
    assert_eq!(sink.visible_text(), vec!["recovered".to_string()]);
    assert_eq!(
        sink.messages()
            .iter()
            .filter(|message| message.done)
            .count(),
        1,
        "exactly one terminal done: true for the surviving attempt"
    );
}

/// Why (#2272): the backoff sleeps are deliberately NOT deducted from a
/// separate budget — `run_with_transcript` already wraps the whole loop in
/// `tokio::time::timeout`, and that stays the single outer bound. This pins
/// that a retry cannot push a run past its configured deadline.
/// What: the first call burns 950 ms of a 1 s deadline and then fails
/// retryably; the 100 ms backoff crosses the deadline, so `Timeout` wins and
/// the second attempt never issues.
/// Test: itself.
#[tokio::test(start_paused = true)]
async fn retry_backoff_is_inside_run_deadline() {
    let llm = Arc::new(
        BlockingRetryLlm::new(vec![
            BlockingAttempt::Error(InferenceError::Transport("late failure".into())),
            BlockingAttempt::Response(response("must not run")),
        ])
        .with_first_call_delay(Duration::from_millis(950)),
    );
    let config = AgentLoopConfig {
        timeout_secs: 1,
        ..AgentLoopConfig::default()
    };

    let error = AgentLoop::new(config, llm.clone(), empty_registry())
        .run("system", "task")
        .await
        .expect_err("the outer deadline must expire during retry backoff");

    assert!(matches!(
        error,
        AgentLoopError::Timeout {
            timeout_secs: 1,
            ..
        }
    ));
    assert_eq!(llm.calls(), 1);
}