vv-agent 0.7.2

VectorVein agent runtime, SDK, CLI, tools, and workspace backends
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
use std::collections::BTreeMap;
use std::time::Duration;

use serde_json::{json, Value};
use tokio::sync::mpsc;
use vv_agent::app_server::protocol::{
    map_run_event_to_notifications, AppItemKind, AppItemStatus, JsonRpcMessage,
    JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, RequestId, ServerNotification,
    ThreadReadResponse, ThreadStartResponse, TurnStartResponse, TurnStatus,
};
use vv_agent::app_server::{outgoing::OutgoingEnvelope, processor::MessageProcessor};
use vv_agent::app_server::{thread_store::SqliteThreadStore, transport::ConnectionId};
use vv_agent::events::ApprovalAction;
use vv_agent::{
    Agent, AgentStatus, CompletionReason, FunctionTool, LLMResponse, ModelRef, NoToolPolicy,
    RunEvent, RunEventPayload, Runner, ScriptedModelProvider, ToolCall, ToolOutput, ToolStatus,
};

const APP_SERVER_CONTRACT: &str = include_str!("fixtures/parity/app_server_observable_v1.json");

fn status_projection(name: &str) -> Value {
    let contract: Value = serde_json::from_str(APP_SERVER_CONTRACT).expect("App Server contract");
    contract["terminal"]["agentStatusProjection"]
        .as_array()
        .expect("agent status projections")
        .iter()
        .find(|case| case["name"] == name)
        .unwrap_or_else(|| panic!("missing App Server projection {name}"))
        .clone()
}

#[test]
fn item_mapping_assistant_delta_becomes_agent_message_delta() {
    let event = RunEvent::assistant_delta("run_1", "trace_1", "assistant", 1, "hello");

    let notifications = map_run_event_to_notifications("thread_1", "turn_1", &event);

    let [ServerNotification::AgentMessageDelta(delta)] = notifications.as_slice() else {
        panic!("expected agent message delta");
    };
    assert_eq!(delta.item.thread_id, "thread_1");
    assert_eq!(delta.item.turn_id, "turn_1");
    assert_eq!(
        delta.item.item_id,
        format!("item_{}", event.event_id().as_str())
    );
    assert_eq!(delta.delta, "hello");
}

#[test]
fn item_mapping_tool_call_started_becomes_started_tool_item() {
    let event = RunEvent::tool_call_started(
        "run_1",
        "trace_1",
        "assistant",
        1,
        "call_1",
        "bash",
        json!({"cmd": "cargo test"}),
    );

    let notifications = map_run_event_to_notifications("thread_1", "turn_1", &event);

    let Some(ServerNotification::ItemStarted(started)) = notifications.first() else {
        panic!("expected item started first");
    };
    assert_eq!(
        started.item.item_id,
        format!("item_{}", event.event_id().as_str())
    );
    assert_eq!(started.item.kind, AppItemKind::ToolCall);
    assert_eq!(started.item.status, AppItemStatus::Started);
    assert_eq!(started.item.payload["toolName"], "bash");
    assert!(matches!(
        notifications.get(1),
        Some(ServerNotification::ToolCallDelta(_))
    ));
}

#[test]
fn item_mapping_tool_call_completed_becomes_completed_item() {
    let event = RunEvent::tool_call_completed(
        "run_1",
        "trace_1",
        "assistant",
        Some(1),
        "call_1",
        "bash",
        ToolStatus::Success,
    );

    let notifications = map_run_event_to_notifications("thread_1", "turn_1", &event);

    let [ServerNotification::ItemCompleted(completed)] = notifications.as_slice() else {
        panic!("expected item completed");
    };
    assert_eq!(completed.item.kind, AppItemKind::ToolCall);
    assert_eq!(completed.item.status, AppItemStatus::Completed);
    assert_eq!(
        completed.item.updated_at,
        event.created_at_ms() as f64 / 1000.0
    );
}

#[test]
fn item_mapping_approval_requested_becomes_approval_notification() {
    let event = RunEvent::approval_requested(
        "run_1",
        "trace_1",
        "assistant",
        "approval_1",
        "call_1",
        "bash",
        "Run cargo test",
    )
    .with_metadata("arguments", json!({"cmd": "cargo test"}))
    .with_metadata("tool_name", json!("bash"));

    let notifications = map_run_event_to_notifications("thread_1", "turn_1", &event);

    let [ServerNotification::ItemStarted(started), ServerNotification::ApprovalRequested(approval)] =
        notifications.as_slice()
    else {
        panic!("expected approval item and request");
    };
    assert_eq!(started.item.payload["message"], "Run cargo test");
    assert_eq!(
        started.item.payload["arguments"],
        json!({"cmd": "cargo test"})
    );
    assert_eq!(approval.thread_id, "thread_1");
    assert_eq!(approval.turn_id, "turn_1");
    assert_eq!(approval.request_id, "approval_1");
    assert_eq!(approval.tool_name, "bash");
    assert_eq!(approval.arguments, json!({"cmd": "cargo test"}));
}

#[test]
fn item_mapping_approval_resolved_preserves_reason_and_metadata() {
    let event = RunEvent::new(
        "run_1",
        "trace_1",
        "assistant",
        Some(1),
        RunEventPayload::ApprovalResolved {
            request_id: "approval_1".to_string(),
            tool_name: "bash".to_string(),
            tool_call_id: "call_1".to_string(),
            approved: true,
        },
    )
    .with_approval_action(ApprovalAction::AllowSession)
    .with_metadata("action", json!("allow_session"))
    .with_metadata("reason", json!("approved by owner"))
    .with_metadata("decision_metadata", json!({"ticket": 7}));

    let notifications = map_run_event_to_notifications("thread_1", "turn_1", &event);

    let [ServerNotification::ItemCompleted(completed), ServerNotification::ApprovalResolved(resolved)] =
        notifications.as_slice()
    else {
        panic!("expected approval completion and resolution");
    };
    assert_eq!(completed.item.payload["reason"], "approved by owner");
    assert_eq!(
        completed.item.payload["decisionMetadata"],
        json!({"ticket": 7})
    );
    assert_eq!(resolved.reason, "approved by owner");
    assert_eq!(resolved.metadata["ticket"], 7);
}

#[test]
fn item_mapping_leaves_run_completion_to_runtime_adapter() {
    let event = RunEvent::run_completed("run_1", "trace_1", "assistant", AgentStatus::Completed);

    let notifications = map_run_event_to_notifications("thread_1", "turn_1", &event);

    assert!(notifications.is_empty());
}

#[tokio::test]
async fn json_rpc_thread_turn_streams_notifications_and_replays_items() {
    let runner = Runner::builder()
        .model_provider(ScriptedModelProvider::new(
            "scripted",
            "demo-model",
            vec![finish_response("hello world")],
        ))
        .workspace(".")
        .build()
        .expect("runner");
    let agent = Agent::builder("assistant")
        .instructions("Answer the user, then finish.")
        .model(ModelRef::named("demo-model"))
        .build()
        .expect("agent");
    let store = SqliteThreadStore::in_memory().expect("store");
    let (mut processor, mut outgoing) =
        MessageProcessor::new_for_tests_with_runtime(32, runner, agent, store);
    let connection_id = ConnectionId::new(1);

    processor
        .process_message(connection_id, initialize_request(1))
        .await;
    let _initialize = expect_response(&mut outgoing).await;
    processor
        .process_message(connection_id, initialized_notification())
        .await;

    processor
        .process_message(
            connection_id,
            request(
                2,
                "thread/start",
                json!({
                    "agentKey": "default",
                    "metadata": {"title": "demo"}
                }),
            ),
        )
        .await;
    let thread_response: ThreadStartResponse =
        decode_response(expect_response(&mut outgoing).await);
    let thread_id = thread_response.thread_id.clone();
    assert_eq!(thread_response.agent_key, "default");
    assert!(matches!(
        expect_notification(&mut outgoing).await,
        ServerNotification::ThreadStarted(_)
    ));

    processor
        .process_message(
            connection_id,
            request(
                3,
                "turn/start",
                json!({
                    "threadId": thread_id,
                    "input": [{"type": "text", "text": "say hello"}]
                }),
            ),
        )
        .await;
    let turn_response: TurnStartResponse = decode_response(expect_response(&mut outgoing).await);
    let turn_id = turn_response.turn_id.clone();
    assert_eq!(turn_response.thread_id, thread_id);
    assert_eq!(turn_response.status, TurnStatus::Running);

    let started = next_notification_matching(&mut outgoing, |notification| {
        matches!(notification, ServerNotification::TurnStarted(_))
    })
    .await;
    assert!(matches!(started, ServerNotification::TurnStarted(_)));

    let item_completed = next_notification_matching(&mut outgoing, |notification| {
        matches!(
            notification,
            ServerNotification::ItemCompleted(completed)
                if completed.item.kind == AppItemKind::AgentMessage
        )
    })
    .await;
    let ServerNotification::ItemCompleted(item_completed) = item_completed else {
        unreachable!("matched completed agent message")
    };
    assert_eq!(item_completed.item.thread_id, thread_id);
    assert_eq!(item_completed.item.turn_id, turn_id);
    assert_eq!(item_completed.item.payload["text"], "hello world");

    let completed = next_notification_matching(&mut outgoing, |notification| {
        matches!(notification, ServerNotification::TurnCompleted(_))
    })
    .await;
    let ServerNotification::TurnCompleted(completed) = completed else {
        unreachable!("matched turn completed")
    };
    assert_eq!(completed.turn_id, turn_id);
    assert_eq!(completed.status, TurnStatus::Completed);
    assert_eq!(completed.final_output.as_deref(), Some("hello world"));
    assert_eq!(completed.completion_reason.as_deref(), Some("tool_finish"));
    assert_eq!(
        completed.completion_tool_name.as_deref(),
        Some("task_finish")
    );
    assert_eq!(completed.partial_output, None);

    processor
        .process_message(
            connection_id,
            request(4, "thread/read", json!({ "threadId": thread_id })),
        )
        .await;
    let read: ThreadReadResponse = decode_response(expect_response(&mut outgoing).await);
    assert_eq!(read.thread.thread_id, thread_id);
    assert!(read
        .items
        .iter()
        .any(|item| item.kind == AppItemKind::AgentMessage));
    assert!(read
        .items
        .iter()
        .any(|item| item.kind == AppItemKind::ToolCall));
    let persisted = read
        .turns
        .iter()
        .find(|turn| turn.turn_id == turn_id)
        .expect("persisted completed turn");
    assert_eq!(persisted.result["completionReason"], "tool_finish");
    assert_eq!(persisted.result["completionToolName"], "task_finish");
    assert!(!persisted.result.contains_key("partialOutput"));
}

#[tokio::test]
async fn wait_user_turn_is_interrupted_in_notification_and_persisted_snapshot() {
    let expected = status_projection("wait_user_is_interrupted_without_error");
    let runner = Runner::builder()
        .model_provider(ScriptedModelProvider::new(
            "scripted",
            "demo-model",
            vec![LLMResponse::new("need more details")],
        ))
        .workspace(".")
        .build()
        .expect("runner");
    let agent = Agent::builder("assistant")
        .instructions("Wait for clarification.")
        .model(ModelRef::named("demo-model"))
        .no_tool_policy(NoToolPolicy::WaitUser)
        .build()
        .expect("agent");
    let store = SqliteThreadStore::in_memory().expect("store");
    let (mut processor, mut outgoing) =
        MessageProcessor::new_for_tests_with_runtime(32, runner, agent, store);
    let connection_id = ConnectionId::new(2);

    processor
        .process_message(connection_id, initialize_request(1))
        .await;
    let _ = expect_response(&mut outgoing).await;
    processor
        .process_message(connection_id, initialized_notification())
        .await;
    processor
        .process_message(
            connection_id,
            request(2, "thread/start", json!({"agentKey": "default"})),
        )
        .await;
    let thread: ThreadStartResponse = decode_response(expect_response(&mut outgoing).await);
    let _ = next_notification_matching(&mut outgoing, |notification| {
        matches!(notification, ServerNotification::ThreadStarted(_))
    })
    .await;
    processor
        .process_message(
            connection_id,
            request(
                3,
                "turn/start",
                json!({
                    "threadId": thread.thread_id,
                    "input": [{"type": "text", "text": "ambiguous request"}]
                }),
            ),
        )
        .await;
    let started: TurnStartResponse = decode_response(expect_response(&mut outgoing).await);
    let completed = next_notification_matching(&mut outgoing, |notification| {
        matches!(notification, ServerNotification::TurnCompleted(_))
    })
    .await;
    let ServerNotification::TurnCompleted(completed) = completed else {
        unreachable!("matched turn completion")
    };

    assert_eq!(
        serde_json::to_value(completed.status).expect("turn status"),
        expected["turnStatus"]
    );
    assert_eq!(
        completed.completion_reason.as_deref(),
        expected["completionReason"].as_str()
    );
    assert_eq!(completed.completion_tool_name, None);
    assert_eq!(
        completed.partial_output.as_deref(),
        Some("need more details")
    );
    assert_eq!(completed.final_output.as_deref(), Some("need more details"));
    assert_eq!(
        completed.error.is_some(),
        expected["errorField"] == "present"
    );
    assert_eq!(
        CompletionReason::parse(completed.completion_reason.as_deref().unwrap()),
        Some(CompletionReason::WaitUser)
    );

    processor
        .process_message(
            connection_id,
            request(4, "thread/read", json!({"threadId": thread.thread_id})),
        )
        .await;
    let read: ThreadReadResponse = decode_response(expect_response(&mut outgoing).await);
    let persisted = read
        .turns
        .iter()
        .find(|turn| turn.turn_id == started.turn_id)
        .expect("persisted wait turn");
    assert_eq!(
        serde_json::to_value(persisted.status).expect("persisted turn status"),
        expected["turnStatus"]
    );
    assert_eq!(
        persisted.result["completionReason"],
        expected["completionReason"]
    );
    assert_eq!(persisted.result["partialOutput"], "need more details");
    assert_eq!(
        persisted.result.contains_key("error"),
        expected["errorField"] == "present"
    );
}

#[tokio::test]
async fn cancelled_failed_turn_stays_failed_in_notification_and_persisted_snapshot() {
    let expected = status_projection("cancelled_failure_stays_failed");
    let approval_tool = FunctionTool::builder("approval_tool")
        .description("Pause at the App Server approval boundary.")
        .json_schema(json!({"type": "object", "properties": {}, "required": []}))
        .needs_approval(true)
        .handler(|_context, _arguments: serde_json::Value| async move {
            Ok(ToolOutput::text("should not execute"))
        })
        .build()
        .expect("approval tool");
    let runner = Runner::builder()
        .model_provider(ScriptedModelProvider::new(
            "scripted",
            "cancel-model",
            vec![LLMResponse::with_tool_calls(
                "waiting for approval",
                vec![ToolCall::from_raw_arguments(
                    "approval-call",
                    "approval_tool",
                    json!({}),
                )],
            )],
        ))
        .workspace(".")
        .build()
        .expect("runner");
    let agent = Agent::builder("assistant")
        .instructions("Finish after the blocking model responds.")
        .model(ModelRef::named("cancel-model"))
        .tool(approval_tool)
        .build()
        .expect("agent");
    let store = SqliteThreadStore::in_memory().expect("store");
    let (mut processor, mut outgoing) =
        MessageProcessor::new_for_tests_with_runtime(32, runner, agent, store);
    let connection_id = ConnectionId::new(3);

    processor
        .process_message(connection_id, initialize_request(1))
        .await;
    let _ = expect_response(&mut outgoing).await;
    processor
        .process_message(connection_id, initialized_notification())
        .await;
    processor
        .process_message(
            connection_id,
            request(2, "thread/start", json!({"agentKey": "default"})),
        )
        .await;
    let thread: ThreadStartResponse = decode_response(expect_response(&mut outgoing).await);
    let _ = next_notification_matching(&mut outgoing, |notification| {
        matches!(notification, ServerNotification::ThreadStarted(_))
    })
    .await;
    processor
        .process_message(
            connection_id,
            request(
                3,
                "turn/start",
                json!({
                    "threadId": thread.thread_id,
                    "input": [{"type": "text", "text": "start and then cancel"}]
                }),
            ),
        )
        .await;
    let started: TurnStartResponse = decode_response(expect_response(&mut outgoing).await);
    let _ = next_notification_matching(&mut outgoing, |notification| {
        matches!(notification, ServerNotification::ApprovalRequested(_))
    })
    .await;
    let _approval_request = next_server_request(&mut outgoing).await;

    processor
        .process_message(
            connection_id,
            request(
                4,
                "turn/interrupt",
                json!({
                    "threadId": thread.thread_id,
                    "expectedTurnId": started.turn_id,
                    "reason": "stop"
                }),
            ),
        )
        .await;
    let interrupt = loop {
        let response = expect_response(&mut outgoing).await;
        if response.id == RequestId::Integer(4) {
            break response;
        }
    };
    assert_eq!(interrupt.result["cancelled"], true);

    let completed = next_notification_matching(&mut outgoing, |notification| {
        matches!(notification, ServerNotification::TurnCompleted(_))
    })
    .await;
    let ServerNotification::TurnCompleted(completed) = completed else {
        unreachable!("matched turn completion")
    };
    assert_eq!(
        serde_json::to_value(completed.status).expect("turn status"),
        expected["turnStatus"]
    );
    assert_eq!(
        completed.completion_reason.as_deref(),
        expected["completionReason"].as_str()
    );
    assert!(completed
        .error
        .as_deref()
        .is_some_and(|error| error.contains("cancel")));

    processor
        .process_message(
            connection_id,
            request(5, "thread/read", json!({"threadId": thread.thread_id})),
        )
        .await;
    let read: ThreadReadResponse = decode_response(expect_response(&mut outgoing).await);
    let persisted = read
        .turns
        .iter()
        .find(|turn| turn.turn_id == started.turn_id)
        .expect("persisted cancelled turn");
    assert_eq!(
        serde_json::to_value(persisted.status).expect("persisted turn status"),
        expected["turnStatus"]
    );
    assert_eq!(
        persisted.result["completionReason"],
        expected["completionReason"]
    );
    assert_eq!(
        persisted.result.contains_key("error"),
        expected["errorField"] == "present"
    );
}

fn request(id: i64, method: &str, params: serde_json::Value) -> JsonRpcMessage {
    JsonRpcMessage::Request(JsonRpcRequest {
        id: RequestId::Integer(id),
        method: method.to_string(),
        params: Some(params),
    })
}

fn initialize_request(id: i64) -> JsonRpcMessage {
    request(
        id,
        "initialize",
        json!({
            "clientInfo": {
                "name": "test_client",
                "title": "Test Client",
                "version": "1.0.0"
            },
            "capabilities": {
                "experimentalApi": false,
                "optOutNotificationMethods": []
            }
        }),
    )
}

fn initialized_notification() -> JsonRpcMessage {
    JsonRpcMessage::Notification(JsonRpcNotification {
        method: "initialized".to_string(),
        params: None,
    })
}

async fn expect_response(rx: &mut mpsc::Receiver<OutgoingEnvelope>) -> JsonRpcResponse {
    let envelope = tokio::time::timeout(Duration::from_secs(3), rx.recv())
        .await
        .expect("message timeout")
        .expect("outgoing message");
    let JsonRpcMessage::Response(response) = envelope.message else {
        panic!("expected response, got {:?}", envelope.message);
    };
    response
}

async fn next_server_request(rx: &mut mpsc::Receiver<OutgoingEnvelope>) -> JsonRpcRequest {
    loop {
        let envelope = tokio::time::timeout(Duration::from_secs(3), rx.recv())
            .await
            .expect("message timeout")
            .expect("outgoing message");
        if let JsonRpcMessage::Request(request) = envelope.message {
            return request;
        }
    }
}

async fn expect_notification(rx: &mut mpsc::Receiver<OutgoingEnvelope>) -> ServerNotification {
    let envelope = tokio::time::timeout(Duration::from_secs(3), rx.recv())
        .await
        .expect("message timeout")
        .expect("outgoing message");
    let JsonRpcMessage::Notification(notification) = envelope.message else {
        panic!("expected notification, got {:?}", envelope.message);
    };
    decode_notification(notification)
}

async fn next_notification_matching(
    rx: &mut mpsc::Receiver<OutgoingEnvelope>,
    predicate: impl Fn(&ServerNotification) -> bool,
) -> ServerNotification {
    loop {
        let notification = expect_notification(rx).await;
        if predicate(&notification) {
            return notification;
        }
    }
}

fn decode_response<T: serde::de::DeserializeOwned>(response: JsonRpcResponse) -> T {
    serde_json::from_value(response.result).expect("response payload")
}

fn decode_notification(notification: JsonRpcNotification) -> ServerNotification {
    let value = match notification.params {
        Some(params) => json!({
            "method": notification.method,
            "params": params,
        }),
        None => json!({
            "method": notification.method,
        }),
    };
    serde_json::from_value(value).expect("server notification")
}

fn finish_response(message: &str) -> LLMResponse {
    let mut args = BTreeMap::new();
    args.insert("message".to_string(), json!(message));
    LLMResponse::with_tool_calls(message, vec![ToolCall::new("finish", "task_finish", args)])
}