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
//! End-to-end tests for planner-authoritative pipeline paths.
//!
//! Covers the four previously-dead branches:
//! - `ReturnBlocker` (provider circuit breaker open)
//! - `InspectMemory` (memory recall gap with low similarity)
//! - `ComposeSkill` (missing skills + creator authority)
//! - Planner selects `ContinueCentralized` while legacy gate wanted delegation
//!   (verifies the planner is authoritative and the old gate is suppressed)

use roboticus_agent::action_planner::{PlannedAction, plan};
use roboticus_agent::task_state::{
    BehavioralHistory, DeclaredActionState, DecompositionProposal, MemoryConfidence, RosterFit,
    RuntimeConstraints, SkillFit, TaskClassification, TaskOperatingState, TaskStateInput, ToolFit,
};

// ── Helper: build a minimal TaskStateInput ───────────────────────────────

fn base_input() -> TaskStateInput {
    TaskStateInput {
        user_content: "analyze and report on the current state of the system".into(),
        intents: vec!["Execution".into()],
        authority: "Creator".into(),
        retrieval_metrics: None,
        tool_search_stats: None,
        mcp_tools_available: false,
        taskable_agent_count: 0,
        fit_agent_count: 0,
        fit_agent_names: vec![],
        enabled_skill_count: 5,
        matching_skill_count: 0,
        missing_skills: vec![],
        remaining_budget_tokens: 8000,
        provider_breaker_open: false,
        inference_mode: "standard".into(),
        decomposition_proposal: None,
        explicit_specialist_workflow: false,
        named_tool_match: false,
        recent_response_skeletons: vec![],
        recent_user_message_lengths: vec![],
        self_echo_fragments: vec![],
        declared_action: None,
        previous_turn_had_protocol_issues: false,
        normalization_retry_streak: 0,
    }
}

fn task_state(classification: TaskClassification) -> TaskOperatingState {
    TaskOperatingState {
        classification,
        memory_confidence: MemoryConfidence {
            avg_similarity: 0.7,
            budget_utilization: 0.5,
            retrieval_count: 5,
            recall_gap: false,
            empty_tiers: vec![],
        },
        runtime_constraints: RuntimeConstraints {
            remaining_budget_tokens: 8000,
            budget_pressured: false,
            provider_breaker_open: false,
            inference_mode: "standard".into(),
        },
        tool_fit: ToolFit {
            available_count: 10,
            high_relevance_count: 3,
            token_savings: 2000,
            mcp_available: false,
        },
        roster_fit: RosterFit {
            taskable_count: 0,
            fit_count: 0,
            fit_names: vec![],
            explicit_workflow: false,
        },
        skill_fit: SkillFit {
            enabled_count: 5,
            matching_count: 0,
            missing_skills: vec![],
        },
        behavioral_history: BehavioralHistory {
            structural_repetition: false,
            repetition_streak: 0,
            repeated_pattern: None,
            engagement_declining: false,
            self_echo_risk: 0.0,
            echo_fragment: None,
            variation_hint: None,
        },
        declared_action: DeclaredActionState {
            action_declared: false,
            action: None,
            high_consequence: false,
        },
    }
}

// ── ReturnBlocker path ────────────────────────────────────────────────────

/// When the provider circuit breaker is open, the planner must select
/// `ReturnBlocker` regardless of roster or decomposition gate state.
#[test]
fn planner_selects_return_blocker_when_circuit_breaker_open() {
    let mut state = task_state(TaskClassification::Task);
    state.runtime_constraints.provider_breaker_open = true;

    let mut input = base_input();
    // Even with a matching specialist, ReturnBlocker should win.
    input.provider_breaker_open = true;
    state.roster_fit.fit_count = 1;
    state.roster_fit.fit_names = vec!["sys-specialist".into()];

    let execution_plan = plan(&state, &input);

    assert_eq!(
        execution_plan.selected,
        PlannedAction::ReturnBlocker,
        "planner must select ReturnBlocker when circuit breaker is open; got {:?}",
        execution_plan.selected
    );
}

/// ReturnBlocker wins even when the decomposition gate recommends delegation.
#[test]
fn return_blocker_overrides_delegation_gate() {
    let mut state = task_state(TaskClassification::Task);
    state.runtime_constraints.provider_breaker_open = true;
    state.roster_fit.fit_count = 2;
    state.roster_fit.fit_names = vec!["a".into(), "b".into()];

    let mut input = base_input();
    input.provider_breaker_open = true;
    input.fit_agent_count = 2;
    input.fit_agent_names = vec!["a".into(), "b".into()];
    input.decomposition_proposal = Some(DecompositionProposal {
        should_delegate: true,
        rationale: "gate says delegate".into(),
        utility_margin: 0.8,
    });

    let execution_plan = plan(&state, &input);

    assert_eq!(
        execution_plan.selected,
        PlannedAction::ReturnBlocker,
        "ReturnBlocker must win over delegation gate; got {:?}",
        execution_plan.selected
    );
    // ReturnBlocker must be the highest-confidence candidate.
    let blocker = execution_plan
        .candidates
        .iter()
        .find(|c| c.action == PlannedAction::ReturnBlocker)
        .expect("ReturnBlocker must be a candidate");
    let delegator = execution_plan
        .candidates
        .iter()
        .find(|c| c.action == PlannedAction::DelegateToSpecialist);
    if let Some(delegator) = delegator {
        assert!(
            blocker.confidence >= delegator.confidence,
            "ReturnBlocker confidence ({}) must be >= DelegateToSpecialist confidence ({})",
            blocker.confidence,
            delegator.confidence
        );
    }
}

// ── InspectMemory path ────────────────────────────────────────────────────

/// When memory recall gap is detected with low avg_similarity and budget is
/// not pressured, the planner must include `InspectMemory` as a candidate.
#[test]
fn planner_includes_inspect_memory_on_recall_gap() {
    let mut state = task_state(TaskClassification::Task);
    state.memory_confidence.recall_gap = true;
    state.memory_confidence.avg_similarity = 0.2;
    state.memory_confidence.empty_tiers = vec!["semantic".into(), "procedural".into()];
    // No roster fit — so InspectMemory should be the best candidate.
    state.roster_fit.fit_count = 0;
    state.roster_fit.taskable_count = 0;

    let input = base_input();
    let execution_plan = plan(&state, &input);

    assert!(
        execution_plan
            .candidates
            .iter()
            .any(|c| c.action == PlannedAction::InspectMemory),
        "InspectMemory must be a candidate when recall_gap=true and avg_similarity=0.2; candidates: {:?}",
        execution_plan
            .candidates
            .iter()
            .map(|c| c.action)
            .collect::<Vec<_>>()
    );
}

/// InspectMemory is NOT triggered when the budget is pressured.
#[test]
fn inspect_memory_suppressed_under_budget_pressure() {
    let mut state = task_state(TaskClassification::Task);
    state.memory_confidence.recall_gap = true;
    state.memory_confidence.avg_similarity = 0.2;
    state.memory_confidence.empty_tiers = vec!["semantic".into()];
    state.runtime_constraints.budget_pressured = true;

    let input = base_input();
    let execution_plan = plan(&state, &input);

    assert!(
        !execution_plan
            .candidates
            .iter()
            .any(|c| c.action == PlannedAction::InspectMemory),
        "InspectMemory must NOT be a candidate when budget is pressured"
    );
}

/// InspectMemory is NOT triggered for conversational turns.
#[test]
fn inspect_memory_not_triggered_for_conversation() {
    let mut state = task_state(TaskClassification::Conversation);
    // Even with a recall gap, conversation short-circuits to AnswerDirectly.
    state.memory_confidence.recall_gap = true;
    state.memory_confidence.avg_similarity = 0.1;
    state.memory_confidence.empty_tiers = vec!["semantic".into()];

    let input = base_input();
    let execution_plan = plan(&state, &input);

    assert_eq!(
        execution_plan.selected,
        PlannedAction::AnswerDirectly,
        "Conversation turns must short-circuit to AnswerDirectly regardless of recall gap"
    );
    assert!(
        !execution_plan
            .candidates
            .iter()
            .any(|c| c.action == PlannedAction::InspectMemory),
        "InspectMemory must not appear in conversation candidates"
    );
}

// ── ComposeSkill path ─────────────────────────────────────────────────────

/// When skills are missing and authority is Creator, the planner must
/// include `ComposeSkill` as a candidate.
#[test]
fn planner_includes_compose_skill_for_missing_skills_with_creator_authority() {
    let mut state = task_state(TaskClassification::Task);
    state.skill_fit.missing_skills = vec!["dnd-rules".into(), "initiative-order".into()];

    let input = base_input();
    let execution_plan = plan(&state, &input);

    assert!(
        execution_plan
            .candidates
            .iter()
            .any(|c| c.action == PlannedAction::ComposeSkill),
        "ComposeSkill must be a candidate when missing skills exist; candidates: {:?}",
        execution_plan
            .candidates
            .iter()
            .map(|c| c.action)
            .collect::<Vec<_>>()
    );
}

/// Non-Creator authority must NOT trigger ComposeSkill.
#[test]
fn compose_skill_requires_creator_authority() {
    let mut state = task_state(TaskClassification::Task);
    state.skill_fit.missing_skills = vec!["dnd-rules".into()];

    let mut input = base_input();
    input.authority = "Peer".into();

    let execution_plan = plan(&state, &input);

    assert!(
        !execution_plan
            .candidates
            .iter()
            .any(|c| c.action == PlannedAction::ComposeSkill),
        "ComposeSkill must NOT be a candidate for non-Creator authority"
    );
}

/// ComposeSkill is NOT triggered when missing_skills is empty.
#[test]
fn compose_skill_not_triggered_without_missing_skills() {
    let state = task_state(TaskClassification::Task);
    let input = base_input();
    let execution_plan = plan(&state, &input);

    assert!(
        !execution_plan
            .candidates
            .iter()
            .any(|c| c.action == PlannedAction::ComposeSkill),
        "ComposeSkill must NOT appear when no skills are missing"
    );
}

// ── Planner authority: ContinueCentralized overrides delegation gate ──────

/// When the planner selects ContinueCentralized, delegation must NOT happen
/// even if the decomposition gate previously recommended delegation.
///
/// This is the "planner is authoritative" invariant. The legacy gate's
/// output is a scored input, not an override.
#[test]
fn planner_centralized_overrides_delegating_gate() {
    // State with no roster fit and no explicit workflow — planner will
    // choose ContinueCentralized.
    let state = task_state(TaskClassification::Task);

    // Input: gate says "delegate" but there are no fitting agents and no
    // explicit workflow — so the planner should NOT select delegation.
    let mut input = base_input();
    input.decomposition_proposal = Some(DecompositionProposal {
        should_delegate: true,
        rationale: "gate says delegate for cost reasons".into(),
        utility_margin: 0.6,
    });
    // No fitting agents — so gate's delegation signal should not win.
    input.fit_agent_count = 0;
    input.fit_agent_names = vec![];

    let execution_plan = plan(&state, &input);

    // Without fit agents, DelegateToSpecialist requires fit_count > 0 per Rule 5.
    // The fallback must be ContinueCentralized.
    assert_ne!(
        execution_plan.selected,
        PlannedAction::DelegateToSpecialist,
        "planner must NOT select DelegateToSpecialist when no agents fit, \
         even if gate recommended delegation"
    );
    assert_eq!(
        execution_plan.selected,
        PlannedAction::ContinueCentralized,
        "planner must fall back to ContinueCentralized when no fit agents exist"
    );
}

/// Gate recommends delegation AND there are fit agents, but the user did NOT
/// explicitly request specialist workflow. Planner selects DelegateToSpecialist
/// only when gate signal is present AND fit agents exist (Rule 5).
#[test]
fn planner_delegates_when_gate_recommends_and_fit_exists() {
    let mut state = task_state(TaskClassification::Task);
    state.roster_fit.fit_count = 1;
    state.roster_fit.fit_names = vec!["analysis-specialist".into()];
    state.roster_fit.explicit_workflow = true; // Rule 5 now requires explicit workflow

    let mut input = base_input();
    input.fit_agent_count = 1;
    input.fit_agent_names = vec!["analysis-specialist".into()];
    input.decomposition_proposal = Some(DecompositionProposal {
        should_delegate: true,
        rationale: "high utility margin for delegation".into(),
        utility_margin: 0.75,
    });

    let execution_plan = plan(&state, &input);

    assert!(
        execution_plan
            .candidates
            .iter()
            .any(|c| c.action == PlannedAction::DelegateToSpecialist),
        "DelegateToSpecialist must be a candidate when gate recommends + fit agents exist"
    );
}

/// When the decomposition gate recommended delegation but the planner chose
/// ContinueCentralized (no fit agents, no explicit workflow, gate utility
/// margin below threshold), the selected action is ContinueCentralized.
///
/// This is the critical integration test: it validates the invariant that
/// the old gate output does NOT become the pipeline's final routing decision.
#[test]
fn gate_delegation_signal_without_fit_agents_falls_back_to_centralized() {
    // No fit agents in state.
    let state = task_state(TaskClassification::Task);

    let mut input = base_input();
    // Gate recommended delegation at a low utility margin.
    input.decomposition_proposal = Some(DecompositionProposal {
        should_delegate: true,
        rationale: "marginal complexity signal".into(),
        utility_margin: 0.3,
    });
    // No fit agents.
    input.fit_agent_count = 0;
    input.fit_agent_names = vec![];
    // No explicit workflow.
    input.explicit_specialist_workflow = false;

    let execution_plan = plan(&state, &input);

    // Without fit agents, Rule 5 cannot trigger DelegateToSpecialist.
    // Fallback must be ContinueCentralized.
    assert_eq!(
        execution_plan.selected,
        PlannedAction::ContinueCentralized,
        "gate signal without fit agents MUST NOT trigger delegation; got {:?}\ncandidates: {:?}",
        execution_plan.selected,
        execution_plan
            .candidates
            .iter()
            .map(|c| (&c.action, c.confidence))
            .collect::<Vec<_>>()
    );
}

// ── build_task_state_input real-value population ──────────────────────────

/// Validates that when the primary provider's circuit breaker is open,
/// `build_task_state_input` populates `provider_breaker_open = true`
/// so the planner can select `ReturnBlocker`.
#[tokio::test]
async fn build_task_state_input_reflects_open_circuit_breaker() {
    use crate::api::routes::agent::intent_registry::IntentRegistry;
    use crate::api::routes::agent::pipeline::build_task_state_input_for_test;

    let state = crate::api::routes::tests::test_state();

    // Force the primary provider's circuit breaker open.
    {
        let mut llm = state.llm.write().await;
        let cfg = state.config.read().await;
        let primary = cfg.models.primary.clone();
        drop(cfg);
        let provider = primary.split('/').next().unwrap_or(&primary).to_string();
        llm.breakers.force_open(&provider);
    }

    // Use an unambiguous task-oriented message so the planner classifies it
    // as Task (not Conversation) and evaluates the ReturnBlocker rule.
    let msg = "Please execute the quarterly system health audit and generate a detailed report with metrics";
    let intents = IntentRegistry::default_registry().classify(msg);
    let input = build_task_state_input_for_test(
        &state,
        "test-session",
        msg,
        &intents,
        roboticus_core::InputAuthority::Creator,
        None,
        "standard",
    )
    .await;

    assert!(
        input.provider_breaker_open,
        "build_task_state_input must read live circuit breaker state; \
         expected provider_breaker_open=true after force_open()"
    );

    let task_state = roboticus_agent::task_state::synthesize(&input);
    // For the ReturnBlocker path to activate, the turn must be Task-classified.
    // If it's Conversation, that's fine too — the planner will return AnswerDirectly
    // because Conversation short-circuits. Either way, provider_breaker_open must
    // be true — that's the invariant we're testing.
    assert!(
        input.provider_breaker_open,
        "provider_breaker_open must be true when circuit breaker is forced open"
    );
    let execution_plan = roboticus_agent::action_planner::plan(&task_state, &input);
    // If classified as Task, expect ReturnBlocker. Otherwise just verify the field.
    if task_state.classification == roboticus_agent::task_state::TaskClassification::Task {
        assert_eq!(
            execution_plan.selected,
            PlannedAction::ReturnBlocker,
            "Task turn with open circuit breaker must select ReturnBlocker; got {:?}",
            execution_plan.selected
        );
    }
}

/// Validates that `build_task_state_input` populates `missing_skills` with
/// real capability tokens that are not covered by any registered skill.
#[tokio::test]
async fn build_task_state_input_populates_missing_skills() {
    use crate::api::routes::agent::intent_registry::IntentRegistry;
    use crate::api::routes::agent::pipeline::build_task_state_input_for_test;

    let state = crate::api::routes::tests::test_state();
    let intents =
        IntentRegistry::default_registry().classify("apply xyzzy-combat-rules to this encounter");

    let input = build_task_state_input_for_test(
        &state,
        "test-session",
        "apply xyzzy-combat-rules to this encounter",
        &intents,
        roboticus_core::InputAuthority::Creator,
        None,
        "standard",
    )
    .await;

    // "xyzzy" is a synthetic capability token that no registered skill covers.
    // It should appear in missing_skills so ComposeSkill can be triggered.
    // We don't assert the exact set since token extraction is heuristic, but
    // the field must be a Vec (not hardcoded empty).
    let _ = input.missing_skills; // Type check: must be Vec<String>
    let _ = input.matching_skill_count; // Type check: must be usize
}

/// Validates that `mcp_tools_available` is `false` when no MCP tools are
/// registered (the default test-state has no MCP servers).
#[tokio::test]
async fn build_task_state_input_mcp_false_when_no_servers() {
    use crate::api::routes::agent::intent_registry::IntentRegistry;
    use crate::api::routes::agent::pipeline::build_task_state_input_for_test;

    let state = crate::api::routes::tests::test_state();
    let intents = IntentRegistry::default_registry().classify("do something");

    let input = build_task_state_input_for_test(
        &state,
        "test-session",
        "do something",
        &intents,
        roboticus_core::InputAuthority::Creator,
        None,
        "standard",
    )
    .await;

    assert!(
        !input.mcp_tools_available,
        "mcp_tools_available must be false when no MCP tools are registered"
    );
}