1use super::*;
2use crate::udiff::apply_diff_to_string;
3use client::{RefreshLlmTokenListener, UserStore, test::FakeServer};
4use clock::FakeSystemClock;
5use clock::ReplicaId;
6use cloud_api_types::{CreateLlmTokenResponse, LlmToken};
7use cloud_llm_client::{
8 EditPredictionRejectReason, EditPredictionRejection, RejectEditPredictionsBody,
9 predict_edits_v3::{PredictEditsV3Request, PredictEditsV3Response},
10};
11
12use futures::{
13 AsyncReadExt, FutureExt, StreamExt,
14 channel::{mpsc, oneshot},
15};
16use gpui::App;
17use gpui::{
18 Entity, TestAppContext,
19 http_client::{FakeHttpClient, Response},
20};
21use indoc::indoc;
22use language::{
23 Anchor, Buffer, Capability, CursorShape, Diagnostic, DiagnosticEntry, DiagnosticSet,
24 DiagnosticSeverity, Operation, Point, Selection, SelectionGoal,
25};
26
27use lsp::LanguageServerId;
28use parking_lot::Mutex;
29use pretty_assertions::{assert_eq, assert_matches};
30use project::{FakeFs, Project};
31use serde_json::json;
32use settings::SettingsStore;
33use std::{ops::Range, path::Path, sync::Arc, time::Duration};
34use util::{
35 path,
36 test::{TextRangeMarker, marked_text_ranges_by},
37};
38use uuid::Uuid;
39use workspace::{AppState, CollaboratorId, MultiWorkspace};
40use zeta_prompt::ZetaPromptInput;
41
42use crate::{
43 BufferEditPrediction, EDIT_PREDICTION_SETTLED_QUIESCENCE, EditPredictionId,
44 EditPredictionJumpsFeatureFlag, EditPredictionStore, REJECT_REQUEST_DEBOUNCE,
45};
46
47#[gpui::test]
48async fn test_current_state(cx: &mut TestAppContext) {
49 let (ep_store, mut requests) = init_test_with_fake_client(cx);
50 let fs = FakeFs::new(cx.executor());
51 fs.insert_tree(
52 "/root",
53 json!({
54 "1.txt": "Hello!\nHow\nBye\n",
55 "2.txt": "Hola!\nComo\nAdios\n"
56 }),
57 )
58 .await;
59 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
60
61 let buffer1 = project
62 .update(cx, |project, cx| {
63 let path = project.find_project_path(path!("/root/1.txt"), cx).unwrap();
64 project.set_active_path(Some(path.clone()), cx);
65 project.open_buffer(path, cx)
66 })
67 .await
68 .unwrap();
69 let snapshot1 = buffer1.read_with(cx, |buffer, _cx| buffer.snapshot());
70 let position = snapshot1.anchor_before(language::Point::new(1, 3));
71
72 ep_store.update(cx, |ep_store, cx| {
73 ep_store.register_project(&project, cx);
74 ep_store.register_buffer(&buffer1, &project, cx);
75 });
76
77 // Prediction for current file
78
79 ep_store.update(cx, |ep_store, cx| {
80 ep_store.refresh_prediction_from_buffer(project.clone(), buffer1.clone(), position, cx)
81 });
82 let (request, respond_tx) = requests.predict.next().await.unwrap();
83
84 respond_tx
85 .send(model_response(
86 &request,
87 indoc! {r"
88 --- a/root/1.txt
89 +++ b/root/1.txt
90 @@ ... @@
91 Hello!
92 -How
93 +How are you?
94 Bye
95 "},
96 ))
97 .unwrap();
98
99 cx.run_until_parked();
100
101 ep_store.update(cx, |ep_store, cx| {
102 let prediction = ep_store
103 .prediction_at(&buffer1, None, &project, cx)
104 .unwrap();
105 assert_matches!(prediction, BufferEditPrediction::Local { .. });
106 });
107
108 ep_store.update(cx, |ep_store, cx| {
109 ep_store.reject_current_prediction(EditPredictionRejectReason::Discarded, &project, cx);
110 });
111
112 // Prediction for diagnostic in another file
113
114 let diagnostic = lsp::Diagnostic {
115 range: lsp::Range::new(lsp::Position::new(1, 1), lsp::Position::new(1, 5)),
116 severity: Some(lsp::DiagnosticSeverity::ERROR),
117 message: "Sentence is incomplete".to_string(),
118 ..Default::default()
119 };
120
121 project.update(cx, |project, cx| {
122 project.lsp_store().update(cx, |lsp_store, cx| {
123 lsp_store
124 .update_diagnostics(
125 LanguageServerId(0),
126 lsp::PublishDiagnosticsParams {
127 uri: lsp::Uri::from_file_path(path!("/root/2.txt")).unwrap(),
128 diagnostics: vec![diagnostic],
129 version: None,
130 },
131 None,
132 language::DiagnosticSourceKind::Pushed,
133 &[],
134 cx,
135 )
136 .unwrap();
137 });
138 });
139
140 let (request, respond_tx) = requests.predict.next().await.unwrap();
141 respond_tx
142 .send(model_response(
143 &request,
144 indoc! {r#"
145 --- a/root/2.txt
146 +++ b/root/2.txt
147 @@ ... @@
148 Hola!
149 -Como
150 +Como estas?
151 Adios
152 "#},
153 ))
154 .unwrap();
155 cx.run_until_parked();
156
157 ep_store.update(cx, |ep_store, cx| {
158 let prediction = ep_store
159 .prediction_at(&buffer1, None, &project, cx)
160 .unwrap();
161 assert_matches!(
162 prediction,
163 BufferEditPrediction::Jump { prediction } if prediction.snapshot.file().unwrap().full_path(cx) == Path::new(path!("root/2.txt"))
164 );
165 });
166
167 let buffer2 = project
168 .update(cx, |project, cx| {
169 let path = project.find_project_path(path!("root/2.txt"), cx).unwrap();
170 project.open_buffer(path, cx)
171 })
172 .await
173 .unwrap();
174
175 ep_store.update(cx, |ep_store, cx| {
176 let prediction = ep_store
177 .prediction_at(&buffer2, None, &project, cx)
178 .unwrap();
179 assert_matches!(prediction, BufferEditPrediction::Local { .. });
180 });
181}
182
183#[gpui::test]
184async fn test_diagnostics_refresh_suppressed_while_following(cx: &mut TestAppContext) {
185 let (ep_store, mut requests) = init_test_with_fake_client(cx);
186
187 cx.update(|cx| {
188 cx.update_flags(
189 false,
190 vec![EditPredictionJumpsFeatureFlag::NAME.to_string()],
191 );
192 });
193
194 let fs = FakeFs::new(cx.executor());
195 fs.insert_tree(
196 "/root",
197 json!({
198 "1.txt": "Hello!\nHow\nBye\n",
199 "2.txt": "Hola!\nComo\nAdios\n"
200 }),
201 )
202 .await;
203 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
204
205 let app_state = cx.update(|cx| {
206 let app_state = AppState::test(cx);
207 AppState::set_global(app_state.clone(), cx);
208 app_state
209 });
210
211 let multi_workspace =
212 cx.add_window(|window, cx| MultiWorkspace::test_new(project.clone(), window, cx));
213 let workspace = multi_workspace
214 .read_with(cx, |multi_workspace, _| multi_workspace.workspace().clone())
215 .unwrap();
216 cx.update(|cx| {
217 AppState::set_global(workspace.read(cx).app_state().clone(), cx);
218 });
219 let _ = app_state;
220
221 let buffer1 = project
222 .update(cx, |project, cx| {
223 let path = project.find_project_path(path!("root/1.txt"), cx).unwrap();
224 project.set_active_path(Some(path.clone()), cx);
225 project.open_buffer(path, cx)
226 })
227 .await
228 .unwrap();
229 let snapshot1 = buffer1.read_with(cx, |buffer, _cx| buffer.snapshot());
230 let position = snapshot1.anchor_before(language::Point::new(1, 3));
231
232 ep_store.update(cx, |ep_store, cx| {
233 ep_store.register_project(&project, cx);
234 ep_store.register_buffer(&buffer1, &project, cx);
235 ep_store.refresh_prediction_from_buffer(project.clone(), buffer1.clone(), position, cx);
236 });
237
238 let (request, respond_tx) = requests.predict.next().await.unwrap();
239 respond_tx
240 .send(model_response(
241 &request,
242 indoc! {r"
243 --- a/root/1.txt
244 +++ b/root/1.txt
245 @@ ... @@
246 Hello!
247 -How
248 +How are you?
249 Bye
250 "},
251 ))
252 .unwrap();
253 cx.run_until_parked();
254
255 ep_store.update(cx, |ep_store, cx| {
256 ep_store.reject_current_prediction(EditPredictionRejectReason::Discarded, &project, cx);
257 });
258
259 let _ = multi_workspace.update(cx, |multi_workspace, window, cx| {
260 multi_workspace.workspace().update(cx, |workspace, cx| {
261 workspace.start_following(CollaboratorId::Agent, window, cx);
262 });
263 });
264 cx.run_until_parked();
265
266 let diagnostic = lsp::Diagnostic {
267 range: lsp::Range::new(lsp::Position::new(1, 1), lsp::Position::new(1, 5)),
268 severity: Some(lsp::DiagnosticSeverity::ERROR),
269 message: "Sentence is incomplete".to_string(),
270 ..Default::default()
271 };
272
273 project.update(cx, |project, cx| {
274 project.lsp_store().update(cx, |lsp_store, cx| {
275 lsp_store
276 .update_diagnostics(
277 LanguageServerId(0),
278 lsp::PublishDiagnosticsParams {
279 uri: lsp::Uri::from_file_path(path!("/root/2.txt")).unwrap(),
280 diagnostics: vec![diagnostic.clone()],
281 version: None,
282 },
283 None,
284 language::DiagnosticSourceKind::Pushed,
285 &[],
286 cx,
287 )
288 .unwrap();
289 });
290 });
291
292 cx.run_until_parked();
293 assert_no_predict_request_ready(&mut requests.predict);
294
295 let _ = multi_workspace.update(cx, |multi_workspace, window, cx| {
296 multi_workspace.workspace().update(cx, |workspace, cx| {
297 workspace.unfollow(CollaboratorId::Agent, window, cx);
298 });
299 });
300 cx.run_until_parked();
301
302 project.update(cx, |project, cx| {
303 project.lsp_store().update(cx, |lsp_store, cx| {
304 lsp_store
305 .update_diagnostics(
306 LanguageServerId(0),
307 lsp::PublishDiagnosticsParams {
308 uri: lsp::Uri::from_file_path(path!("/root/2.txt")).unwrap(),
309 diagnostics: vec![diagnostic],
310 version: None,
311 },
312 None,
313 language::DiagnosticSourceKind::Pushed,
314 &[],
315 cx,
316 )
317 .unwrap();
318 });
319 });
320
321 let (request, respond_tx) = requests.predict.next().await.unwrap();
322 respond_tx
323 .send(model_response(
324 &request,
325 indoc! {r#"
326 --- a/root/2.txt
327 +++ b/root/2.txt
328 @@ ... @@
329 Hola!
330 -Como
331 +Como estas?
332 Adios
333 "#},
334 ))
335 .unwrap();
336 cx.run_until_parked();
337
338 ep_store.update(cx, |ep_store, cx| {
339 let prediction = ep_store
340 .prediction_at(&buffer1, None, &project, cx)
341 .unwrap();
342 assert_matches!(
343 prediction,
344 BufferEditPrediction::Jump { prediction } if prediction.snapshot.file().unwrap().full_path(cx) == Path::new(path!("root/2.txt"))
345 );
346 });
347}
348
349#[gpui::test]
350async fn test_simple_request(cx: &mut TestAppContext) {
351 let (ep_store, mut requests) = init_test_with_fake_client(cx);
352 let fs = FakeFs::new(cx.executor());
353 fs.insert_tree(
354 "/root",
355 json!({
356 "foo.md": "Hello!\nHow\nBye\n"
357 }),
358 )
359 .await;
360 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
361
362 let buffer = project
363 .update(cx, |project, cx| {
364 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
365 project.open_buffer(path, cx)
366 })
367 .await
368 .unwrap();
369 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
370 let position = snapshot.anchor_before(language::Point::new(1, 3));
371
372 let prediction_task = ep_store.update(cx, |ep_store, cx| {
373 ep_store.request_prediction(&project, &buffer, position, Default::default(), cx)
374 });
375
376 let (request, respond_tx) = requests.predict.next().await.unwrap();
377
378 // TODO Put back when we have a structured request again
379 // assert_eq!(
380 // request.excerpt_path.as_ref(),
381 // Path::new(path!("root/foo.md"))
382 // );
383 // assert_eq!(
384 // request.cursor_point,
385 // Point {
386 // line: Line(1),
387 // column: 3
388 // }
389 // );
390
391 respond_tx
392 .send(model_response(
393 &request,
394 indoc! { r"
395 --- a/root/foo.md
396 +++ b/root/foo.md
397 @@ ... @@
398 Hello!
399 -How
400 +How are you?
401 Bye
402 "},
403 ))
404 .unwrap();
405
406 let prediction = prediction_task.await.unwrap().unwrap().prediction.unwrap();
407
408 assert_eq!(prediction.edits.len(), 1);
409 assert_eq!(
410 prediction.edits[0].0.to_point(&snapshot).start,
411 language::Point::new(1, 3)
412 );
413 assert_eq!(prediction.edits[0].1.as_ref(), " are you?");
414}
415
416#[gpui::test]
417async fn test_request_events(cx: &mut TestAppContext) {
418 let (ep_store, mut requests) = init_test_with_fake_client(cx);
419 let fs = FakeFs::new(cx.executor());
420 fs.insert_tree(
421 "/root",
422 json!({
423 "foo.md": "Hello!\n\nBye\n"
424 }),
425 )
426 .await;
427 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
428
429 let buffer = project
430 .update(cx, |project, cx| {
431 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
432 project.open_buffer(path, cx)
433 })
434 .await
435 .unwrap();
436
437 ep_store.update(cx, |ep_store, cx| {
438 ep_store.register_buffer(&buffer, &project, cx);
439 });
440
441 buffer.update(cx, |buffer, cx| {
442 buffer.edit(vec![(7..7, "How")], None, cx);
443 });
444
445 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
446 let position = snapshot.anchor_before(language::Point::new(1, 3));
447
448 let prediction_task = ep_store.update(cx, |ep_store, cx| {
449 ep_store.request_prediction(&project, &buffer, position, Default::default(), cx)
450 });
451
452 let (request, respond_tx) = requests.predict.next().await.unwrap();
453
454 let prompt = prompt_from_request(&request);
455 assert!(
456 prompt.contains(indoc! {"
457 --- a/root/foo.md
458 +++ b/root/foo.md
459 @@ -1,3 +1,3 @@
460 Hello!
461 -
462 +How
463 Bye
464 "}),
465 "{prompt}"
466 );
467
468 respond_tx
469 .send(model_response(
470 &request,
471 indoc! {r#"
472 --- a/root/foo.md
473 +++ b/root/foo.md
474 @@ ... @@
475 Hello!
476 -How
477 +How are you?
478 Bye
479 "#},
480 ))
481 .unwrap();
482
483 let prediction = prediction_task.await.unwrap().unwrap().prediction.unwrap();
484
485 assert_eq!(prediction.edits.len(), 1);
486 assert_eq!(prediction.edits[0].1.as_ref(), " are you?");
487}
488
489#[gpui::test]
490async fn test_edit_history_getter_pause_splits_last_event(cx: &mut TestAppContext) {
491 let (ep_store, _requests) = init_test_with_fake_client(cx);
492 let fs = FakeFs::new(cx.executor());
493 fs.insert_tree(
494 "/root",
495 json!({
496 "foo.md": "Hello!\n\nBye\n"
497 }),
498 )
499 .await;
500 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
501
502 let buffer = project
503 .update(cx, |project, cx| {
504 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
505 project.open_buffer(path, cx)
506 })
507 .await
508 .unwrap();
509
510 ep_store.update(cx, |ep_store, cx| {
511 ep_store.register_buffer(&buffer, &project, cx);
512 });
513
514 // First burst: insert "How"
515 buffer.update(cx, |buffer, cx| {
516 buffer.edit(vec![(7..7, "How")], None, cx);
517 });
518
519 // Simulate a pause longer than the grouping threshold (e.g. 500ms).
520 cx.executor().advance_clock(LAST_CHANGE_GROUPING_TIME * 2);
521 cx.run_until_parked();
522
523 // Second burst: append " are you?" immediately after "How" on the same line.
524 //
525 // Keeping both bursts on the same line ensures the existing line-span coalescing logic
526 // groups them into a single `LastEvent`, allowing the pause-split getter to return two diffs.
527 buffer.update(cx, |buffer, cx| {
528 buffer.edit(vec![(10..10, " are you?")], None, cx);
529 });
530
531 // A second edit shortly after the first post-pause edit ensures the last edit timestamp is
532 // advanced after the pause boundary is recorded, making pause-splitting deterministic.
533 buffer.update(cx, |buffer, cx| {
534 buffer.edit(vec![(19..19, "!")], None, cx);
535 });
536
537 // With time-based splitting, there are two distinct events.
538 let events = ep_store.update(cx, |ep_store, cx| {
539 ep_store.edit_history_for_project(&project, cx)
540 });
541 assert_eq!(events.len(), 2);
542
543 let first_total_edit_range = buffer.read_with(cx, |buffer, _| {
544 events[0].total_edit_range.to_point(&buffer.snapshot())
545 });
546 assert_eq!(first_total_edit_range, Point::new(1, 0)..Point::new(1, 3));
547
548 let zeta_prompt::Event::BufferChange { diff, .. } = events[0].event.as_ref();
549 assert_eq!(
550 diff.as_str(),
551 indoc! {"
552 @@ -1,3 +1,3 @@
553 Hello!
554 -
555 +How
556 Bye
557 "}
558 );
559
560 let second_total_edit_range = buffer.read_with(cx, |buffer, _| {
561 events[1].total_edit_range.to_point(&buffer.snapshot())
562 });
563 assert_eq!(second_total_edit_range, Point::new(1, 3)..Point::new(1, 13));
564
565 let zeta_prompt::Event::BufferChange { diff, .. } = events[1].event.as_ref();
566 assert_eq!(
567 diff.as_str(),
568 indoc! {"
569 @@ -1,3 +1,3 @@
570 Hello!
571 -How
572 +How are you?!
573 Bye
574 "}
575 );
576}
577
578#[gpui::test]
579async fn test_predicted_edits_are_separated_in_edit_history(cx: &mut TestAppContext) {
580 let (ep_store, _requests) = init_test_with_fake_client(cx);
581 let fs = FakeFs::new(cx.executor());
582
583 // Create a file with 30 lines to test line-based coalescing
584 let content = (1..=30)
585 .map(|i| format!("Line {}\n", i))
586 .collect::<String>();
587 fs.insert_tree(
588 "/root",
589 json!({
590 "foo.md": content
591 }),
592 )
593 .await;
594 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
595
596 let buffer = project
597 .update(cx, |project, cx| {
598 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
599 project.open_buffer(path, cx)
600 })
601 .await
602 .unwrap();
603
604 ep_store.update(cx, |ep_store, cx| {
605 ep_store.register_buffer(&buffer, &project, cx);
606 });
607
608 // First edit: multi-line edit spanning rows 10-12 (replacing lines 11-13)
609 buffer.update(cx, |buffer, cx| {
610 let start = Point::new(10, 0).to_offset(buffer);
611 let end = Point::new(13, 0).to_offset(buffer);
612 buffer.edit(vec![(start..end, "Middle A\nMiddle B\n")], None, cx);
613 });
614
615 let events = ep_store.update(cx, |ep_store, cx| {
616 ep_store.edit_history_for_project(&project, cx)
617 });
618 assert_eq!(
619 render_events(&events),
620 indoc! {"
621 @@ -8,9 +8,8 @@
622 Line 8
623 Line 9
624 Line 10
625 -Line 11
626 -Line 12
627 -Line 13
628 +Middle A
629 +Middle B
630 Line 14
631 Line 15
632 Line 16
633 "},
634 "After first edit"
635 );
636
637 // Second edit: insert ABOVE the first edit's range (row 5, within 8 lines of row 10)
638 // This tests that coalescing considers the START of the existing range
639 buffer.update(cx, |buffer, cx| {
640 let offset = Point::new(5, 0).to_offset(buffer);
641 buffer.edit(vec![(offset..offset, "Above\n")], None, cx);
642 });
643
644 let events = ep_store.update(cx, |ep_store, cx| {
645 ep_store.edit_history_for_project(&project, cx)
646 });
647 assert_eq!(
648 render_events(&events),
649 indoc! {"
650 @@ -3,14 +3,14 @@
651 Line 3
652 Line 4
653 Line 5
654 +Above
655 Line 6
656 Line 7
657 Line 8
658 Line 9
659 Line 10
660 -Line 11
661 -Line 12
662 -Line 13
663 +Middle A
664 +Middle B
665 Line 14
666 Line 15
667 Line 16
668 "},
669 "After inserting above (should coalesce)"
670 );
671
672 // Third edit: insert BELOW the first edit's range (row 14 in current buffer, within 8 lines of row 12)
673 // This tests that coalescing considers the END of the existing range
674 buffer.update(cx, |buffer, cx| {
675 let offset = Point::new(14, 0).to_offset(buffer);
676 buffer.edit(vec![(offset..offset, "Below\n")], None, cx);
677 });
678
679 let events = ep_store.update(cx, |ep_store, cx| {
680 ep_store.edit_history_for_project(&project, cx)
681 });
682 assert_eq!(
683 render_events(&events),
684 indoc! {"
685 @@ -3,15 +3,16 @@
686 Line 3
687 Line 4
688 Line 5
689 +Above
690 Line 6
691 Line 7
692 Line 8
693 Line 9
694 Line 10
695 -Line 11
696 -Line 12
697 -Line 13
698 +Middle A
699 +Middle B
700 Line 14
701 +Below
702 Line 15
703 Line 16
704 Line 17
705 "},
706 "After inserting below (should coalesce)"
707 );
708
709 // Fourth edit: insert FAR BELOW (row 25, beyond 8 lines from the current range end ~row 15)
710 // This should NOT coalesce - creates a new event
711 buffer.update(cx, |buffer, cx| {
712 let offset = Point::new(25, 0).to_offset(buffer);
713 buffer.edit(vec![(offset..offset, "Far below\n")], None, cx);
714 });
715
716 let events = ep_store.update(cx, |ep_store, cx| {
717 ep_store.edit_history_for_project(&project, cx)
718 });
719 assert_eq!(
720 render_events(&events),
721 indoc! {"
722 @@ -3,15 +3,16 @@
723 Line 3
724 Line 4
725 Line 5
726 +Above
727 Line 6
728 Line 7
729 Line 8
730 Line 9
731 Line 10
732 -Line 11
733 -Line 12
734 -Line 13
735 +Middle A
736 +Middle B
737 Line 14
738 +Below
739 Line 15
740 Line 16
741 Line 17
742
743 ---
744 @@ -23,6 +23,7 @@
745 Line 22
746 Line 23
747 Line 24
748 +Far below
749 Line 25
750 Line 26
751 Line 27
752 "},
753 "After inserting far below (should NOT coalesce)"
754 );
755}
756
757fn render_events(events: &[StoredEvent]) -> String {
758 events
759 .iter()
760 .map(|e| {
761 let zeta_prompt::Event::BufferChange { diff, .. } = e.event.as_ref();
762 diff.as_str()
763 })
764 .collect::<Vec<_>>()
765 .join("\n---\n")
766}
767
768fn render_events_with_predicted(events: &[StoredEvent]) -> Vec<String> {
769 events
770 .iter()
771 .map(|e| {
772 let zeta_prompt::Event::BufferChange {
773 diff, predicted, ..
774 } = e.event.as_ref();
775 let prefix = if *predicted { "predicted" } else { "manual" };
776 format!("{}\n{}", prefix, diff)
777 })
778 .collect()
779}
780
781fn make_collaborator_replica(
782 buffer: &Entity<Buffer>,
783 cx: &mut TestAppContext,
784) -> (Entity<Buffer>, clock::Global) {
785 let (state, version) =
786 buffer.read_with(cx, |buffer, _cx| (buffer.to_proto(_cx), buffer.version()));
787 let collaborator = cx.new(|_cx| {
788 Buffer::from_proto(ReplicaId::new(1), Capability::ReadWrite, state, None).unwrap()
789 });
790 (collaborator, version)
791}
792
793async fn apply_collaborator_edit(
794 collaborator: &Entity<Buffer>,
795 buffer: &Entity<Buffer>,
796 since_version: &mut clock::Global,
797 edit_range: Range<usize>,
798 new_text: &str,
799 cx: &mut TestAppContext,
800) {
801 collaborator.update(cx, |collaborator, cx| {
802 collaborator.edit([(edit_range, new_text)], None, cx);
803 });
804
805 let serialize_task = collaborator.read_with(cx, |collaborator, cx| {
806 collaborator.serialize_ops(Some(since_version.clone()), cx)
807 });
808 let ops = serialize_task.await;
809 *since_version = collaborator.read_with(cx, |collaborator, _cx| collaborator.version());
810
811 buffer.update(cx, |buffer, cx| {
812 buffer.apply_ops(
813 ops.into_iter()
814 .map(|op| language::proto::deserialize_operation(op).unwrap()),
815 cx,
816 );
817 });
818}
819
820#[gpui::test]
821async fn test_nearby_collaborator_edits_are_kept_in_history(cx: &mut TestAppContext) {
822 let (ep_store, _requests) = init_test_with_fake_client(cx);
823 let fs = FakeFs::new(cx.executor());
824 fs.insert_tree(
825 "/root",
826 json!({
827 "foo.rs": "line 0\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\nline 10\nline 11\nline 12\nline 13\nline 14\n"
828 }),
829 )
830 .await;
831 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
832
833 let buffer = project
834 .update(cx, |project, cx| {
835 let path = project.find_project_path(path!("root/foo.rs"), cx).unwrap();
836 project.set_active_path(Some(path.clone()), cx);
837 project.open_buffer(path, cx)
838 })
839 .await
840 .unwrap();
841
842 let cursor = buffer.read_with(cx, |buffer, _cx| buffer.anchor_before(Point::new(1, 0)));
843
844 ep_store.update(cx, |ep_store, cx| {
845 ep_store.register_buffer(&buffer, &project, cx);
846 let _ = ep_store.prediction_at(&buffer, Some(cursor), &project, cx);
847 });
848
849 buffer.update(cx, |buffer, cx| {
850 buffer.edit(vec![(0..6, "LOCAL ZERO")], None, cx);
851 });
852
853 let (collaborator, mut collaborator_version) = make_collaborator_replica(&buffer, cx);
854
855 let (line_one_start, line_one_len) = collaborator.read_with(cx, |buffer, _cx| {
856 (Point::new(1, 0).to_offset(buffer), buffer.line_len(1))
857 });
858
859 apply_collaborator_edit(
860 &collaborator,
861 &buffer,
862 &mut collaborator_version,
863 line_one_start..line_one_start + line_one_len as usize,
864 "REMOTE ONE",
865 cx,
866 )
867 .await;
868
869 let events = ep_store.update(cx, |ep_store, cx| {
870 ep_store.edit_history_for_project(&project, cx)
871 });
872
873 assert_eq!(
874 render_events_with_predicted(&events),
875 vec![indoc! {"
876 manual
877 @@ -1,5 +1,5 @@
878 -line 0
879 -line 1
880 +LOCAL ZERO
881 +REMOTE ONE
882 line 2
883 line 3
884 line 4
885 "}]
886 );
887}
888
889#[gpui::test]
890async fn test_distant_collaborator_edits_are_omitted_from_history(cx: &mut TestAppContext) {
891 let (ep_store, _requests) = init_test_with_fake_client(cx);
892 let fs = FakeFs::new(cx.executor());
893 fs.insert_tree(
894 "/root",
895 json!({
896 "foo.rs": (0..1000)
897 .map(|i| format!("line {i}\n"))
898 .collect::<String>()
899 }),
900 )
901 .await;
902 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
903
904 let buffer = project
905 .update(cx, |project, cx| {
906 let path = project.find_project_path(path!("root/foo.rs"), cx).unwrap();
907 project.set_active_path(Some(path.clone()), cx);
908 project.open_buffer(path, cx)
909 })
910 .await
911 .unwrap();
912
913 let cursor = buffer.read_with(cx, |buffer, _cx| buffer.anchor_before(Point::new(1, 0)));
914
915 ep_store.update(cx, |ep_store, cx| {
916 ep_store.register_buffer(&buffer, &project, cx);
917 let _ = ep_store.prediction_at(&buffer, Some(cursor), &project, cx);
918 });
919
920 buffer.update(cx, |buffer, cx| {
921 buffer.edit(vec![(0..6, "LOCAL ZERO")], None, cx);
922 });
923
924 let (collaborator, mut collaborator_version) = make_collaborator_replica(&buffer, cx);
925
926 let far_line_start = buffer.read_with(cx, |buffer, _cx| Point::new(900, 0).to_offset(buffer));
927
928 apply_collaborator_edit(
929 &collaborator,
930 &buffer,
931 &mut collaborator_version,
932 far_line_start..far_line_start + 7,
933 "REMOTE FAR",
934 cx,
935 )
936 .await;
937
938 let events = ep_store.update(cx, |ep_store, cx| {
939 ep_store.edit_history_for_project(&project, cx)
940 });
941
942 assert_eq!(
943 render_events_with_predicted(&events),
944 vec![indoc! {"
945 manual
946 @@ -1,4 +1,4 @@
947 -line 0
948 +LOCAL ZERO
949 line 1
950 line 2
951 line 3
952 "}]
953 );
954}
955
956#[gpui::test]
957async fn test_irrelevant_collaborator_edits_in_different_files_are_omitted_from_history(
958 cx: &mut TestAppContext,
959) {
960 let (ep_store, _requests) = init_test_with_fake_client(cx);
961 let fs = FakeFs::new(cx.executor());
962 fs.insert_tree(
963 "/root",
964 json!({
965 "foo.rs": "line 0\nline 1\nline 2\nline 3\n",
966 "bar.rs": "line 0\nline 1\nline 2\nline 3\n"
967 }),
968 )
969 .await;
970 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
971
972 let foo_buffer = project
973 .update(cx, |project, cx| {
974 let path = project.find_project_path(path!("root/foo.rs"), cx).unwrap();
975 project.set_active_path(Some(path.clone()), cx);
976 project.open_buffer(path, cx)
977 })
978 .await
979 .unwrap();
980 let bar_buffer = project
981 .update(cx, |project, cx| {
982 let path = project.find_project_path(path!("root/bar.rs"), cx).unwrap();
983 project.open_buffer(path, cx)
984 })
985 .await
986 .unwrap();
987
988 let foo_cursor = foo_buffer.read_with(cx, |buffer, _cx| buffer.anchor_before(Point::new(1, 0)));
989
990 ep_store.update(cx, |ep_store, cx| {
991 ep_store.register_buffer(&foo_buffer, &project, cx);
992 ep_store.register_buffer(&bar_buffer, &project, cx);
993 let _ = ep_store.prediction_at(&foo_buffer, Some(foo_cursor), &project, cx);
994 });
995
996 let (bar_collaborator, mut bar_version) = make_collaborator_replica(&bar_buffer, cx);
997
998 apply_collaborator_edit(
999 &bar_collaborator,
1000 &bar_buffer,
1001 &mut bar_version,
1002 0..6,
1003 "REMOTE BAR",
1004 cx,
1005 )
1006 .await;
1007
1008 let events = ep_store.update(cx, |ep_store, cx| {
1009 ep_store.edit_history_for_project(&project, cx)
1010 });
1011
1012 assert!(events.is_empty());
1013}
1014
1015#[gpui::test]
1016async fn test_large_edits_are_omitted_from_history(cx: &mut TestAppContext) {
1017 let (ep_store, _requests) = init_test_with_fake_client(cx);
1018 let fs = FakeFs::new(cx.executor());
1019 fs.insert_tree(
1020 "/root",
1021 json!({
1022 "foo.rs": (0..20)
1023 .map(|i| format!("line {i}\n"))
1024 .collect::<String>()
1025 }),
1026 )
1027 .await;
1028 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
1029
1030 let buffer = project
1031 .update(cx, |project, cx| {
1032 let path = project.find_project_path(path!("root/foo.rs"), cx).unwrap();
1033 project.set_active_path(Some(path.clone()), cx);
1034 project.open_buffer(path, cx)
1035 })
1036 .await
1037 .unwrap();
1038
1039 let cursor = buffer.read_with(cx, |buffer, _cx| buffer.anchor_before(Point::new(1, 0)));
1040
1041 ep_store.update(cx, |ep_store, cx| {
1042 ep_store.register_buffer(&buffer, &project, cx);
1043 let _ = ep_store.prediction_at(&buffer, Some(cursor), &project, cx);
1044 });
1045
1046 buffer.update(cx, |buffer, cx| {
1047 buffer.edit(vec![(0..6, "LOCAL ZERO")], None, cx);
1048 });
1049
1050 let (collaborator, mut collaborator_version) = make_collaborator_replica(&buffer, cx);
1051
1052 let (line_three_start, line_three_len) = collaborator.read_with(cx, |buffer, _cx| {
1053 (Point::new(3, 0).to_offset(buffer), buffer.line_len(3))
1054 });
1055 let large_edit = "X".repeat(EDIT_HISTORY_DIFF_SIZE_LIMIT + 1);
1056
1057 apply_collaborator_edit(
1058 &collaborator,
1059 &buffer,
1060 &mut collaborator_version,
1061 line_three_start..line_three_start + line_three_len as usize,
1062 &large_edit,
1063 cx,
1064 )
1065 .await;
1066
1067 buffer.update(cx, |buffer, cx| {
1068 let line_seven_start = Point::new(7, 0).to_offset(buffer);
1069 let line_seven_end = Point::new(7, 6).to_offset(buffer);
1070 buffer.edit(
1071 vec![(line_seven_start..line_seven_end, "LOCAL SEVEN")],
1072 None,
1073 cx,
1074 );
1075 });
1076
1077 let events = ep_store.update(cx, |ep_store, cx| {
1078 ep_store.edit_history_for_project(&project, cx)
1079 });
1080
1081 let rendered_events = render_events_with_predicted(&events);
1082
1083 assert_eq!(rendered_events.len(), 2);
1084 assert!(rendered_events[0].contains("+LOCAL ZERO"));
1085 assert!(!rendered_events[0].contains(&large_edit));
1086 assert!(rendered_events[1].contains("+LOCAL SEVEN"));
1087 assert!(!rendered_events[1].contains(&large_edit));
1088}
1089
1090#[gpui::test]
1091async fn test_predicted_flag_coalescing(cx: &mut TestAppContext) {
1092 let (ep_store, _requests) = init_test_with_fake_client(cx);
1093 let fs = FakeFs::new(cx.executor());
1094 fs.insert_tree(
1095 "/root",
1096 json!({
1097 "foo.rs": "line 0\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\nline 10\nline 11\nline 12\nline 13\nline 14\n"
1098 }),
1099 )
1100 .await;
1101 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
1102
1103 let buffer = project
1104 .update(cx, |project, cx| {
1105 let path = project.find_project_path(path!("root/foo.rs"), cx).unwrap();
1106 project.open_buffer(path, cx)
1107 })
1108 .await
1109 .unwrap();
1110
1111 ep_store.update(cx, |ep_store, cx| {
1112 ep_store.register_buffer(&buffer, &project, cx);
1113 });
1114
1115 // Case 1: Manual edits have `predicted` set to false.
1116 buffer.update(cx, |buffer, cx| {
1117 buffer.edit(vec![(0..6, "LINE ZERO")], None, cx);
1118 });
1119
1120 let events = ep_store.update(cx, |ep_store, cx| {
1121 ep_store.edit_history_for_project(&project, cx)
1122 });
1123
1124 assert_eq!(
1125 render_events_with_predicted(&events),
1126 vec![indoc! {"
1127 manual
1128 @@ -1,4 +1,4 @@
1129 -line 0
1130 +LINE ZERO
1131 line 1
1132 line 2
1133 line 3
1134 "}]
1135 );
1136
1137 // Case 2: Multiple successive manual edits near each other are merged into one
1138 // event with `predicted` set to false.
1139 buffer.update(cx, |buffer, cx| {
1140 let offset = Point::new(1, 0).to_offset(buffer);
1141 let end = Point::new(1, 6).to_offset(buffer);
1142 buffer.edit(vec![(offset..end, "LINE ONE")], None, cx);
1143 });
1144
1145 let events = ep_store.update(cx, |ep_store, cx| {
1146 ep_store.edit_history_for_project(&project, cx)
1147 });
1148 assert_eq!(
1149 render_events_with_predicted(&events),
1150 vec![indoc! {"
1151 manual
1152 @@ -1,5 +1,5 @@
1153 -line 0
1154 -line 1
1155 +LINE ZERO
1156 +LINE ONE
1157 line 2
1158 line 3
1159 line 4
1160 "}]
1161 );
1162
1163 // Case 3: Accepted predictions have `predicted` set to true.
1164 // Case 5: A manual edit that follows a predicted edit is not merged with the
1165 // predicted edit, even if it is nearby.
1166 ep_store.update(cx, |ep_store, cx| {
1167 buffer.update(cx, |buffer, cx| {
1168 let offset = Point::new(2, 0).to_offset(buffer);
1169 let end = Point::new(2, 6).to_offset(buffer);
1170 buffer.edit(vec![(offset..end, "LINE TWO")], None, cx);
1171 });
1172 ep_store.report_changes_for_buffer(&buffer, &project, true, true, cx);
1173 });
1174
1175 let events = ep_store.update(cx, |ep_store, cx| {
1176 ep_store.edit_history_for_project(&project, cx)
1177 });
1178 assert_eq!(
1179 render_events_with_predicted(&events),
1180 vec![
1181 indoc! {"
1182 manual
1183 @@ -1,5 +1,5 @@
1184 -line 0
1185 -line 1
1186 +LINE ZERO
1187 +LINE ONE
1188 line 2
1189 line 3
1190 line 4
1191 "},
1192 indoc! {"
1193 predicted
1194 @@ -1,6 +1,6 @@
1195 LINE ZERO
1196 LINE ONE
1197 -line 2
1198 +LINE TWO
1199 line 3
1200 line 4
1201 line 5
1202 "}
1203 ]
1204 );
1205
1206 // Case 4: Multiple successive accepted predictions near each other are merged
1207 // into one event with `predicted` set to true.
1208 ep_store.update(cx, |ep_store, cx| {
1209 buffer.update(cx, |buffer, cx| {
1210 let offset = Point::new(3, 0).to_offset(buffer);
1211 let end = Point::new(3, 6).to_offset(buffer);
1212 buffer.edit(vec![(offset..end, "LINE THREE")], None, cx);
1213 });
1214 ep_store.report_changes_for_buffer(&buffer, &project, true, true, cx);
1215 });
1216
1217 let events = ep_store.update(cx, |ep_store, cx| {
1218 ep_store.edit_history_for_project(&project, cx)
1219 });
1220 assert_eq!(
1221 render_events_with_predicted(&events),
1222 vec![
1223 indoc! {"
1224 manual
1225 @@ -1,5 +1,5 @@
1226 -line 0
1227 -line 1
1228 +LINE ZERO
1229 +LINE ONE
1230 line 2
1231 line 3
1232 line 4
1233 "},
1234 indoc! {"
1235 predicted
1236 @@ -1,7 +1,7 @@
1237 LINE ZERO
1238 LINE ONE
1239 -line 2
1240 -line 3
1241 +LINE TWO
1242 +LINE THREE
1243 line 4
1244 line 5
1245 line 6
1246 "}
1247 ]
1248 );
1249
1250 // Case 5 (continued): A manual edit that follows a predicted edit is not merged
1251 // with the predicted edit, even if it is nearby.
1252 buffer.update(cx, |buffer, cx| {
1253 let offset = Point::new(4, 0).to_offset(buffer);
1254 let end = Point::new(4, 6).to_offset(buffer);
1255 buffer.edit(vec![(offset..end, "LINE FOUR")], None, cx);
1256 });
1257
1258 let events = ep_store.update(cx, |ep_store, cx| {
1259 ep_store.edit_history_for_project(&project, cx)
1260 });
1261 assert_eq!(
1262 render_events_with_predicted(&events),
1263 vec![
1264 indoc! {"
1265 manual
1266 @@ -1,5 +1,5 @@
1267 -line 0
1268 -line 1
1269 +LINE ZERO
1270 +LINE ONE
1271 line 2
1272 line 3
1273 line 4
1274 "},
1275 indoc! {"
1276 predicted
1277 @@ -1,7 +1,7 @@
1278 LINE ZERO
1279 LINE ONE
1280 -line 2
1281 -line 3
1282 +LINE TWO
1283 +LINE THREE
1284 line 4
1285 line 5
1286 line 6
1287 "},
1288 indoc! {"
1289 manual
1290 @@ -2,7 +2,7 @@
1291 LINE ONE
1292 LINE TWO
1293 LINE THREE
1294 -line 4
1295 +LINE FOUR
1296 line 5
1297 line 6
1298 line 7
1299 "}
1300 ]
1301 );
1302
1303 // Case 6: If we then perform a manual edit at a *different* location (more than
1304 // 8 lines away), then the edits at the prior location can be merged with each
1305 // other, even if some are predicted and some are not. `predicted` means all
1306 // constituent edits were predicted.
1307 buffer.update(cx, |buffer, cx| {
1308 let offset = Point::new(14, 0).to_offset(buffer);
1309 let end = Point::new(14, 7).to_offset(buffer);
1310 buffer.edit(vec![(offset..end, "LINE FOURTEEN")], None, cx);
1311 });
1312
1313 let events = ep_store.update(cx, |ep_store, cx| {
1314 ep_store.edit_history_for_project(&project, cx)
1315 });
1316 assert_eq!(
1317 render_events_with_predicted(&events),
1318 vec![
1319 indoc! {"
1320 manual
1321 @@ -1,8 +1,8 @@
1322 -line 0
1323 -line 1
1324 -line 2
1325 -line 3
1326 -line 4
1327 +LINE ZERO
1328 +LINE ONE
1329 +LINE TWO
1330 +LINE THREE
1331 +LINE FOUR
1332 line 5
1333 line 6
1334 line 7
1335 "},
1336 indoc! {"
1337 manual
1338 @@ -12,4 +12,4 @@
1339 line 11
1340 line 12
1341 line 13
1342 -line 14
1343 +LINE FOURTEEN
1344 "}
1345 ]
1346 );
1347}
1348
1349#[gpui::test]
1350async fn test_empty_prediction(cx: &mut TestAppContext) {
1351 let (ep_store, mut requests) = init_test_with_fake_client(cx);
1352 let fs = FakeFs::new(cx.executor());
1353 fs.insert_tree(
1354 "/root",
1355 json!({
1356 "foo.md": "Hello!\nHow\nBye\n"
1357 }),
1358 )
1359 .await;
1360 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
1361
1362 let buffer = project
1363 .update(cx, |project, cx| {
1364 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
1365 project.open_buffer(path, cx)
1366 })
1367 .await
1368 .unwrap();
1369 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
1370 let position = snapshot.anchor_before(language::Point::new(1, 3));
1371
1372 ep_store.update(cx, |ep_store, cx| {
1373 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1374 });
1375
1376 let (request, respond_tx) = requests.predict.next().await.unwrap();
1377 let response = model_response(&request, "");
1378 let id = response.request_id.clone();
1379 respond_tx.send(response).unwrap();
1380
1381 cx.run_until_parked();
1382
1383 ep_store.update(cx, |ep_store, cx| {
1384 assert!(
1385 ep_store
1386 .prediction_at(&buffer, None, &project, cx)
1387 .is_none()
1388 );
1389 });
1390
1391 // prediction is reported as rejected
1392 let (reject_request, _) = requests.reject.next().await.unwrap();
1393
1394 assert_eq!(
1395 &reject_request.rejections,
1396 &[EditPredictionRejection {
1397 request_id: id,
1398 reason: EditPredictionRejectReason::Empty,
1399 was_shown: false,
1400 model_version: None,
1401 e2e_latency_ms: Some(0),
1402 }]
1403 );
1404}
1405
1406#[gpui::test]
1407async fn test_interpolated_empty(cx: &mut TestAppContext) {
1408 let (ep_store, mut requests) = init_test_with_fake_client(cx);
1409 let fs = FakeFs::new(cx.executor());
1410 fs.insert_tree(
1411 "/root",
1412 json!({
1413 "foo.md": "Hello!\nHow\nBye\n"
1414 }),
1415 )
1416 .await;
1417 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
1418
1419 let buffer = project
1420 .update(cx, |project, cx| {
1421 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
1422 project.open_buffer(path, cx)
1423 })
1424 .await
1425 .unwrap();
1426 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
1427 let position = snapshot.anchor_before(language::Point::new(1, 3));
1428
1429 ep_store.update(cx, |ep_store, cx| {
1430 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1431 });
1432
1433 let (request, respond_tx) = requests.predict.next().await.unwrap();
1434
1435 buffer.update(cx, |buffer, cx| {
1436 buffer.set_text("Hello!\nHow are you?\nBye", cx);
1437 });
1438
1439 let response = model_response(&request, SIMPLE_DIFF);
1440 let id = response.request_id.clone();
1441 respond_tx.send(response).unwrap();
1442
1443 cx.run_until_parked();
1444
1445 ep_store.update(cx, |ep_store, cx| {
1446 assert!(
1447 ep_store
1448 .prediction_at(&buffer, None, &project, cx)
1449 .is_none()
1450 );
1451 });
1452
1453 // prediction is reported as rejected
1454 let (reject_request, _) = requests.reject.next().await.unwrap();
1455
1456 assert_eq!(
1457 &reject_request.rejections,
1458 &[EditPredictionRejection {
1459 request_id: id,
1460 reason: EditPredictionRejectReason::InterpolatedEmpty,
1461 was_shown: false,
1462 model_version: None,
1463 e2e_latency_ms: Some(0),
1464 }]
1465 );
1466}
1467
1468const SIMPLE_DIFF: &str = indoc! { r"
1469 --- a/root/foo.md
1470 +++ b/root/foo.md
1471 @@ ... @@
1472 Hello!
1473 -How
1474 +How are you?
1475 Bye
1476"};
1477
1478#[gpui::test]
1479async fn test_replace_current(cx: &mut TestAppContext) {
1480 let (ep_store, mut requests) = init_test_with_fake_client(cx);
1481 let fs = FakeFs::new(cx.executor());
1482 fs.insert_tree(
1483 "/root",
1484 json!({
1485 "foo.md": "Hello!\nHow\nBye\n"
1486 }),
1487 )
1488 .await;
1489 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
1490
1491 let buffer = project
1492 .update(cx, |project, cx| {
1493 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
1494 project.open_buffer(path, cx)
1495 })
1496 .await
1497 .unwrap();
1498 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
1499 let position = snapshot.anchor_before(language::Point::new(1, 3));
1500
1501 ep_store.update(cx, |ep_store, cx| {
1502 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1503 });
1504
1505 let (request, respond_tx) = requests.predict.next().await.unwrap();
1506 let first_response = model_response(&request, SIMPLE_DIFF);
1507 let first_id = first_response.request_id.clone();
1508 respond_tx.send(first_response).unwrap();
1509
1510 cx.run_until_parked();
1511
1512 ep_store.update(cx, |ep_store, cx| {
1513 assert_eq!(
1514 ep_store
1515 .prediction_at(&buffer, None, &project, cx)
1516 .unwrap()
1517 .id
1518 .0,
1519 first_id
1520 );
1521 });
1522
1523 // a second request is triggered
1524 ep_store.update(cx, |ep_store, cx| {
1525 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1526 });
1527
1528 let (request, respond_tx) = requests.predict.next().await.unwrap();
1529 let second_response = model_response(&request, SIMPLE_DIFF);
1530 let second_id = second_response.request_id.clone();
1531 respond_tx.send(second_response).unwrap();
1532
1533 cx.run_until_parked();
1534
1535 ep_store.update(cx, |ep_store, cx| {
1536 // second replaces first
1537 assert_eq!(
1538 ep_store
1539 .prediction_at(&buffer, None, &project, cx)
1540 .unwrap()
1541 .id
1542 .0,
1543 second_id
1544 );
1545 });
1546
1547 // first is reported as replaced
1548 let (reject_request, _) = requests.reject.next().await.unwrap();
1549
1550 assert_eq!(
1551 &reject_request.rejections,
1552 &[EditPredictionRejection {
1553 request_id: first_id,
1554 reason: EditPredictionRejectReason::Replaced,
1555 was_shown: false,
1556 model_version: None,
1557 e2e_latency_ms: Some(0),
1558 }]
1559 );
1560}
1561
1562#[gpui::test]
1563async fn test_current_preferred(cx: &mut TestAppContext) {
1564 let (ep_store, mut requests) = init_test_with_fake_client(cx);
1565 let fs = FakeFs::new(cx.executor());
1566 fs.insert_tree(
1567 "/root",
1568 json!({
1569 "foo.md": "Hello!\nHow\nBye\n"
1570 }),
1571 )
1572 .await;
1573 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
1574
1575 let buffer = project
1576 .update(cx, |project, cx| {
1577 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
1578 project.open_buffer(path, cx)
1579 })
1580 .await
1581 .unwrap();
1582 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
1583 let position = snapshot.anchor_before(language::Point::new(1, 3));
1584
1585 ep_store.update(cx, |ep_store, cx| {
1586 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1587 });
1588
1589 let (request, respond_tx) = requests.predict.next().await.unwrap();
1590 let first_response = model_response(&request, SIMPLE_DIFF);
1591 let first_id = first_response.request_id.clone();
1592 respond_tx.send(first_response).unwrap();
1593
1594 cx.run_until_parked();
1595
1596 ep_store.update(cx, |ep_store, cx| {
1597 assert_eq!(
1598 ep_store
1599 .prediction_at(&buffer, None, &project, cx)
1600 .unwrap()
1601 .id
1602 .0,
1603 first_id
1604 );
1605 });
1606
1607 // a second request is triggered
1608 ep_store.update(cx, |ep_store, cx| {
1609 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1610 });
1611
1612 let (request, respond_tx) = requests.predict.next().await.unwrap();
1613 // worse than current prediction
1614 let second_response = model_response(
1615 &request,
1616 indoc! { r"
1617 --- a/root/foo.md
1618 +++ b/root/foo.md
1619 @@ ... @@
1620 Hello!
1621 -How
1622 +How are
1623 Bye
1624 "},
1625 );
1626 let second_id = second_response.request_id.clone();
1627 respond_tx.send(second_response).unwrap();
1628
1629 cx.run_until_parked();
1630
1631 ep_store.update(cx, |ep_store, cx| {
1632 // first is preferred over second
1633 assert_eq!(
1634 ep_store
1635 .prediction_at(&buffer, None, &project, cx)
1636 .unwrap()
1637 .id
1638 .0,
1639 first_id
1640 );
1641 });
1642
1643 // second is reported as rejected
1644 let (reject_request, _) = requests.reject.next().await.unwrap();
1645
1646 assert_eq!(
1647 &reject_request.rejections,
1648 &[EditPredictionRejection {
1649 request_id: second_id,
1650 reason: EditPredictionRejectReason::CurrentPreferred,
1651 was_shown: false,
1652 model_version: None,
1653 e2e_latency_ms: Some(0),
1654 }]
1655 );
1656}
1657
1658#[gpui::test]
1659async fn test_cancel_earlier_pending_requests(cx: &mut TestAppContext) {
1660 let (ep_store, mut requests) = init_test_with_fake_client(cx);
1661 let fs = FakeFs::new(cx.executor());
1662 fs.insert_tree(
1663 "/root",
1664 json!({
1665 "foo.md": "Hello!\nHow\nBye\n"
1666 }),
1667 )
1668 .await;
1669 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
1670
1671 let buffer = project
1672 .update(cx, |project, cx| {
1673 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
1674 project.open_buffer(path, cx)
1675 })
1676 .await
1677 .unwrap();
1678 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
1679 let position = snapshot.anchor_before(language::Point::new(1, 3));
1680
1681 // start two refresh tasks
1682 ep_store.update(cx, |ep_store, cx| {
1683 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1684 });
1685
1686 let (request1, respond_first) = requests.predict.next().await.unwrap();
1687
1688 ep_store.update(cx, |ep_store, cx| {
1689 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1690 });
1691
1692 let (request, respond_second) = requests.predict.next().await.unwrap();
1693
1694 // wait for throttle
1695 cx.run_until_parked();
1696
1697 // second responds first
1698 let second_response = model_response(&request, SIMPLE_DIFF);
1699 let second_id = second_response.request_id.clone();
1700 respond_second.send(second_response).unwrap();
1701
1702 cx.run_until_parked();
1703
1704 ep_store.update(cx, |ep_store, cx| {
1705 // current prediction is second
1706 assert_eq!(
1707 ep_store
1708 .prediction_at(&buffer, None, &project, cx)
1709 .unwrap()
1710 .id
1711 .0,
1712 second_id
1713 );
1714 });
1715
1716 let first_response = model_response(&request1, SIMPLE_DIFF);
1717 let first_id = first_response.request_id.clone();
1718 respond_first.send(first_response).unwrap();
1719
1720 cx.run_until_parked();
1721
1722 ep_store.update(cx, |ep_store, cx| {
1723 // current prediction is still second, since first was cancelled
1724 assert_eq!(
1725 ep_store
1726 .prediction_at(&buffer, None, &project, cx)
1727 .unwrap()
1728 .id
1729 .0,
1730 second_id
1731 );
1732 });
1733
1734 // first is reported as rejected
1735 let (reject_request, _) = requests.reject.next().await.unwrap();
1736
1737 cx.run_until_parked();
1738
1739 assert_eq!(
1740 &reject_request.rejections,
1741 &[EditPredictionRejection {
1742 request_id: first_id,
1743 reason: EditPredictionRejectReason::Canceled,
1744 was_shown: false,
1745 model_version: None,
1746 e2e_latency_ms: None,
1747 }]
1748 );
1749}
1750
1751#[gpui::test]
1752async fn test_cancel_second_on_third_request(cx: &mut TestAppContext) {
1753 let (ep_store, mut requests) = init_test_with_fake_client(cx);
1754 let fs = FakeFs::new(cx.executor());
1755 fs.insert_tree(
1756 "/root",
1757 json!({
1758 "foo.md": "Hello!\nHow\nBye\n"
1759 }),
1760 )
1761 .await;
1762 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
1763
1764 let buffer = project
1765 .update(cx, |project, cx| {
1766 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
1767 project.open_buffer(path, cx)
1768 })
1769 .await
1770 .unwrap();
1771 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
1772 let position = snapshot.anchor_before(language::Point::new(1, 3));
1773
1774 // start two refresh tasks
1775 ep_store.update(cx, |ep_store, cx| {
1776 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1777 });
1778
1779 let (request1, respond_first) = requests.predict.next().await.unwrap();
1780
1781 ep_store.update(cx, |ep_store, cx| {
1782 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1783 });
1784
1785 let (request2, respond_second) = requests.predict.next().await.unwrap();
1786
1787 // wait for throttle, so requests are sent
1788 cx.run_until_parked();
1789
1790 ep_store.update(cx, |ep_store, cx| {
1791 // start a third request
1792 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1793
1794 // 2 are pending, so 2nd is cancelled
1795 assert_eq!(
1796 ep_store
1797 .get_or_init_project(&project, cx)
1798 .cancelled_predictions
1799 .iter()
1800 .copied()
1801 .collect::<Vec<_>>(),
1802 [1]
1803 );
1804 });
1805
1806 // wait for throttle
1807 cx.run_until_parked();
1808
1809 let (request3, respond_third) = requests.predict.next().await.unwrap();
1810
1811 let first_response = model_response(&request1, SIMPLE_DIFF);
1812 let first_id = first_response.request_id.clone();
1813 respond_first.send(first_response).unwrap();
1814
1815 cx.run_until_parked();
1816
1817 ep_store.update(cx, |ep_store, cx| {
1818 // current prediction is first
1819 assert_eq!(
1820 ep_store
1821 .prediction_at(&buffer, None, &project, cx)
1822 .unwrap()
1823 .id
1824 .0,
1825 first_id
1826 );
1827 });
1828
1829 let cancelled_response = model_response(&request2, SIMPLE_DIFF);
1830 let cancelled_id = cancelled_response.request_id.clone();
1831 respond_second.send(cancelled_response).unwrap();
1832
1833 cx.run_until_parked();
1834
1835 ep_store.update(cx, |ep_store, cx| {
1836 // current prediction is still first, since second was cancelled
1837 assert_eq!(
1838 ep_store
1839 .prediction_at(&buffer, None, &project, cx)
1840 .unwrap()
1841 .id
1842 .0,
1843 first_id
1844 );
1845 });
1846
1847 let third_response = model_response(&request3, SIMPLE_DIFF);
1848 let third_response_id = third_response.request_id.clone();
1849 respond_third.send(third_response).unwrap();
1850
1851 cx.run_until_parked();
1852
1853 ep_store.update(cx, |ep_store, cx| {
1854 // third completes and replaces first
1855 assert_eq!(
1856 ep_store
1857 .prediction_at(&buffer, None, &project, cx)
1858 .unwrap()
1859 .id
1860 .0,
1861 third_response_id
1862 );
1863 });
1864
1865 // second is reported as rejected
1866 let (reject_request, _) = requests.reject.next().await.unwrap();
1867
1868 cx.run_until_parked();
1869
1870 assert_eq!(
1871 &reject_request.rejections,
1872 &[
1873 EditPredictionRejection {
1874 request_id: cancelled_id,
1875 reason: EditPredictionRejectReason::Canceled,
1876 was_shown: false,
1877 model_version: None,
1878 e2e_latency_ms: None,
1879 },
1880 EditPredictionRejection {
1881 request_id: first_id,
1882 reason: EditPredictionRejectReason::Replaced,
1883 was_shown: false,
1884 model_version: None,
1885 // 2 throttle waits (for 2nd and 3rd requests) elapsed
1886 // between this request's start and response.
1887 e2e_latency_ms: Some(2 * EditPredictionStore::THROTTLE_TIMEOUT.as_millis()),
1888 }
1889 ]
1890 );
1891}
1892
1893#[gpui::test]
1894async fn test_jump_and_edit_throttles_are_independent(cx: &mut TestAppContext) {
1895 let (ep_store, mut requests) = init_test_with_fake_client(cx);
1896
1897 let fs = FakeFs::new(cx.executor());
1898 fs.insert_tree(
1899 "/root",
1900 json!({
1901 "foo.md": "Hello!\nHow\nBye\n",
1902 "bar.md": "Hola!\nComo\nAdios\n"
1903 }),
1904 )
1905 .await;
1906 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
1907
1908 let buffer = project
1909 .update(cx, |project, cx| {
1910 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
1911 project.set_active_path(Some(path.clone()), cx);
1912 project.open_buffer(path, cx)
1913 })
1914 .await
1915 .unwrap();
1916 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
1917 let position = snapshot.anchor_before(language::Point::new(1, 3));
1918
1919 ep_store.update(cx, |ep_store, cx| {
1920 ep_store.register_project(&project, cx);
1921 ep_store.register_buffer(&buffer, &project, cx);
1922 });
1923
1924 // First edit request - no prior edit, so not throttled.
1925 ep_store.update(cx, |ep_store, cx| {
1926 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1927 });
1928 let (_edit_request, edit_response_tx) = requests.predict.next().await.unwrap();
1929 edit_response_tx.send(empty_response()).unwrap();
1930 cx.run_until_parked();
1931
1932 let diagnostic = lsp::Diagnostic {
1933 range: lsp::Range::new(lsp::Position::new(1, 1), lsp::Position::new(1, 5)),
1934 severity: Some(lsp::DiagnosticSeverity::ERROR),
1935 message: "Sentence is incomplete".to_string(),
1936 ..Default::default()
1937 };
1938
1939 // First jump request triggered by diagnostic event on buffer - no prior jump, so not throttled (independent from edit).
1940 project.update(cx, |project, cx| {
1941 project.lsp_store().update(cx, |lsp_store, cx| {
1942 lsp_store
1943 .update_diagnostics(
1944 LanguageServerId(0),
1945 lsp::PublishDiagnosticsParams {
1946 uri: lsp::Uri::from_file_path(path!("/root/bar.md")).unwrap(),
1947 diagnostics: vec![diagnostic],
1948 version: None,
1949 },
1950 None,
1951 language::DiagnosticSourceKind::Pushed,
1952 &[],
1953 cx,
1954 )
1955 .unwrap();
1956 });
1957 });
1958 let (_jump_request, jump_response_tx) = requests.predict.next().await.unwrap();
1959 jump_response_tx.send(empty_response()).unwrap();
1960 cx.run_until_parked();
1961
1962 // Second edit request - should be throttled by the first edit.
1963 ep_store.update(cx, |ep_store, cx| {
1964 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
1965 });
1966 assert_no_predict_request_ready(&mut requests.predict);
1967
1968 // Second jump request - should be throttled by the first jump.
1969 ep_store.update(cx, |ep_store, cx| {
1970 ep_store.refresh_prediction_from_diagnostics(
1971 project.clone(),
1972 DiagnosticSearchScope::Global,
1973 cx,
1974 );
1975 });
1976 assert_no_predict_request_ready(&mut requests.predict);
1977
1978 // Wait for both throttles to expire.
1979 cx.background_executor
1980 .advance_clock(EditPredictionStore::THROTTLE_TIMEOUT);
1981 cx.background_executor.run_until_parked();
1982 cx.run_until_parked();
1983
1984 // Both requests should now go through.
1985 let (_request_1, response_tx_1) = requests.predict.next().await.unwrap();
1986 response_tx_1.send(empty_response()).unwrap();
1987 cx.run_until_parked();
1988
1989 let (_request_2, response_tx_2) = requests.predict.next().await.unwrap();
1990 response_tx_2.send(empty_response()).unwrap();
1991 cx.run_until_parked();
1992}
1993
1994#[gpui::test]
1995async fn test_same_frame_duplicate_requests_deduplicated(cx: &mut TestAppContext) {
1996 let (ep_store, mut requests) = init_test_with_fake_client(cx);
1997 let fs = FakeFs::new(cx.executor());
1998 fs.insert_tree(
1999 "/root",
2000 json!({
2001 "foo.md": "Hello!\nHow\nBye\n"
2002 }),
2003 )
2004 .await;
2005 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
2006
2007 let buffer = project
2008 .update(cx, |project, cx| {
2009 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
2010 project.open_buffer(path, cx)
2011 })
2012 .await
2013 .unwrap();
2014 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
2015 let position = snapshot.anchor_before(language::Point::new(1, 3));
2016
2017 // Enqueue two refresh calls in the same synchronous frame (no yielding).
2018 // Both `cx.spawn` tasks are created before either executes, so they both
2019 // capture the same `proceed_count_at_enqueue`. Only the first task should
2020 // pass the deduplication gate; the second should be skipped.
2021 ep_store.update(cx, |ep_store, cx| {
2022 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
2023 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
2024 });
2025
2026 // Let both spawned tasks run to completion (including any throttle waits).
2027 cx.run_until_parked();
2028
2029 // Exactly one prediction request should have been sent.
2030 let (request, respond_tx) = requests.predict.next().await.unwrap();
2031 respond_tx
2032 .send(model_response(&request, SIMPLE_DIFF))
2033 .unwrap();
2034 cx.run_until_parked();
2035
2036 // No second request should be pending.
2037 assert_no_predict_request_ready(&mut requests.predict);
2038}
2039
2040#[gpui::test]
2041async fn test_rejections_flushing(cx: &mut TestAppContext) {
2042 let (ep_store, mut requests) = init_test_with_fake_client(cx);
2043
2044 ep_store.update(cx, |ep_store, cx| {
2045 ep_store.reject_prediction(
2046 EditPredictionId("test-1".into()),
2047 EditPredictionRejectReason::Discarded,
2048 false,
2049 None,
2050 None,
2051 cx,
2052 );
2053 ep_store.reject_prediction(
2054 EditPredictionId("test-2".into()),
2055 EditPredictionRejectReason::Canceled,
2056 true,
2057 None,
2058 None,
2059 cx,
2060 );
2061 });
2062
2063 cx.executor().advance_clock(REJECT_REQUEST_DEBOUNCE);
2064 cx.run_until_parked();
2065
2066 let (reject_request, respond_tx) = requests.reject.next().await.unwrap();
2067 respond_tx.send(()).unwrap();
2068
2069 // batched
2070 assert_eq!(reject_request.rejections.len(), 2);
2071 assert_eq!(
2072 reject_request.rejections[0],
2073 EditPredictionRejection {
2074 request_id: "test-1".to_string(),
2075 reason: EditPredictionRejectReason::Discarded,
2076 was_shown: false,
2077 model_version: None,
2078 e2e_latency_ms: None
2079 }
2080 );
2081 assert_eq!(
2082 reject_request.rejections[1],
2083 EditPredictionRejection {
2084 request_id: "test-2".to_string(),
2085 reason: EditPredictionRejectReason::Canceled,
2086 was_shown: true,
2087 model_version: None,
2088 e2e_latency_ms: None
2089 }
2090 );
2091
2092 // Reaching batch size limit sends without debounce
2093 ep_store.update(cx, |ep_store, cx| {
2094 for i in 0..70 {
2095 ep_store.reject_prediction(
2096 EditPredictionId(format!("batch-{}", i).into()),
2097 EditPredictionRejectReason::Discarded,
2098 false,
2099 None,
2100 None,
2101 cx,
2102 );
2103 }
2104 });
2105
2106 // First MAX/2 items are sent immediately
2107 cx.run_until_parked();
2108 let (reject_request, respond_tx) = requests.reject.next().await.unwrap();
2109 respond_tx.send(()).unwrap();
2110
2111 assert_eq!(reject_request.rejections.len(), 50);
2112 assert_eq!(reject_request.rejections[0].request_id, "batch-0");
2113 assert_eq!(reject_request.rejections[49].request_id, "batch-49");
2114
2115 // Remaining items are debounced with the next batch
2116 cx.executor().advance_clock(Duration::from_secs(15));
2117 cx.run_until_parked();
2118
2119 let (reject_request, respond_tx) = requests.reject.next().await.unwrap();
2120 respond_tx.send(()).unwrap();
2121
2122 assert_eq!(reject_request.rejections.len(), 20);
2123 assert_eq!(reject_request.rejections[0].request_id, "batch-50");
2124 assert_eq!(reject_request.rejections[19].request_id, "batch-69");
2125
2126 // Request failure
2127 ep_store.update(cx, |ep_store, cx| {
2128 ep_store.reject_prediction(
2129 EditPredictionId("retry-1".into()),
2130 EditPredictionRejectReason::Discarded,
2131 false,
2132 None,
2133 None,
2134 cx,
2135 );
2136 });
2137
2138 cx.executor().advance_clock(REJECT_REQUEST_DEBOUNCE);
2139 cx.run_until_parked();
2140
2141 let (reject_request, _respond_tx) = requests.reject.next().await.unwrap();
2142 assert_eq!(reject_request.rejections.len(), 1);
2143 assert_eq!(reject_request.rejections[0].request_id, "retry-1");
2144 // Simulate failure
2145 drop(_respond_tx);
2146
2147 // Add another rejection
2148 ep_store.update(cx, |ep_store, cx| {
2149 ep_store.reject_prediction(
2150 EditPredictionId("retry-2".into()),
2151 EditPredictionRejectReason::Discarded,
2152 false,
2153 None,
2154 None,
2155 cx,
2156 );
2157 });
2158
2159 cx.executor().advance_clock(REJECT_REQUEST_DEBOUNCE);
2160 cx.run_until_parked();
2161
2162 // Retry should include both the failed item and the new one
2163 let (reject_request, respond_tx) = requests.reject.next().await.unwrap();
2164 respond_tx.send(()).unwrap();
2165
2166 assert_eq!(reject_request.rejections.len(), 2);
2167 assert_eq!(reject_request.rejections[0].request_id, "retry-1");
2168 assert_eq!(reject_request.rejections[1].request_id, "retry-2");
2169}
2170
2171#[gpui::test]
2172fn test_active_buffer_diagnostics_fetching(cx: &mut TestAppContext) {
2173 let diagnostic_marker: TextRangeMarker = ('«', '»').into();
2174 let search_range_marker: TextRangeMarker = ('[', ']').into();
2175
2176 let (text, mut ranges) = marked_text_ranges_by(
2177 indoc! {r#"
2178 fn alpha() {
2179 let «first_value» = 1;
2180 }
2181
2182 [fn beta() {
2183 let «second_value» = 2;
2184 let third_value = second_value + missing_symbol;
2185 }ˇ]
2186
2187 fn gamma() {
2188 let «fourth_value» = missing_other_symbol;
2189 }
2190 "#},
2191 vec![diagnostic_marker.clone(), search_range_marker.clone()],
2192 );
2193
2194 let diagnostic_ranges = ranges.remove(&diagnostic_marker).unwrap_or_default();
2195 let search_ranges = ranges.remove(&search_range_marker).unwrap_or_default();
2196
2197 let buffer = cx.new(|cx| Buffer::local(&text, cx));
2198
2199 buffer.update(cx, |buffer, cx| {
2200 let snapshot = buffer.snapshot();
2201 let diagnostics = DiagnosticSet::new(
2202 diagnostic_ranges
2203 .iter()
2204 .enumerate()
2205 .map(|(index, range)| DiagnosticEntry {
2206 range: snapshot.offset_to_point_utf16(range.start)
2207 ..snapshot.offset_to_point_utf16(range.end),
2208 diagnostic: Diagnostic {
2209 severity: match index {
2210 0 => DiagnosticSeverity::WARNING,
2211 1 => DiagnosticSeverity::ERROR,
2212 _ => DiagnosticSeverity::HINT,
2213 },
2214 message: match index {
2215 0 => "first warning".to_string(),
2216 1 => "second error".to_string(),
2217 _ => "third hint".to_string(),
2218 },
2219 group_id: index + 1,
2220 is_primary: true,
2221 source_kind: language::DiagnosticSourceKind::Pushed,
2222 ..Diagnostic::default()
2223 },
2224 }),
2225 &snapshot,
2226 );
2227 buffer.update_diagnostics(LanguageServerId(0), diagnostics, cx);
2228 });
2229
2230 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
2231 let search_range = snapshot.offset_to_point(search_ranges[0].start)
2232 ..snapshot.offset_to_point(search_ranges[0].end);
2233
2234 let active_buffer_diagnostics = zeta::active_buffer_diagnostics(&snapshot, search_range, 100);
2235
2236 assert_eq!(
2237 active_buffer_diagnostics,
2238 vec![zeta_prompt::ActiveBufferDiagnostic {
2239 severity: Some(1),
2240 message: "second error".to_string(),
2241 snippet: text,
2242 snippet_buffer_row_range: 5..5,
2243 diagnostic_range_in_snippet: 61..73,
2244 }]
2245 );
2246
2247 let buffer = cx.new(|cx| {
2248 Buffer::local(
2249 indoc! {"
2250 one
2251 two
2252 three
2253 four
2254 five
2255 "},
2256 cx,
2257 )
2258 });
2259
2260 buffer.update(cx, |buffer, cx| {
2261 let snapshot = buffer.snapshot();
2262 let diagnostics = DiagnosticSet::new(
2263 vec![
2264 DiagnosticEntry {
2265 range: text::PointUtf16::new(0, 0)..text::PointUtf16::new(0, 3),
2266 diagnostic: Diagnostic {
2267 severity: DiagnosticSeverity::ERROR,
2268 message: "row zero".to_string(),
2269 group_id: 1,
2270 is_primary: true,
2271 source_kind: language::DiagnosticSourceKind::Pushed,
2272 ..Diagnostic::default()
2273 },
2274 },
2275 DiagnosticEntry {
2276 range: text::PointUtf16::new(2, 0)..text::PointUtf16::new(2, 5),
2277 diagnostic: Diagnostic {
2278 severity: DiagnosticSeverity::WARNING,
2279 message: "row two".to_string(),
2280 group_id: 2,
2281 is_primary: true,
2282 source_kind: language::DiagnosticSourceKind::Pushed,
2283 ..Diagnostic::default()
2284 },
2285 },
2286 DiagnosticEntry {
2287 range: text::PointUtf16::new(4, 0)..text::PointUtf16::new(4, 4),
2288 diagnostic: Diagnostic {
2289 severity: DiagnosticSeverity::INFORMATION,
2290 message: "row four".to_string(),
2291 group_id: 3,
2292 is_primary: true,
2293 source_kind: language::DiagnosticSourceKind::Pushed,
2294 ..Diagnostic::default()
2295 },
2296 },
2297 ],
2298 &snapshot,
2299 );
2300 buffer.update_diagnostics(LanguageServerId(0), diagnostics, cx);
2301 });
2302
2303 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
2304
2305 let active_buffer_diagnostics =
2306 zeta::active_buffer_diagnostics(&snapshot, Point::new(2, 0)..Point::new(4, 0), 100);
2307
2308 assert_eq!(
2309 active_buffer_diagnostics
2310 .iter()
2311 .map(|diagnostic| (
2312 diagnostic.severity,
2313 diagnostic.message.clone(),
2314 diagnostic.snippet.clone(),
2315 diagnostic.snippet_buffer_row_range.clone(),
2316 diagnostic.diagnostic_range_in_snippet.clone(),
2317 ))
2318 .collect::<Vec<_>>(),
2319 vec![
2320 (
2321 Some(2),
2322 "row two".to_string(),
2323 "one\ntwo\nthree\nfour\nfive\n".to_string(),
2324 2..2,
2325 8..13,
2326 ),
2327 (
2328 Some(3),
2329 "row four".to_string(),
2330 "one\ntwo\nthree\nfour\nfive\n".to_string(),
2331 4..4,
2332 19..23,
2333 ),
2334 ]
2335 );
2336}
2337
2338// Generate a model response that would apply the given diff to the active file.
2339fn model_response(request: &PredictEditsV3Request, diff_to_apply: &str) -> PredictEditsV3Response {
2340 let editable_range =
2341 zeta_prompt::excerpt_range_for_format(Default::default(), &request.input.excerpt_ranges).1;
2342 let excerpt = request.input.cursor_excerpt[editable_range.clone()].to_string();
2343 let new_excerpt = apply_diff_to_string(diff_to_apply, &excerpt).unwrap();
2344
2345 PredictEditsV3Response {
2346 request_id: Uuid::new_v4().to_string(),
2347 editable_range,
2348 output: new_excerpt,
2349 model_version: None,
2350 }
2351}
2352
2353fn empty_response() -> PredictEditsV3Response {
2354 PredictEditsV3Response {
2355 request_id: Uuid::new_v4().to_string(),
2356 editable_range: 0..0,
2357 output: String::new(),
2358 model_version: None,
2359 }
2360}
2361
2362fn prompt_from_request(request: &PredictEditsV3Request) -> String {
2363 zeta_prompt::format_zeta_prompt(&request.input, zeta_prompt::ZetaFormat::default())
2364 .expect("default zeta prompt formatting should succeed in edit prediction tests")
2365}
2366
2367fn assert_no_predict_request_ready(
2368 requests: &mut mpsc::UnboundedReceiver<(
2369 PredictEditsV3Request,
2370 oneshot::Sender<PredictEditsV3Response>,
2371 )>,
2372) {
2373 if requests.next().now_or_never().flatten().is_some() {
2374 panic!("Unexpected prediction request while throttled.");
2375 }
2376}
2377
2378struct RequestChannels {
2379 predict: mpsc::UnboundedReceiver<(
2380 PredictEditsV3Request,
2381 oneshot::Sender<PredictEditsV3Response>,
2382 )>,
2383 reject: mpsc::UnboundedReceiver<(RejectEditPredictionsBody, oneshot::Sender<()>)>,
2384}
2385
2386fn init_test_with_fake_client(
2387 cx: &mut TestAppContext,
2388) -> (Entity<EditPredictionStore>, RequestChannels) {
2389 cx.update(move |cx| {
2390 let settings_store = SettingsStore::test(cx);
2391 cx.set_global(settings_store);
2392 zlog::init_test();
2393
2394 let (predict_req_tx, predict_req_rx) = mpsc::unbounded();
2395 let (reject_req_tx, reject_req_rx) = mpsc::unbounded();
2396
2397 let http_client = FakeHttpClient::create({
2398 move |req| {
2399 let uri = req.uri().path().to_string();
2400 let mut body = req.into_body();
2401 let predict_req_tx = predict_req_tx.clone();
2402 let reject_req_tx = reject_req_tx.clone();
2403 async move {
2404 let resp = match uri.as_str() {
2405 "/client/llm_tokens" => serde_json::to_string(&json!({
2406 "token": "test"
2407 }))
2408 .unwrap(),
2409 "/predict_edits/v3" => {
2410 let mut buf = Vec::new();
2411 body.read_to_end(&mut buf).await.ok();
2412 let decompressed = zstd::decode_all(&buf[..]).unwrap();
2413 let req = serde_json::from_slice(&decompressed).unwrap();
2414
2415 let (res_tx, res_rx) = oneshot::channel();
2416 predict_req_tx.unbounded_send((req, res_tx)).unwrap();
2417 serde_json::to_string(&res_rx.await?).unwrap()
2418 }
2419 "/predict_edits/reject" => {
2420 let mut buf = Vec::new();
2421 body.read_to_end(&mut buf).await.ok();
2422 let req = serde_json::from_slice(&buf).unwrap();
2423
2424 let (res_tx, res_rx) = oneshot::channel();
2425 reject_req_tx.unbounded_send((req, res_tx)).unwrap();
2426 serde_json::to_string(&res_rx.await?).unwrap()
2427 }
2428 _ => {
2429 panic!("Unexpected path: {}", uri)
2430 }
2431 };
2432
2433 Ok(Response::builder().body(resp.into()).unwrap())
2434 }
2435 }
2436 });
2437
2438 let client = client::Client::new(Arc::new(FakeSystemClock::new()), http_client, cx);
2439 client.cloud_client().set_credentials(1, "test".into());
2440
2441 let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
2442 language_model::init(cx);
2443 RefreshLlmTokenListener::register(client.clone(), user_store.clone(), cx);
2444 let ep_store = EditPredictionStore::global(&client, &user_store, cx);
2445
2446 (
2447 ep_store,
2448 RequestChannels {
2449 predict: predict_req_rx,
2450 reject: reject_req_rx,
2451 },
2452 )
2453 })
2454}
2455
2456#[gpui::test]
2457async fn test_edit_prediction_basic_interpolation(cx: &mut TestAppContext) {
2458 let buffer = cx.new(|cx| Buffer::local("Lorem ipsum dolor", cx));
2459 let edits: Arc<[(Range<Anchor>, Arc<str>)]> = cx.update(|cx| {
2460 to_completion_edits([(2..5, "REM".into()), (9..11, "".into())], &buffer, cx).into()
2461 });
2462
2463 let edit_preview = cx
2464 .read(|cx| buffer.read(cx).preview_edits(edits.clone(), cx))
2465 .await;
2466
2467 let prediction = EditPrediction {
2468 edits,
2469 cursor_position: None,
2470 edit_preview,
2471 buffer: buffer.clone(),
2472 snapshot: cx.read(|cx| buffer.read(cx).snapshot()),
2473 id: EditPredictionId("the-id".into()),
2474 inputs: ZetaPromptInput {
2475 events: Default::default(),
2476 related_files: Default::default(),
2477 active_buffer_diagnostics: vec![],
2478 cursor_path: Path::new("").into(),
2479 cursor_excerpt: "".into(),
2480 cursor_offset_in_excerpt: 0,
2481 excerpt_start_row: None,
2482 excerpt_ranges: Default::default(),
2483 syntax_ranges: None,
2484 experiment: None,
2485 in_open_source_repo: false,
2486 can_collect_data: false,
2487 repo_url: None,
2488 },
2489 model_version: None,
2490 };
2491
2492 cx.update(|cx| {
2493 assert_eq!(
2494 from_completion_edits(
2495 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
2496 &buffer,
2497 cx
2498 ),
2499 vec![(2..5, "REM".into()), (9..11, "".into())]
2500 );
2501
2502 buffer.update(cx, |buffer, cx| buffer.edit([(2..5, "")], None, cx));
2503 assert_eq!(
2504 from_completion_edits(
2505 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
2506 &buffer,
2507 cx
2508 ),
2509 vec![(2..2, "REM".into()), (6..8, "".into())]
2510 );
2511
2512 buffer.update(cx, |buffer, cx| buffer.undo(cx));
2513 assert_eq!(
2514 from_completion_edits(
2515 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
2516 &buffer,
2517 cx
2518 ),
2519 vec![(2..5, "REM".into()), (9..11, "".into())]
2520 );
2521
2522 buffer.update(cx, |buffer, cx| buffer.edit([(2..5, "R")], None, cx));
2523 assert_eq!(
2524 from_completion_edits(
2525 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
2526 &buffer,
2527 cx
2528 ),
2529 vec![(3..3, "EM".into()), (7..9, "".into())]
2530 );
2531
2532 buffer.update(cx, |buffer, cx| buffer.edit([(3..3, "E")], None, cx));
2533 assert_eq!(
2534 from_completion_edits(
2535 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
2536 &buffer,
2537 cx
2538 ),
2539 vec![(4..4, "M".into()), (8..10, "".into())]
2540 );
2541
2542 buffer.update(cx, |buffer, cx| buffer.edit([(4..4, "M")], None, cx));
2543 assert_eq!(
2544 from_completion_edits(
2545 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
2546 &buffer,
2547 cx
2548 ),
2549 vec![(9..11, "".into())]
2550 );
2551
2552 buffer.update(cx, |buffer, cx| buffer.edit([(4..5, "")], None, cx));
2553 assert_eq!(
2554 from_completion_edits(
2555 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
2556 &buffer,
2557 cx
2558 ),
2559 vec![(4..4, "M".into()), (8..10, "".into())]
2560 );
2561
2562 buffer.update(cx, |buffer, cx| buffer.edit([(8..10, "")], None, cx));
2563 assert_eq!(
2564 from_completion_edits(
2565 &prediction.interpolate(&buffer.read(cx).snapshot()).unwrap(),
2566 &buffer,
2567 cx
2568 ),
2569 vec![(4..4, "M".into())]
2570 );
2571
2572 buffer.update(cx, |buffer, cx| buffer.edit([(4..6, "")], None, cx));
2573 assert_eq!(prediction.interpolate(&buffer.read(cx).snapshot()), None);
2574 })
2575}
2576
2577#[gpui::test]
2578async fn test_clean_up_diff(cx: &mut TestAppContext) {
2579 init_test(cx);
2580
2581 assert_eq!(
2582 apply_edit_prediction(
2583 indoc! {"
2584 fn main() {
2585 let word_1 = \"lorem\";
2586 let range = word.len()..word.len();
2587 }
2588 "},
2589 indoc! {"
2590 fn main() {
2591 let word_1 = \"lorem\";
2592 let range = word_1.len()..word_1.len();
2593 }
2594 "},
2595 cx,
2596 )
2597 .await,
2598 indoc! {"
2599 fn main() {
2600 let word_1 = \"lorem\";
2601 let range = word_1.len()..word_1.len();
2602 }
2603 "},
2604 );
2605
2606 assert_eq!(
2607 apply_edit_prediction(
2608 indoc! {"
2609 fn main() {
2610 let story = \"the quick\"
2611 }
2612 "},
2613 indoc! {"
2614 fn main() {
2615 let story = \"the quick brown fox jumps over the lazy dog\";
2616 }
2617 "},
2618 cx,
2619 )
2620 .await,
2621 indoc! {"
2622 fn main() {
2623 let story = \"the quick brown fox jumps over the lazy dog\";
2624 }
2625 "},
2626 );
2627}
2628
2629#[gpui::test]
2630async fn test_edit_prediction_end_of_buffer(cx: &mut TestAppContext) {
2631 init_test(cx);
2632
2633 let buffer_content = "lorem\n";
2634 let completion_response = "lorem\nipsum\n";
2635
2636 assert_eq!(
2637 apply_edit_prediction(buffer_content, completion_response, cx).await,
2638 "lorem\nipsum\n"
2639 );
2640}
2641
2642#[gpui::test]
2643async fn test_edit_prediction_no_spurious_trailing_newline(cx: &mut TestAppContext) {
2644 // Test that zeta2's newline normalization logic doesn't insert spurious newlines.
2645 // When the buffer ends without a trailing newline, but the model returns output
2646 // with a trailing newline, zeta2 should normalize both sides before diffing
2647 // so no spurious newline is inserted.
2648 let (ep_store, mut requests) = init_test_with_fake_client(cx);
2649 let fs = FakeFs::new(cx.executor());
2650
2651 // Single line buffer with no trailing newline
2652 fs.insert_tree(
2653 "/root",
2654 json!({
2655 "foo.txt": "hello"
2656 }),
2657 )
2658 .await;
2659 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
2660
2661 let buffer = project
2662 .update(cx, |project, cx| {
2663 let path = project
2664 .find_project_path(path!("root/foo.txt"), cx)
2665 .unwrap();
2666 project.open_buffer(path, cx)
2667 })
2668 .await
2669 .unwrap();
2670
2671 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
2672 let position = snapshot.anchor_before(language::Point::new(0, 5));
2673
2674 ep_store.update(cx, |ep_store, cx| {
2675 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
2676 });
2677
2678 let (request, respond_tx) = requests.predict.next().await.unwrap();
2679
2680 // Model returns output WITH a trailing newline, even though the buffer doesn't have one.
2681 // Zeta2 should normalize both sides before diffing, so no spurious newline is inserted.
2682 let excerpt_length = request.input.cursor_excerpt.len();
2683 let response = PredictEditsV3Response {
2684 request_id: Uuid::new_v4().to_string(),
2685 output: "hello world\n".to_string(),
2686 editable_range: 0..excerpt_length,
2687 model_version: None,
2688 };
2689 respond_tx.send(response).unwrap();
2690
2691 cx.run_until_parked();
2692
2693 // The prediction should insert " world" without adding a newline
2694 ep_store.update(cx, |ep_store, cx| {
2695 let prediction = ep_store
2696 .prediction_at(&buffer, None, &project, cx)
2697 .expect("should have prediction");
2698 let edits: Vec<_> = prediction
2699 .edits
2700 .iter()
2701 .map(|(range, text)| {
2702 let snapshot = buffer.read(cx).snapshot();
2703 (range.to_offset(&snapshot), text.clone())
2704 })
2705 .collect();
2706 assert_eq!(edits, vec![(5..5, " world".into())]);
2707 });
2708}
2709
2710#[gpui::test]
2711async fn test_v3_prediction_strips_cursor_marker_from_edit_text(cx: &mut TestAppContext) {
2712 let (ep_store, mut requests) = init_test_with_fake_client(cx);
2713 let fs = FakeFs::new(cx.executor());
2714
2715 fs.insert_tree(
2716 "/root",
2717 json!({
2718 "foo.txt": "hello"
2719 }),
2720 )
2721 .await;
2722 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
2723
2724 let buffer = project
2725 .update(cx, |project, cx| {
2726 let path = project
2727 .find_project_path(path!("root/foo.txt"), cx)
2728 .unwrap();
2729 project.open_buffer(path, cx)
2730 })
2731 .await
2732 .unwrap();
2733
2734 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
2735 let position = snapshot.anchor_before(language::Point::new(0, 5));
2736
2737 ep_store.update(cx, |ep_store, cx| {
2738 ep_store.refresh_prediction_from_buffer(project.clone(), buffer.clone(), position, cx);
2739 });
2740
2741 let (request, respond_tx) = requests.predict.next().await.unwrap();
2742 let excerpt_length = request.input.cursor_excerpt.len();
2743 respond_tx
2744 .send(PredictEditsV3Response {
2745 request_id: Uuid::new_v4().to_string(),
2746 output: "hello<|user_cursor|> world".to_string(),
2747 editable_range: 0..excerpt_length,
2748 model_version: None,
2749 })
2750 .unwrap();
2751
2752 cx.run_until_parked();
2753
2754 ep_store.update(cx, |ep_store, cx| {
2755 let prediction = ep_store
2756 .prediction_at(&buffer, None, &project, cx)
2757 .expect("should have prediction");
2758 let snapshot = buffer.read(cx).snapshot();
2759 let edits: Vec<_> = prediction
2760 .edits
2761 .iter()
2762 .map(|(range, text)| (range.to_offset(&snapshot), text.clone()))
2763 .collect();
2764
2765 assert_eq!(edits, vec![(5..5, " world".into())]);
2766 });
2767}
2768
2769fn init_test(cx: &mut TestAppContext) {
2770 cx.update(|cx| {
2771 let settings_store = SettingsStore::test(cx);
2772 cx.set_global(settings_store);
2773 });
2774}
2775
2776async fn apply_edit_prediction(
2777 buffer_content: &str,
2778 completion_response: &str,
2779 cx: &mut TestAppContext,
2780) -> String {
2781 let fs = project::FakeFs::new(cx.executor());
2782 let project = Project::test(fs.clone(), [path!("/project").as_ref()], cx).await;
2783 let buffer = cx.new(|cx| Buffer::local(buffer_content, cx));
2784 let (ep_store, response) = make_test_ep_store(&project, cx).await;
2785 *response.lock() = completion_response.to_string();
2786 let edit_prediction = run_edit_prediction(&buffer, &project, &ep_store, cx).await;
2787 buffer.update(cx, |buffer, cx| {
2788 buffer.edit(edit_prediction.edits.iter().cloned(), None, cx)
2789 });
2790 buffer.read_with(cx, |buffer, _| buffer.text())
2791}
2792
2793async fn run_edit_prediction(
2794 buffer: &Entity<Buffer>,
2795 project: &Entity<Project>,
2796 ep_store: &Entity<EditPredictionStore>,
2797 cx: &mut TestAppContext,
2798) -> EditPrediction {
2799 let cursor = buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(1, 0)));
2800 ep_store.update(cx, |ep_store, cx| {
2801 ep_store.register_buffer(buffer, &project, cx)
2802 });
2803 cx.background_executor.run_until_parked();
2804 let prediction_task = ep_store.update(cx, |ep_store, cx| {
2805 ep_store.request_prediction(&project, buffer, cursor, Default::default(), cx)
2806 });
2807 prediction_task.await.unwrap().unwrap().prediction.unwrap()
2808}
2809
2810async fn make_test_ep_store(
2811 project: &Entity<Project>,
2812 cx: &mut TestAppContext,
2813) -> (Entity<EditPredictionStore>, Arc<Mutex<String>>) {
2814 let default_response = "hello world\n".to_string();
2815 let completion_response: Arc<Mutex<String>> = Arc::new(Mutex::new(default_response));
2816 let http_client = FakeHttpClient::create({
2817 let completion_response = completion_response.clone();
2818 let mut next_request_id = 0;
2819 move |req| {
2820 let completion_response = completion_response.clone();
2821 let method = req.method().clone();
2822 let uri = req.uri().path().to_string();
2823 let mut body = req.into_body();
2824 async move {
2825 match (method, uri.as_str()) {
2826 (Method::POST, "/client/llm_tokens") => Ok(http_client::Response::builder()
2827 .status(200)
2828 .body(
2829 serde_json::to_string(&CreateLlmTokenResponse {
2830 token: LlmToken("the-llm-token".to_string()),
2831 })
2832 .unwrap()
2833 .into(),
2834 )
2835 .unwrap()),
2836 (Method::POST, "/predict_edits/v3") => {
2837 let mut buf = Vec::new();
2838 body.read_to_end(&mut buf).await.ok();
2839 let decompressed = zstd::decode_all(&buf[..]).unwrap();
2840 let req: PredictEditsV3Request =
2841 serde_json::from_slice(&decompressed).unwrap();
2842
2843 next_request_id += 1;
2844 Ok(http_client::Response::builder()
2845 .status(200)
2846 .body(
2847 serde_json::to_string(&PredictEditsV3Response {
2848 request_id: format!("request-{next_request_id}"),
2849 editable_range: 0..req.input.cursor_excerpt.len(),
2850 output: completion_response.lock().clone(),
2851 model_version: None,
2852 })
2853 .unwrap()
2854 .into(),
2855 )
2856 .unwrap())
2857 }
2858 _ => Ok(http_client::Response::builder()
2859 .status(404)
2860 .body("Not Found".to_string().into())
2861 .unwrap()),
2862 }
2863 }
2864 }
2865 });
2866
2867 let client = cx.update(|cx| Client::new(Arc::new(FakeSystemClock::new()), http_client, cx));
2868 let user_store = cx.update(|cx| cx.new(|cx| client::UserStore::new(client.clone(), cx)));
2869 cx.update(|cx| {
2870 RefreshLlmTokenListener::register(client.clone(), user_store.clone(), cx);
2871 });
2872 let _server = FakeServer::for_client(42, &client, cx).await;
2873
2874 let ep_store = cx.new(|cx| {
2875 let mut ep_store = EditPredictionStore::new(client, project.read(cx).user_store(), cx);
2876 ep_store.set_edit_prediction_model(EditPredictionModel::Zeta);
2877
2878 let worktrees = project.read(cx).worktrees(cx).collect::<Vec<_>>();
2879 for worktree in worktrees {
2880 let worktree_id = worktree.read(cx).id();
2881 ep_store
2882 .get_or_init_project(project, cx)
2883 .license_detection_watchers
2884 .entry(worktree_id)
2885 .or_insert_with(|| Rc::new(LicenseDetectionWatcher::new(&worktree, cx)));
2886 }
2887
2888 ep_store
2889 });
2890
2891 (ep_store, completion_response)
2892}
2893
2894fn to_completion_edits(
2895 iterator: impl IntoIterator<Item = (Range<usize>, Arc<str>)>,
2896 buffer: &Entity<Buffer>,
2897 cx: &App,
2898) -> Vec<(Range<Anchor>, Arc<str>)> {
2899 let buffer = buffer.read(cx);
2900 iterator
2901 .into_iter()
2902 .map(|(range, text)| {
2903 (
2904 buffer.anchor_after(range.start)..buffer.anchor_before(range.end),
2905 text,
2906 )
2907 })
2908 .collect()
2909}
2910
2911fn from_completion_edits(
2912 editor_edits: &[(Range<Anchor>, Arc<str>)],
2913 buffer: &Entity<Buffer>,
2914 cx: &App,
2915) -> Vec<(Range<usize>, Arc<str>)> {
2916 let buffer = buffer.read(cx);
2917 editor_edits
2918 .iter()
2919 .map(|(range, text)| {
2920 (
2921 range.start.to_offset(buffer)..range.end.to_offset(buffer),
2922 text.clone(),
2923 )
2924 })
2925 .collect()
2926}
2927
2928#[gpui::test]
2929async fn test_unauthenticated_without_custom_url_blocks_prediction_impl(cx: &mut TestAppContext) {
2930 init_test(cx);
2931
2932 let fs = FakeFs::new(cx.executor());
2933 fs.insert_tree(
2934 "/project",
2935 serde_json::json!({
2936 "main.rs": "fn main() {\n \n}\n"
2937 }),
2938 )
2939 .await;
2940
2941 let project = Project::test(fs.clone(), [path!("/project").as_ref()], cx).await;
2942
2943 let http_client = FakeHttpClient::create(|_req| async move {
2944 Ok(gpui::http_client::Response::builder()
2945 .status(401)
2946 .body("Unauthorized".into())
2947 .unwrap())
2948 });
2949
2950 let client =
2951 cx.update(|cx| client::Client::new(Arc::new(FakeSystemClock::new()), http_client, cx));
2952 let user_store = cx.update(|cx| cx.new(|cx| client::UserStore::new(client.clone(), cx)));
2953 cx.update(|cx| {
2954 RefreshLlmTokenListener::register(client.clone(), user_store.clone(), cx);
2955 });
2956
2957 let ep_store = cx.new(|cx| EditPredictionStore::new(client, project.read(cx).user_store(), cx));
2958
2959 let buffer = project
2960 .update(cx, |project, cx| {
2961 let path = project
2962 .find_project_path(path!("/project/main.rs"), cx)
2963 .unwrap();
2964 project.open_buffer(path, cx)
2965 })
2966 .await
2967 .unwrap();
2968
2969 let cursor = buffer.read_with(cx, |buffer, _| buffer.anchor_before(Point::new(1, 4)));
2970 ep_store.update(cx, |ep_store, cx| {
2971 ep_store.register_buffer(&buffer, &project, cx)
2972 });
2973 cx.background_executor.run_until_parked();
2974
2975 let completion_task = ep_store.update(cx, |ep_store, cx| {
2976 ep_store.set_edit_prediction_model(EditPredictionModel::Zeta);
2977 ep_store.request_prediction(&project, &buffer, cursor, Default::default(), cx)
2978 });
2979
2980 let result = completion_task.await;
2981 assert!(
2982 result.is_err(),
2983 "Without authentication and without custom URL, prediction should fail"
2984 );
2985}
2986
2987#[gpui::test]
2988async fn test_diagnostic_jump_excludes_collaborator_regions(cx: &mut TestAppContext) {
2989 fn set_collaborator_cursor(buffer: &Entity<Buffer>, row: u32, cx: &mut TestAppContext) {
2990 let collab_replica = clock::ReplicaId::new(10);
2991 let anchor = buffer.read_with(cx, |buffer, _| {
2992 buffer.snapshot().anchor_before(Point::new(row, 0))
2993 });
2994 let selections: Arc<[Selection<Anchor>]> = Arc::new([Selection {
2995 id: 1,
2996 start: anchor,
2997 end: anchor,
2998 reversed: false,
2999 goal: SelectionGoal::None,
3000 }]);
3001 buffer.update(cx, |buffer, cx| {
3002 buffer.apply_ops(
3003 [Operation::UpdateSelections {
3004 selections,
3005 lamport_timestamp: clock::Lamport {
3006 replica_id: collab_replica,
3007 value: 1,
3008 },
3009 line_mode: false,
3010 cursor_shape: CursorShape::Bar,
3011 }],
3012 cx,
3013 );
3014 });
3015 }
3016
3017 fn publish_diagnostics(
3018 uri_path: &'static str,
3019 rows: &[u32],
3020 project: &Entity<Project>,
3021 cx: &mut TestAppContext,
3022 ) {
3023 let diagnostics: Vec<_> = rows
3024 .iter()
3025 .map(|&row| lsp::Diagnostic {
3026 range: lsp::Range::new(lsp::Position::new(row, 0), lsp::Position::new(row, 5)),
3027 severity: Some(lsp::DiagnosticSeverity::ERROR),
3028 message: format!("error at row {row}"),
3029 ..Default::default()
3030 })
3031 .collect();
3032 project.update(cx, |project, cx| {
3033 project.lsp_store().update(cx, |lsp_store, cx| {
3034 lsp_store
3035 .update_diagnostics(
3036 LanguageServerId(0),
3037 lsp::PublishDiagnosticsParams {
3038 uri: lsp::Uri::from_file_path(uri_path).expect("invalid uri"),
3039 diagnostics,
3040 version: None,
3041 },
3042 None,
3043 language::DiagnosticSourceKind::Pushed,
3044 &[],
3045 cx,
3046 )
3047 .expect("failed to update diagnostics");
3048 });
3049 });
3050 }
3051
3052 init_test(cx);
3053
3054 let mut lines = String::new();
3055 for i in 0..60 {
3056 lines.push_str(&format!("line {i}\n"));
3057 }
3058
3059 let fs = FakeFs::new(cx.executor());
3060 fs.insert_tree(
3061 "/root",
3062 json!({
3063 "active.txt": lines,
3064 "collab_file.txt": "error here\nsecond line\n",
3065 "free_file.txt": "another error\nsecond line\n",
3066 }),
3067 )
3068 .await;
3069 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
3070
3071 let active_buffer = project
3072 .update(cx, |project, cx| {
3073 let path = project
3074 .find_project_path(path!("/root/active.txt"), cx)
3075 .expect("active.txt not found");
3076 project.set_active_path(Some(path.clone()), cx);
3077 project.open_buffer(path, cx)
3078 })
3079 .await
3080 .expect("failed to open active buffer");
3081
3082 set_collaborator_cursor(&active_buffer, 5, cx);
3083
3084 publish_diagnostics(path!("/root/active.txt"), &[3, 25, 50], &project, cx);
3085
3086 cx.run_until_parked();
3087
3088 let cursor_point = Point::new(25, 0);
3089 let empty_search_range: Range<Point> = Default::default();
3090
3091 let snapshot = active_buffer.read_with(cx, |buffer, _| buffer.snapshot());
3092 let result = EditPredictionStore::next_diagnostic_location(
3093 active_buffer.clone(),
3094 &snapshot,
3095 empty_search_range.clone(),
3096 cursor_point,
3097 &project,
3098 &mut cx.to_async(),
3099 )
3100 .await
3101 .expect("next_diagnostic_location failed");
3102
3103 let (result_buffer, result_anchor) = result.expect("expected a diagnostic location");
3104 assert_eq!(result_buffer.entity_id(), active_buffer.entity_id());
3105 let result_row = result_buffer.read_with(cx, |buffer, _| {
3106 result_anchor.to_point(&buffer.snapshot()).row
3107 });
3108 assert_ne!(
3109 result_row, 3,
3110 "row 3 is near collaborator (row 5) but far from local cursor (row 25), should be excluded"
3111 );
3112 assert!(
3113 result_row == 25 || result_row == 50,
3114 "expected row 25 or 50, got {result_row}"
3115 );
3116
3117 let snapshot_near = active_buffer.read_with(cx, |buffer, _| buffer.snapshot());
3118 let near_cursor_point = Point::new(4, 0);
3119 let result_near = EditPredictionStore::next_diagnostic_location(
3120 active_buffer.clone(),
3121 &snapshot_near,
3122 empty_search_range.clone(),
3123 near_cursor_point,
3124 &project,
3125 &mut cx.to_async(),
3126 )
3127 .await
3128 .expect("next_diagnostic_location failed");
3129
3130 let (_, near_anchor) = result_near.expect("expected a diagnostic location when both are near");
3131 let near_row =
3132 active_buffer.read_with(cx, |buffer, _| near_anchor.to_point(&buffer.snapshot()).row);
3133 assert_eq!(
3134 near_row, 3,
3135 "row 3 should be included when local cursor (row 4) is also near the collaborator"
3136 );
3137
3138 let snapshot_far = active_buffer.read_with(cx, |buffer, _| buffer.snapshot());
3139 let far_cursor_point = Point::new(50, 0);
3140 let result_far = EditPredictionStore::next_diagnostic_location(
3141 active_buffer.clone(),
3142 &snapshot_far,
3143 empty_search_range.clone(),
3144 far_cursor_point,
3145 &project,
3146 &mut cx.to_async(),
3147 )
3148 .await
3149 .expect("next_diagnostic_location failed");
3150
3151 let (_, far_anchor) = result_far.expect("expected a diagnostic location");
3152 let far_row =
3153 active_buffer.read_with(cx, |buffer, _| far_anchor.to_point(&buffer.snapshot()).row);
3154 assert_eq!(
3155 far_row, 50,
3156 "row 50 is near local cursor (row 50) and far from collaborator, should be picked"
3157 );
3158
3159 publish_diagnostics(path!("/root/collab_file.txt"), &[0], &project, cx);
3160 publish_diagnostics(path!("/root/free_file.txt"), &[0], &project, cx);
3161 cx.run_until_parked();
3162
3163 let collab_buffer = project
3164 .update(cx, |project, cx| {
3165 let path = project
3166 .find_project_path(path!("/root/collab_file.txt"), cx)
3167 .expect("collab_file.txt not found");
3168 project.open_buffer(path, cx)
3169 })
3170 .await
3171 .expect("failed to open collab buffer");
3172
3173 set_collaborator_cursor(&collab_buffer, 0, cx);
3174 cx.run_until_parked();
3175
3176 let no_same_file_search_range = Point::new(0, 0)..Point::new(59, 0);
3177 let snapshot_cross = active_buffer.read_with(cx, |buffer, _| buffer.snapshot());
3178 let result_cross = EditPredictionStore::next_diagnostic_location(
3179 active_buffer.clone(),
3180 &snapshot_cross,
3181 no_same_file_search_range,
3182 Point::new(0, 0),
3183 &project,
3184 &mut cx.to_async(),
3185 )
3186 .await
3187 .expect("cross-file next_diagnostic_location failed");
3188
3189 let (cross_buffer, _) = result_cross.expect("expected a cross-file diagnostic location");
3190 let cross_path = cross_buffer.read_with(cx, |buffer, cx| {
3191 buffer
3192 .file()
3193 .expect("buffer should have a file")
3194 .full_path(cx)
3195 });
3196 assert_eq!(
3197 cross_path,
3198 Path::new(path!("root/free_file.txt")),
3199 "should skip collab_file.txt (has collaborator) and pick free_file.txt"
3200 );
3201}
3202
3203#[gpui::test]
3204async fn test_edit_prediction_settled(cx: &mut TestAppContext) {
3205 let (ep_store, _requests) = init_test_with_fake_client(cx);
3206 let fs = FakeFs::new(cx.executor());
3207
3208 // Buffer with two clearly separated regions:
3209 // Region A = lines 0-9 (offsets 0..50)
3210 // Region B = lines 20-29 (offsets 105..155)
3211 // A big gap in between so edits in one region never overlap the other.
3212 let mut content = String::new();
3213 for i in 0..30 {
3214 content.push_str(&format!("line {i:02}\n"));
3215 }
3216
3217 fs.insert_tree(
3218 "/root",
3219 json!({
3220 "foo.md": content.clone()
3221 }),
3222 )
3223 .await;
3224 let project = Project::test(fs, vec![path!("/root").as_ref()], cx).await;
3225
3226 let buffer = project
3227 .update(cx, |project, cx| {
3228 let path = project.find_project_path(path!("root/foo.md"), cx).unwrap();
3229 project.open_buffer(path, cx)
3230 })
3231 .await
3232 .unwrap();
3233
3234 type SettledEventRecord = (EditPredictionId, String);
3235 let settled_events: Arc<Mutex<Vec<SettledEventRecord>>> = Arc::new(Mutex::new(Vec::new()));
3236
3237 ep_store.update(cx, |ep_store, cx| {
3238 ep_store.register_buffer(&buffer, &project, cx);
3239
3240 let settled_events = settled_events.clone();
3241 ep_store.settled_event_callback = Some(Box::new(move |id, text| {
3242 settled_events.lock().push((id, text));
3243 }));
3244 });
3245
3246 // --- Phase 1: edit in region A and enqueue prediction A ---
3247
3248 buffer.update(cx, |buffer, cx| {
3249 // Edit at the start of line 0.
3250 buffer.edit(vec![(0..0, "ADDED ")], None, cx);
3251 });
3252 cx.run_until_parked();
3253
3254 let snapshot_a = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
3255
3256 // Region A: first 10 lines of the buffer.
3257 let editable_region_a = 0..snapshot_a.point_to_offset(Point::new(10, 0));
3258
3259 ep_store.update(cx, |ep_store, cx| {
3260 ep_store.enqueue_settled_prediction(
3261 EditPredictionId("prediction-a".into()),
3262 &project,
3263 &buffer,
3264 &snapshot_a,
3265 editable_region_a.clone(),
3266 None,
3267 Duration::from_secs(0),
3268 cx,
3269 );
3270 });
3271
3272 // --- Phase 2: repeatedly edit in region A to keep it unsettled ---
3273
3274 // Let the worker process the channel message before we start advancing.
3275 cx.run_until_parked();
3276
3277 let mut region_a_edit_offset = 5;
3278 for _ in 0..3 {
3279 // Edit inside region A (not at the boundary) so `last_edit_at` is
3280 // updated before the worker's next wake.
3281 buffer.update(cx, |buffer, cx| {
3282 buffer.edit(
3283 vec![(region_a_edit_offset..region_a_edit_offset, "x")],
3284 None,
3285 cx,
3286 );
3287 });
3288 region_a_edit_offset += 1;
3289 cx.run_until_parked();
3290
3291 cx.executor()
3292 .advance_clock(EDIT_PREDICTION_SETTLED_QUIESCENCE / 2);
3293 cx.run_until_parked();
3294 assert!(
3295 settled_events.lock().is_empty(),
3296 "no settled events should fire while region A is still being edited"
3297 );
3298 }
3299
3300 // Still nothing settled.
3301 assert!(settled_events.lock().is_empty());
3302
3303 // --- Phase 3: edit in distinct region B, enqueue prediction B ---
3304 // Advance a small amount so B's quiescence window starts later than A's,
3305 // but not so much that A settles (A's last edit was at the start of
3306 // iteration 3, and it needs a full Q to settle).
3307 cx.executor()
3308 .advance_clock(EDIT_PREDICTION_SETTLED_QUIESCENCE / 4);
3309 cx.run_until_parked();
3310 assert!(settled_events.lock().is_empty());
3311
3312 let snapshot_b = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
3313 let line_20_offset = snapshot_b.point_to_offset(Point::new(20, 0));
3314
3315 buffer.update(cx, |buffer, cx| {
3316 buffer.edit(vec![(line_20_offset..line_20_offset, "NEW ")], None, cx);
3317 });
3318 cx.run_until_parked();
3319
3320 let snapshot_b2 = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
3321 let editable_region_b = line_20_offset..snapshot_b2.point_to_offset(Point::new(25, 0));
3322
3323 ep_store.update(cx, |ep_store, cx| {
3324 ep_store.enqueue_settled_prediction(
3325 EditPredictionId("prediction-b".into()),
3326 &project,
3327 &buffer,
3328 &snapshot_b2,
3329 editable_region_b.clone(),
3330 None,
3331 Duration::from_secs(0),
3332 cx,
3333 );
3334 });
3335
3336 cx.run_until_parked();
3337 assert!(
3338 settled_events.lock().is_empty(),
3339 "neither prediction should have settled yet"
3340 );
3341
3342 // --- Phase 4: let enough time pass for region A to settle ---
3343 // A's last edit was at T_a (during the last loop iteration). The worker is
3344 // sleeping until T_a + Q. We advance just enough to reach that wake time
3345 // (Q/4 since we already advanced Q/4 in phase 3 on top of the loop's
3346 // 3*Q/2). At that point A has been quiet for Q and settles, but B was
3347 // enqueued only Q/4 ago and stays pending.
3348 cx.executor()
3349 .advance_clock(EDIT_PREDICTION_SETTLED_QUIESCENCE / 4);
3350 cx.run_until_parked();
3351
3352 {
3353 let events = settled_events.lock().clone();
3354 assert_eq!(
3355 events.len(),
3356 1,
3357 "prediction and capture_sample for A should have settled, got: {events:?}"
3358 );
3359 assert_eq!(events[0].0, EditPredictionId("prediction-a".into()));
3360 }
3361
3362 // --- Phase 5: let more time pass for region B to settle ---
3363 // B's last edit was Q/4 before A settled. The worker rescheduled to
3364 // B's last_edit_at + Q, which is 3Q/4 from now.
3365 cx.executor()
3366 .advance_clock(EDIT_PREDICTION_SETTLED_QUIESCENCE * 3 / 4);
3367 cx.run_until_parked();
3368
3369 {
3370 let events = settled_events.lock().clone();
3371 assert_eq!(
3372 events.len(),
3373 2,
3374 "both prediction and capture_sample settled events should be emitted for each request, got: {events:?}"
3375 );
3376 assert_eq!(events[1].0, EditPredictionId("prediction-b".into()));
3377 }
3378}
3379
3380#[ctor::ctor]
3381fn init_logger() {
3382 zlog::init_test();
3383}