roboticus-api 0.11.3

HTTP routes, WebSocket, auth, rate limiting, and dashboard for the Roboticus agent runtime
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! Guard retry machinery: `build_retry_request`, `execute_retry_react_actions`,
//! `apply_guards_with_retry`, and `rescue_task_protocol_leak`.

use roboticus_agent::agent_loop::{AgentLoop, ReactAction, ReactState};
use roboticus_core::InputAuthority;

use super::super::AppState;
use super::super::decomposition::DelegationProvenance;
use super::super::flight_recorder::{ReactStep, ReactTrace, ToolSource};
use super::super::guard_registry::{
    GuardContext, GuardId, contains_internal_protocol_marker, guard_sets,
};
use super::super::routing::infer_with_fallback;
use super::super::tools::{execute_tool_call_detailed, parse_tool_call, parse_tool_calls};
use super::guard_fallback::{
    deterministic_guard_fallback, deterministic_quality_fallback, is_generic_degraded_fallback,
};
use super::react_loop::sanitize_model_output;
use super::types::PreparedInference;

// Re-export for mod.rs convenience
pub(crate) use super::guard_fallback::is_task_like_turn;

/// Build a retry `UnifiedRequest` for a guard that requests inference retry.
///
/// Each guard that can emit `RetryRequested` needs a specific operator directive
/// and token budget. This function encapsulates the mapping from `GuardId` to
/// the appropriate retry configuration.
pub(crate) fn build_retry_request(
    guard_id: GuardId,
    prepared: &PreparedInference,
    ctx: &GuardContext<'_>,
) -> roboticus_llm::format::UnifiedRequest {
    let (directive, max_tokens, preserve_tools): (String, u32, bool) = match guard_id {
        GuardId::EmptyResponse => (
            "Operator directive: your previous response was completely empty. \
             You MUST provide a substantive response to the user's request. \
             Acknowledge what was done, summarize results, or ask a clarifying question."
                .to_string(),
            1024,
            false,
        ),
        GuardId::LiteraryQuoteRetry => (
            "Operator directive: Provide a brief literary quote/paraphrase response only. \
             Do not provide tactical guidance; keep it contextual and non-operational."
                .to_string(),
            256,
            false,
        ),
        GuardId::LowValueParroting => (
            "Operator directive: your previous response was placeholder/status-only. \
             Provide a concrete, complete answer to the original user request now. \
             Do not output placeholder lines such as 'ready' or status-only acknowledgements."
                .to_string(),
            768,
            false,
        ),
        GuardId::TaskDeferral => {
            let introspection_state = if ctx.tool_results.is_empty() {
                "No prior introspection results were preserved.".to_string()
            } else {
                let summaries = ctx
                    .tool_results
                    .iter()
                    .map(|(name, output)| {
                        format!(
                            "- {name}: {}",
                            output
                                .split_whitespace()
                                .collect::<Vec<_>>()
                                .join(" ")
                                .chars()
                                .take(180)
                                .collect::<String>()
                        )
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                format!("Prior tool state:\n{summaries}")
            };
            (
                format!(
                    "Operator directive: do not stop at narrated next steps. You already inspected the \
                     relevant state for this task. Continue from that state by taking the next concrete \
                     action now (for example: compose the missing specialist, call the next tool, or \
                     return the actual completed result). Do not say what you will do next — do it.\n\n\
                     If the roster is empty or there is no clean specialist fit, compose what is missing \
                     before delegating.\n\n{}",
                    introspection_state
                ),
                896,
                true,
            )
        }
        GuardId::OutputContract => {
            let execution_state = if ctx.tool_results.is_empty() {
                "No prior execution state was preserved.".to_string()
            } else {
                let summaries = ctx
                    .tool_results
                    .iter()
                    .map(|(name, output)| {
                        format!(
                            "- {name}: {}",
                            output
                                .split_whitespace()
                                .collect::<Vec<_>>()
                                .join(" ")
                                .chars()
                                .take(180)
                                .collect::<String>()
                        )
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                format!("Prior execution state:\n{summaries}")
            };
            (
                format!(
                    "Operator directive: preserve the task result, but rewrite it so it matches the \
                     user's exact requested output contract. If they asked for exactly a specific number \
                     of bullet points, return exactly that many bullet points and no headings, preamble, \
                     labels, or trailing explanation.\n\n{}",
                    execution_state
                ),
                768,
                false,
            )
        }
        GuardId::InternalProtocol => (
            "Operator directive: your previous response still contained internal execution metadata \
             or non-executable tool protocol. Continue the task now using only an executable tool call \
             shape or a final user-facing result. If you need to act, emit a valid tool call object. \
             Do not narrate internal protocol, and do not stop at 'I will' or 'let me'."
                .to_string(),
            896,
            true,
        ),
        GuardId::Perspective => (
            "Operator directive: your previous response narrated the user's actions or thoughts \
             in first person ('I glance', 'I feel', 'my sword'). You must NEVER speak AS the user. \
             Rewrite using second person ('you see', 'your blade') to describe observable facts, \
             or describe the world's response to the user's actions. For NPC dialogue, use quoted \
             speech attributed to the NPC. Do not assert the user's internal states as facts."
                .to_string(),
            768,
            false,
        ),
        GuardId::UserEcho => (
            "Operator directive: your previous response echoed the user's own words back verbatim. \
             React to the sentiment and meaning of what the user said using your own original phrasing. \
             Do not quote or parrot the user's memorable phrases."
                .to_string(),
            768,
            false,
        ),
        GuardId::ExecutionTruth => (
            "Operator directive: your previous response either claimed to execute a tool without \
             actually calling one, or denied having tool access when tools are available. \
             Actually invoke the appropriate tool (e.g., call `bash` with the command) and report \
             the real output. Do not narrate what a tool would do — call it."
                .to_string(),
            896,
            true,
        ),
        GuardId::DeclaredAction => (
            "Operator directive: the user declared an action but your response did not resolve it. \
             Acknowledge the declared action, determine if a roll or check is needed, and describe \
             the outcome. Do not redirect to a different action or ignore what the user said they do."
                .to_string(),
            768,
            false,
        ),
        _ => (
            "Operator directive: the previous response was filtered. Provide a concrete, \
             complete answer now."
                .to_string(),
            512,
            false,
        ),
    };

    let retry_note = format!(
        "RETRY: Your previous response was rejected: {directive}\n\
         The user's original request was: \"{user_prompt}\"\n\
         Generate a new response that addresses the user's request while avoiding the issue described above.",
        user_prompt = ctx.user_prompt,
    );

    let mut retry_messages = prepared.request.messages.clone();
    retry_messages.push(roboticus_llm::format::UnifiedMessage {
        role: "user".into(),
        content: retry_note,
        parts: None,
    });

    roboticus_llm::format::UnifiedRequest {
        model: prepared.request.model.clone(),
        messages: retry_messages,
        max_tokens: Some(max_tokens),
        temperature: match guard_id {
            GuardId::LowValueParroting => prepared.request.temperature,
            _ => None,
        },
        system: None,
        quality_target: match guard_id {
            GuardId::LowValueParroting => prepared.request.quality_target,
            _ => None,
        },
        tools: if preserve_tools {
            prepared.request.tools.clone()
        } else {
            vec![]
        },
    }
}

#[allow(clippy::too_many_arguments)]
pub(crate) async fn execute_retry_react_actions(
    state: &AppState,
    retry_req: &roboticus_llm::format::UnifiedRequest,
    preferred_model: &str,
    initial_content: String,
    turn_id: &str,
    authority: InputAuthority,
    channel_label: Option<&str>,
    delegation_provenance: &mut DelegationProvenance,
    react_loop: &mut AgentLoop,
    react_trace: &mut ReactTrace,
    tool_results_acc: &mut Vec<(String, String)>,
    resolved_model: &mut String,
    total_in: &mut i64,
    total_out: &mut i64,
    total_cost: &mut f64,
) -> String {
    let mut pending_calls = parse_tool_calls(&initial_content);
    if pending_calls.is_empty()
        && let Some(single) = parse_tool_call(&initial_content)
    {
        pending_calls.push(single);
    }
    if pending_calls.is_empty() {
        return initial_content;
    }

    let mut final_content = initial_content.clone();
    let mut react_messages = retry_req.messages.clone();
    react_messages.push(roboticus_llm::format::UnifiedMessage {
        role: "assistant".into(),
        content: initial_content,
        parts: None,
    });

    while !pending_calls.is_empty() {
        let mut observations = Vec::new();
        let mut batch_aborted = false;

        for (tn, tp) in &pending_calls {
            if react_loop.is_looping(tn, &tp.to_string()) {
                tracing::warn!(
                    tool = tn.as_str(),
                    "ReAct loop detected during retry — same tool+params repeated"
                );
                batch_aborted = true;
                break;
            }

            if tn.to_ascii_lowercase().contains("subagent")
                || tn.to_ascii_lowercase().contains("delegate")
            {
                delegation_provenance.subagent_task_started = true;
            }

            react_loop.transition(ReactAction::Act {
                tool_name: tn.clone(),
                params: tp.to_string(),
            });
            if react_loop.state == ReactState::Done {
                batch_aborted = true;
                break;
            }

            let tool_start = std::time::Instant::now();
            let tool_result =
                execute_tool_call_detailed(state, tn, tp, turn_id, authority, channel_label).await;
            let tool_duration_ms = tool_start.elapsed().as_millis() as u64;
            let result_text = match &tool_result {
                Ok(details) => {
                    if tn.to_ascii_lowercase().contains("subagent")
                        || tn.to_ascii_lowercase().contains("delegate")
                    {
                        delegation_provenance.subagent_task_completed = true;
                        delegation_provenance.subagent_result_attached =
                            !details.output.trim().is_empty();
                    }
                    details.output.clone()
                }
                Err(err) => format!("error: {err}"),
            };
            react_trace.record(ReactStep::ToolCall {
                tool_name: tn.clone(),
                parameters_redacted: false,
                result_summary: result_text.chars().take(120).collect(),
                duration_ms: tool_duration_ms,
                success: tool_result.is_ok(),
                source: tool_result
                    .as_ref()
                    .map(|details| details.source.clone())
                    .unwrap_or(ToolSource::BuiltIn),
            });
            tool_results_acc.push((tn.clone(), result_text.clone()));

            let observation = match tool_result {
                Ok(_) => format!("[Tool {tn} succeeded]: {result_text}"),
                Err(err) => format!("[Tool {tn} failed]: {err}"),
            };
            observations.push(if roboticus_agent::injection::scan_output(&observation) {
                format!("[Tool {tn} result blocked by safety filter]")
            } else {
                observation
            });
        }

        if batch_aborted && observations.is_empty() {
            let last_error = tool_results_acc
                .iter()
                .rev()
                .find(|(_, output)| output.starts_with("error:") || output.starts_with("Error:"))
                .map(|(tool, output)| {
                    let snippet: String = output.chars().take(200).collect();
                    format!("The tool `{tool}` failed with: {snippet}")
                })
                .unwrap_or_default();
            return if last_error.is_empty() {
                "I attempted this task multiple times but the same tool call kept \
                 repeating without making progress. This usually means the approach \
                 needs to change — could you rephrase or suggest a different strategy?"
                    .to_string()
            } else {
                format!(
                    "I attempted this task multiple times but got stuck in a loop. \
                     {last_error}. Could you help me take a different approach?"
                )
            };
        }

        react_loop.transition(ReactAction::Observe);
        react_messages.push(roboticus_llm::format::UnifiedMessage {
            role: "user".into(),
            content: observations.join("\n\n"),
            parts: None,
        });

        let follow_req = roboticus_llm::format::UnifiedRequest {
            model: retry_req.model.clone(),
            messages: react_messages.clone(),
            max_tokens: Some(2048),
            temperature: None,
            system: None,
            quality_target: None,
            tools: retry_req.tools.clone(),
        };
        let follow_content = match infer_with_fallback(state, &follow_req, preferred_model).await {
            Ok(result) => {
                *resolved_model = result.model.clone();
                *total_in += result.tokens_in;
                *total_out += result.tokens_out;
                *total_cost += result.cost;
                result.content
            }
            Err(e) => format!("LLM follow-up error: {e}"),
        };
        react_messages.push(roboticus_llm::format::UnifiedMessage {
            role: "assistant".into(),
            content: follow_content.clone(),
            parts: None,
        });
        let follow_content = sanitize_model_output(follow_content, state.hmac_secret.as_ref());
        pending_calls = parse_tool_calls(&follow_content);
        if pending_calls.is_empty()
            && let Some(single) = parse_tool_call(&follow_content)
        {
            pending_calls.push(single);
        }
        if pending_calls.is_empty() {
            react_loop.transition(ReactAction::Finish);
            final_content = follow_content;
        }
    }

    final_content
}

/// Apply the full guard chain to post-inference output, handling retries.
///
/// When a guard emits `RetryRequested`, this function:
/// 1. Builds a retry request with the appropriate operator directive.
/// 2. Calls `infer_with_fallback()` to re-infer.
/// 3. Sanitizes the retried output.
/// 4. Resumes the guard chain from the guard *after* the one that requested the retry.
/// 5. If the retried output still fails, uses `deterministic_quality_fallback()`.
///
/// Returns the final content and any additional token/cost deltas from retries.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn apply_guards_with_retry(
    content: String,
    ctx: &GuardContext<'_>,
    state: &AppState,
    prepared: &PreparedInference,
    turn_id: &str,
    authority: InputAuthority,
    channel_label: Option<&str>,
    delegation_provenance: &mut DelegationProvenance,
    react_loop: &mut AgentLoop,
    react_trace: &mut ReactTrace,
    tool_results_acc: &mut Vec<(String, String)>,
    resolved_model: &mut String,
    total_in: &mut i64,
    total_out: &mut i64,
    total_cost: &mut f64,
) -> String {
    let chain = guard_sets::full();
    let original_content = content.clone();
    let result = chain.apply(content, ctx);

    match result.retry {
        None => result.content,
        Some(signal) => {
            tracing::warn!(
                guard = ?signal.guard_id,
                reason = %signal.reason,
                "guard chain requested inference retry"
            );
            react_trace.record(super::super::flight_recorder::ReactStep::Guard {
                guard_name: format!("{:?}", signal.guard_id),
                fired: true,
                action: "retry_requested".into(),
                detail: Some(signal.reason.clone()),
                rejected_content: Some(truncate_for_trace(&original_content)),
                replacement_content: None,
            });
            let retry_req = build_retry_request(signal.guard_id, prepared, ctx);
            match infer_with_fallback(state, &retry_req, &prepared.model).await {
                Ok(retried_result) => {
                    *resolved_model = retried_result.model.clone();
                    *total_in += retried_result.tokens_in;
                    *total_out += retried_result.tokens_out;
                    *total_cost += retried_result.cost;

                    let retried_initial =
                        sanitize_model_output(retried_result.content, state.hmac_secret.as_ref());
                    let retried = if retry_req.tools.is_empty() {
                        retried_initial
                    } else {
                        execute_retry_react_actions(
                            state,
                            &retry_req,
                            &prepared.model,
                            retried_initial,
                            turn_id,
                            authority,
                            channel_label,
                            delegation_provenance,
                            react_loop,
                            react_trace,
                            tool_results_acc,
                            resolved_model,
                            total_in,
                            total_out,
                            total_cost,
                        )
                        .await
                    };
                    // Re-run the FULL guard chain on retried content with FRESH semantic scores.
                    // The original ctx.semantic_guard_scores were computed against the pre-retry
                    // output. We must recompute them against the retried output so that semantic
                    // guards (SubagentClaim, CapabilityDenial) evaluate fresh evidence.
                    let fresh_scores = super::super::guard_registry::precompute_guard_scores(
                        &state.semantic_classifier,
                        &retried,
                    )
                    .await;
                    // Use the UPDATED tool_results_acc, not the original ctx.tool_results.
                    // The retry may have executed tools (via execute_retry_react_actions),
                    // and guards like ExecutionTruthGuard need to see those new results
                    // to avoid judging the retry against stale evidence.
                    let retry_ctx = GuardContext {
                        user_prompt: ctx.user_prompt,
                        intents: ctx.intents,
                        tool_results: tool_results_acc,
                        agent_name: ctx.agent_name,
                        resolved_model: ctx.resolved_model,
                        delegation_provenance: ctx.delegation_provenance,
                        previous_assistant: ctx.previous_assistant,
                        prior_assistant_messages: ctx.prior_assistant_messages,
                        semantic_guard_scores: fresh_scores,
                        subagent_names: ctx.subagent_names.clone(),
                    };
                    let resumed = chain.apply(retried, &retry_ctx);
                    match resumed.retry {
                        None => {
                            if matches!(signal.guard_id, GuardId::TaskDeferral)
                                && is_generic_degraded_fallback(&resumed.content)
                            {
                                tracing::warn!(
                                    "task-deferral retry degraded into generic fallback; using deterministic blocker/result fallback"
                                );
                                deterministic_guard_fallback(
                                    signal.guard_id,
                                    &resumed.content,
                                    tool_results_acc,
                                    ctx.user_prompt,
                                    ctx.agent_name,
                                )
                            } else {
                                resumed.content
                            }
                        }
                        Some(signal2) => {
                            // Second retry failed — use deterministic fallback.
                            tracing::warn!(
                                "guard chain retry still failing; using deterministic fallback"
                            );
                            let fallback = deterministic_guard_fallback(
                                signal.guard_id,
                                &resumed.content,
                                tool_results_acc,
                                ctx.user_prompt,
                                ctx.agent_name,
                            );
                            react_trace.record(super::super::flight_recorder::ReactStep::Guard {
                                guard_name: format!("{:?}", signal2.guard_id),
                                fired: true,
                                action: "fallback_after_retry_exhausted".into(),
                                detail: Some(signal2.reason.clone()),
                                rejected_content: Some(truncate_for_trace(&resumed.content)),
                                replacement_content: Some(truncate_for_trace(&fallback)),
                            });
                            fallback
                        }
                    }
                }
                Err(e) => {
                    tracing::warn!(error = %e, "guard retry inference failed; using deterministic fallback");
                    deterministic_guard_fallback(
                        signal.guard_id,
                        &result.content,
                        tool_results_acc,
                        ctx.user_prompt,
                        ctx.agent_name,
                    )
                }
            }
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub(crate) async fn rescue_task_protocol_leak(
    content: String,
    state: &AppState,
    prepared: &PreparedInference,
    user_prompt: &str,
    agent_name: &str,
    turn_id: &str,
    authority: InputAuthority,
    channel_label: Option<&str>,
    delegation_provenance: &mut DelegationProvenance,
    react_loop: &mut AgentLoop,
    react_trace: &mut ReactTrace,
    tool_results_acc: &mut Vec<(String, String)>,
    resolved_model: &mut String,
    total_in: &mut i64,
    total_out: &mut i64,
    total_cost: &mut f64,
) -> String {
    let retry_req = build_retry_request(
        GuardId::InternalProtocol,
        prepared,
        &GuardContext {
            user_prompt,
            intents: &prepared.intents,
            tool_results: tool_results_acc,
            agent_name,
            resolved_model,
            delegation_provenance,
            previous_assistant: prepared.previous_assistant.as_deref(),
            prior_assistant_messages: &[],
            semantic_guard_scores: std::collections::HashMap::new(),
            subagent_names: vec![],
        },
    );
    let rescued = execute_retry_react_actions(
        state,
        &retry_req,
        &prepared.model,
        content.clone(),
        turn_id,
        authority,
        channel_label,
        delegation_provenance,
        react_loop,
        react_trace,
        tool_results_acc,
        resolved_model,
        total_in,
        total_out,
        total_cost,
    )
    .await;
    if rescued.trim() != content.trim() && !contains_internal_protocol_marker(&rescued) {
        rescued
    } else {
        let fallback = deterministic_quality_fallback(user_prompt, agent_name);
        react_trace.record(super::super::flight_recorder::ReactStep::Guard {
            guard_name: "streaming_buffer_guard".into(),
            fired: true,
            action: "fallback_after_retry_exhausted".into(),
            detail: Some("streaming buffer retry did not produce improved output".into()),
            rejected_content: Some(truncate_for_trace(&content)),
            replacement_content: Some(truncate_for_trace(&fallback)),
        });
        fallback
    }
}

/// Truncate content to a reasonable size for trace storage.
/// Full content is available in the turn's stored message; the trace
/// captures enough for quick diagnosis without bloating the trace JSON.
fn truncate_for_trace(content: &str) -> String {
    const MAX: usize = 500;
    if content.len() <= MAX {
        content.to_string()
    } else {
        let boundary = content
            .char_indices()
            .map(|(i, _)| i)
            .take_while(|&i| i <= MAX)
            .last()
            .unwrap_or(0);
        format!(
            "{}... [truncated, {} total chars]",
            &content[..boundary],
            content.len()
        )
    }
}