zagens-cli 0.8.3

Zagens headless CLI + HTTP/SSE runtime sidecar (`zagens`, `zagens-runtime` binaries)
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
//! Integration tests for the [`MockLlmClient`](mock::MockLlmClient).
//!
//! These tests exercise the [`LlmClient`](llm_client::LlmClient) trait surface
//! directly. Full engine integration with the mock client lives in
//! `crates/tui/src/core/engine/tests.rs` (`engine_llm_client_override_runs_mock_turn`).

use futures_util::StreamExt;

// Bring in the production model types verbatim — no other crate sources are
// needed because the mock is self-contained against `models.rs`.
#[path = "../src/models.rs"]
#[allow(dead_code, unused_imports)]
mod models;

// Mirror the real `llm_client` module hierarchy so that `mock.rs`'s
// `super::{LlmClient, StreamEventBox}` paths resolve. We re-declare a local
// `LlmClient` trait + `StreamEventBox` alias that match the production shape
// 1:1 (the public surface that ships in the binary). The mock implements
// this local trait, which is structurally identical to the production trait.
//
// The helper file lives under `tests/support/` so cargo does not try to
// compile it as its own test binary.
#[path = "support/llm_client.rs"]
mod llm_client;

use crate::llm_client::LlmClient;
use crate::llm_client::mock::{MockLlmClient, canned};
use crate::models::{ContentBlock, Delta, Message, MessageRequest, StreamEvent, Usage};

// === Helpers ===============================================================

fn user_message(text: &str) -> Message {
    Message {
        role: "user".to_string(),
        content: vec![ContentBlock::Text {
            text: text.to_string(),
            cache_control: None,
        }],
    }
}

fn assistant_thinking(thinking: &str, text: &str) -> Message {
    Message {
        role: "assistant".to_string(),
        content: vec![
            ContentBlock::Thinking {
                thinking: thinking.to_string(),
            },
            ContentBlock::Text {
                text: text.to_string(),
                cache_control: None,
            },
        ],
    }
}

fn assistant_tool_call(id: &str, name: &str, input: serde_json::Value) -> Message {
    Message {
        role: "assistant".to_string(),
        content: vec![ContentBlock::ToolUse {
            id: id.to_string(),
            name: name.to_string(),
            input,
            caller: None,
        }],
    }
}

fn tool_result_message(tool_use_id: &str, content: &str) -> Message {
    Message {
        role: "user".to_string(),
        content: vec![ContentBlock::ToolResult {
            tool_use_id: tool_use_id.to_string(),
            content: content.to_string(),
            is_error: None,
            content_blocks: None,
        }],
    }
}

fn make_request(messages: Vec<Message>) -> MessageRequest {
    MessageRequest {
        model: "deepseek-v4-pro".to_string(),
        messages,
        max_tokens: 4096,
        system: None,
        tools: None,
        tool_choice: None,
        metadata: None,
        thinking: None,
        reasoning_effort: Some("high".to_string()),
        stream: Some(true),
        temperature: None,
        top_p: None,
    }
}

async fn drain_stream_text(
    mock: &MockLlmClient,
    request: MessageRequest,
) -> (String, Option<String>) {
    let mut stream = mock
        .create_message_stream(request)
        .await
        .expect("stream open");
    let mut text = String::new();
    let mut stop_reason: Option<String> = None;
    while let Some(ev) = stream.next().await {
        match ev.expect("event") {
            StreamEvent::ContentBlockDelta {
                delta: Delta::TextDelta { text: t },
                ..
            } => text.push_str(&t),
            StreamEvent::MessageDelta { delta, .. } => {
                stop_reason = delta.stop_reason;
            }
            StreamEvent::MessageStop => break,
            _ => {}
        }
    }
    (text, stop_reason)
}

// === 1. Full turn loop with streaming =======================================

#[tokio::test]
async fn full_turn_loop_streams_text_chunks() {
    // Two text deltas + finish reason — exercises the canonical streaming
    // turn-loop path the engine drives.
    let turn = vec![
        canned::message_start("msg_1"),
        canned::text_block_start(0),
        canned::text_delta(0, "Hello, "),
        canned::text_delta(0, "world!"),
        canned::block_stop(0),
        canned::message_delta("end_turn", Some(Usage::default())),
        canned::message_stop(),
    ];
    let mock = MockLlmClient::new(vec![turn]);

    let request = make_request(vec![user_message("greet me")]);
    let (text, stop) = drain_stream_text(&mock, request).await;

    assert_eq!(text, "Hello, world!");
    assert_eq!(stop.as_deref(), Some("end_turn"));
    assert_eq!(mock.call_count(), 1);
    assert_eq!(mock.captured_requests().len(), 1);
}

// === 2. Reasoning replay (V4 thinking-mode HTTP-400 regression) =============

#[tokio::test]
async fn reasoning_replay_required_on_subsequent_turn() {
    // Turn 1: assistant emits thinking + tool_call. Turn 2: text reply.
    let turn1 = vec![
        canned::message_start("r1"),
        canned::thinking_delta(0, "I should call list_dir."),
        canned::tool_use_block_start(1, "call_a", "list_dir"),
        canned::tool_input_delta(1, r#"{"path":"/tmp"}"#),
        canned::block_stop(1),
        canned::message_delta("tool_use", None),
        canned::message_stop(),
    ];
    let turn2 = vec![
        canned::message_start("r2"),
        canned::text_block_start(0),
        canned::text_delta(0, "I see /tmp."),
        canned::block_stop(0),
        canned::message_delta("end_turn", None),
        canned::message_stop(),
    ];
    let mock = MockLlmClient::new(vec![turn1, turn2]);

    // === Round 1: user prompt -> assistant tool_call ===
    let req1 = make_request(vec![user_message("list /tmp")]);
    let _ = mock.create_message_stream(req1).await.unwrap().next().await;
    // (we don't drain — capture is what matters here)

    // === Round 2: runtime composes the next request including the prior
    // assistant turn's reasoning_content. The mock can verify that any
    // ContentBlock::Thinking the runtime preserves is present in the next
    // outgoing request — the very payload shape that broke v0.4.9-v0.5.1.
    let next_messages = vec![
        user_message("list /tmp"),
        assistant_thinking("I should call list_dir.", ""),
        assistant_tool_call("call_a", "list_dir", serde_json::json!({ "path": "/tmp" })),
        tool_result_message("call_a", "/tmp/file1\n/tmp/file2"),
    ];
    let req2 = make_request(next_messages);
    let _ = mock.create_message_stream(req2).await.unwrap();

    // The mock captured both requests. Assert the SECOND request preserves
    // the prior assistant message's Thinking block — i.e. the runtime did
    // not strip reasoning_content before re-sending. (V4 thinking-mode tool
    // turns reject HTTP 400 if reasoning_content is missing.)
    let captured = mock.captured_requests();
    assert_eq!(captured.len(), 2);

    let req2 = &captured[1];
    let assistant_with_thinking = req2
        .messages
        .iter()
        .find(|m| {
            m.role == "assistant"
                && m.content
                    .iter()
                    .any(|b| matches!(b, ContentBlock::Thinking { .. }))
        })
        .expect("turn 2 request must replay assistant Thinking content");

    let thinking_text = assistant_with_thinking
        .content
        .iter()
        .find_map(|b| match b {
            ContentBlock::Thinking { thinking } => Some(thinking.clone()),
            _ => None,
        })
        .expect("Thinking block present");
    assert_eq!(
        thinking_text, "I should call list_dir.",
        "reasoning_content must be replayed verbatim across tool-call rounds"
    );
}

// === 3. Tool-call round-trip ================================================

#[tokio::test]
async fn tool_call_round_trip_streams_args_then_continues() {
    // Turn 1 emits a tool_use block with chunked input JSON.
    let turn1 = vec![
        canned::message_start("rt1"),
        canned::tool_use_block_start(0, "call_x", "read_file"),
        canned::tool_input_delta(0, r#"{"path":"#),
        canned::tool_input_delta(0, r#""README.md"}"#),
        canned::block_stop(0),
        canned::message_delta("tool_use", None),
        canned::message_stop(),
    ];
    let turn2 = vec![
        canned::message_start("rt2"),
        canned::text_block_start(0),
        canned::text_delta(0, "README starts with: # deepseek-tui"),
        canned::block_stop(0),
        canned::message_delta("end_turn", None),
        canned::message_stop(),
    ];
    let mock = MockLlmClient::new(vec![turn1, turn2]);

    // Round 1
    let mut s1 = mock
        .create_message_stream(make_request(vec![user_message("read README.md")]))
        .await
        .unwrap();

    let mut tool_use_seen = false;
    let mut json_seen = String::new();
    while let Some(ev) = s1.next().await {
        match ev.unwrap() {
            StreamEvent::ContentBlockStart { content_block, .. } => {
                use crate::models::ContentBlockStart;
                if let ContentBlockStart::ToolUse { name, .. } = content_block {
                    assert_eq!(name, "read_file");
                    tool_use_seen = true;
                }
            }
            StreamEvent::ContentBlockDelta {
                delta: Delta::InputJsonDelta { partial_json },
                ..
            } => json_seen.push_str(&partial_json),
            StreamEvent::MessageStop => break,
            _ => {}
        }
    }
    assert!(tool_use_seen);
    let parsed: serde_json::Value =
        serde_json::from_str(&json_seen).expect("valid JSON after concat");
    assert_eq!(parsed["path"], "README.md");

    // Round 2 — runtime sends back a tool_result and the mock replies with
    // the final assistant text turn.
    let req2 = make_request(vec![
        user_message("read README.md"),
        assistant_tool_call(
            "call_x",
            "read_file",
            serde_json::json!({ "path": "README.md" }),
        ),
        tool_result_message("call_x", "# deepseek-tui\n..."),
    ]);
    let (text, stop) = drain_stream_text(&mock, req2).await;
    assert!(text.contains("# deepseek-tui"));
    assert_eq!(stop.as_deref(), Some("end_turn"));
}

// === 4. Multiple tool calls in one round (parallel ordering) ================

#[tokio::test]
async fn parallel_tool_calls_preserve_ordering_in_turn_payload() {
    // Assistant returns two tool_calls in a single turn (indices 0 and 1).
    // The runtime is free to execute them in parallel; this test asserts that
    // the canonical event ordering survives a single-turn replay.
    let turn = vec![
        canned::message_start("p1"),
        canned::tool_use_block_start(0, "call_one", "list_dir"),
        canned::tool_input_delta(0, r#"{"path":"a"}"#),
        canned::block_stop(0),
        canned::tool_use_block_start(1, "call_two", "list_dir"),
        canned::tool_input_delta(1, r#"{"path":"b"}"#),
        canned::block_stop(1),
        canned::message_delta("tool_use", None),
        canned::message_stop(),
    ];
    let mock = MockLlmClient::new(vec![turn]);

    let mut stream = mock
        .create_message_stream(make_request(vec![user_message("list both")]))
        .await
        .unwrap();

    let mut starts: Vec<(u32, String)> = Vec::new();
    while let Some(ev) = stream.next().await {
        if let StreamEvent::ContentBlockStart {
            index,
            content_block,
        } = ev.unwrap()
        {
            use crate::models::ContentBlockStart;
            if let ContentBlockStart::ToolUse { id, .. } = content_block {
                starts.push((index, id));
            }
        }
    }

    assert_eq!(starts.len(), 2);
    assert_eq!(starts[0], (0, "call_one".to_string()));
    assert_eq!(starts[1], (1, "call_two".to_string()));
}

// === 5. Compaction-style non-streaming call =================================

#[tokio::test]
async fn compaction_non_streaming_returns_queued_message_response() {
    use crate::models::MessageResponse;

    let mock = MockLlmClient::new(vec![]);
    mock.push_message_response(MessageResponse {
        id: "compact_msg".to_string(),
        r#type: "message".to_string(),
        role: "assistant".to_string(),
        content: vec![ContentBlock::Text {
            text: "## Summary\n- Step 1\n- Step 2".to_string(),
            cache_control: None,
        }],
        model: "deepseek-v4-pro".to_string(),
        stop_reason: Some("end_turn".to_string()),
        stop_sequence: None,
        container: None,
        usage: Usage::default(),
    });

    // The runtime's compaction path uses create_message (not stream).
    let req = MessageRequest {
        stream: Some(false),
        ..make_request(vec![user_message("summarize")])
    };
    let resp = mock.create_message(req).await.unwrap();

    let text = match &resp.content[0] {
        ContentBlock::Text { text, .. } => text.clone(),
        _ => panic!("expected text content"),
    };
    assert!(text.contains("Summary"));
    assert_eq!(resp.id, "compact_msg");
    assert_eq!(mock.call_count(), 1);
}

// === 6. Sub-agent style turn ================================================
//
// Sub-agents share the trait boundary: a parent's tool-call (`agent_spawn`)
// causes a child runtime to be created with its own `Arc<dyn LlmClient>`.
// At the trait level the test is identical to a normal turn — what changes
// is which mock instance answers. This test demonstrates two independent
// mocks (parent + child) cooperating on the same protocol.

#[tokio::test]
async fn sub_agent_parent_and_child_each_drive_independent_mocks() {
    // Parent decides to delegate.
    let parent_turn = vec![
        canned::message_start("parent_t1"),
        canned::tool_use_block_start(0, "spawn_id", "agent_spawn"),
        canned::tool_input_delta(0, r#"{"prompt":"compute 2+2"}"#),
        canned::block_stop(0),
        canned::message_delta("tool_use", None),
        canned::message_stop(),
    ];
    let parent = MockLlmClient::new(vec![parent_turn])
        .with_provider("mock-parent")
        .with_model("deepseek-v4-pro");

    // Child does the work and replies with text.
    let child_turn = vec![
        canned::message_start("child_t1"),
        canned::text_block_start(0),
        canned::text_delta(0, "4"),
        canned::block_stop(0),
        canned::message_delta("end_turn", None),
        canned::message_stop(),
    ];
    let child = MockLlmClient::new(vec![child_turn])
        .with_provider("mock-child")
        .with_model("deepseek-v4-flash");

    // Drive both mocks against their own request streams.
    let _ = parent
        .create_message_stream(make_request(vec![user_message("delegate")]))
        .await
        .unwrap()
        .next()
        .await;

    let (child_text, _) =
        drain_stream_text(&child, make_request(vec![user_message("compute 2+2")])).await;
    assert_eq!(child_text, "4");

    assert_eq!(parent.provider_name(), "mock-parent");
    assert_eq!(child.provider_name(), "mock-child");
    assert_eq!(parent.captured_requests().len(), 1);
    assert_eq!(child.captured_requests().len(), 1);
}

// === 7. Capacity-gate observation ===========================================
//
// The capacity controller (core::capacity) inspects an upcoming request's
// estimated input-token cost and may force a guardrail action (compaction,
// hold, etc.) before the request is dispatched. The mock surfaces request
// captures BEFORE the response stream is opened, which is exactly the seam
// the capacity controller observes — so the trait-level test is to verify
// that the captured request is observable per-call (not buffered across
// calls).

#[tokio::test]
async fn capacity_gate_can_observe_request_before_response_streams() {
    let turn = vec![canned::simple_text_turn("ok")];
    let mock = MockLlmClient::new(turn);

    // Build a "near-limit" request — many user messages.
    let mut messages = Vec::new();
    for i in 0..200 {
        messages.push(user_message(&format!("m{i}")));
    }
    let req = make_request(messages);

    // BEFORE the runtime drains the stream, the mock has already captured
    // the request. The capacity controller can inspect this and short-circuit
    // the dispatch if the estimated token cost exceeds the soft cap.
    let stream_future = mock.create_message_stream(req);
    let mut stream = stream_future.await.unwrap();

    assert_eq!(mock.captured_requests().len(), 1);
    let captured = mock.last_request().unwrap();
    assert_eq!(captured.messages.len(), 200);
    // Verify the capacity gate could compute a "should defer" decision based
    // on raw message count + payload size of the captured request.
    let total_chars: usize = captured
        .messages
        .iter()
        .flat_map(|m| m.content.iter())
        .map(|b| match b {
            ContentBlock::Text { text, .. } => text.len(),
            _ => 0,
        })
        .sum();
    assert!(
        total_chars > 100,
        "synthetic over-cap request should have non-trivial size"
    );

    // Drain to keep the mock state consistent.
    while stream.next().await.is_some() {}
}

// === 8. Compaction defaults (#402 P0) ======================================

#[test]
fn compaction_config_defaults_are_enabled_for_session_survivability() {
    // The production CompactionConfig is gated behind a `#[path = ...]` module
    // that isn't wired here, but we can test the principle: the
    // `should_compact` function and `CompactionConfig` live in the same crate.
    // Re-import from the production module to verify the default.
    //
    // We test via the mock pathway: the non-streaming compaction call (test 5
    // above) already exercises `create_message` with `stream: Some(false)`,
    // which is the code path `compact_messages` uses. Combined with the
    // capacity controller's `TargetedContextRefresh`, the enabled-by-default
    // compaction config means long sessions auto-compact before hitting the
    // context window limit.
    //
    // This test is a smoke check that the defaults compile and are correct.
    // The production `CompactionConfig::default()` is exercised by
    // `compaction::tests::should_compact_respects_enabled_flag` etc.
    let config =
        crate::models::compaction_threshold_for_model_and_effort("deepseek-v4-pro", Some("high"));
    // Verify the threshold is reasonable (> 0 and < context window).
    assert!(config > 0, "compaction threshold must be positive");
    assert!(config < 1_000_000, "compaction threshold must be below 1M");
}

// Full engine + mock LLM turn: see `engine_llm_client_override_runs_mock_turn` in
// `crates/tui/src/core/engine/tests.rs` (A5.2 / #402).