agent-works 0.17.0

Batteries-included Agent toolbox built on agent-base
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
648
649
// ── judge prompt assembly ──────────────────────────────────────────────

use super::super::judge::judge_user_prompt;

/// Value: protects=the judge can see the work, not just the words, so a
/// report after real work is not called incomplete; fails_when=the tool
/// trace is dropped from the prompt and the judge sees only the response.
/// why_new=no test covers what the judge is actually handed.
/// seam=none; no_claim=judge verdicts from the trace alone.
#[test]
fn the_judge_prompt_carries_the_run_tool_trace() {
    let p = judge_user_prompt(
        "look at this project",
        "Here is the review.",
        &["read_file".into(), "execute_command".into()],
    );
    assert!(
        p.contains("read_file") && p.contains("execute_command"),
        "{p}"
    );
    assert!(p.contains("Agent Response"), "{p}");
}

#[test]
fn a_run_with_no_tools_says_so_rather_than_leaving_a_blank() {
    let p = judge_user_prompt("hi", "Hello.", &[]);
    assert!(p.contains("no tool calls"), "{p}");
}

// ── call_completion_judge direct tests ──

use super::super::judge::call_completion_judge;
use super::*;

/// Helper to cast a concrete Arc to Arc<dyn LlmProvider>.
fn to_dyn<T: LlmProvider + 'static>(client: Arc<T>) -> Arc<dyn LlmProvider> {
    client
}

#[tokio::test]
async fn test_judge_no_client_fail_open() {
    let result = call_completion_judge(
        None,
        "input",
        "response",
        &["input".to_string()],
        &[],
        true, // fail_open
        10,
        5,
        None,
    )
    .await;

    assert!(result.is_ok());
    let judge = result.unwrap();
    assert!(judge.done, "no client + fail_open → done=true");
}

#[tokio::test]
async fn test_judge_no_client_fail_closed() {
    let result = call_completion_judge(
        None,
        "input",
        "response",
        &["input".to_string()],
        &[],
        false, // fail_closed
        10,
        5,
        None,
    )
    .await;

    assert!(result.is_err(), "no client + fail_closed → error");
}

#[tokio::test]
async fn test_judge_with_valid_response() {
    let client = to_dyn(Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": true,
        "reason": "all tasks complete"
    }))));

    let result = call_completion_judge(
        Some(&client),
        "do something",
        "done",
        &["do something".to_string()],
        &[],
        false,
        10,
        5,
        None,
    )
    .await;

    assert!(result.is_ok());
    let judge = result.unwrap();
    assert!(judge.done);
    assert_eq!(judge.reason, "all tasks complete");
}

#[tokio::test]
async fn test_judge_empty_user_inputs() {
    let client = to_dyn(Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": false,
        "reason": "not enough context"
    }))));

    let result = call_completion_judge(
        Some(&client),
        "single input",
        "response",
        &[], // empty all_user_inputs
        &[],
        false,
        10,
        5,
        None,
    )
    .await;

    assert!(result.is_ok());
    let judge = result.unwrap();
    assert!(!judge.done);
}

#[tokio::test]
async fn test_judge_single_user_input() {
    let client = to_dyn(Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": true,
        "reason": "complete"
    }))));

    let result = call_completion_judge(
        Some(&client),
        "only message",
        "response",
        &["only message".to_string()],
        &[],
        false,
        10,
        5,
        None,
    )
    .await;

    assert!(result.is_ok());
    assert!(result.unwrap().done);
}

#[tokio::test]
async fn test_judge_timeout_fail_open() {
    let client = to_dyn(Arc::new(MockTimeoutClient::new()));

    let result = call_completion_judge(
        Some(&client),
        "input",
        "response",
        &["input".to_string()],
        &[],
        true, // fail_open
        1,    // 1 second timeout
        5,
        None,
    )
    .await;

    assert!(result.is_ok(), "timeout + fail_open → Ok");
    assert!(result.unwrap().done);
}

#[tokio::test]
async fn test_judge_timeout_fail_closed() {
    let client = to_dyn(Arc::new(MockTimeoutClient::new()));

    let result = call_completion_judge(
        Some(&client),
        "input",
        "response",
        &["input".to_string()],
        &[],
        false, // fail_closed
        1,
        5,
        None,
    )
    .await;

    assert!(result.is_err(), "timeout + fail_closed → Err");
}

#[tokio::test]
async fn test_judge_error_fail_open() {
    let client = to_dyn(Arc::new(MockErrorClient));

    // fail_open=true → client errors also handled: trust model → Ok(done: true)
    let result = call_completion_judge(
        Some(&client),
        "input",
        "response",
        &["input".to_string()],
        &[],
        true,
        10,
        5,
        None,
    )
    .await;

    assert!(result.is_ok(), "error + fail_open → Ok (trust model)");
    let judge = result.unwrap();
    assert!(judge.done, "fail_open → done=true");
    assert!(
        judge.reason.contains("trusting model"),
        "reason should mention trusting model: {}",
        judge.reason
    );
}

#[tokio::test]
async fn test_judge_error_fail_closed() {
    let client = to_dyn(Arc::new(MockErrorClient));

    let result = call_completion_judge(
        Some(&client),
        "input",
        "response",
        &["input".to_string()],
        &[],
        false,
        10,
        5,
        None,
    )
    .await;

    assert!(result.is_err(), "error + fail_closed → Err");
}

#[tokio::test]
async fn test_judge_recent_user_count_truncation() {
    let client = to_dyn(Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": true,
        "reason": "complete"
    }))));

    // recent_user_count=2 but 5 inputs → only last 2 used
    let result = call_completion_judge(
        Some(&client),
        "msg5",
        "response",
        &[
            "msg1".to_string(),
            "msg2".to_string(),
            "msg3".to_string(),
            "msg4".to_string(),
            "msg5".to_string(),
        ],
        &[],
        false,
        10,
        2, // recent_user_count
        None,
    )
    .await;

    assert!(result.is_ok());
}

// ── Parse tolerance (issue #31): fences / prose around the JSON object ──

#[tokio::test]
async fn test_judge_fenced_json_parses() {
    let raw = "```json\n{\"done\": true, \"reason\": \"complete\"}\n```";
    let client = to_dyn(Arc::new(MockJudgeClient::raw(raw)));

    // fail_closed: a parse failure surfaces as Err, so Ok proves it parsed.
    let result = call_completion_judge(
        Some(&client),
        "do something",
        "done",
        &["do something".to_string()],
        &[],
        false,
        10,
        5,
        None,
    )
    .await;

    let judge = result.expect("fenced JSON should parse (issue #31)");
    assert!(judge.done);
    assert_eq!(judge.reason, "complete");
}

#[tokio::test]
async fn test_judge_prose_wrapped_json_parses() {
    let raw = "Sure, here is my assessment: {\"done\": false, \"reason\": \"still incomplete\"} Let me know if you need more.";
    let client = to_dyn(Arc::new(MockJudgeClient::raw(raw)));

    let result = call_completion_judge(
        Some(&client),
        "do something",
        "response",
        &["do something".to_string()],
        &[],
        false,
        10,
        5,
        None,
    )
    .await;

    let judge = result.expect("prose-wrapped JSON should parse (issue #31)");
    assert!(!judge.done);
    assert_eq!(judge.reason, "still incomplete");
}

#[tokio::test]
async fn test_judge_json_with_braces_inside_string_parses() {
    // Braces and escaped quotes inside the value must not break the
    // balanced-object scan.
    let raw = "Result: {\"done\": true, \"reason\": \"used {braces} and \\\"quotes\\\" inside\"}";
    let client = to_dyn(Arc::new(MockJudgeClient::raw(raw)));

    let result = call_completion_judge(
        Some(&client),
        "do something",
        "response",
        &["do something".to_string()],
        &[],
        false,
        10,
        5,
        None,
    )
    .await;

    let judge = result.expect("JSON with braces inside strings should parse");
    assert!(judge.done);
    assert_eq!(judge.reason, "used {braces} and \"quotes\" inside");
}

// ── Strict single retry on parse failure (issue #31) ──

#[tokio::test]
async fn test_judge_garbage_retries_once_then_fails_open() {
    let client = Arc::new(SequenceJudgeClient::new(&[
        "total garbage",
        "still garbage",
    ]));

    let result = call_completion_judge(
        Some(&(Arc::clone(&client) as Arc<dyn LlmProvider>)),
        "do something",
        "response",
        &["do something".to_string()],
        &[],
        true, // fail_open
        10,
        5,
        None,
    )
    .await;

    // Exactly one retry: two calls total, never a loop.
    assert_eq!(
        client.call_count(),
        2,
        "parse failure must retry exactly once"
    );
    // Honest fail-open: Ok(done=true) but reason carries the raw text.
    let judge = result.expect("fail_open must convert failure to done=true");
    assert!(judge.done);
    assert!(
        judge.reason.contains("still garbage"),
        "reason must carry the raw judge text (WARN detail): {}",
        judge.reason
    );
}

#[tokio::test]
async fn test_judge_garbage_then_valid_retry_succeeds() {
    let valid = r#"{"done": true, "reason": "complete"}"#;
    let client = Arc::new(SequenceJudgeClient::new(&["I cannot answer that", valid]));

    let result = call_completion_judge(
        Some(&(Arc::clone(&client) as Arc<dyn LlmProvider>)),
        "do something",
        "response",
        &["do something".to_string()],
        &[],
        false,
        10,
        5,
        None,
    )
    .await;

    assert_eq!(
        client.call_count(),
        2,
        "garbage → valid retry uses both calls"
    );
    let judge = result.expect("retry with valid JSON should succeed");
    assert!(judge.done);
    assert_eq!(judge.reason, "complete");
}

#[tokio::test]
async fn test_judge_timeout_does_not_retry() {
    let client = Arc::new(MockTimeoutClient::new());

    let result = call_completion_judge(
        Some(&(Arc::clone(&client) as Arc<dyn LlmProvider>)),
        "input",
        "response",
        &["input".to_string()],
        &[],
        false, // fail_closed: timeout surfaces as Err
        1,     // 1s timeout
        5,
        None,
    )
    .await;

    assert!(result.is_err(), "timeout + fail_closed → Err");
    assert_eq!(
        client.calls.load(std::sync::atomic::Ordering::SeqCst),
        1,
        "transport failures must not be retried"
    );
}

#[tokio::test]
async fn test_judge_garbage_fail_closed_retries_then_errs() {
    let client = Arc::new(SequenceJudgeClient::new(&["garbage one", "garbage two"]));

    let result = call_completion_judge(
        Some(&(Arc::clone(&client) as Arc<dyn LlmProvider>)),
        "do something",
        "response",
        &["do something".to_string()],
        &[],
        false, // fail_closed
        10,
        5,
        None,
    )
    .await;

    assert_eq!(
        client.call_count(),
        2,
        "fail_closed parse failure also retries once"
    );
    let err = result.expect_err("fail_closed + unparseable → Err");
    assert!(
        err.contains("garbage two"),
        "error must carry the raw judge text (WARN detail): {}",
        err
    );
}

#[tokio::test]
async fn test_truncate_respects_char_boundary() {
    // Chinese chars are multi-byte; a byte-slice cut would panic.
    let raw = "解析失败原文".repeat(200);
    let truncated = super::super::judge::truncate_for_log(&raw, 500);
    assert!(truncated.chars().count() <= 500);
    // Short input passes through untouched.
    assert_eq!(super::super::judge::truncate_for_log("short", 500), "short");
}

// ── Judge prompt contract + live client resolution (20261107_27dc727a) ──
//
// The incident: the completion judge fabricated ground truth ("the session's
// configured model is MiniMax-M3.1-Flash" — its own stale boot identity) and
// misquoted the answer it judged ("mimo-v2.5-pro" for "mimo-v2.6-pro"), then
// injected a false "answer is incomplete" correction. These tests pin both
// halves of the fix: the prompt is completeness-only with a scoped ground
// truth, and the judge resolves its client through the engine's live provider
// handle instead of its build-time clone.

use super::super::judge::judge_system_prompt;
use agent_base::ProviderHandle;
use agent_base::types::ChatMessage;
use std::sync::atomic::{AtomicUsize, Ordering};

/// A mock provider that records the request of its last `chat()` call and
/// answers a fixed verdict — for prompt-contract and client-resolution tests.
struct RecordingClient {
    model: &'static str,
    verdict: &'static str,
    seen: std::sync::Mutex<Option<ChatRequest>>,
    calls: AtomicUsize,
}

impl RecordingClient {
    fn new(model: &'static str, verdict: &'static str) -> Arc<Self> {
        Arc::new(Self {
            model,
            verdict,
            seen: std::sync::Mutex::new(None),
            calls: AtomicUsize::new(0),
        })
    }
}

#[async_trait::async_trait]
impl LlmProvider for RecordingClient {
    async fn stream(&self, _request: ChatRequest) -> Result<ChatStream, LlmError> {
        let chunks = vec![
            Ok(StreamChunk::Text(self.verdict.to_string())),
            Ok(StreamChunk::Stop {
                finish_reason: Some("stop".to_string()),
            }),
        ];
        Ok(ChatStream::new(Box::pin(futures_util::stream::iter(
            chunks,
        ))))
    }

    async fn chat(&self, request: ChatRequest) -> Result<ChatResponse, LlmError> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        *self.seen.lock().unwrap() = Some(request);
        Ok(ChatResponse {
            content: self.verdict.to_string(),
            tool_calls: vec![],
            usage: agent_base::UsageInfo::default(),
            finish_reason: LlmFinishReason::Stop,
            raw: None,
            reasoning_content: None,
            thinking_signature: None,
        })
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities::default()
    }

    fn info(&self) -> agent_base::llm_trait::ProviderInfo {
        agent_base::llm_trait::ProviderInfo {
            name: "recording".to_string(),
            model: self.model.to_string(),
            version: None,
        }
    }
}

#[test]
fn judge_prompt_is_completeness_only_with_scoped_ground_truth() {
    let prompt = judge_system_prompt("mimo-v2.6-pro");
    // The three contract elements: completion-only mandate, no-fabrication
    // rule (no invented facts, no paraphrased values, no "unverifiable"
    // penalties), and the single sanctioned fact — the session's model.
    for needle in [
        // The side-call classifier anchor: phimint's test and PTY harnesses
        // recognize judge requests by the phrase "completion judge" in the
        // prompt's first line. Keep this phrase stable (hyphenation of the
        // surrounding words is fine) — a silent rewording desyncs their LLM
        // scripts (20261107_27dc727a).
        "completion judge",
        "Judge COMPLETENESS only",
        "Do NOT fact-check",
        "Never state, assume, or introduce facts",
        "quote names only exactly as they appear",
        "Do not penalize a response as unverifiable",
        "A concise answer to the question asked is complete",
        "Ground truth: the model serving this session is `mimo-v2.6-pro`",
        "Reply with JSON",
    ] {
        assert!(
            prompt.contains(needle),
            "judge prompt missing {needle:?}:\n{prompt}"
        );
    }
    // No model name → no ground-truth line at all (never an empty fact).
    assert!(
        !judge_system_prompt("").contains("Ground truth"),
        "empty model must not produce a ground-truth line"
    );
}

#[tokio::test]
async fn judge_request_ground_truth_is_the_clients_model() {
    let client = RecordingClient::new("mimo-v2.6-pro", r#"{"done": true, "reason": "ok"}"#);
    let dyn_client: Arc<dyn LlmProvider> = client.clone();
    call_completion_judge(
        Some(&dyn_client),
        "现在呢",
        "现在是 mimo-v2.6-pro。",
        &["现在呢".to_string()],
        &[],
        true,
        10,
        5,
        None,
    )
    .await
    .expect("judge call succeeds");

    let seen = client.seen.lock().unwrap();
    let request = seen.as_ref().expect("judge call recorded");
    let system = request
        .messages
        .iter()
        .find_map(|m| match m {
            ChatMessage::System { content, .. } => Some(content.clone()),
            _ => None,
        })
        .expect("judge request carries a system message");
    assert!(
        system.contains("the model serving this session is `mimo-v2.6-pro`"),
        "ground truth must be the client's live model name:\n{system}"
    );
}

#[tokio::test]
async fn guard_judges_on_the_provider_source_not_the_build_client() {
    let frozen = RecordingClient::new("boot-model", r#"{"done": true, "reason": "frozen"}"#);
    let live = RecordingClient::new("live-model", r#"{"done": false, "reason": "from live"}"#);
    let guard = DefaultGuard::with_llm_client(DefaultGuardConfig::default(), frozen.clone());
    guard.set_provider_source(ProviderHandle::new(live.clone()));
    let ctx = make_ctx_with_text("现在呢", "现在是 mimo-v2.6-pro。", true, true);

    let decision = guard.on_turn(&ctx).await;
    match &decision {
        GuardDecision::Continue { nudge } => {
            assert!(
                nudge.as_ref().unwrap().contains("from live"),
                "nudge must carry the live judge's reason: {nudge:?}"
            );
        }
        other => panic!("live judge said done:false → Continue, got {other:?}"),
    }
    assert_eq!(
        live.calls.load(Ordering::SeqCst),
        1,
        "the judge must run on the provider source"
    );
    assert_eq!(
        frozen.calls.load(Ordering::SeqCst),
        0,
        "the build-time client must not be used once a source is set"
    );
}