agent-works 0.15.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
use super::*;

// ── LLM judge tests ──

#[tokio::test]
async fn test_text_only_judge_says_done() {
    let judge_client = Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": true,
        "reason": "task is complete"
    })));
    let guard = DefaultGuard::with_llm_client(DefaultGuardConfig::default(), judge_client);
    let ctx = make_ctx_with_text("what is 2+2?", "The answer is 4.", true, true);

    let decision = guard.on_turn(&ctx).await;
    assert!(
        matches!(decision, GuardDecision::Complete),
        "judge says done → Complete, got: {:?}",
        decision
    );
}

#[tokio::test]
async fn test_text_only_judge_says_not_done() {
    let judge_client = Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": false,
        "reason": "only answered part of the question"
    })));
    let guard = DefaultGuard::with_llm_client(DefaultGuardConfig::default(), judge_client);
    let ctx = make_ctx_with_text(
        "list all files and explain each",
        "Here are the files:",
        true,
        true,
    );

    let decision = guard.on_turn(&ctx).await;
    match &decision {
        GuardDecision::Continue { nudge } => {
            let msg = nudge.as_ref().unwrap();
            assert!(
                msg.contains("incomplete"),
                "nudge should mention incomplete: {}",
                msg
            );
            assert!(
                msg.contains("only answered part"),
                "nudge should include judge reason: {}",
                msg
            );
        }
        other => panic!("expected Continue, got: {:?}", other),
    }
}

#[tokio::test]
async fn test_text_only_judge_reason_propagated_to_nudge() {
    let judge_client = Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": false,
        "reason": "missing file list and explanation"
    })));
    let guard = DefaultGuard::with_llm_client(DefaultGuardConfig::default(), judge_client);
    let ctx = make_ctx_with_text("list files and explain", "here are the files:", true, true);

    let decision = guard.on_turn(&ctx).await;
    match &decision {
        GuardDecision::Continue { nudge } => {
            let msg = nudge.as_ref().unwrap();
            assert!(
                msg.contains("missing file list and explanation"),
                "nudge should include judge reason: {}",
                msg
            );
        }
        other => panic!("expected Continue, got: {:?}", other),
    }
}

// ── Awaiting-human park: a run parked on a human answer is waiting, not incomplete ──

/// Session 20261022_bb8c2dc3: `canvas_push` parked on a pick, the completion
/// judge saw the whole multi-step task and nudged "continue", and the model
/// decided the user's design choices for them. A parked run must complete
/// without the judge being consulted — a judge that says "not done" here would
/// reproduce the steamroll exactly.
#[tokio::test]
async fn awaiting_human_park_completes_even_when_judge_would_nudge() {
    let judge_client = Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": false,
        "reason": "four design steps are still unanswered"
    })));
    let guard = DefaultGuard::with_llm_client(DefaultGuardConfig::default(), judge_client);
    let mut ctx = make_ctx_with_text(
        "design a dashboard and let me pick each option",
        "The screen is up — click an option on the canvas and I'll continue.",
        true,
        true,
    );
    ctx.awaiting_human = true;

    let decision = guard.on_turn(&ctx).await;
    assert!(
        matches!(decision, GuardDecision::Complete),
        "parked-on-human must Complete even when the judge would nudge, got: {:?}",
        decision
    );
}

/// Degenerate turns still fail while a pick is pending — a reasoning-only
/// response is broken regardless of the park.
#[tokio::test]
async fn awaiting_human_does_not_mask_degenerate_turns() {
    let guard = DefaultGuard::new(DefaultGuardConfig::default());
    let mut ctx = make_ctx(1, 0, true, true, false, false);
    ctx.awaiting_human = true;

    let decision = guard.on_turn(&ctx).await;
    assert!(
        !matches!(decision, GuardDecision::Complete),
        "reasoning-only must not Complete just because a pick is pending, got: {:?}",
        decision
    );
}

// ── Short response detection tests ──

#[tokio::test]
async fn test_text_only_short_response_detected_no_tools() {
    let long_input = "a".repeat(200);
    let short_output = "done";
    let guard = DefaultGuard::new(DefaultGuardConfig::default());
    let ctx = make_ctx_with_text(&long_input, short_output, false, true);

    let decision = guard.on_turn(&ctx).await;
    match &decision {
        GuardDecision::Continue { nudge } => {
            let msg = nudge.as_ref().unwrap();
            assert!(msg.contains("incomplete"), "short response nudge: {}", msg);
        }
        other => panic!("expected Continue for short response, got: {:?}", other),
    }
}

#[tokio::test]
async fn test_text_only_short_response_with_judge_done() {
    let long_input = "a".repeat(200);
    let short_output = "42";
    let judge_client = Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": true,
        "reason": "answer is correct"
    })));
    let guard = DefaultGuard::with_llm_client(DefaultGuardConfig::default(), judge_client);
    let ctx = make_ctx_with_text(&long_input, short_output, true, true);

    let decision = guard.on_turn(&ctx).await;
    assert!(
        matches!(decision, GuardDecision::Complete),
        "short response + judge done → Complete, got: {:?}",
        decision
    );
}

#[tokio::test]
async fn test_text_only_after_rejected_tool_calls_never_judges_complete() {
    // Session 20260904_efad759c: the truncation guard rejected a spawn_agent
    // call, the model answered with a fabricated text-only success narrative,
    // and the completion judge — blind to the rejected tool_result — passed
    // it, ending the run with zero children spawned. last_tool_calls_invalid
    // must short-circuit the judge: no client here, so a Complete decision
    // could only come from the old (buggy) judge path failing open.
    let guard = DefaultGuard::new(DefaultGuardConfig::default());
    let mut ctx = make_ctx_with_text(
        "analyze 3 projects with sub-agents",
        "已启动 4 个子 agent 分析,稍后汇总。",
        true,
        true,
    );
    ctx.last_tool_calls_invalid = true;

    let decision = guard.on_turn(&ctx).await;
    match decision {
        GuardDecision::Continue { nudge: Some(msg) } => {
            assert!(
                msg.contains("NOT executed"),
                "nudge must tell the model the tool call never executed, got: {msg}"
            );
        }
        other => panic!(
            "text-only after rejected tool calls must Continue with a re-issue \
             nudge, got: {other:?}"
        ),
    }
}

// ── Skip threshold tests ──

#[tokio::test]
async fn test_text_only_skip_threshold() {
    let long_output = "x".repeat(300);
    let judge_client = Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": false,
        "reason": "would be incomplete but skipped"
    })));
    let guard = DefaultGuard::with_llm_client(DefaultGuardConfig::default(), judge_client);
    let ctx = make_ctx_with_text("query", &long_output, true, true);

    let decision = guard.on_turn(&ctx).await;
    assert!(
        matches!(decision, GuardDecision::Complete),
        "response >= skip_threshold → Complete without calling judge, got: {:?}",
        decision
    );
}

#[tokio::test]
async fn test_text_only_skip_judge_when_input_too_large() {
    let long_input = "a".repeat(10_001);
    let judge_client = Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": false,
        "reason": "would be incomplete but skipped"
    })));
    let guard = DefaultGuard::with_llm_client(DefaultGuardConfig::default(), judge_client);
    let ctx = make_ctx_with_text(&long_input, "short answer", true, true);

    let decision = guard.on_turn(&ctx).await;
    assert!(
        matches!(decision, GuardDecision::Complete),
        "input > 10k → skip judge → Complete, got: {:?}",
        decision
    );
}

// ── Judge timeout tests ──

#[tokio::test]
async fn test_text_only_judge_timeout_fail_open() {
    let judge_client = Arc::new(MockTimeoutClient::new());
    let config = DefaultGuardConfig {
        judge_fail_open: true,
        judge_timeout_secs: 1,
        ..DefaultGuardConfig::default()
    };
    let guard = DefaultGuard::with_llm_client(config, judge_client);
    let ctx = make_ctx_with_text("query", "short answer", true, true);

    let decision = guard.on_turn(&ctx).await;
    assert!(
        matches!(decision, GuardDecision::Complete),
        "timeout + fail_open → Complete (trust model), got: {:?}",
        decision
    );
}

#[tokio::test]
async fn test_text_only_judge_timeout_fail_closed() {
    let judge_client = Arc::new(MockTimeoutClient::new());
    let config = DefaultGuardConfig {
        judge_fail_open: false,
        judge_timeout_secs: 1,
        ..DefaultGuardConfig::default()
    };
    let guard = DefaultGuard::with_llm_client(config, judge_client);
    let ctx = make_ctx_with_text("query", "short answer", true, true);

    let decision = guard.on_turn(&ctx).await;
    match &decision {
        GuardDecision::Continue { nudge } => {
            let msg = nudge.as_ref().unwrap();
            assert!(
                msg.contains("Cannot verify"),
                "fail_closed timeout → Continue with verify message: {}",
                msg
            );
        }
        other => panic!(
            "expected Continue for timeout + fail_closed, got: {:?}",
            other
        ),
    }
}

// ── Judge error tests ──

#[tokio::test]
async fn test_text_only_judge_error_fail_closed() {
    let judge_client = Arc::new(MockErrorClient);
    let config = DefaultGuardConfig {
        judge_fail_open: false,
        ..DefaultGuardConfig::default()
    };
    let guard = DefaultGuard::with_llm_client(config, judge_client);
    let ctx = make_ctx_with_text("query", "short", true, true);

    let decision = guard.on_turn(&ctx).await;
    match &decision {
        GuardDecision::Continue { nudge } => {
            let msg = nudge.as_ref().unwrap();
            assert!(
                msg.contains("Cannot verify"),
                "fail_closed error → Continue with verify message: {}",
                msg
            );
        }
        other => panic!(
            "expected Continue for judge error + fail_closed, got: {:?}",
            other
        ),
    }
}

#[tokio::test]
async fn test_text_only_judge_error_fail_open() {
    let judge_client = Arc::new(MockErrorClient);
    let config = DefaultGuardConfig {
        judge_fail_open: true,
        ..DefaultGuardConfig::default()
    };
    let guard = DefaultGuard::with_llm_client(config, judge_client);
    let ctx = make_ctx_with_text("query", "short", true, true);

    let decision = guard.on_turn(&ctx).await;
    assert!(
        matches!(decision, GuardDecision::Complete),
        "fail_open + judge error → Complete (trust model), got: {:?}",
        decision
    );
}

// ── Short response + judge combination tests ──

#[tokio::test]
async fn test_text_only_short_response_judge_timeout_fail_closed() {
    let judge_client = Arc::new(MockTimeoutClient::new());
    let config = DefaultGuardConfig {
        judge_fail_open: false,
        judge_timeout_secs: 1,
        ..DefaultGuardConfig::default()
    };
    let guard = DefaultGuard::with_llm_client(config, judge_client);
    let long_input = "a".repeat(200);
    let ctx = make_ctx_with_text(&long_input, "42", true, true);

    let decision = guard.on_turn(&ctx).await;
    match &decision {
        GuardDecision::Continue { nudge } => {
            let msg = nudge.as_ref().unwrap();
            assert!(
                msg.contains("Cannot verify"),
                "short + timeout + fail_closed → Continue: {}",
                msg
            );
        }
        other => panic!(
            "expected Continue for short response + timeout + fail_closed, got: {:?}",
            other
        ),
    }
}

#[tokio::test]
async fn test_text_only_short_response_judge_error_fail_closed() {
    let judge_client = Arc::new(MockErrorClient);
    let config = DefaultGuardConfig {
        judge_fail_open: false,
        ..DefaultGuardConfig::default()
    };
    let guard = DefaultGuard::with_llm_client(config, judge_client);
    let long_input = "a".repeat(200);
    let ctx = make_ctx_with_text(&long_input, "42", true, true);

    let decision = guard.on_turn(&ctx).await;
    match &decision {
        GuardDecision::Continue { nudge } => {
            let msg = nudge.as_ref().unwrap();
            assert!(
                msg.contains("Cannot verify"),
                "short + error + fail_closed → Continue: {}",
                msg
            );
        }
        other => panic!(
            "expected Continue for short response + judge error + fail_closed, got: {:?}",
            other
        ),
    }
}

#[tokio::test]
async fn test_text_only_short_response_judge_error_fail_open() {
    let judge_client = Arc::new(MockErrorClient);
    let config = DefaultGuardConfig {
        judge_fail_open: true,
        ..DefaultGuardConfig::default()
    };
    let guard = DefaultGuard::with_llm_client(config, judge_client);
    let long_input = "a".repeat(200);
    let ctx = make_ctx_with_text(&long_input, "42", true, true);

    let decision = guard.on_turn(&ctx).await;
    assert!(
        matches!(decision, GuardDecision::Complete),
        "short + error + fail_open → Complete, got: {:?}",
        decision
    );
}

// ── Multi-turn context tests ──

#[tokio::test]
async fn test_text_only_judge_with_multiple_user_messages() {
    let judge_client = Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": false,
        "reason": "agent has not finished the task yet"
    })));
    let guard = DefaultGuard::with_llm_client(DefaultGuardConfig::default(), judge_client);

    let mut ctx = make_ctx_with_text("继续", "I'll continue working on it.", true, true);
    ctx.all_user_inputs = vec![
        "帮我分析一下这个 bug 的根因".to_string(),
        "好的,那你帮我修复一下".to_string(),
        "继续".to_string(),
    ];

    let decision = guard.on_turn(&ctx).await;
    match &decision {
        GuardDecision::Continue { nudge } => {
            let msg = nudge.as_ref().unwrap();
            assert!(
                msg.contains("incomplete"),
                "judge should see history and say incomplete: {}",
                msg
            );
        }
        other => panic!("expected Continue (judge says not done), got: {:?}", other),
    }
}

#[tokio::test]
async fn test_text_only_judge_respects_recent_user_count() {
    let judge_client = Arc::new(MockJudgeClient::new(serde_json::json!({
        "done": true,
        "reason": "task complete"
    })));
    let config = DefaultGuardConfig {
        recent_user_count: 2,
        ..DefaultGuardConfig::default()
    };
    let guard = DefaultGuard::with_llm_client(config, judge_client);

    let mut ctx = make_ctx_with_text("继续", "done", true, true);
    ctx.all_user_inputs = vec![
        "msg1".to_string(),
        "msg2".to_string(),
        "msg3".to_string(),
        "msg4".to_string(),
        "继续".to_string(),
    ];

    let decision = guard.on_turn(&ctx).await;
    assert!(
        matches!(decision, GuardDecision::Complete),
        "judge says done → Complete, got: {:?}",
        decision
    );
}