aether-agent-cli 0.7.9

CLI and ACP server for the Aether AI coding agent
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
use acp_utils::notifications::{ContextClearedParams, ContextUsageParams, SubAgentProgressParams};
use aether_core::events::{AgentMessage, SubAgentProgressPayload};
use agent_client_protocol::schema::{
    self as acp, Content, ContentBlock, ContentChunk, Diff, PlanEntry, PlanEntryPriority, PlanEntryStatus, SessionId,
    SessionNotification, SessionUpdate, StopReason, TextContent, ToolCall, ToolCallContent, ToolCallId, ToolCallStatus,
    ToolCallUpdate, ToolCallUpdateFields,
};
use llm::{ToolCallError, ToolCallRequest, ToolCallResult};
use mcp_utils::display_meta::{PlanMetaStatus, ToolResultMeta};

/// Converts Aether `AgentMessage` to ACP `SessionUpdate`
pub fn map_agent_message_to_session_notification(
    session_id: SessionId,
    msg: &AgentMessage,
) -> Option<SessionNotification> {
    map_agent_message_to_notification(session_id, msg, NotificationMode::Live)
}

/// Determines the stop reason from the final agent message
pub fn map_agent_message_to_stop_reason(msg: &AgentMessage) -> acp::StopReason {
    match msg {
        AgentMessage::Cancelled { .. } => StopReason::Cancelled,
        _ => StopReason::EndTurn,
    }
}

/// Typed union of agent-side extension notifications that the actor forwards
/// to the client. Each variant serializes to its own `_aether/*` wire method
/// and is sent via [`ConnectionTo<Client>::send_notification`].
pub enum AgentExtNotification {
    ContextUsage(ContextUsageParams),
    ContextCleared(ContextClearedParams),
    SubAgentProgress(SubAgentProgressParams),
}

pub fn try_into_agent_notification(msg: &AgentMessage) -> Option<AgentExtNotification> {
    match msg {
        AgentMessage::ContextUsageUpdate {
            usage_ratio,
            context_limit,
            input_tokens,
            output_tokens,
            cache_read_tokens,
            cache_creation_tokens,
            reasoning_tokens,
            total_input_tokens,
            total_output_tokens,
            total_cache_read_tokens,
            total_cache_creation_tokens,
            total_reasoning_tokens,
        } => Some(AgentExtNotification::ContextUsage(ContextUsageParams {
            usage_ratio: *usage_ratio,
            context_limit: *context_limit,
            input_tokens: *input_tokens,
            output_tokens: *output_tokens,
            cache_read_tokens: *cache_read_tokens,
            cache_creation_tokens: *cache_creation_tokens,
            reasoning_tokens: *reasoning_tokens,
            total_input_tokens: *total_input_tokens,
            total_output_tokens: *total_output_tokens,
            total_cache_read_tokens: *total_cache_read_tokens,
            total_cache_creation_tokens: *total_cache_creation_tokens,
            total_reasoning_tokens: *total_reasoning_tokens,
        })),
        AgentMessage::ToolProgress { request, message, .. } => {
            let msg_str = message.as_ref()?;
            let params = try_parse_sub_agent_progress(msg_str, request)?;
            Some(AgentExtNotification::SubAgentProgress(params))
        }
        AgentMessage::ContextCleared => Some(AgentExtNotification::ContextCleared(ContextClearedParams::default())),
        _ => None,
    }
}

/// If the tool result carries plan metadata, build a `SessionUpdate::Plan` notification.
pub fn try_extract_plan_notification(
    session_id: SessionId,
    result_meta: Option<&ToolResultMeta>,
) -> Option<SessionNotification> {
    let plan_meta = result_meta?.plan.as_ref()?;
    let entries = plan_meta
        .entries
        .iter()
        .map(|e| PlanEntry::new(e.content.clone(), PlanEntryPriority::Medium, plan_status_to_acp(e.status)))
        .collect();
    Some(SessionNotification::new(session_id, SessionUpdate::Plan(acp::Plan::new(entries))))
}

#[derive(Clone, Copy)]
pub(crate) enum NotificationMode {
    Live,
    Replay,
}

pub(crate) fn map_agent_message_to_notification(
    session_id: SessionId,
    msg: &AgentMessage,
    mode: NotificationMode,
) -> Option<SessionNotification> {
    match msg {
        AgentMessage::Text { chunk, is_complete, .. } => {
            map_chunk_to_notification(session_id, chunk, *is_complete, mode, SessionUpdate::AgentMessageChunk)
        }

        AgentMessage::Thought { chunk, is_complete, .. } => {
            map_chunk_to_notification(session_id, chunk, *is_complete, mode, SessionUpdate::AgentThoughtChunk)
        }

        AgentMessage::ToolCall { request, .. } => Some(map_tool_call_to_notification(session_id, request)),

        AgentMessage::ToolCallUpdate { tool_call_id, chunk, .. } => {
            Some(map_tool_call_update_to_notification(session_id, tool_call_id, chunk))
        }

        AgentMessage::ToolResult { result, result_meta, .. } => {
            Some(map_tool_result_to_notification(session_id, result, result_meta.as_ref()))
        }

        AgentMessage::ToolError { error, .. } => Some(map_tool_error_to_notification(session_id, error)),

        AgentMessage::ToolProgress { request, progress, total, message } => {
            map_tool_progress_to_notification(session_id, request, *progress, *total, message.as_ref())
        }

        AgentMessage::Error { message } => Some(acp::SessionNotification::new(
            session_id,
            SessionUpdate::AgentMessageChunk(ContentChunk::new(ContentBlock::Text(TextContent::new(format!(
                "[Error] {message}"
            ))))),
        )),

        AgentMessage::ContextUsageUpdate { .. }
        | AgentMessage::ContextCleared
        | AgentMessage::Cancelled { .. }
        | AgentMessage::Done
        | AgentMessage::ContextCompactionStarted { .. }
        | AgentMessage::ContextCompactionResult { .. }
        | AgentMessage::AutoContinue { .. }
        | AgentMessage::Retrying { .. }
        | AgentMessage::ModelSwitched { .. } => None,
    }
}

/// Convert internal plan status to ACP protocol status.
fn plan_status_to_acp(status: PlanMetaStatus) -> PlanEntryStatus {
    match status {
        PlanMetaStatus::InProgress => PlanEntryStatus::InProgress,
        PlanMetaStatus::Completed => PlanEntryStatus::Completed,
        PlanMetaStatus::Pending => PlanEntryStatus::Pending,
    }
}

fn map_chunk_to_notification(
    session_id: SessionId,
    chunk: &str,
    is_complete: bool,
    mode: NotificationMode,
    wrap: fn(ContentChunk) -> SessionUpdate,
) -> Option<SessionNotification> {
    match mode {
        // Skip the final completion message to avoid sending duplicate content.
        // The client has already received all the chunks during streaming.
        NotificationMode::Live if is_complete => return None,
        NotificationMode::Replay if !is_complete => return None,
        NotificationMode::Live | NotificationMode::Replay => {}
    }

    Some(acp::SessionNotification::new(
        session_id,
        wrap(ContentChunk::new(ContentBlock::Text(TextContent::new(chunk.to_owned())))),
    ))
}

fn map_tool_call_to_notification(session_id: SessionId, request: &ToolCallRequest) -> SessionNotification {
    let raw_input = serde_json::from_str(&request.arguments).ok();
    SessionNotification::new(
        session_id,
        SessionUpdate::ToolCall(
            ToolCall::new(ToolCallId::new(request.id.clone()), humanize_tool_name(&request.name))
                .status(acp::ToolCallStatus::InProgress)
                .raw_input(raw_input),
        ),
    )
}

fn parse_tool_call_chunk(chunk: &str) -> serde_json::Value {
    serde_json::from_str(chunk).unwrap_or_else(|_| serde_json::Value::String(chunk.to_string()))
}

fn map_tool_call_update_to_notification(session_id: SessionId, tool_call_id: &str, chunk: &str) -> SessionNotification {
    let fields = ToolCallUpdateFields::new().status(ToolCallStatus::InProgress).raw_input(parse_tool_call_chunk(chunk));

    SessionNotification::new(
        session_id,
        SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(ToolCallId::new(tool_call_id.to_string()), fields)),
    )
}

/// Produces the initial human-readable title for a tool call (e.g., "Read file").
/// This is sent when the tool call starts.
fn humanize_tool_name(name: &str) -> String {
    let base = name.split("__").last().unwrap_or(name);
    let mut result = base.replace('_', " ");
    if let Some(first) = result.get_mut(0..1) {
        first.make_ascii_uppercase();
    }
    result
}

fn map_tool_result_to_notification(
    session_id: SessionId,
    result: &ToolCallResult,
    result_meta: Option<&ToolResultMeta>,
) -> SessionNotification {
    let mut content =
        vec![ToolCallContent::Content(Content::new(ContentBlock::Text(TextContent::new(result.result.clone()))))];

    if let Some(rm) = result_meta
        && let Some(fd) = &rm.file_diff
    {
        let mut diff = Diff::new(&fd.path, &fd.new_text);
        if let Some(old) = &fd.old_text {
            diff = diff.old_text(old.clone());
        }
        content.push(ToolCallContent::Diff(diff));
    }

    let mut fields = ToolCallUpdateFields::new().status(ToolCallStatus::Completed).content(content);

    if let Some(rm) = result_meta {
        fields = fields.title(&rm.display.title);
    }

    let mut update = ToolCallUpdate::new(ToolCallId::new(result.id.clone()), fields);

    if let Some(rm) = result_meta
        && !rm.display.value.is_empty()
    {
        let mut meta_map = serde_json::Map::new();
        meta_map.insert("display_value".into(), rm.display.value.clone().into());
        update = update.meta(meta_map);
    }

    SessionNotification::new(session_id, SessionUpdate::ToolCallUpdate(update))
}

fn map_tool_error_to_notification(session_id: SessionId, error: &ToolCallError) -> SessionNotification {
    SessionNotification::new(
        session_id,
        SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(
            ToolCallId::new(error.id.clone()),
            ToolCallUpdateFields::new().status(ToolCallStatus::Failed).content(vec![ToolCallContent::Content(
                Content::new(ContentBlock::Text(TextContent::new(error.error.clone()))),
            )]),
        )),
    )
}

fn map_tool_progress_to_notification(
    session_id: SessionId,
    request: &ToolCallRequest,
    progress: f64,
    total: Option<f64>,
    message: Option<&String>,
) -> Option<SessionNotification> {
    tracing::debug!("Tool progress: {message:?}");

    if message.and_then(|msg_str| try_parse_sub_agent_progress(msg_str, request)).is_some() {
        return None;
    }

    if let Some(result_meta) = message.and_then(|m| try_parse_display_meta(m)) {
        let fields = ToolCallUpdateFields::new().status(ToolCallStatus::InProgress).title(&result_meta.display.title);

        let mut update = ToolCallUpdate::new(ToolCallId::new(request.id.clone()), fields);

        if !result_meta.display.value.is_empty() {
            let mut meta_map = serde_json::Map::new();
            meta_map.insert("display_value".into(), result_meta.display.value.into());
            update = update.meta(meta_map);
        }

        return Some(SessionNotification::new(session_id, SessionUpdate::ToolCallUpdate(update)));
    }

    let total_str = total.map_or_else(|| "?".to_string(), |t| t.to_string());
    let progress_text = message
        .map_or_else(|| format!("Progress: {progress}/{total_str}"), |msg| format!("{msg} ({progress}/{total_str})"));

    Some(SessionNotification::new(
        session_id,
        SessionUpdate::ToolCallUpdate(ToolCallUpdate::new(
            ToolCallId::new(request.id.clone()),
            ToolCallUpdateFields::new().status(ToolCallStatus::InProgress).content(vec![ToolCallContent::Content(
                Content::new(ContentBlock::Text(TextContent::new(progress_text))),
            )]),
        )),
    ))
}

fn try_parse_display_meta(message: &str) -> Option<ToolResultMeta> {
    serde_json::from_str::<ToolResultMeta>(message).ok()
}

/// Attempt to parse a tool progress message as sub-agent progress.
fn try_parse_sub_agent_progress(message: &str, request: &llm::ToolCallRequest) -> Option<SubAgentProgressParams> {
    let payload: SubAgentProgressPayload = serde_json::from_str(message).ok()?;

    Some(SubAgentProgressParams {
        parent_tool_id: request.id.clone(),
        task_id: payload.task_id,
        agent_name: payload.agent_name,
        event: (&payload.event).into(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use acp_utils::notifications::SubAgentEvent;
    use llm::ToolCallRequest;

    #[test]
    fn test_tool_progress_with_sub_agent_payload_emits_ext_notification() {
        let session_id = acp::SessionId::new("test-session");

        let payload = SubAgentProgressPayload {
            task_id: "task_1".to_string(),
            agent_name: "sub-agent".to_string(),
            event: AgentMessage::Text {
                message_id: "msg_1".to_string(),
                chunk: "Hello".to_string(),
                is_complete: false,
                model_name: "TestModel".to_string(),
            },
        };
        let serialized_msg = serde_json::to_string(&payload).unwrap();

        let tool_progress = AgentMessage::ToolProgress {
            request: ToolCallRequest {
                id: "call_123".to_string(),
                name: "plugins__spawn_subagent".to_string(),
                arguments: "{}".to_string(),
            },
            progress: 42.0,
            total: Some(100.0),
            message: Some(serialized_msg.clone()),
        };

        let notification = map_agent_message_to_session_notification(session_id.clone(), &tool_progress);

        assert!(notification.is_none());

        let agent_notif = try_into_agent_notification(&tool_progress).expect("agent notification");
        match agent_notif {
            AgentExtNotification::SubAgentProgress(params) => {
                assert_eq!(params.parent_tool_id, "call_123");
                assert_eq!(params.task_id, "task_1");
                assert_eq!(params.agent_name, "sub-agent");
                assert!(matches!(params.event, SubAgentEvent::Other));
            }
            _ => panic!("expected SubAgentProgress"),
        }
    }

    #[test]
    fn test_thought_maps_to_agent_thought_chunk() {
        let session_id = acp::SessionId::new("test-session");
        let thought = AgentMessage::Thought {
            message_id: "msg_1".to_string(),
            chunk: "thinking...".to_string(),
            is_complete: false,
            model_name: "TestModel".to_string(),
        };

        let notification = map_agent_message_to_session_notification(session_id, &thought).expect("notification");

        match notification.update {
            acp::SessionUpdate::AgentThoughtChunk(chunk) => match chunk.content {
                acp::ContentBlock::Text(text) => assert_eq!(text.text, "thinking..."),
                other => panic!("Expected text content, got {other:?}"),
            },
            other => panic!("Expected AgentThoughtChunk, got {other:?}"),
        }
    }

    #[test]
    fn test_tool_call_maps_to_tool_call_notification() {
        let session_id = acp::SessionId::new("test-session");
        let message = AgentMessage::ToolCall {
            request: ToolCallRequest {
                id: "call_1".to_string(),
                name: "coding__read_file".to_string(),
                arguments: "{}".to_string(),
            },
            model_name: "TestModel".to_string(),
        };

        let notification = map_agent_message_to_session_notification(session_id, &message).expect("notification");

        match notification.update {
            acp::SessionUpdate::ToolCall(tool_call) => {
                assert_eq!(tool_call.tool_call_id.0.as_ref(), "call_1");
                assert_eq!(tool_call.title, "Read file");
                assert_eq!(tool_call.status, acp::ToolCallStatus::InProgress);
            }
            other => panic!("Expected ToolCall, got {other:?}"),
        }
    }

    #[test]
    fn test_tool_call_update_maps_to_tool_call_update_notification() {
        let session_id = acp::SessionId::new("test-session");
        let message = AgentMessage::ToolCallUpdate {
            tool_call_id: "call_1".to_string(),
            chunk: r#"{"filePath":"Cargo.toml"}"#.to_string(),
            model_name: "TestModel".to_string(),
        };

        let notification = map_agent_message_to_session_notification(session_id, &message).expect("notification");

        match notification.update {
            acp::SessionUpdate::ToolCallUpdate(update) => {
                assert_eq!(update.tool_call_id.0.as_ref(), "call_1");
                assert_eq!(update.fields.status, Some(acp::ToolCallStatus::InProgress));
                assert_eq!(update.fields.raw_input, Some(serde_json::json!({ "filePath": "Cargo.toml" })));
            }
            other => panic!("Expected ToolCallUpdate, got {other:?}"),
        }
    }

    #[test]
    fn test_tool_call_update_has_same_live_and_replay_mapping() {
        let session_id = acp::SessionId::new("test-session");
        let message = AgentMessage::ToolCallUpdate {
            tool_call_id: "call_1".to_string(),
            chunk: r#"{"filePath":"Cargo.toml"}"#.to_string(),
            model_name: "TestModel".to_string(),
        };

        let live = map_agent_message_to_notification(session_id.clone(), &message, NotificationMode::Live)
            .expect("live notification");
        let replay = map_agent_message_to_notification(session_id, &message, NotificationMode::Replay)
            .expect("replay notification");

        match (live.update, replay.update) {
            (acp::SessionUpdate::ToolCallUpdate(live), acp::SessionUpdate::ToolCallUpdate(replay)) => {
                assert_eq!(live.tool_call_id.0, replay.tool_call_id.0);
                assert_eq!(live.fields.status, replay.fields.status);
                assert_eq!(live.fields.raw_input, replay.fields.raw_input);
            }
            other => panic!("Expected ToolCallUpdate pair, got {other:?}"),
        }
    }

    #[test]
    fn test_live_mapping_skips_completed_chunks_but_replay_keeps_them() {
        let cases: Vec<(AgentMessage, &str)> = vec![
            (
                AgentMessage::Text {
                    message_id: "msg_1".to_string(),
                    chunk: "done".to_string(),
                    is_complete: true,
                    model_name: "TestModel".to_string(),
                },
                "done",
            ),
            (
                AgentMessage::Thought {
                    message_id: "msg_1".to_string(),
                    chunk: "final reasoning".to_string(),
                    is_complete: true,
                    model_name: "TestModel".to_string(),
                },
                "final reasoning",
            ),
        ];

        for (message, expected_text) in cases {
            let session_id = acp::SessionId::new("test-session");
            assert!(
                map_agent_message_to_notification(session_id.clone(), &message, NotificationMode::Live).is_none(),
                "live mode should skip completed chunk"
            );

            let notification = map_agent_message_to_notification(session_id, &message, NotificationMode::Replay)
                .expect("replay notification");

            match notification.update {
                acp::SessionUpdate::AgentMessageChunk(chunk) | acp::SessionUpdate::AgentThoughtChunk(chunk) => {
                    match chunk.content {
                        acp::ContentBlock::Text(text) => assert_eq!(text.text, expected_text),
                        other => panic!("Expected text content, got {other:?}"),
                    }
                }
                other => panic!("Expected chunk update, got {other:?}"),
            }
        }
    }

    #[test]
    fn test_context_cleared_maps_to_agent_notification() {
        let notif = try_into_agent_notification(&AgentMessage::ContextCleared)
            .expect("context cleared should emit agent notification");
        assert!(matches!(notif, AgentExtNotification::ContextCleared(_)));
    }

    #[test]
    fn test_tool_progress_with_invalid_json_falls_back_to_simple_message() {
        let session_id = acp::SessionId::new("test-session");

        // Simulate a tool progress message with invalid JSON
        let tool_progress = AgentMessage::ToolProgress {
            request: ToolCallRequest {
                id: "call_456".to_string(),
                name: "some_tool".to_string(),
                arguments: "{}".to_string(),
            },
            progress: 50.0,
            total: None,
            message: Some("not valid json".to_string()),
        };

        let notification = map_agent_message_to_session_notification(session_id.clone(), &tool_progress);

        assert!(notification.is_some());

        // Should still produce a notification with the message as-is
        let notification = notification.unwrap();
        match notification.update {
            acp::SessionUpdate::ToolCallUpdate(update) => {
                if let Some(content) = &update.fields.content
                    && let acp::ToolCallContent::Content(c) = &content[0]
                    && let acp::ContentBlock::Text(text) = &c.content
                {
                    // Should contain the original message
                    assert!(text.text.contains("not valid json"));
                }
            }
            _ => panic!("Expected ToolCallUpdate"),
        }
    }

    #[test]
    fn test_humanize_tool_name() {
        assert_eq!(humanize_tool_name("coding__read_file"), "Read file");
        assert_eq!(humanize_tool_name("read_file"), "Read file");
        assert_eq!(humanize_tool_name("bash"), "Bash");
        assert_eq!(humanize_tool_name("plugins__coding__read_file"), "Read file");
    }

    #[test]
    fn test_result_with_result_meta_sets_meta() {
        use mcp_utils::display_meta::ToolDisplayMeta;

        let session_id = acp::SessionId::new("test-session");
        let result = ToolCallResult {
            id: "call_1".to_string(),
            name: "coding__read_file".to_string(),
            arguments: "{}".to_string(),
            result: "file contents".to_string(),
        };
        let rm: ToolResultMeta = ToolDisplayMeta::new("Read file", "Cargo.toml, 156 lines").into();

        let notification = map_tool_result_to_notification(session_id, &result, Some(&rm));
        match notification.update {
            acp::SessionUpdate::ToolCallUpdate(update) => {
                assert_eq!(update.fields.title.as_deref(), Some("Read file"), "native title should be set");
                let meta = update.meta.expect("meta should be present");
                assert_eq!(
                    meta.get("display_value").and_then(|v| v.as_str()),
                    Some("Cargo.toml, 156 lines"),
                    "display_value should be a flat key in _meta"
                );
                assert!(meta.get("display").is_none(), "old nested display object should not be in _meta");
            }
            other => panic!("Expected ToolCallUpdate, got {other:?}"),
        }
    }

    #[test]
    fn test_result_without_result_meta() {
        let session_id = acp::SessionId::new("test-session");
        let result = ToolCallResult {
            id: "call_1".to_string(),
            name: "external__some_tool".to_string(),
            arguments: "{}".to_string(),
            result: "ok".to_string(),
        };

        let notification = map_tool_result_to_notification(session_id, &result, None);
        match notification.update {
            acp::SessionUpdate::ToolCallUpdate(update) => {
                assert!(update.fields.title.is_none());
                assert!(update.meta.is_none());
            }
            other => panic!("Expected ToolCallUpdate, got {other:?}"),
        }
    }

    #[test]
    fn test_plan_notification_extracted_from_result_meta() {
        use mcp_utils::display_meta::{PlanMeta, PlanMetaEntry, PlanMetaStatus, ToolDisplayMeta};

        let session_id = acp::SessionId::new("test-session");
        let meta = ToolResultMeta::with_plan(
            ToolDisplayMeta::new("Todo", "Research AI agents"),
            PlanMeta {
                entries: vec![
                    PlanMetaEntry { content: "Research AI agents".to_string(), status: PlanMetaStatus::InProgress },
                    PlanMetaEntry { content: "Write tests".to_string(), status: PlanMetaStatus::Pending },
                ],
            },
        );

        let notification = try_extract_plan_notification(session_id, Some(&meta)).expect("should produce plan");
        match notification.update {
            acp::SessionUpdate::Plan(plan) => {
                assert_eq!(plan.entries.len(), 2);
                assert_eq!(plan.entries[0].content, "Research AI agents");
                assert_eq!(plan.entries[0].status, acp::PlanEntryStatus::InProgress);
                assert_eq!(plan.entries[1].content, "Write tests");
                assert_eq!(plan.entries[1].status, acp::PlanEntryStatus::Pending);
            }
            other => panic!("Expected Plan, got {other:?}"),
        }
    }

    #[test]
    fn test_plan_notification_none_when_no_plan_or_no_meta() {
        use mcp_utils::display_meta::ToolDisplayMeta;

        let sid = acp::SessionId::new("test-session");
        let meta: ToolResultMeta = ToolDisplayMeta::new("Read file", "main.rs").into();
        assert!(try_extract_plan_notification(sid.clone(), Some(&meta)).is_none());
        assert!(try_extract_plan_notification(sid, None).is_none());
    }

    #[test]
    fn test_tool_progress_with_display_meta_emits_meta_update() {
        use mcp_utils::display_meta::ToolDisplayMeta;

        let session_id = acp::SessionId::new("test-session");
        let meta = ToolResultMeta::from(ToolDisplayMeta::new("Read file", "main.rs"));
        let serialized = serde_json::to_string(&meta).unwrap();

        let request = ToolCallRequest {
            id: "call_789".to_string(),
            name: "coding__read_file".to_string(),
            arguments: "{}".to_string(),
        };

        let notification = map_tool_progress_to_notification(session_id, &request, 0.0, None, Some(&serialized))
            .expect("should produce notification");

        match notification.update {
            acp::SessionUpdate::ToolCallUpdate(update) => {
                assert_eq!(&*update.tool_call_id.0, "call_789");
                assert_eq!(update.fields.title.as_deref(), Some("Read file"), "native title should be set");
                let meta_map = update.meta.expect("meta should be present");
                assert_eq!(
                    meta_map.get("display_value").and_then(|v| v.as_str()),
                    Some("main.rs"),
                    "display_value should be a flat key in _meta"
                );
                assert!(meta_map.get("display").is_none(), "old nested display object should not be in _meta");
                assert_eq!(update.fields.status, Some(acp::ToolCallStatus::InProgress));
                // Should NOT have content (no text progress fallback)
                assert!(update.fields.content.is_none());
            }
            other => panic!("Expected ToolCallUpdate, got {other:?}"),
        }
    }
}