Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
ep: Hashed region format (#59837)
Ben Kunkle784b14e207b7
diff --git a/crates/cloud_api_types/src/cloud_api_types.rs b/crates/cloud_api_types/src/cloud_api_types.rsindex 6fef7c586df..54abe293545 100644--- a/crates/cloud_api_types/src/cloud_api_types.rs+++ b/crates/cloud_api_types/src/cloud_api_types.rs@@ -162,6 +162,8 @@ pub struct SettledEditPredictionSampleData {#[serde(default, skip_serializing_if = "Vec::is_empty")]pub buffer_diagnostics: Vec<zeta_prompt::ActiveBufferDiagnostic>,#[serde(default, skip_serializing_if = "Vec::is_empty")]+ pub editable_context: Vec<zeta_prompt::RelatedFile>,+ #[serde(default, skip_serializing_if = "Vec::is_empty")]pub future_edit_history_events: Vec<Arc<zeta_prompt::Event>>,#[serde(default, skip_serializing_if = "Vec::is_empty")]pub navigation_history: Vec<EditPredictionRecentFile>,diff --git a/crates/edit_prediction/src/capture_prediction_context.rs b/crates/edit_prediction/src/capture_prediction_context.rsindex 449ce8da706..39f05532e7a 100644--- a/crates/edit_prediction/src/capture_prediction_context.rs+++ b/crates/edit_prediction/src/capture_prediction_context.rs@@ -16,6 +16,7 @@ pub(crate) struct CapturedPredictionContext {pub(crate) revision: Option<String>,pub(crate) uncommitted_diff: Option<String>,pub(crate) buffer_diagnostics: Vec<zeta_prompt::ActiveBufferDiagnostic>,+ pub(crate) editable_context: Vec<zeta_prompt::RelatedFile>,}pub(crate) fn capture_prediction_context(@@ -25,6 +26,7 @@ pub(crate) fn capture_prediction_context(stored_events: Vec<StoredEvent>,repository_url: Option<String>,revision: Option<String>,+ editable_context_task: Task<Result<Vec<zeta_prompt::RelatedFile>>>,cx: &mut Context<EditPredictionStore>,) -> Option<Task<Result<CapturedPredictionContext>>> {let snapshot = buffer.read(cx).snapshot();@@ -74,12 +76,20 @@ pub(crate) fn capture_prediction_context(cursor_anchor.to_point(&snapshot).row,100,);+ let editable_context = match editable_context_task.await {+ Ok(editable_context) => editable_context,+ Err(error) => {+ log::debug!("failed to capture editable context: {error:?}");+ Vec::new()+ }+ };Ok(CapturedPredictionContext {repository_url,revision,uncommitted_diff,buffer_diagnostics,+ editable_context,})}))}diff --git a/crates/edit_prediction/src/edit_prediction.rs b/crates/edit_prediction/src/edit_prediction.rsindex 4e576580fc2..f9b07daa4f4 100644--- a/crates/edit_prediction/src/edit_prediction.rs+++ b/crates/edit_prediction/src/edit_prediction.rs@@ -56,7 +56,6 @@ use std::env;use std::rc::Rc;use text::{AnchorRangeExt, Edit};use workspace::{AppState, Workspace};-#[cfg(feature = "cli-support")]use zeta_prompt::ContextSource;use zeta_prompt::{ZetaFormat, ZetaPromptInput};@@ -2076,6 +2075,7 @@ impl EditPredictionStore {editable_path: sample_data.editable_path,editable_offset_range: sample_data.editable_offset_range,buffer_diagnostics: context.buffer_diagnostics,+ editable_context: context.editable_context,future_edit_history_events: sample_data.future_edit_history_events,navigation_history: sample_data@@ -2093,7 +2093,6 @@ impl EditPredictionStore {} else {None};-let settled_editable_region =can_collect_data.then_some(settled_editable_region);@@ -2795,6 +2794,14 @@ impl EditPredictionStore {EditPredictionModel::Zeta => {let context_task = can_collect_data.then(|| {+ let editable_context_task = self.collect_editable_context(+ inputs.project.clone(),+ inputs.buffer.clone(),+ inputs.position,+ Vec::new(),+ vec![ContextSource::CurrentFile, ContextSource::EditHistory],+ cx,+ );capture_prediction_context(inputs.project.clone(),inputs.buffer.clone(),@@ -2802,6 +2809,7 @@ impl EditPredictionStore {stored_events,repository_url.clone(),revision,+ editable_context_task,cx,)})@@ -2978,13 +2986,12 @@ impl EditPredictionStore {});}- #[cfg(feature = "cli-support")]pub fn collect_editable_context(&mut self,project: Entity<Project>,buffer: Entity<language::Buffer>,cursor_position: language::Anchor,- oracle_paths: Vec<Arc<Path>>,+ oracle_targets: Vec<edit_prediction_context::OracleTarget>,context_sources: Vec<ContextSource>,cx: &mut Context<Self>,) -> Task<anyhow::Result<Vec<RelatedFile>>> {@@ -3015,7 +3022,7 @@ impl EditPredictionStore {buffer,cursor_position,edit_history,- oracle_paths,+ oracle_targets,context_sources,cx,)diff --git a/crates/edit_prediction/src/edit_prediction_tests.rs b/crates/edit_prediction/src/edit_prediction_tests.rsindex bf9ed2c2052..6c131390198 100644--- a/crates/edit_prediction/src/edit_prediction_tests.rs+++ b/crates/edit_prediction/src/edit_prediction_tests.rs@@ -3514,6 +3514,17 @@ async fn test_edit_prediction_settled_sends_sample_data_after_quiescence(cx: &musnippet_buffer_row_range: 0..0,diagnostic_range_in_snippet: 0..0,}],+ editable_context: vec![zeta_prompt::RelatedFile {+ path: Path::new("foo.md").into(),+ max_row: 60,+ excerpts: vec![zeta_prompt::RelatedExcerpt {+ row_range: 0..2,+ text: "line 0\nline 1\n".into(),+ order: 0,+ context_source: zeta_prompt::ContextSource::CurrentFile,+ }],+ in_open_source_repo: true,+ }],},Some(boundary),VecDeque::from([RecentFile {@@ -3553,6 +3564,7 @@ async fn test_edit_prediction_settled_sends_sample_data_after_quiescence(cx: &murevision: None,uncommitted_diff: None,buffer_diagnostics: Vec::new(),+ editable_context: Vec::new(),},Some(boundary),VecDeque::new(),@@ -3617,6 +3629,14 @@ async fn test_edit_prediction_settled_sends_sample_data_after_quiescence(cx: &muassert_eq!(sample_data.editable_path.as_ref(), Path::new("foo.md"));assert_eq!(sample_data.editable_offset_range, editable_offset_range);assert_eq!(sample_data.buffer_diagnostics.len(), 1);+ assert_eq!(sample_data.editable_context.len(), 1);+ let editable_context = &sample_data.editable_context[0];+ assert_eq!(editable_context.path.as_ref(), Path::new("foo.md"));+ assert_eq!(editable_context.excerpts.len(), 1);+ assert_eq!(+ editable_context.excerpts[0].context_source,+ zeta_prompt::ContextSource::CurrentFile+ );assert_eq!(sample_data.future_edit_history_events.len(), 4);assert_eq!(sample_data.navigation_history.len(), 1);assert_eq!(sample_data.edit_events_before_quiescence, 5);@@ -3700,6 +3720,7 @@ async fn test_edit_prediction_settled_sample_data_requires_observing_all_events_revision: None,uncommitted_diff: None,buffer_diagnostics: Vec::new(),+ editable_context: Vec::new(),},Some(boundary_observed),VecDeque::new(),@@ -3731,6 +3752,7 @@ async fn test_edit_prediction_settled_sample_data_requires_observing_all_events_revision: None,uncommitted_diff: None,buffer_diagnostics: Vec::new(),+ editable_context: Vec::new(),},Some(boundary_missed),VecDeque::new(),@@ -3784,6 +3806,7 @@ async fn test_edit_prediction_settled_drops_future_events_when_their_oss_status_revision: None,uncommitted_diff: None,buffer_diagnostics: Vec::new(),+ editable_context: Vec::new(),},None,VecDeque::new(),diff --git a/crates/edit_prediction_cli/src/example.rs b/crates/edit_prediction_cli/src/example.rsindex 0b5a75260fc..bce5b2bc2a5 100644--- a/crates/edit_prediction_cli/src/example.rs+++ b/crates/edit_prediction_cli/src/example.rs@@ -116,6 +116,16 @@ impl ActualCursor {editable_region_byte_offset: usize,editable_region_start_line: usize,) -> Self {+ // Defensive: a malformed/edge-case cursor offset must never panic and+ // abort an (expensive) batch run. Clamp into range and snap down to a+ // char boundary before slicing.+ let mut editable_region_cursor_offset =+ editable_region_cursor_offset.min(new_editable_region.len());+ while editable_region_cursor_offset > 0+ && !new_editable_region.is_char_boundary(editable_region_cursor_offset)+ {+ editable_region_cursor_offset -= 1;+ }let global_offset = editable_region_byte_offset + editable_region_cursor_offset;let new_region_prefix = &new_editable_region[..editable_region_cursor_offset];let row = (editable_region_start_line + new_region_prefix.matches('\n').count()) as u32;diff --git a/crates/edit_prediction_cli/src/format_prompt.rs b/crates/edit_prediction_cli/src/format_prompt.rsindex 8007499315a..23c9c7dde11 100644--- a/crates/edit_prediction_cli/src/format_prompt.rs+++ b/crates/edit_prediction_cli/src/format_prompt.rs@@ -10,7 +10,10 @@ use gpui::AsyncApp;use std::ops::Range;use std::sync::Arc;use zeta_prompt::{- ZetaFormat, format_expected_output, format_zeta_prompt, multi_region, resolve_cursor_region,+ ZetaFormat, ZetaPromptInput, format_edit_history_within_budget, format_expected_output,+ format_zeta_prompt,+ hashed_regions::{self, SnippetMarkers},+ max_edit_event_count_for_format, resolve_cursor_region,};fn resolved_excerpt_ranges_for_format(@@ -40,6 +43,21 @@ pub async fn run_format_prompt().await?;+ // Teacher-jumps addresses every edit through related-file excerpts and+ // hard-errors unless the cursor file is covered by one. Settled-data+ // samples carry a `cursor_excerpt` but were not run through current-file+ // context retrieval, so normalize the input to synthesize a current-file+ // excerpt from it when the cursor isn't already covered (a no-op for proper+ // `ep context --type=current-file` runs).+ if matches!(+ args.provider,+ PredictionProvider::TeacherJumps(_) | PredictionProvider::TeacherJumpsNonBatching(_)+ ) {+ if let Some(prompt_inputs) = example.prompt_inputs.as_mut() {+ hashed_regions::ensure_cursor_file_excerpt(prompt_inputs);+ }+ }+let step_progress = example_progress.start(Step::FormatPrompt);let prompt_inputs = example@@ -71,22 +89,10 @@ pub async fn run_format_prompt(provider: args.provider,});}- PredictionProvider::TeacherMultiRegion(_)- | PredictionProvider::TeacherMultiRegionNonBatching(_) => {- step_progress.set_substatus("formatting teacher multi-region prompt");+ PredictionProvider::TeacherJumps(_) | PredictionProvider::TeacherJumpsNonBatching(_) => {+ step_progress.set_substatus("formatting teacher jumps prompt");- let zeta_format = ZetaFormat::default();- let (editable_range, context_range) =- resolved_excerpt_ranges_for_format(prompt_inputs, zeta_format);-- let include_diagnostics = matches!(zeta_format, ZetaFormat::V0420Diagnostics);-- let prompt = TeacherMultiRegionPrompt::format_prompt(- example,- editable_range,- context_range,- include_diagnostics,- );+ let prompt = TeacherJumpsPrompt::format_prompt(example, args.related_files_budget)?;example.prompt = Some(ExamplePrompt {input: prompt,expected_output: None,@@ -351,36 +357,48 @@ impl TeacherPrompt {}}-pub struct TeacherMultiRegionPrompt;+/// Teacher prompt for long-range edit prediction ("jumps"). All prompt+/// context — the cursor file and every related-file excerpt — is annotated+/// with hashed region markers (V0609HashedRegions), and the teacher may+/// output a sequence of marker-bounded edits targeting any of it.+pub struct TeacherJumpsPrompt;-impl TeacherMultiRegionPrompt {+impl TeacherJumpsPrompt {pub(crate) const USER_CURSOR_MARKER: &str = "<|user_cursor|>";pub(crate) const NO_EDITS: &str = "NO_EDITS";- /// Truncate edit history to this number of last lines- const MAX_HISTORY_LINES: usize = 128;+ const MAX_HISTORY_TOKENS: usize = 4000;- pub fn format_prompt(- example: &Example,- editable_range: Range<usize>,- context_range: Range<usize>,- include_diagnostics: bool,- ) -> String {- let edit_history = Self::format_edit_history(&example.spec.edit_history);- let context = Self::format_context(example);- let cursor_excerpt = Self::format_cursor_excerpt(example, editable_range, context_range);- let diagnostics = include_diagnostics- .then(|| TeacherPrompt::format_diagnostics(example))- .map(|diagnostics| format!("# 4. Diagnostics\n\n{diagnostics}"));+ pub const DEFAULT_RELATED_FILES_BUDGET: usize = 8192;- let prompt_template = crate::prompt_assets::get_prompt("teacher_multi_region.md");+ pub fn format_prompt(example: &Example, related_files_budget: usize) -> Result<String> {+ let prompt_inputs = example+ .prompt_inputs+ .as_ref()+ .context("example is missing prompt inputs")?;+ let marker_table = hashed_regions::build_marker_table(prompt_inputs);+ let cursor = hashed_regions::locate_cursor_in_related_files(prompt_inputs).context(+ "cursor position is not covered by any related-file excerpt of the cursor file; \+ teacher-jumps requires current-file context retrieval (e.g. `ep context --type=current-file,...`)",+ )?;++ let edit_history = Self::format_edit_history(&prompt_inputs);+ let context = Self::format_context(+ prompt_inputs,+ &marker_table,+ related_files_budget,+ cursor.file_ix,+ );+ let cursor_excerpt =+ Self::format_cursor_excerpt(example, prompt_inputs, &marker_table, &cursor)?;++ let prompt_template = crate::prompt_assets::get_prompt("teacher_jumps.md");let prompt = prompt_template.replace("{{context}}", &context).replace("{{edit_history}}", &edit_history)- .replace("{{diagnostics}}", diagnostics.as_deref().unwrap_or("")).replace("{{cursor_excerpt}}", &cursor_excerpt);- prompt+ Ok(prompt)}pub fn parse(example: &Example, response: &str) -> Result<(String, Option<ActualCursor>)> {@@ -400,155 +418,238 @@ impl TeacherMultiRegionPrompt {.as_ref().context("example is missing prompt inputs")?;- let zeta_format = ZetaFormat::default();- let (editable_range, _) = resolved_excerpt_ranges_for_format(prompt_inputs, zeta_format);- let excerpt = prompt_inputs.cursor_excerpt.as_ref();- let old_editable_region = &excerpt[editable_range.clone()];- let marker_offsets = multi_region::compute_marker_offsets(old_editable_region);-- let codeblock =- extract_last_codeblock(&response).context("no codeblock found in model response")?;- let (start_num, end_num, raw_new_span) = multi_region::extract_marker_span(&codeblock)?;-- let start_idx = start_num- .checked_sub(1)- .context("marker numbers are 1-indexed")?;- let end_idx = end_num- .checked_sub(1)- .context("marker numbers are 1-indexed")?;- let start_byte = *marker_offsets- .get(start_idx)- .context("start marker number out of range")?;- let end_byte = *marker_offsets- .get(end_idx)- .context("end marker number out of range")?;-- if start_byte > end_byte {- return Err(anyhow!("start marker must come before end marker"));+ // The teacher emits reasoning plus a sequence of markdown code fences,+ // one per edit, each a marker-bounded span. Extract the spans from the+ // fences, then hand off to the shared hash-region patch assembler that+ // the student parser also uses.+ let codeblocks: Vec<String> = extract_all_codeblocks(response)+ .into_iter()+ .filter(|block| block.contains(hashed_regions::MARKER_TAG_PREFIX))+ .collect();+ if codeblocks.is_empty() {+ return Err(anyhow!(+ "no marker-bounded edit codeblocks found in model response"+ ));}- let cursor_in_span = raw_new_span.find(Self::USER_CURSOR_MARKER);- let new_span = raw_new_span.replace(Self::USER_CURSOR_MARKER, "");-- let old_span = &old_editable_region[start_byte..end_byte];- let mut new_span = new_span;- if old_span.ends_with('\n') && !new_span.ends_with('\n') && !new_span.is_empty() {- new_span.push('\n');+ let mut spans = Vec::with_capacity(codeblocks.len());+ for codeblock in &codeblocks {+ spans.push(hashed_regions::extract_marker_span(codeblock)?);}- if !old_span.ends_with('\n') && new_span.ends_with('\n') {- new_span.pop();- }-- let mut new_editable_region = String::new();- new_editable_region.push_str(&old_editable_region[..start_byte]);- new_editable_region.push_str(&new_span);- new_editable_region.push_str(&old_editable_region[end_byte..]);- let cursor_offset = cursor_in_span.map(|pos| start_byte + pos);-- if old_editable_region.starts_with('\n') && !new_editable_region.starts_with('\n') {- new_editable_region.insert(0, '\n');- }-- let editable_region_offset = editable_range.start;- let editable_region_start_line = excerpt[..editable_region_offset].matches('\n').count();-- let editable_region_lines = old_editable_region.lines().count() as u32;- let diff = language::unified_diff_with_context(- old_editable_region,- &new_editable_region,- editable_region_start_line as u32,- editable_region_start_line as u32,- editable_region_lines,- );-- let diff = indoc::formatdoc! {"- --- a/{path}- +++ b/{path}- {diff}",- path = example.spec.cursor_path.to_string_lossy(),- diff = diff,- };+ let (patch, cursor) = hashed_regions::build_patch_from_spans(+ prompt_inputs,+ &spans,+ Self::USER_CURSOR_MARKER,+ )?;- let actual_cursor = cursor_offset.map(|editable_region_cursor_offset| {+ let actual_cursor = cursor.map(|cursor| {ActualCursor::from_editable_region(- &example.spec.cursor_path,- editable_region_cursor_offset,- &new_editable_region,- excerpt,- editable_region_offset,- editable_region_start_line,+ &cursor.path,+ cursor.cursor_offset_in_new_text,+ &cursor.new_text,+ &cursor.old_text,+ 0,+ cursor.start_row as usize,)});- Ok((diff, actual_cursor))+ Ok((patch, actual_cursor))}- fn format_edit_history(edit_history: &str) -> String {- let lines: Vec<&str> = edit_history.lines().collect();-- if lines.is_empty() {- return "(No edit history)".to_string();- }-- if lines.len() > Self::MAX_HISTORY_LINES {- let truncated = lines[lines.len() - Self::MAX_HISTORY_LINES..].join("\n");- format!("{truncated}\n[...truncated...]")- } else {- lines.join("\n")- }+ fn format_edit_history(prompt_inputs: &ZetaPromptInput) -> String {+ format_edit_history_within_budget(+ &prompt_inputs.events,+ "",+ "",+ Self::MAX_HISTORY_TOKENS,+ max_edit_event_count_for_format(&ZetaFormat::V0327SingleFile),+ )}- pub fn format_context(example: &Example) -> String {- let related_files = example- .prompt_inputs- .as_ref()- .and_then(|pi| pi.related_files.as_deref());- let Some(related_files) = related_files else {+ /// Render related files with hashed region markers, within a token+ /// budget. Mirrors `zeta_prompt::format_related_files_within_budget`,+ /// but inserts marker tags into every included excerpt. The cursor file+ /// is skipped: it renders in its own prompt section via+ /// `format_cursor_excerpt`, and including it here would duplicate it.+ fn format_context(+ prompt_inputs: &ZetaPromptInput,+ marker_table: &[SnippetMarkers],+ max_tokens: usize,+ cursor_file_ix: usize,+ ) -> String {+ let Some(related_files) = prompt_inputs.related_files.as_deref() else {return "(No context)".to_string();};-if related_files.is_empty() {return "(No context)".to_string();}- let prefix = "`````";- let suffix = "`````\n\n";- let max_tokens = 1024;- zeta_prompt::format_related_files_within_budget(related_files, &prefix, &suffix, max_tokens)- }+ let estimate_tokens = |bytes: usize| bytes / 3;- fn format_cursor_excerpt(- example: &Example,- editable_range: Range<usize>,- context_range: Range<usize>,- ) -> String {- let mut result = String::new();+ struct RenderedExcerpt {+ file_ix: usize,+ excerpt_ix: usize,+ order: usize,+ rendered: String,+ }- let prompt_inputs = example.prompt_inputs.as_ref().unwrap();- let excerpt = prompt_inputs.cursor_excerpt.as_ref();- let cursor_offset = prompt_inputs.cursor_offset_in_excerpt;+ let mut candidates = Vec::new();+ for (file_ix, file) in related_files.iter().enumerate() {+ if file_ix == cursor_file_ix {+ continue;+ }+ for (excerpt_ix, excerpt) in file.excerpts.iter().enumerate() {+ let markers = marker_table.iter().find_map(|snippet| {+ (snippet.file_ix == file_ix && snippet.excerpt_ix == excerpt_ix)+ .then_some(&snippet.markers)+ });+ let mut rendered = String::new();+ match markers {+ Some(markers) => hashed_regions::write_snippet_with_markers(+ &mut rendered,+ &excerpt.text,+ markers,+ None,+ ),+ None => rendered.push_str(&excerpt.text),+ }+ if !rendered.ends_with('\n') {+ rendered.push('\n');+ }+ candidates.push(RenderedExcerpt {+ file_ix,+ excerpt_ix,+ order: excerpt.order,+ rendered,+ });+ }+ }- let editable_text = &excerpt[editable_range.clone()];- let cursor_in_editable = cursor_offset - editable_range.start;+ let file_headers: Vec<String> = related_files+ .iter()+ .map(|file| format!("`````{}\n", file.path.to_string_lossy()))+ .collect();+ let file_suffix = "`````\n\n";- let path_str = example.spec.cursor_path.to_string_lossy();- result.push_str(&format!("`````{path_str}\n"));+ let mut selection_order: Vec<usize> = (0..candidates.len()).collect();+ selection_order.sort_by_key(|&candidate_ix| {+ let candidate = &candidates[candidate_ix];+ (candidate.order, candidate.file_ix, candidate.excerpt_ix)+ });- result.push_str(&excerpt[context_range.start..editable_range.start]);+ let mut total_tokens = 0;+ let mut included = vec![false; candidates.len()];+ let mut file_included = vec![false; related_files.len()];+ for &candidate_ix in &selection_order {+ let candidate = &candidates[candidate_ix];+ let header_cost = if file_included[candidate.file_ix] {+ 0+ } else {+ estimate_tokens(file_headers[candidate.file_ix].len() + file_suffix.len())+ };+ let excerpt_cost = estimate_tokens(candidate.rendered.len());+ if total_tokens + header_cost + excerpt_cost > max_tokens {+ break;+ }+ total_tokens += header_cost + excerpt_cost;+ file_included[candidate.file_ix] = true;+ included[candidate_ix] = true;+ }- multi_region::write_editable_with_markers(- &mut result,- editable_text,- cursor_in_editable,- Self::USER_CURSOR_MARKER,- );+ let mut result = String::new();+ let mut last_file_ix = None;+ for (candidate_ix, candidate) in candidates.iter().enumerate() {+ if !included[candidate_ix] {+ continue;+ }+ if last_file_ix != Some(candidate.file_ix) {+ if last_file_ix.is_some() {+ result.push_str(file_suffix);+ }+ result.push_str(&file_headers[candidate.file_ix]);+ last_file_ix = Some(candidate.file_ix);+ }+ result.push_str(&candidate.rendered);++ let file = &related_files[candidate.file_ix];+ let excerpt = &file.excerpts[candidate.excerpt_ix];+ let next_excerpt_start = candidates+ .iter()+ .enumerate()+ .skip(candidate_ix + 1)+ .find(|(next_ix, next)| included[*next_ix] && next.file_ix == candidate.file_ix)+ .map(|(_, next)| file.excerpts[next.excerpt_ix].row_range.start);+ if zeta_prompt::rows_omitted_after_excerpt(excerpt, next_excerpt_start, file.max_row) {+ result.push_str("...\n");+ }+ }+ if last_file_ix.is_some() {+ result.push_str(file_suffix);+ }- result.push_str(&excerpt[editable_range.end..context_range.end]);- result.push_str("\n`````");+ if result.is_empty() {+ "(No context)".to_string()+ } else {+ result+ }+ }- result+ /// Render the current file from its related-file entry, with marker tags+ /// and the user cursor injected. The current file gets its own prompt+ /// section but shares the related-file snippets and markers, so its+ /// content appears in the prompt exactly once.+ fn format_cursor_excerpt(+ example: &Example,+ prompt_inputs: &ZetaPromptInput,+ marker_table: &[SnippetMarkers],+ cursor: &hashed_regions::RelatedFileCursor,+ ) -> Result<String> {+ let related_files = prompt_inputs+ .related_files+ .as_deref()+ .context("prompt inputs are missing related files")?;+ let file = related_files+ .get(cursor.file_ix)+ .context("cursor file index out of range")?;++ let path_str = example.spec.cursor_path.to_string_lossy();+ let mut result = format!("`````{path_str}\n");+ for (excerpt_ix, excerpt) in file.excerpts.iter().enumerate() {+ let markers = marker_table+ .iter()+ .find_map(|snippet| {+ (snippet.file_ix == cursor.file_ix && snippet.excerpt_ix == excerpt_ix)+ .then_some(&snippet.markers)+ })+ .context("marker table is missing a cursor file snippet")?;+ let cursor_in_excerpt = (excerpt_ix == cursor.excerpt_ix)+ .then_some((cursor.offset_in_excerpt, Self::USER_CURSOR_MARKER));+ hashed_regions::write_snippet_with_markers(+ &mut result,+ &excerpt.text,+ markers,+ cursor_in_excerpt,+ );+ if !result.ends_with('\n') {+ result.push('\n');+ }+ if excerpt.row_range.end < file.max_row {+ result.push_str("...\n");+ }+ }+ result.push_str("`````");++ Ok(result)+ }+}++pub(crate) fn line_start_offset(text: &str, row: usize) -> Option<usize> {+ let mut offset = 0;+ for _ in 0..row {+ offset += text[offset..].find('\n')? + 1;}+ Some(offset)}/// Extract the cursor excerpt from an example.@@ -597,6 +698,42 @@ pub fn extract_cursor_excerpt_from_example(example: &Example) -> Option<String>Some(result)}+/// Extract all top-level fenced codeblocks from `text`, in order.+///+/// A fence opens with 3+ backticks (optionally followed by an info string)+/// and closes with a line of at least as many backticks, so codeblocks that+/// themselves contain shorter fences are handled.+pub(crate) fn extract_all_codeblocks(text: &str) -> Vec<String> {+ let mut codeblocks = Vec::new();+ let mut current_block: Option<(usize, Vec<&str>)> = None;++ for line in text.lines() {+ match &mut current_block {+ None => {+ let backtick_count = line.chars().take_while(|&c| c == '`').count();+ if backtick_count >= 3 {+ current_block = Some((backtick_count, Vec::new()));+ }+ }+ Some((opening_count, lines)) => {+ let trimmed = line.trim();+ if trimmed.len() >= *opening_count && trimmed.chars().all(|c| c == '`') {+ let mut content = lines.join("\n");+ if !content.is_empty() {+ content.push('\n');+ }+ codeblocks.push(content);+ current_block = None;+ } else {+ lines.push(line);+ }+ }+ }+ }++ codeblocks+}+pub(crate) fn extract_last_codeblock(text: &str) -> Option<String> {let lines: Vec<&str> = text.lines().collect();@@ -645,6 +782,571 @@ pub(crate) fn extract_last_codeblock(text: &str) -> Option<String> {#[cfg(test)]mod tests {use super::*;+ use zeta_prompt::multi_region;++ fn make_example(+ cursor_excerpt: &str,+ cursor_offset: usize,+ related: &[(&str, &[(&str, u32)])],+ ) -> Example {+ // The cursor file is included as the first related file, mirroring+ // `ContextSource::CurrentFile` context retrieval.+ let cursor_file = zeta_prompt::RelatedFile {+ path: std::sync::Arc::from(std::path::Path::new("src/main.rs")),+ max_row: 1000,+ excerpts: vec![zeta_prompt::RelatedExcerpt {+ row_range: 0..cursor_excerpt.matches('\n').count() as u32,+ text: std::sync::Arc::from(cursor_excerpt),+ order: 0,+ context_source: zeta_prompt::ContextSource::CurrentFile,+ }],+ in_open_source_repo: false,+ };+ let related_files = std::iter::once(cursor_file)+ .chain(related.iter().map(|(path, excerpts)| {+ zeta_prompt::RelatedFile {+ path: std::sync::Arc::from(std::path::Path::new(path)),+ max_row: 1000,+ excerpts: excerpts+ .iter()+ .map(|(text, start_row)| zeta_prompt::RelatedExcerpt {+ row_range: *start_row..*start_row + text.matches('\n').count() as u32,+ text: std::sync::Arc::from(*text),+ order: 0,+ context_source: zeta_prompt::ContextSource::CurrentFile,+ })+ .collect(),+ in_open_source_repo: false,+ }+ }))+ .collect();++ Example {+ spec: edit_prediction::example_spec::ExampleSpec {+ name: "test".to_string(),+ repository_url: "https://github.com/zed-industries/zed.git".to_string(),+ revision: "HEAD".to_string(),+ tags: Vec::new(),+ reasoning: None,+ uncommitted_diff: String::new(),+ recently_opened_files: Vec::new(),+ recently_viewed_files: Vec::new(),+ uncommitted_diff_contains_edit_history: false,+ cursor_path: std::sync::Arc::from(std::path::Path::new("src/main.rs")),+ cursor_position: "0:0".to_string(),+ edit_history: String::new(),+ expected_patches: Vec::new(),+ rejected_patch: None,+ telemetry: None,+ human_feedback: Vec::new(),+ rating: None,+ },+ prompt_inputs: Some(zeta_prompt::ZetaPromptInput {+ cursor_path: std::path::Path::new("src/main.rs").into(),+ cursor_excerpt: cursor_excerpt.into(),+ cursor_offset_in_excerpt: cursor_offset,+ excerpt_start_row: Some(0),+ events: Vec::new(),+ related_files: Some(related_files),+ active_buffer_diagnostics: Vec::new(),+ excerpt_ranges: zeta_prompt::ExcerptRanges::default(),+ syntax_ranges: None,+ in_open_source_repo: false,+ can_collect_data: false,+ repo_url: None,+ }),+ prompt: None,+ predictions: Vec::new(),+ score: Vec::new(),+ qa: Vec::new(),+ zed_version: None,+ state: None,+ }+ }++ #[test]+ fn test_teacher_jumps_format_prompt_markers_everywhere() {+ let example = make_example(+ "fn main() {\n let x = 1;\n}\n",+ 16,+ &[("src/lib.rs", &[("pub fn helper() {}\n", 5)])],+ );+ let prompt = TeacherJumpsPrompt::format_prompt(&example, 8192).unwrap();++ assert!(prompt.contains(TeacherJumpsPrompt::USER_CURSOR_MARKER));+ assert!(prompt.contains("`````src/main.rs\n"));+ assert!(prompt.contains("`````src/lib.rs\n"));+ // Markers in both the current file and the related excerpt.+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ for snippet in &marker_table {+ for (id, _) in &snippet.markers {+ assert!(+ prompt.contains(&hashed_regions::marker_tag(id)),+ "prompt is missing marker {id}"+ );+ }+ }+ // The current file appears exactly once, in its own section, with the+ // user cursor injected.+ assert_eq!(prompt.matches("let x = 1;").count(), 1);+ assert!(prompt.contains("<|user_cursor|>let x = 1;"));+ }++ #[test]+ fn test_teacher_jumps_cursor_file_with_coinciding_worktree_root_name() {+ // Worktree root `jaq` contains a `jaq/` subdirectory: the cursor path+ // is `jaq/src/main.rs` while the related-file entry is prefixed with+ // the root name (`jaq/jaq/src/main.rs`).+ let mut example = make_example("fn main() {\n let x = 1;\n}\n", 16, &[]);+ example.spec.cursor_path = std::sync::Arc::from(std::path::Path::new("jaq/src/main.rs"));+ {+ let prompt_inputs = example.prompt_inputs.as_mut().unwrap();+ prompt_inputs.cursor_path = std::path::Path::new("jaq/src/main.rs").into();+ prompt_inputs.related_files.as_mut().unwrap()[0].path =+ std::sync::Arc::from(std::path::Path::new("jaq/jaq/src/main.rs"));+ }++ let prompt = TeacherJumpsPrompt::format_prompt(&example, 8192).unwrap();+ assert!(prompt.contains("<|user_cursor|>let x = 1;"));++ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ let cursor_markers = &marker_table[0].markers;+ let start_tag = hashed_regions::marker_tag(&cursor_markers[0].0);+ let end_tag = hashed_regions::marker_tag(&cursor_markers[cursor_markers.len() - 1].0);+ let response =+ format!("`````\n{start_tag}\nfn main() {{\n let x = 2;\n}}\n{end_tag}\n`````\n");+ let (patch, _) = TeacherJumpsPrompt::parse(&example, &response).unwrap();+ assert!(patch.contains("--- a/jaq/src/main.rs"), "patch: {patch}");+ }++ #[test]+ fn test_teacher_jumps_format_prompt_requires_current_file_context() {+ let mut example = make_example("fn main() {}\n", 0, &[]);+ example.prompt_inputs.as_mut().unwrap().related_files = Some(Vec::new());+ assert!(TeacherJumpsPrompt::format_prompt(&example, 8192).is_err());+ }++ #[test]+ fn test_teacher_jumps_synthesizes_missing_cursor_file_excerpt() {+ // Simulate a settled-data sample: `cursor_excerpt` is present, but the+ // related-file excerpts of the cursor file don't cover the cursor (only+ // an unrelated fragment elsewhere in the file).+ let mut example = make_example("fn main() {\n let x = 1;\n}\n", 16, &[]);+ {+ let prompt_inputs = example.prompt_inputs.as_mut().unwrap();+ prompt_inputs.related_files.as_mut().unwrap()[0].excerpts =+ vec![zeta_prompt::RelatedExcerpt {+ row_range: 40..42,+ text: std::sync::Arc::from("// unrelated\n// fragment\n"),+ order: 0,+ context_source: zeta_prompt::ContextSource::Bm25,+ }];+ }++ // Without a covering cursor-file excerpt, formatting hard-errors.+ assert!(TeacherJumpsPrompt::format_prompt(&example, 8192).is_err());++ // `ensure_cursor_file_excerpt` (in zeta_prompt) synthesizes one from+ // `cursor_excerpt`, so the prompt formats with the cursor in the+ // current-file window and the unrelated fragment is replaced (no+ // duplicated content with overlapping markers).+ hashed_regions::ensure_cursor_file_excerpt(example.prompt_inputs.as_mut().unwrap());+ let prompt = TeacherJumpsPrompt::format_prompt(&example, 8192).unwrap();+ assert!(prompt.contains("<|user_cursor|>let x = 1;"));+ assert!(!prompt.contains("// unrelated"));+ assert_eq!(prompt.matches("let x = 1;").count(), 1);+ }++ #[test]+ fn test_teacher_jumps_cursor_file_hunks_are_file_absolute() {+ let mut example = make_example("fn main() {\n let x = 1;\n}\n", 16, &[]);+ {+ let prompt_inputs = example.prompt_inputs.as_mut().unwrap();+ prompt_inputs.excerpt_start_row = Some(10);+ prompt_inputs.related_files.as_mut().unwrap()[0].excerpts[0].row_range = 10..13;+ }+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ let cursor_markers = &marker_table[0].markers;+ let start_tag = hashed_regions::marker_tag(&cursor_markers[0].0);+ let end_tag = hashed_regions::marker_tag(&cursor_markers[cursor_markers.len() - 1].0);++ let response = format!(+ "The user is changing x.\n\n`````\n{start_tag}\nfn main() {{\n let x = 2;<|user_cursor|>\n}}\n{end_tag}\n`````\n"+ );+ let (patch, cursor) = TeacherJumpsPrompt::parse(&example, &response).unwrap();++ // Hunk rows are file-absolute (1-based in the hunk header, excerpt+ // starts at 0-based row 10).+ assert!(patch.contains("@@ -11,"), "patch: {patch}");+ let cursor = cursor.unwrap();+ assert_eq!(cursor.row, 11);+ }++ #[test]+ fn test_teacher_jumps_parse_single_edit_in_cursor_file() {+ let example = make_example("fn main() {\n let x = 1;\n}\n", 16, &[]);+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ let cursor_markers = &marker_table[0].markers;+ let start_tag = hashed_regions::marker_tag(&cursor_markers[0].0);+ let end_tag = hashed_regions::marker_tag(&cursor_markers[cursor_markers.len() - 1].0);++ let response = format!(+ "The user is changing x.\n\n`````\n{start_tag}\nfn main() {{\n let x = 2;<|user_cursor|>\n}}\n{end_tag}\n`````\n"+ );+ let (patch, cursor) = TeacherJumpsPrompt::parse(&example, &response).unwrap();++ assert!(patch.contains("--- a/src/main.rs"), "patch: {patch}");+ assert!(patch.contains("- let x = 1;"), "patch: {patch}");+ assert!(patch.contains("+ let x = 2;"), "patch: {patch}");+ let cursor = cursor.unwrap();+ assert_eq!(cursor.path, "src/main.rs");+ assert_eq!(cursor.row, 1);+ }++ #[test]+ fn test_teacher_jumps_parse_sequence_across_files() {+ let example = make_example(+ "fn fetch_user_cached() {}\n",+ 0,+ &[(+ "src/server.rs",+ &[("fn handle() {\n fetch_user();\n}\n", 10)],+ )],+ );+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ assert_eq!(marker_table.len(), 2);+ let related_markers = &marker_table[1].markers;+ let start_tag = hashed_regions::marker_tag(&related_markers[0].0);+ let end_tag = hashed_regions::marker_tag(&related_markers[related_markers.len() - 1].0);++ let response = format!(+ "Updating the call site to use the new name.\n\n\+ `````\n{start_tag}\nfn handle() {{\n fetch_user_cached();\n}}\n{end_tag}\n`````\n"+ );+ let (patch, cursor) = TeacherJumpsPrompt::parse(&example, &response).unwrap();++ assert!(patch.contains("--- a/src/server.rs"), "patch: {patch}");+ assert!(patch.contains("- fetch_user();"), "patch: {patch}");+ assert!(+ patch.contains("+ fetch_user_cached();"),+ "patch: {patch}"+ );+ // Hunk rows are file-absolute for related files (1-based in the+ // hunk header, excerpt starts at 0-based row 10).+ assert!(patch.contains("@@ -11,"), "patch: {patch}");+ assert!(cursor.is_none());+ }++ #[test]+ fn test_teacher_jumps_parse_multiple_edits_same_file() {+ let cursor_excerpt = "\+ fn alpha() {\n one();\n}\n\nfn beta() {\n two();\n}\n\n\+ fn gamma() {\n three();\n}\n\nfn delta() {\n four();\n}\n";+ let example = make_example(cursor_excerpt, 0, &[]);+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ let markers = &marker_table[0].markers;+ assert!(+ markers.len() >= 3,+ "expected internal markers, got {markers:?}"+ );++ // First edit: between the first two markers; second edit: between the+ // second and last markers.+ let tag = |ix: usize| hashed_regions::marker_tag(&markers[ix].0);+ let old_first_span = &cursor_excerpt[markers[0].1..markers[1].1];+ let old_second_span = &cursor_excerpt[markers[1].1..markers[markers.len() - 1].1];+ let new_first_span = old_first_span.replace("one()", "uno()");+ let new_second_span = old_second_span.replace("four()", "cuatro()");++ let response = format!(+ "Renaming calls.\n\n`````\n{}\n{}{}\n`````\n\n`````\n{}\n{}{}\n`````\n",+ tag(0),+ new_first_span,+ tag(1),+ tag(1),+ new_second_span,+ tag(markers.len() - 1),+ );+ let (patch, _) = TeacherJumpsPrompt::parse(&example, &response).unwrap();++ assert!(patch.contains("+ uno();"), "patch: {patch}");+ assert!(patch.contains("+ cuatro();"), "patch: {patch}");+ assert_eq!(patch.matches("--- a/src/main.rs").count(), 1);+ }++ #[test]+ fn test_teacher_jumps_parse_no_edits() {+ let example = make_example("fn main() {}\n", 0, &[]);+ let (patch, cursor) =+ TeacherJumpsPrompt::parse(&example, "All good.\n\n`````\nNO_EDITS\n`````\n").unwrap();+ assert!(patch.is_empty());+ assert!(cursor.is_none());+ }++ #[test]+ fn test_teacher_jumps_parse_rejects_truncated_span() {+ let cursor_excerpt = "\+ fn alpha() {\n one();\n}\n\nfn beta() {\n two();\n}\n\n\+ fn gamma() {\n three();\n}\n\nfn delta() {\n four();\n}\n";+ let example = make_example(cursor_excerpt, 0, &[]);+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ let markers = &marker_table[0].markers;+ assert!(markers.len() >= 3);+ let start_tag = hashed_regions::marker_tag(&markers[0].0);+ let end_tag = hashed_regions::marker_tag(&markers[markers.len() - 1].0);++ // The model reproduces only the head of the span and stops before the+ // end marker; accepting this would silently delete the rest.+ let head = &cursor_excerpt[markers[0].1..markers[1].1];+ let response = format!("Minor cleanup.\n\n`````\n{start_tag}\n{head}{end_tag}\n`````\n");+ let error = TeacherJumpsPrompt::parse(&example, &response).unwrap_err();+ assert!(+ error.to_string().contains("looks truncated"),+ "unexpected error: {error}"+ );+ }++ #[test]+ fn test_teacher_jumps_parse_rejects_tail_deletion_after_head_edit() {+ let cursor_excerpt = "\+ fn alpha() {\n one();\n}\n\nfn beta() {\n two();\n}\n\n\+ fn gamma() {\n three();\n}\n\nfn delta() {\n four();\n}\n";+ let example = make_example(cursor_excerpt, 0, &[]);+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ let markers = &marker_table[0].markers;+ assert!(markers.len() >= 3);+ let start_tag = hashed_regions::marker_tag(&markers[0].0);+ let end_tag = hashed_regions::marker_tag(&markers[markers.len() - 1].0);++ // The dropped tail is large enough to trip the trailing-deletion check.+ let tail = &cursor_excerpt[markers[1].1..];+ assert!(tail.lines().filter(|line| !line.trim().is_empty()).count() > 3);++ // The model makes a real edit at the head of the span, reproduces+ // some context, and then stops before the end marker. The replacement+ // is not a verbatim prefix of the span, but the tail is still+ // silently deleted.+ let head = &cursor_excerpt[markers[0].1..markers[1].1];+ assert!(head.contains("fn alpha()"));+ let edited_head = head.replacen("fn alpha()", "fn alpha_renamed()", 1);+ let response =+ format!("Renaming alpha.\n\n`````\n{start_tag}\n{edited_head}{end_tag}\n`````\n");+ let error = TeacherJumpsPrompt::parse(&example, &response).unwrap_err();+ assert!(+ error.to_string().contains("looks truncated"),+ "unexpected error: {error}"+ );+ }++ #[test]+ fn test_teacher_jumps_parse_allows_mid_span_deletion() {+ let cursor_excerpt = "\+ fn alpha() {\n one();\n}\n\nfn beta() {\n two();\n}\n\n\+ fn gamma() {\n three();\n}\n\nfn delta() {\n four();\n}\n";+ let example = make_example(cursor_excerpt, 0, &[]);+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ let markers = &marker_table[0].markers;+ assert!(markers.len() >= 3);+ let start_tag = hashed_regions::marker_tag(&markers[0].0);+ let end_tag = hashed_regions::marker_tag(&markers[markers.len() - 1].0);++ // Deleting code in the middle while reproducing the span's tail shows+ // the model kept writing to the end marker, so it must be accepted.+ let head = &cursor_excerpt[markers[0].1..markers[1].1];+ let reproduced_tail = &cursor_excerpt[markers[markers.len() - 2].1..];+ assert!(!reproduced_tail.trim().is_empty());+ let response = format!(+ "Removing the middle.\n\n`````\n{start_tag}\n{head}{reproduced_tail}{end_tag}\n`````\n"+ );+ let (patch, _) = TeacherJumpsPrompt::parse(&example, &response).unwrap();+ assert!(patch.contains("-fn beta() {"), "patch: {patch}");+ }++ #[test]+ fn test_teacher_jumps_parse_allows_small_tail_deletion() {+ let cursor_excerpt = "\+ fn alpha() {\n one();\n}\n\nfn beta() {\n two();\n}\n\n\+ fn gamma() {\n three();\n}\n\nfn delta() {\n four();\n}\n";+ let example = make_example(cursor_excerpt, 0, &[]);+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ let markers = &marker_table[0].markers;+ let start_tag = hashed_regions::marker_tag(&markers[0].0);+ let end_tag = hashed_regions::marker_tag(&markers[markers.len() - 1].0);++ // Dropping only the last line of the span may be a genuine+ // end-of-snippet deletion, so it stays below the threshold.+ let new_span = cursor_excerpt.strip_suffix("}\n").unwrap();+ let response =+ format!("Dropping the brace.\n\n`````\n{start_tag}\n{new_span}{end_tag}\n`````\n");+ let (patch, _) = TeacherJumpsPrompt::parse(&example, &response).unwrap();+ assert!(patch.contains("-}"), "patch: {patch}");+ }++ #[test]+ fn test_teacher_jumps_parse_allows_empty_span_deletion() {+ let cursor_excerpt = "\+ fn alpha() {\n one();\n}\n\nfn beta() {\n two();\n}\n\n\+ fn gamma() {\n three();\n}\n\nfn delta() {\n four();\n}\n";+ let example = make_example(cursor_excerpt, 0, &[]);+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ let markers = &marker_table[0].markers;+ assert!(markers.len() >= 3);+ let start_tag = hashed_regions::marker_tag(&markers[0].0);+ let end_tag = hashed_regions::marker_tag(&markers[1].0);++ // Deleting an entire span by replacing it with nothing is fine.+ let response = format!("Removing alpha.\n\n`````\n{start_tag}\n{end_tag}\n`````\n");+ let (patch, _) = TeacherJumpsPrompt::parse(&example, &response).unwrap();+ assert!(patch.contains("-fn alpha() {"), "patch: {patch}");+ }++ #[test]+ fn test_teacher_jumps_parse_span_across_contiguous_excerpts() {+ // Two excerpts of src/lib.rs with touching row ranges (5..8 and+ // 8..11) render seamlessly in the prompt, so the model may span a+ // single edit across the excerpt boundary.+ let example = make_example(+ "fn main() {}\n",+ 0,+ &[(+ "src/lib.rs",+ &[+ ("fn a() {\n one();\n}\n", 5),+ ("fn b() {\n two();\n}\n", 8),+ ],+ )],+ );+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ assert_eq!(marker_table.len(), 3);+ let start_tag = hashed_regions::marker_tag(&marker_table[1].markers[0].0);+ let last_markers = &marker_table[2].markers;+ let end_tag = hashed_regions::marker_tag(&last_markers[last_markers.len() - 1].0);++ let response = format!(+ "Renaming both.\n\n`````\n{start_tag}\nfn a() {{\n uno();\n}}\nfn b() {{\n dos();\n}}\n{end_tag}\n`````\n"+ );+ let (patch, _) = TeacherJumpsPrompt::parse(&example, &response).unwrap();++ assert!(patch.contains("+ uno();"), "patch: {patch}");+ assert!(patch.contains("+ dos();"), "patch: {patch}");+ assert_eq!(patch.matches("--- a/src/lib.rs").count(), 1);+ // Hunk rows are file-absolute (merged region starts at 0-based row 5).+ assert!(patch.contains("@@ -6,"), "patch: {patch}");+ }++ #[test]+ fn test_teacher_jumps_parse_insertion_at_contiguous_excerpt_seam() {+ // The two markers at the seam between contiguous excerpts map to the+ // same merged offset; bracketing them expresses a pure insertion.+ let example = make_example(+ "fn main() {}\n",+ 0,+ &[(+ "src/lib.rs",+ &[+ ("fn a() {\n one();\n}\n", 5),+ ("fn b() {\n two();\n}\n", 8),+ ],+ )],+ );+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ assert_eq!(marker_table.len(), 3);+ let first_markers = &marker_table[1].markers;+ let start_tag = hashed_regions::marker_tag(&first_markers[first_markers.len() - 1].0);+ let end_tag = hashed_regions::marker_tag(&marker_table[2].markers[0].0);++ let response = format!(+ "Adding a function between a and b.\n\n`````\n{start_tag}\nfn between() {{}}\n{end_tag}\n`````\n"+ );+ let (patch, _) = TeacherJumpsPrompt::parse(&example, &response).unwrap();++ assert!(patch.contains("+fn between() {}"), "patch: {patch}");+ assert!(+ !patch.contains("-\n"),+ "patch should be pure insertion: {patch}"+ );+ // Insertion lands between the excerpts (after 0-based row 7).+ assert!(patch.contains("@@ -6,"), "patch: {patch}");+ }++ #[test]+ fn test_teacher_jumps_parse_rejects_span_across_gapped_excerpts() {+ // Same file, but the excerpts don't touch (5..8 and 20..23): rows in+ // between were never shown to the model, so a span across them is+ // invalid.+ let example = make_example(+ "fn main() {}\n",+ 0,+ &[(+ "src/lib.rs",+ &[+ ("fn a() {\n one();\n}\n", 5),+ ("fn b() {\n two();\n}\n", 20),+ ],+ )],+ );+ let marker_table =+ hashed_regions::build_marker_table(example.prompt_inputs.as_ref().unwrap());+ assert_eq!(marker_table.len(), 3);+ let start_tag = hashed_regions::marker_tag(&marker_table[1].markers[0].0);+ let last_markers = &marker_table[2].markers;+ let end_tag = hashed_regions::marker_tag(&last_markers[last_markers.len() - 1].0);++ let response = format!(+ "Renaming both.\n\n`````\n{start_tag}\nfn a() {{\n uno();\n}}\nfn b() {{\n dos();\n}}\n{end_tag}\n`````\n"+ );+ let error = TeacherJumpsPrompt::parse(&example, &response).unwrap_err();+ assert!(+ error.to_string().contains("different context snippets"),+ "unexpected error: {error}"+ );+ }++ #[test]+ fn test_teacher_jumps_parse_rejects_unknown_marker() {+ let example = make_example("fn main() {}\n", 0, &[]);+ let response = "`````\n<|marker_zzzz|>\nnew\n<|marker_yyyy|>\n`````\n";+ assert!(TeacherJumpsPrompt::parse(&example, response).is_err());+ }++ #[test]+ fn test_extract_all_codeblocks_multiple() {+ let text = indoc::indoc! {"+ First edit:++ `````+ block one+ `````++ Second edit:++ `````+ block two+ with ``` nested+ `````+ "};+ let blocks = extract_all_codeblocks(text);+ assert_eq!(+ blocks,+ vec![+ "block one\n".to_string(),+ "block two\nwith ``` nested\n".to_string()+ ]+ );+ }#[test]fn test_extract_last_code_block() {diff --git a/crates/edit_prediction_cli/src/main.rs b/crates/edit_prediction_cli/src/main.rsindex 78f8324432d..a449b8d4be9 100644--- a/crates/edit_prediction_cli/src/main.rs+++ b/crates/edit_prediction_cli/src/main.rs@@ -342,6 +342,9 @@ struct ReadArgs {}struct FormatPromptArgs {#[clap(long, short('p'), default_value_t = PredictionProvider::default())]provider: PredictionProvider,+ /// Token budget for related-file context in teacher-jumps prompts.+ #[clap(long, default_value_t = format_prompt::TeacherJumpsPrompt::DEFAULT_RELATED_FILES_BUDGET)]+ related_files_budget: usize,}#[derive(Debug, Args, Clone)]@@ -396,6 +399,8 @@ pub enum TeacherBackend {#[default]Sonnet45,Gpt52,+ Gpt54,+ Gpt55,}impl std::fmt::Display for TeacherBackend {@@ -404,6 +409,8 @@ impl std::fmt::Display for TeacherBackend {TeacherBackend::Sonnet46 => write!(f, "sonnet46"),TeacherBackend::Sonnet45 => write!(f, "sonnet45"),TeacherBackend::Gpt52 => write!(f, "gpt52"),+ TeacherBackend::Gpt54 => write!(f, "gpt54"),+ TeacherBackend::Gpt55 => write!(f, "gpt55"),}}}@@ -415,10 +422,12 @@ impl std::str::FromStr for TeacherBackend {match s.to_lowercase().as_str() {"sonnet45" | "sonnet" | "claude" => Ok(TeacherBackend::Sonnet45),"sonnet46" => Ok(TeacherBackend::Sonnet46),- "gpt52" | "gpt" | "openai" => Ok(TeacherBackend::Gpt52),+ "gpt52" => Ok(TeacherBackend::Gpt52),+ "gpt54" | "gpt" | "openai" => Ok(TeacherBackend::Gpt54),+ "gpt55" => Ok(TeacherBackend::Gpt55),"v0114180editableregion" => Ok(TeacherBackend::Sonnet45),_ => anyhow::bail!(- "unknown teacher backend `{s}`. Valid options: sonnet45, sonnet46, gpt52"+ "unknown teacher backend `{s}`. Valid options: sonnet45, sonnet46, gpt52, gpt54, gpt55"),}}@@ -430,6 +439,8 @@ impl TeacherBackend {TeacherBackend::Sonnet45 => "claude-sonnet-4-5",TeacherBackend::Sonnet46 => "claude-sonnet-4-6",TeacherBackend::Gpt52 => "gpt-5.2",+ TeacherBackend::Gpt54 => "gpt-5.4",+ TeacherBackend::Gpt55 => "gpt-5.5",}}}@@ -441,9 +452,9 @@ enum PredictionProvider {Zeta2(ZetaFormat),Baseten(ZetaFormat),Teacher(TeacherBackend, ZetaFormat),- TeacherMultiRegion(TeacherBackend),+ TeacherJumps(TeacherBackend),TeacherNonBatching(TeacherBackend, ZetaFormat),- TeacherMultiRegionNonBatching(TeacherBackend),+ TeacherJumpsNonBatching(TeacherBackend),Repair,}@@ -463,14 +474,14 @@ impl std::fmt::Display for PredictionProvider {PredictionProvider::Teacher(backend, format) => {write!(f, "teacher:{backend}:{format:?}")}- PredictionProvider::TeacherMultiRegion(backend) => {- write!(f, "teacher-multi-region:{backend}")+ PredictionProvider::TeacherJumps(backend) => {+ write!(f, "teacher-jumps:{backend}")}PredictionProvider::TeacherNonBatching(backend, format) => {write!(f, "teacher-non-batching:{backend}:{format:?}")}- PredictionProvider::TeacherMultiRegionNonBatching(backend) => {- write!(f, "teacher-multi-region-non-batching:{backend}")+ PredictionProvider::TeacherJumpsNonBatching(backend) => {+ write!(f, "teacher-jumps-non-batching:{backend}")}PredictionProvider::Repair => write!(f, "repair"),}@@ -499,19 +510,19 @@ impl std::str::FromStr for PredictionProvider {let (backend, format) = parse_teacher_args(arg)?;Ok(PredictionProvider::TeacherNonBatching(backend, format))}- "teacher-multi-region" | "teacher_multi_region" => {+ "teacher-jumps" | "teacher_jumps" => {let backend = arg.map(|a| a.parse()).transpose()?.unwrap_or(TeacherBackend::default());- Ok(PredictionProvider::TeacherMultiRegion(backend))+ Ok(PredictionProvider::TeacherJumps(backend))}- "teacher-multi-region-non-batching" | "teacher_multi_region_non_batching" => {+ "teacher-jumps-non-batching" | "teacher_jumps_non_batching" => {let backend = arg.map(|a| a.parse()).transpose()?.unwrap_or(TeacherBackend::default());- Ok(PredictionProvider::TeacherMultiRegionNonBatching(backend))+ Ok(PredictionProvider::TeacherJumpsNonBatching(backend))}"repair" => Ok(PredictionProvider::Repair),"baseten" => {@@ -523,9 +534,9 @@ impl std::str::FromStr for PredictionProvider {}_ => {anyhow::bail!(- "unknown provider `{provider}`. Valid options: mercury, zeta1, zeta2, zeta2:<version>, teacher, teacher:<backend>, teacher-multi-region, teacher-multi-region:<backend>, teacher-non-batching, teacher-multi-region-non-batching, repair\n\+ "unknown provider `{provider}`. Valid options: mercury, zeta1, zeta2, zeta2:<version>, teacher, teacher:<backend>, teacher-jumps, teacher-jumps:<backend>, teacher-non-batching, teacher-jumps-non-batching, repair\n\For zeta2, you can optionally specify a version like `zeta2:ordered` or `zeta2:V0113_Ordered`.\n\- For teacher providers, you can specify a backend like `teacher:sonnet46`, `teacher-multi-region:sonnet46`, `teacher-multi-region-non-batching:sonnet46`, or `teacher:gpt52`.\n\+ For teacher providers, you can specify a backend like `teacher:sonnet46`, `teacher-jumps:sonnet46`, `teacher-jumps-non-batching:sonnet46`, or `teacher:gpt52`.\n\Available zeta versions:\n{}",ZetaFormat::options_as_string())@@ -614,32 +625,23 @@ mod tests {use super::*;#[test]- fn prediction_provider_multi_region_non_batched_round_trips_to_primary_spelling() {- let provider: PredictionProvider = "teacher-multi-region-non-batching:sonnet46"- .parse()- .unwrap();+ fn prediction_provider_jumps_non_batched_round_trips_to_primary_spelling() {+ let provider: PredictionProvider = "teacher-jumps-non-batching:sonnet46".parse().unwrap();assert_eq!(provider,- PredictionProvider::TeacherMultiRegionNonBatching(TeacherBackend::Sonnet46)- );- assert_eq!(- provider.to_string(),- "teacher-multi-region-non-batching:sonnet46"+ PredictionProvider::TeacherJumpsNonBatching(TeacherBackend::Sonnet46));+ assert_eq!(provider.to_string(), "teacher-jumps-non-batching:sonnet46");}#[test]- fn prediction_provider_multi_region_non_batched_alias_round_trips_to_primary_spelling() {- let provider: PredictionProvider =- "teacher_multi_region_non_batching:gpt52".parse().unwrap();+ fn prediction_provider_jumps_non_batched_alias_round_trips_to_primary_spelling() {+ let provider: PredictionProvider = "teacher_jumps_non_batching:gpt52".parse().unwrap();assert_eq!(provider,- PredictionProvider::TeacherMultiRegionNonBatching(TeacherBackend::Gpt52)- );- assert_eq!(- provider.to_string(),- "teacher-multi-region-non-batching:gpt52"+ PredictionProvider::TeacherJumpsNonBatching(TeacherBackend::Gpt52));+ assert_eq!(provider.to_string(), "teacher-jumps-non-batching:gpt52");}}diff --git a/crates/edit_prediction_cli/src/parse_output.rs b/crates/edit_prediction_cli/src/parse_output.rsindex c8e0fa7568c..c6ce8094653 100644--- a/crates/edit_prediction_cli/src/parse_output.rs+++ b/crates/edit_prediction_cli/src/parse_output.rs@@ -1,11 +1,13 @@use crate::{PredictionProvider,example::{ActualCursor, Example},- format_prompt::{TeacherMultiRegionPrompt, TeacherPrompt},+ format_prompt::{TeacherJumpsPrompt, TeacherPrompt},repair,};use anyhow::{Context as _, Result};-use zeta_prompt::{ZetaFormat, parse_zeta2_model_output, parsed_output_to_patch};+use zeta_prompt::{+ ZetaFormat, parse_zeta2_model_output, parse_zeta2_model_output_as_patch, parsed_output_to_patch,+};pub fn run_parse_output(example: &mut Example) -> Result<()> {example@@ -40,9 +42,8 @@ pub fn parse_prediction_output(PredictionProvider::Teacher(_, _) | PredictionProvider::TeacherNonBatching(_, _) => {TeacherPrompt::parse(example, actual_output)}- PredictionProvider::TeacherMultiRegion(_)- | PredictionProvider::TeacherMultiRegionNonBatching(_) => {- TeacherMultiRegionPrompt::parse(example, actual_output)+ PredictionProvider::TeacherJumps(_) | PredictionProvider::TeacherJumpsNonBatching(_) => {+ TeacherJumpsPrompt::parse(example, actual_output)}PredictionProvider::Zeta2(version) => parse_zeta2_output(example, actual_output, version),PredictionProvider::Repair => repair::parse(example, actual_output),@@ -63,6 +64,13 @@ fn parse_zeta2_output(.as_ref().context("prompt_inputs required")?;+ if format == ZetaFormat::V0615HashRegions {+ return Ok((+ parse_zeta2_model_output_as_patch(actual_output, format, prompt_inputs)?,+ None,+ ));+ }+let parsed = parse_zeta2_model_output(actual_output, format, prompt_inputs)?;let range_in_excerpt = parsed.range_in_excerpt.clone();let excerpt = prompt_inputs.cursor_excerpt.as_ref();diff --git a/crates/edit_prediction_cli/src/predict.rs b/crates/edit_prediction_cli/src/predict.rsindex a0e1ffdcafc..643b0bf6fd8 100644--- a/crates/edit_prediction_cli/src/predict.rs+++ b/crates/edit_prediction_cli/src/predict.rs@@ -2,7 +2,7 @@ use crate::{FormatPromptArgs, PredictArgs, PredictionProvider, TeacherBackend,anthropic_client::AnthropicClient,example::{Example, ExamplePrediction, ExamplePrompt},- format_prompt::{TeacherMultiRegionPrompt, TeacherPrompt, run_format_prompt},+ format_prompt::{TeacherJumpsPrompt, TeacherPrompt, run_format_prompt},headless::EpAppState,load_project::run_load_project,openai_client::OpenAiClient,@@ -57,16 +57,10 @@ pub async fn run_prediction();};- if matches!(- provider,- PredictionProvider::TeacherMultiRegion(..)- | PredictionProvider::TeacherMultiRegionNonBatching(..)- ) {- anyhow::bail!("Teacher multi-region providers are not supported for prediction.");- }-if let PredictionProvider::Teacher(backend, _)- | PredictionProvider::TeacherNonBatching(backend, _) = provider+ | PredictionProvider::TeacherNonBatching(backend, _)+ | PredictionProvider::TeacherJumps(backend)+ | PredictionProvider::TeacherJumpsNonBatching(backend) = provider{run_context_retrieval(example,@@ -79,7 +73,10 @@ pub async fn run_prediction(.await?;run_format_prompt(example,- &FormatPromptArgs { provider },+ &FormatPromptArgs {+ provider,+ related_files_budget: TeacherJumpsPrompt::DEFAULT_RELATED_FILES_BUDGET,+ },app_state.clone(),example_progress,cx,@@ -89,7 +86,7 @@ pub async fn run_prediction(let step_progress = example_progress.start(Step::Predict);let batched = matches!(provider,- PredictionProvider::Teacher(..) | PredictionProvider::TeacherMultiRegion(..)+ PredictionProvider::Teacher(..) | PredictionProvider::TeacherJumps(..));return predict_teacher(example,@@ -107,6 +104,7 @@ pub async fn run_prediction(example,&FormatPromptArgs {provider: PredictionProvider::Zeta2(format),+ related_files_budget: TeacherJumpsPrompt::DEFAULT_RELATED_FILES_BUDGET,},app_state.clone(),example_progress,@@ -161,9 +159,9 @@ pub async fn run_prediction(PredictionProvider::Zeta2(_) => edit_prediction::EditPredictionModel::Zeta,PredictionProvider::Mercury => edit_prediction::EditPredictionModel::Mercury,PredictionProvider::Teacher(..)- | PredictionProvider::TeacherMultiRegion(..)+ | PredictionProvider::TeacherJumps(..)| PredictionProvider::TeacherNonBatching(..)- | PredictionProvider::TeacherMultiRegionNonBatching(..)+ | PredictionProvider::TeacherJumpsNonBatching(..)| PredictionProvider::Repair| PredictionProvider::Baseten(_) => {unreachable!()@@ -301,8 +299,8 @@ pub async fn run_prediction(}).await?;- let actual_patch = prediction.and_then(|prediction| {- let prediction = prediction.prediction;+ let actual_patch = prediction.and_then(|result| {+ let prediction = result.prediction;prediction.edit_preview.as_unified_diff(prediction.snapshot.file(), &prediction.edits)@@ -360,7 +358,7 @@ async fn predict_teacher().await}- TeacherBackend::Gpt52 => {+ TeacherBackend::Gpt52 | TeacherBackend::Gpt54 | TeacherBackend::Gpt55 => {predict_openai(example,backend,@@ -441,39 +439,48 @@ async fn predict_anthropic(.unwrap_or(PredictionProvider::Teacher(backend, ZetaFormat::default()))} else {match example.prompt.as_ref().map(|prompt| prompt.provider) {- Some(PredictionProvider::TeacherMultiRegion(_))- | Some(PredictionProvider::TeacherMultiRegionNonBatching(_)) => {- PredictionProvider::TeacherMultiRegionNonBatching(backend)+ Some(PredictionProvider::TeacherJumps(_))+ | Some(PredictionProvider::TeacherJumpsNonBatching(_)) => {+ PredictionProvider::TeacherJumpsNonBatching(backend)}_ => PredictionProvider::TeacherNonBatching(backend, ZetaFormat::default()),}};- let (actual_patch, actual_cursor) = match parser_provider {- PredictionProvider::TeacherMultiRegion(_)- | PredictionProvider::TeacherMultiRegionNonBatching(_) => {- TeacherMultiRegionPrompt::parse(example, &actual_output)?+ let parse_result = match parser_provider {+ PredictionProvider::TeacherJumps(_)+ | PredictionProvider::TeacherJumpsNonBatching(_) => {+ TeacherJumpsPrompt::parse(example, &actual_output)}- _ => TeacherPrompt::parse(example, &actual_output)?,+ _ => TeacherPrompt::parse(example, &actual_output),+ };+ // A teacher response can parse as text yet describe an invalid edit+ // (e.g. an edit span crossing non-contiguous snippets, or a truncated+ // span). Record the rejection on the prediction instead of propagating+ // it: the raw output is preserved for `parse-output`/inspection, and a+ // single bad example no longer aborts an entire (already paid for) batch.+ let (actual_patch, actual_cursor, error) = match parse_result {+ Ok((patch, cursor)) => (Some(patch), cursor, None),+ Err(err) => (None, None, Some(format!("{err:#}"))),};let prediction = ExamplePrediction {- actual_patch: Some(actual_patch),+ actual_patch,actual_output,actual_cursor,- error: None,+ error,provider: if batched {match example.prompt.as_ref().map(|prompt| prompt.provider) {- Some(PredictionProvider::TeacherMultiRegion(_)) => {- PredictionProvider::TeacherMultiRegion(backend)+ Some(PredictionProvider::TeacherJumps(_)) => {+ PredictionProvider::TeacherJumps(backend)}_ => PredictionProvider::Teacher(backend, ZetaFormat::default()),}} else {match example.prompt.as_ref().map(|prompt| prompt.provider) {- Some(PredictionProvider::TeacherMultiRegion(_))- | Some(PredictionProvider::TeacherMultiRegionNonBatching(_)) => {- PredictionProvider::TeacherMultiRegionNonBatching(backend)+ Some(PredictionProvider::TeacherJumps(_))+ | Some(PredictionProvider::TeacherJumpsNonBatching(_)) => {+ PredictionProvider::TeacherJumpsNonBatching(backend)}_ => PredictionProvider::TeacherNonBatching(backend, ZetaFormat::default()),}@@ -560,39 +567,45 @@ async fn predict_openai(.unwrap_or(PredictionProvider::Teacher(backend, ZetaFormat::default()))} else {match example.prompt.as_ref().map(|prompt| prompt.provider) {- Some(PredictionProvider::TeacherMultiRegion(_))- | Some(PredictionProvider::TeacherMultiRegionNonBatching(_)) => {- PredictionProvider::TeacherMultiRegionNonBatching(backend)+ Some(PredictionProvider::TeacherJumps(_))+ | Some(PredictionProvider::TeacherJumpsNonBatching(_)) => {+ PredictionProvider::TeacherJumpsNonBatching(backend)}_ => PredictionProvider::TeacherNonBatching(backend, ZetaFormat::default()),}};- let (actual_patch, actual_cursor) = match parser_provider {- PredictionProvider::TeacherMultiRegion(_)- | PredictionProvider::TeacherMultiRegionNonBatching(_) => {- TeacherMultiRegionPrompt::parse(example, &actual_output)?+ let parse_result = match parser_provider {+ PredictionProvider::TeacherJumps(_)+ | PredictionProvider::TeacherJumpsNonBatching(_) => {+ TeacherJumpsPrompt::parse(example, &actual_output)}- _ => TeacherPrompt::parse(example, &actual_output)?,+ _ => TeacherPrompt::parse(example, &actual_output),+ };+ // See `predict_anthropic`: an unparseable/invalid teacher edit is+ // recorded as a per-prediction error rather than aborting the batch.+ let (actual_patch, actual_cursor, error) = match parse_result {+ Ok((patch, cursor)) => (Some(patch), cursor, None),+ Err(err) => (None, None, Some(format!("{err:#}"))),};let prediction = ExamplePrediction {- actual_patch: Some(actual_patch),+ actual_patch,actual_output,actual_cursor,- error: None,+ error,provider: if batched {match example.prompt.as_ref().map(|prompt| prompt.provider) {- Some(PredictionProvider::TeacherMultiRegion(_)) => {- PredictionProvider::TeacherMultiRegion(backend)+ Some(PredictionProvider::TeacherJumps(_)) => {+ PredictionProvider::TeacherJumps(backend)}_ => PredictionProvider::Teacher(backend, ZetaFormat::default()),}} else {match example.prompt.as_ref().map(|prompt| prompt.provider) {- Some(PredictionProvider::TeacherMultiRegion(_))- | Some(PredictionProvider::TeacherMultiRegionNonBatching(_)) => {- PredictionProvider::TeacherMultiRegionNonBatching(backend)+ Some(PredictionProvider::TeacherJumps(_))+ | Some(PredictionProvider::TeacherJumpsNonBatching(_)) => {+ PredictionProvider::TeacherJumpsNonBatching(backend)}_ => PredictionProvider::TeacherNonBatching(backend, ZetaFormat::default()),}@@ -694,7 +707,7 @@ pub async fn predict_baseten(pub async fn sync_batches(provider: Option<&PredictionProvider>) -> anyhow::Result<()> {match provider {Some(PredictionProvider::Teacher(backend, _))- | Some(PredictionProvider::TeacherMultiRegion(backend)) => match backend {+ | Some(PredictionProvider::TeacherJumps(backend)) => match backend {TeacherBackend::Sonnet45 | TeacherBackend::Sonnet46 => {let llm_client = ANTHROPIC_CLIENT.get_or_init(|| {AnthropicClient::batch(&crate::paths::LLM_CACHE_DB)@@ -705,7 +718,7 @@ pub async fn sync_batches(provider: Option<&PredictionProvider>) -> anyhow::Resu.await.context("Failed to sync Anthropic batches")?;}- TeacherBackend::Gpt52 => {+ TeacherBackend::Gpt52 | TeacherBackend::Gpt54 | TeacherBackend::Gpt55 => {let llm_client = OPENAI_CLIENT.get_or_init(|| {OpenAiClient::batch(&crate::paths::LLM_CACHE_DB).expect("Failed to create OpenAI client")@@ -725,7 +738,9 @@ pub async fn reprocess_after_batch_wait(examples: &mut [Example],args: &PredictArgs,) -> anyhow::Result<()> {- let Some(PredictionProvider::Teacher(backend, _)) = args.provider else {+ let (Some(PredictionProvider::Teacher(backend, _))+ | Some(PredictionProvider::TeacherJumps(backend))) = args.provider+ else {return Ok(());};@@ -784,7 +799,8 @@ pub async fn wait_for_batches(provider: Option<&PredictionProvider>) -> anyhow::fn pending_batch_count(provider: Option<&PredictionProvider>) -> anyhow::Result<usize> {match provider {- Some(PredictionProvider::Teacher(backend, _)) => match backend {+ Some(PredictionProvider::Teacher(backend, _))+ | Some(PredictionProvider::TeacherJumps(backend)) => match backend {TeacherBackend::Sonnet45 | TeacherBackend::Sonnet46 => {let llm_client = ANTHROPIC_CLIENT.get_or_init(|| {AnthropicClient::batch(&crate::paths::LLM_CACHE_DB)@@ -792,7 +808,7 @@ fn pending_batch_count(provider: Option<&PredictionProvider>) -> anyhow::Result<});llm_client.pending_batch_count()}- TeacherBackend::Gpt52 => {+ TeacherBackend::Gpt52 | TeacherBackend::Gpt54 | TeacherBackend::Gpt55 => {let llm_client = OPENAI_CLIENT.get_or_init(|| {OpenAiClient::batch(&crate::paths::LLM_CACHE_DB).expect("Failed to create OpenAI client")diff --git a/crates/edit_prediction_cli/src/prompts/teacher_multi_region.md b/crates/edit_prediction_cli/src/prompts/teacher_jumps.mdsimilarity index 61%rename from crates/edit_prediction_cli/src/prompts/teacher_multi_region.mdrename to crates/edit_prediction_cli/src/prompts/teacher_jumps.mdindex 61c5c8f3837..44543dc7f6f 100644--- a/crates/edit_prediction_cli/src/prompts/teacher_multi_region.md+++ b/crates/edit_prediction_cli/src/prompts/teacher_jumps.md@@ -1,14 +1,15 @@# Instructions-You are an edit prediction assistant in a code editor. Your task is to predict the next edit to a given region of code surrounding the user's cursor.+You are an edit prediction assistant in a code editor. Your task is to predict the next edits the user will make, based on their recent edit history and the code they can see.1. Analyze the edit history to understand what the programmer is trying to achieve2. Identify any incomplete refactoring or changes that need to be finished-3. Make the remaining edits that a human programmer would logically make next (by rewriting a region of code near their cursor)+3. Predict the edits a human programmer would logically make next — including edits **far from the cursor**, in other parts of the current file, or in **other files** shown in the related excerpts## Focus on- Completing any partially-applied changes made+- Propagating changes to every place they affect: callers after a signature change, imports after a new reference, tests after a behavior change, and so on- Ensuring consistency with the programming style and patterns already established- Making edits that maintain or improve code quality@@ -24,7 +25,7 @@ You are an edit prediction assistant in a code editor. Your task is to predict t- Auto-generated code can be modified: Hunks marked with `// User accepted prediction:` contain code from a previous prediction the user accepted. Unlike user-typed content, these hunks CAN be edited, corrected, or replaced if it improves the code. The "never undo/revert" rule protects the user's *current typing intent*—auto-generated code doesn't carry this protection- Do not just mechanically apply patterns - reason about what changes make sense given the context and the programmer's apparent goals.- Do not just fix syntax errors - look for the broader refactoring pattern and apply it systematically throughout the code.-- Keep existing formatting unless it's absolutely necessary+- Keep existing formatting unless it's absolutely necessary. In particular, never add or delete blank lines or make other whitespace-only changes that are separate from your actual edit — reproduce untouched lines exactly as they appear, including blank ones- When edit history and surrounding code suggest different edits, prioritize the most recent edits in the history as they best reflect current intent.- Treat partial text at or near the cursor as the beginning of something the user is actively typing. Complete the code the user appears to be creating based on context.- When completing partial code, prefer predictions that save meaningful keystrokes, even if this requires making educated guesses about the user's intent.@@ -36,27 +37,32 @@ You are an edit prediction assistant in a code editor. Your task is to predict tYou will be provided with:1. The user's *edit history*, in chronological order. Use this to infer the user's trajectory and predict the next most logical edit.- Hunks preceded by `// User accepted prediction:` indicate code that was auto-generated by a previous prediction and accepted by the user. These are treated differently than user-typed edits (see Rules).-2. A set of *related excerpts* from the user's codebase. Some of these may be needed for correctly predicting the next edit.- - `…` may appear within a related file to indicate that some code has been skipped.-3. An excerpt from the user's *current file*.- - The excerpt contains numbered *marker* tags (`<|marker_1|>`, `<|marker_2|>`, etc.) placed at block boundaries throughout the code. These markers divide the excerpt into spans that you can target for editing.- - Code that appears before the first marker or after the last marker is read-only context and cannot be edited.+2. A set of *related excerpts* from the user's codebase. Some of these may be needed for correctly predicting the next edit, and you can also predict edits inside them.+ - `...` may appear within a related file to indicate that some code has been skipped.+3. The user's *current file*.- The `<|user_cursor|>` tag marks the user's current cursor position, as it stands after the last edit in the history.+ - `...` may appear here too when parts of the file are skipped.++All of the code you can see — the current file **and** every related excerpt — contains *marker* tags (e.g. `<|marker_b1f8|>`). Each marker has a unique four-character identifier derived from the code near it. Markers are placed at block boundaries and divide the code into spans that you can target for editing. Any span between two markers in the same excerpt can be rewritten.# Output Format-- Briefly explain the user's current intent based on the edit history and their current cursor location.-- Output a markdown codeblock containing your predicted edit as a **marker-bounded span**:- - The codeblock must **start** with a marker tag (e.g. `<|marker_2|>`) and **end** with a marker tag (e.g. `<|marker_4|>`).- - The content between these two markers is the full replacement for that span in the original file.- - Choose the **narrowest** pair of markers that fully contains your predicted edits, to minimize unnecessary output.- - Reproduce any unchanged lines within the chosen span faithfully — do not omit or alter them.+- Briefly explain the user's current intent based on the edit history and their current cursor location, and which locations need to change next.+- Output your predicted edits as a **sequence of markdown codeblocks** — one codeblock per edit — in the order you expect the user to make them. Often a single edit is enough; output multiple edits only when the user's trajectory clearly requires changes in several places.+- Each codeblock contains one **marker-bounded span**:+ - The codeblock must **start** with a marker tag (e.g. `<|marker_b1f8|>`) and **end** with another marker tag (e.g. `<|marker_Tx2v|>`).+ - Both markers must come from the **same excerpt**, with the start marker appearing before the end marker.+ - The content between these two markers is the full replacement for that span in the original code. **Every original line you do not reproduce is deleted.** You cannot stop early: write the span's content all the way to the end marker, even when your actual change only touches the beginning of the span.+ - Choose the **narrowest** pair of markers that fully contains your predicted edit, to minimize unnecessary output.+ - Reproduce any unchanged lines within the chosen span faithfully — do not omit or alter them. Before writing the end marker, verify that the lines just before it match the original lines just before that marker (unless you intend to delete them).+ - To delete code, prefer a span whose end marker lies **beyond** the deleted code, so that your output still ends with reproduced unchanged lines. Output that reproduces the start of a span and then stops at the end marker is treated as malformed (truncated), not as a deletion.- Do not include any intermediate marker tags in your output — only the start and end markers.-- If no edit is needed (the code is already complete and correct, or there is no clear next edit to make), output a codeblock containing only `NO_EDITS`:+- All edits apply to the code **as shown above** (the original snapshot): the spans of different edits must not overlap, and a later edit must not depend on the text inserted by an earlier one.+- If no edit is needed (the code is already complete and correct, or there is no clear next edit to make), output a single codeblock containing only `NO_EDITS`:`````NO_EDITS`````-- If there is a specific place in the predicted output where the user is likely to edit next, indicate it using the `<|user_cursor|>` tag.+- If there is a specific place in the predicted output where the user is likely to edit next, indicate it using the `<|user_cursor|>` tag (in at most one edit).## Example 1@@ -64,11 +70,13 @@ There is code missing at the cursor location. The related excerpts includes the### Related Excerpts-`````+`````src/product.rs+<|marker_Hw3q|>struct Product {name: String,price: u32,}+<|marker_pY7c|>`````### User Edit History@@ -89,15 +97,17 @@ struct Product {### Current File`````src/calculate.rs+<|marker_b1f8|>fn calculate_total(products: &[Product]) -> u32 {-<|marker_1|>+<|marker_Tx2v|>let mut total = 0;for product in products {total += <|user_cursor|>;}total-<|marker_2|>+<|marker_e9Kd|>}+<|marker_0aRm|>`````### Output@@ -105,13 +115,13 @@ fn calculate_total(products: &[Product]) -> u32 {The user is computing a sum based on a list of products. The only numeric field on `Product` is `price`, so they must intend to sum the prices.`````-<|marker_1|>+<|marker_Tx2v|>let mut total = 0;for product in products {total += product.price;}total-<|marker_2|>+<|marker_e9Kd|>`````## Example 2@@ -134,14 +144,14 @@ The user appears to be in the process of typing an eprintln call. Rather than fi### Current File`````src/modal.rs-<|marker_1|>+<|marker_kQ4z|>// handle the close button clickfn handle_close_button_click(modal_state: &mut ModalState, evt: &Event) {-<|marker_2|>+<|marker_8sWn|>modal_state.close();epr<|user_cursor|>modal_state.dismiss();}-<|marker_3|>+<|marker_vJ2p|>`````### Output@@ -149,66 +159,64 @@ fn handle_close_button_click(modal_state: &mut ModalState, evt: &Event) {The user is clearly starting to type `eprintln!()`, however, what they intend to print is not obvious. I should fill in the print call and string literal, with the cursor positioned inside the string literal so the user can print whatever they want.`````-<|marker_2|>+<|marker_8sWn|>modal_state.close();eprintln!("<|user_cursor|>");modal_state.dismiss();}-<|marker_3|>+<|marker_vJ2p|>`````## Example 3-Here, the user is adding a function. There's no way to tell for sure what the function's name will be. In this situation, you should make a reasonable guess at the function's name and signature, and place the user's cursor in the function body. This way, if you guess correctly, it will save the user a meaningful number of keystrokes, and the file will be left in a coherent state.+Here, the user has renamed a function in the current file. The old name is still used in another file, shown in the related excerpts. You should predict the sequence of edits that completes the rename: first where they are typing, then the call site in the other file.-### User Edit History+### Related Excerpts+`````src/server.rs+<|marker_Fq6a|>+fn handle_request(request: Request) -> Response {+ let user = fetch_user(request.user_id);+<|marker_mC1t|>+ let permissions = fetch_permissions(&user);+ Response::ok(user, permissions)+}+<|marker_2dXe|>`````---- a/src/modal.rs-+++ b/src/modal.rs-@@ -100,4 +100,4 @@- fn handle_close_button_click(modal_state: &mut ModalState, evt: &Event) {- modal_state.close();- modal_state.dismiss();- }-+-+fn- fn handle_keystroke(modal_state: &mut ModalState, evt: &Event) {+### User Edit History++`````+--- a/src/db.rs++++ b/src/db.rs+@@ -10,7 +10,7 @@+-fn fetch_user(user_id: UserId) -> User {++fn fetch_user_cached(user_id: UserId) -> User {`````### Current File-`````src/modal.rs-// handle the close button click-fn handle_close_button_click(modal_state: &mut ModalState, evt: &Event) {- modal_state.close();-<|marker_1|>- modal_state.dismiss();+`````src/db.rs+<|marker_pL5w|>+fn fetch_user_cached<|user_cursor|>(user_id: UserId) -> User {+ CACHE.get_or_insert(user_id, || load_user(user_id))}--fn<|user_cursor|>--<|marker_2|>-fn handle_keystroke(modal_state: &mut ModalState, evt: &Event) {- modal_state.begin_edit();-<|marker_3|>+<|marker_zN8j|>+fn fetch_permissions(user: &User) -> Permissions {+ load_permissions(user.role)+}+<|marker_4tGb|>`````### Output-The user is adding a new function. The existing functions I see are `handle_close_button_click` and `handle_keystroke`, which have similar signatures. One possible function they might be adding is `handle_submit`.+The user renamed `fetch_user` to `fetch_user_cached`. The related excerpt from `src/server.rs` still calls the old name `fetch_user`, so that call site must be updated to match.`````-<|marker_1|>- modal_state.dismiss();-}--fn handle_submit(modal_state: &mut ModalState, evt: &Event) {- <|user_cursor|>-}--<|marker_2|>+<|marker_Fq6a|>+fn handle_request(request: Request) -> Response {+ let user = fetch_user_cached(request.user_id);+<|marker_mC1t|>`````## Example 4@@ -230,11 +238,11 @@ The code is already complete and there is no clear next edit to make. You should### Current File`````src/utils.rs-<|marker_1|>+<|marker_Wd9u|>fn add(a: i32, b: i32) -> i32 {a + b<|user_cursor|>}-<|marker_2|>+<|marker_aE3h|>`````### Output@@ -264,11 +272,11 @@ The user just deleted code, leaving behind what looks incomplete. You must NOT "### Current File`````config.nix-<|marker_1|>+<|marker_Jr7y|># /etc/modular/crashdb needs to be mutableln -s /tmp/cr<|user_cursor|> $out/etc/modular/crashdb'';-<|marker_2|>+<|marker_sB1v|>`````### Output@@ -281,6 +289,61 @@ NO_EDITS## Example 6+The user is using a type that is not yet imported, and is partway through using it in the function body. The next edits are: finish the line they are typing, then add the missing import at the top of the file — a sequence of two edits in different places.++### User Edit History++`````+--- a/src/api.py++++ b/src/api.py+@@ -40,6 +40,7 @@+ def get_report(report_id):+ report = load_report(report_id)++ timestamp = datetime.+ return render(report)+`````++### Current File++`````src/api.py+<|marker_Gc2f|>+import json++from app.reports import load_report, render+<|marker_x7Qe|>++def get_report(report_id):+ report = load_report(report_id)+ timestamp = datetime.<|user_cursor|>+ return render(report)+<|marker_nV5s|>+`````++### Output++The user is adding a timestamp using `datetime`, which is not imported yet. The most likely completion is `datetime.now()`. After completing that line, the user will need to import `datetime` at the top of the file.++`````+<|marker_x7Qe|>++def get_report(report_id):+ report = load_report(report_id)+ timestamp = datetime.now(<|user_cursor|>)+ return render(report)+<|marker_nV5s|>+`````++`````+<|marker_Gc2f|>+import json+from datetime import datetime++from app.reports import load_report, render+<|marker_x7Qe|>+`````++## Example 7+The user accepted a prediction for a function, then started renaming it. The original arguments were auto-generated (marked with `// User accepted prediction:`), so they CAN be updated to match the new function name. This is NOT reverting user input—it's improving auto-generated scaffolding.### User Edit History@@ -319,14 +382,14 @@ The user accepted a prediction for a function, then started renaming it. The ori### Current File`````math_utils.py-<|marker_1|>+<|marker_Ub4n|>def calculate_rectangle_area(width, height):return width * height-<|marker_2|>+<|marker_h3Zt|>def calculate_sq<|user_cursor|>_perimeter(width, height):-<|marker_3|>+<|marker_q8Pj|>`````### Output@@ -334,10 +397,10 @@ def calculate_sq<|user_cursor|>_perimeter(width, height):The user accepted a prediction for `calculate_rectangle_perimeter(width, height)`, then started renaming `rectangle` to `square`. Since squares have equal sides, the arguments should change from `(width, height)` to `(side)`. The arguments were auto-generated (from an accepted prediction), so modifying them is appropriate.`````-<|marker_2|>+<|marker_h3Zt|>def calculate_square_perimeter(side):<|user_cursor|>-<|marker_3|>+<|marker_q8Pj|>`````@@ -363,4 +426,4 @@ def calculate_square_perimeter(side):------Based on the edit history and context above, predict the user's next edit within the marker-bounded spans.+Based on the edit history and context above, predict the user's next edits as a sequence of marker-bounded spans.diff --git a/crates/edit_prediction_cli/src/retrieve_context.rs b/crates/edit_prediction_cli/src/retrieve_context.rsindex 93ce5ecb2c3..802fca21d4d 100644--- a/crates/edit_prediction_cli/src/retrieve_context.rs+++ b/crates/edit_prediction_cli/src/retrieve_context.rs@@ -1,13 +1,15 @@use crate::{example::Example,+ format_prompt::line_start_offset,headless::EpAppState,load_project::run_load_project,progress::{ExampleProgress, InfoStyle, Step, StepProgress},};use anyhow::Context as _;use clap::ValueEnum;-use collections::HashSet;+use collections::{HashMap, HashSet};use edit_prediction::{DebugEvent, EditPredictionStore, udiff::refresh_worktree_entries};+use edit_prediction_context::OracleTarget;use futures::{FutureExt as _, StreamExt as _, channel::mpsc};use gpui::{AsyncApp, Entity};use language::Buffer;@@ -29,6 +31,7 @@ pub enum ContextRetrievalType {GitLog,Bm25,OracleFile,+ OracleSnippet,#[default]All,None,@@ -45,6 +48,7 @@ impl std::fmt::Display for ContextRetrievalType {ContextRetrievalType::GitLog => write!(f, "git-log"),ContextRetrievalType::Bm25 => write!(f, "bm25"),ContextRetrievalType::OracleFile => write!(f, "oracle-file"),+ ContextRetrievalType::OracleSnippet => write!(f, "oracle-snippet"),ContextRetrievalType::All => write!(f, "all"),ContextRetrievalType::None => write!(f, "none"),}@@ -62,6 +66,7 @@ impl ContextRetrievalType {ContextRetrievalType::GitLog => vec![ContextSource::GitLog],ContextRetrievalType::Bm25 => vec![ContextSource::Bm25],ContextRetrievalType::OracleFile => vec![ContextSource::OracleFile],+ ContextRetrievalType::OracleSnippet => vec![ContextSource::OracleSnippet],ContextRetrievalType::All => {let mut sources = vec![ContextSource::Lsp];sources.extend(editable_context_sources());@@ -158,10 +163,16 @@ pub async fn run_context_retrieval(.filter(|context_source| *context_source != ContextSource::Lsp).collect::<Vec<_>>();if !editable_context_sources.is_empty() {- let oracle_paths = if editable_context_sources.contains(&ContextSource::OracleFile) {- let oracle_paths = oracle_paths_from_expected_patches(example);+ let oracle_targets = if editable_context_sources.contains(&ContextSource::OracleFile)+ || editable_context_sources.contains(&ContextSource::OracleSnippet)+ {+ let oracle_targets = oracle_targets_from_expected_patches(example);+ let oracle_paths = oracle_targets+ .iter()+ .map(|target| target.path.clone())+ .collect::<Vec<_>>();refresh_paths(&project, &oracle_paths, &mut cx).await?;- oracle_paths+ oracle_targets} else {Vec::new()};@@ -172,7 +183,7 @@ pub async fn run_context_retrieval(project.clone(),state.buffer.clone(),state.cursor_position,- oracle_paths,+ oracle_targets,editable_context_sources,cx,)@@ -201,41 +212,121 @@ fn merge_context_files({existing_file.max_row = existing_file.max_row.max(new_file.max_row);existing_file.excerpts.append(&mut new_file.excerpts);- existing_file- .excerpts- .sort_by_key(|excerpt| (excerpt.order, excerpt.row_range.start));existing_file.in_open_source_repo =existing_file.in_open_source_repo && new_file.in_open_source_repo;} else {context_files.push(new_file);}}+ for file in context_files.iter_mut() {+ coalesce_touching_excerpts(&mut file.excerpts);+ }}-fn oracle_paths_from_expected_patches(example: &Example) -> Vec<Arc<Path>> {- let mut seen_paths = HashSet::default();- let mut paths = Vec::new();-- for patch in &example.spec.expected_patches {- for path in paths_from_diff(patch) {- if seen_paths.insert(path.clone()) {- paths.push(path.into());+/// Sort a file's excerpts by position and merge those whose row ranges touch+/// or overlap. Touching excerpts render seamlessly in prompts (no `...`+/// separator), so keeping them separate splits edit addressing across a+/// boundary the model can't see and wastes adjacent marker tags (see+/// `TeacherJumpsPrompt::parse`).+///+/// A merged excerpt keeps the minimum `order` (and that excerpt's context+/// source), so the highest-priority content still survives budget-based+/// selection downstream; the tradeoff is that lower-priority touching+/// content is now selected together with it.+fn coalesce_touching_excerpts(excerpts: &mut Vec<zeta_prompt::RelatedExcerpt>) {+ excerpts.sort_by_key(|excerpt| (excerpt.row_range.start, excerpt.row_range.end));+ let mut coalesced: Vec<zeta_prompt::RelatedExcerpt> = Vec::with_capacity(excerpts.len());+ for excerpt in excerpts.drain(..) {+ let Some(last) = coalesced.last_mut() else {+ coalesced.push(excerpt);+ continue;+ };+ if excerpt.row_range.start > last.row_range.end {+ coalesced.push(excerpt);+ continue;+ }+ if excerpt.row_range.end > last.row_range.end {+ // Touching or overlapping. Shared rows come from the same buffer+ // snapshot, so drop the duplicated prefix of the new excerpt and+ // append the rest.+ let mut overlap_rows = (last.row_range.end - excerpt.row_range.start) as usize;+ if !last.text.ends_with('\n') && !last.text.is_empty() {+ // An unterminated final line means `last` includes the+ // content of its `row_range.end` row itself.+ overlap_rows += 1;+ }+ let Some(tail_start) = line_start_offset(&excerpt.text, overlap_rows) else {+ // The excerpt has fewer lines than its row range claims;+ // leave it unmerged rather than corrupt the text.+ coalesced.push(excerpt);+ continue;+ };+ let mut text =+ String::with_capacity(last.text.len() + excerpt.text.len() - tail_start + 1);+ text.push_str(&last.text);+ if !text.is_empty() && !text.ends_with('\n') {+ text.push('\n');}+ text.push_str(&excerpt.text[tail_start..]);+ last.text = text.into();+ last.row_range.end = excerpt.row_range.end;+ }+ if excerpt.order < last.order {+ last.order = excerpt.order;+ last.context_source = excerpt.context_source;}}-- paths+ *excerpts = coalesced;}-fn paths_from_diff(diff: &str) -> Vec<PathBuf> {- diff.lines()- .filter_map(|line| match DiffLine::parse(line) {- DiffLine::OldPath { path } | DiffLine::NewPath { path }- if path.as_ref() != "/dev/null" =>- {- Some(Path::new(path.as_ref()).to_path_buf())+fn oracle_targets_from_expected_patches(example: &Example) -> Vec<OracleTarget> {+ let mut target_indices: HashMap<PathBuf, usize> = HashMap::default();+ let mut targets: Vec<(PathBuf, Vec<std::ops::Range<u32>>)> = Vec::new();++ let mut target_index = |path: &str, targets: &mut Vec<(PathBuf, Vec<std::ops::Range<u32>>)>| {+ let path = Path::new(path).to_path_buf();+ *target_indices.entry(path.clone()).or_insert_with(|| {+ targets.push((path, Vec::new()));+ targets.len() - 1+ })+ };++ for patch in &example.spec.expected_patches {+ // Index of the target whose old-side rows the current hunk headers+ // refer to. Hunk old rows refer to the file's current state only for+ // the first expected patch; later patches drift, but the snippet+ // padding absorbs small offsets.+ let mut current_target: Option<usize> = None;+ for line in patch.lines() {+ match DiffLine::parse(line) {+ DiffLine::OldPath { path } => {+ current_target = (path.as_ref() != "/dev/null")+ .then(|| target_index(path.as_ref(), &mut targets));+ }+ DiffLine::NewPath { path } => {+ if path.as_ref() != "/dev/null" {+ let index = target_index(path.as_ref(), &mut targets);+ if current_target.is_none() {+ current_target = Some(index);+ }+ }+ }+ DiffLine::HunkHeader(Some(location)) => {+ if let Some(index) = current_target {+ let start = location.start_line_old;+ targets[index].1.push(start..start + location.count_old);+ }+ }+ _ => {}}- _ => None,+ }+ }++ targets+ .into_iter()+ .map(|(path, row_ranges)| OracleTarget {+ path: path.into(),+ row_ranges,}).collect()}@@ -369,3 +460,132 @@ async fn wait_for_language_servers_to_start(step_progress.clear_substatus();Ok(())}++#[cfg(test)]+mod tests {+ use super::*;+ use zeta_prompt::{ContextSource, RelatedExcerpt, RelatedFile};++ fn excerpt(+ row_range: std::ops::Range<u32>,+ text: &str,+ order: usize,+ context_source: ContextSource,+ ) -> RelatedExcerpt {+ RelatedExcerpt {+ row_range,+ text: Arc::from(text),+ order,+ context_source,+ }+ }++ #[test]+ fn test_coalesce_touching_excerpts() {+ let mut excerpts = vec![+ excerpt(2..4, "line2\nline3\n", 1, ContextSource::EditHistory),+ excerpt(0..2, "line0\nline1\n", 3, ContextSource::Bm25),+ excerpt(4..6, "line4\nline5\n", 2, ContextSource::OracleSnippet),+ excerpt(10..12, "line10\nline11\n", 0, ContextSource::Bm25),+ ];+ coalesce_touching_excerpts(&mut excerpts);++ assert_eq!(excerpts.len(), 2);+ assert_eq!(excerpts[0].row_range, 0..6);+ assert_eq!(+ excerpts[0].text.as_ref(),+ "line0\nline1\nline2\nline3\nline4\nline5\n"+ );+ // The merged excerpt keeps the highest priority (minimum order) and+ // its context source.+ assert_eq!(excerpts[0].order, 1);+ assert_eq!(excerpts[0].context_source, ContextSource::EditHistory);+ // The gapped excerpt stays separate.+ assert_eq!(excerpts[1].row_range, 10..12);+ }++ #[test]+ fn test_coalesce_overlapping_excerpts_drops_duplicated_rows() {+ let mut excerpts = vec![+ excerpt(0..3, "line0\nline1\nline2\n", 0, ContextSource::Bm25),+ excerpt(+ 2..5,+ "line2\nline3\nline4\n",+ 1,+ ContextSource::OracleSnippet,+ ),+ ];+ coalesce_touching_excerpts(&mut excerpts);++ assert_eq!(excerpts.len(), 1);+ assert_eq!(excerpts[0].row_range, 0..5);+ assert_eq!(+ excerpts[0].text.as_ref(),+ "line0\nline1\nline2\nline3\nline4\n"+ );+ assert_eq!(excerpts[0].order, 0);+ assert_eq!(excerpts[0].context_source, ContextSource::Bm25);+ }++ #[test]+ fn test_coalesce_contained_excerpt_upgrades_order() {+ let mut excerpts = vec![+ excerpt(0..4, "line0\nline1\nline2\nline3\n", 5, ContextSource::Bm25),+ excerpt(1..3, "line1\nline2\n", 2, ContextSource::OracleSnippet),+ ];+ coalesce_touching_excerpts(&mut excerpts);++ assert_eq!(excerpts.len(), 1);+ assert_eq!(excerpts[0].row_range, 0..4);+ assert_eq!(excerpts[0].text.as_ref(), "line0\nline1\nline2\nline3\n");+ assert_eq!(excerpts[0].order, 2);+ assert_eq!(excerpts[0].context_source, ContextSource::OracleSnippet);+ }++ #[test]+ fn test_coalesce_handles_unterminated_final_line() {+ // An excerpt ending without a newline includes the content of its+ // `row_range.end` row (e.g. git-log excerpts ending at EOF), so a+ // touching excerpt's first row duplicates it.+ let mut excerpts = vec![+ excerpt(0..2, "line0\nline1\nline2", 0, ContextSource::GitLog),+ excerpt(2..4, "line2\nline3\n", 1, ContextSource::Bm25),+ ];+ coalesce_touching_excerpts(&mut excerpts);++ assert_eq!(excerpts.len(), 1);+ assert_eq!(excerpts[0].row_range, 0..4);+ assert_eq!(excerpts[0].text.as_ref(), "line0\nline1\nline2\nline3\n");+ }++ #[test]+ fn test_merge_context_files_coalesces_across_sources() {+ let path: Arc<Path> = Path::new("root/src/lib.rs").into();+ let mut context_files = vec![RelatedFile {+ path: path.clone(),+ max_row: 100,+ excerpts: vec![excerpt(+ 0..2,+ "line0\nline1\n",+ 0,+ ContextSource::CurrentFile,+ )],+ in_open_source_repo: false,+ }];+ let new_files = vec![RelatedFile {+ path,+ max_row: 100,+ excerpts: vec![excerpt(2..4, "line2\nline3\n", 1, ContextSource::Bm25)],+ in_open_source_repo: false,+ }];+ merge_context_files(&mut context_files, new_files);++ assert_eq!(context_files.len(), 1);+ assert_eq!(context_files[0].excerpts.len(), 1);+ assert_eq!(context_files[0].excerpts[0].row_range, 0..4);+ assert_eq!(+ context_files[0].excerpts[0].text.as_ref(),+ "line0\nline1\nline2\nline3\n"+ );+ }+}diff --git a/crates/edit_prediction_cli/src/score.rs b/crates/edit_prediction_cli/src/score.rsindex 841d15541e4..5a0f763d049 100644--- a/crates/edit_prediction_cli/src/score.rs+++ b/crates/edit_prediction_cli/src/score.rs@@ -66,18 +66,6 @@ pub async fn run_scoring(None};- let prepared_expected_patches = edit_prediction_metrics::prepare_expected_patches(- &expected_patches_with_cursors,- original_text,- old_editable_region.as_deref(),- )- .with_context(|| {- format!(- "Expected patch did not apply for {}",- example_for_scoring.spec.name- )- })?;-let cursor_path = example_for_scoring.spec.cursor_path.as_ref();let context = context_excerpts(&example_for_scoring,@@ -86,6 +74,30 @@ pub async fn run_scoring(context_source_filter.as_deref(),);+ let prepared_expected_patches = match edit_prediction_metrics::prepare_expected_patches(+ &expected_patches_with_cursors,+ original_text,+ old_editable_region.as_deref(),+ ) {+ Ok(prepared_expected_patches) => prepared_expected_patches,+ Err(_) if !context.is_empty() => expected_patches_with_cursors+ .iter()+ .map(|(patch, cursor_offset)| edit_prediction_metrics::PreparedExpectedPatch {+ patch: patch.clone(),+ text: original_text.to_string(),+ cursor_editable_region_offset: *cursor_offset,+ })+ .collect(),+ Err(error) => {+ return Err(error).with_context(|| {+ format!(+ "Expected patch did not apply for {}",+ example_for_scoring.spec.name+ )+ });+ }+ };+let mut scores = vec![];if allow_missing_predictions && example_for_scoring.predictions.is_empty() {scores.push(edit_prediction_metrics::score_prediction(@@ -295,8 +307,6 @@ pub fn print_report(context_source_filter: Option<&[ContextSource]>,) {const MAX_EXAMPLES_DEFAULT: usize = 20;- use crate::metrics::ClassificationMetrics;-const LINE_WIDTH: usize = 101;if context_only {@@ -318,59 +328,14 @@ pub fn print_report();println!("{}", separator);- let mut all_delta_chr_f_scores = Vec::new();- let mut all_reversal_ratios = Vec::new();- let mut braces_disbalance_sum: usize = 0;- let mut total_delta_chr_f = ClassificationMetrics::default();- let mut total_delta_chr_f_precision = 0.0;- let mut total_delta_chr_f_recall = 0.0;- let mut delta_chr_f_beta = 0.0;- let mut total_exact_lines = ClassificationMetrics::default();- let mut total_scores: usize = 0;- let mut qa_reverts_count: usize = 0;- let mut qa_reverts_total: usize = 0;- let mut qa_confidence_sum: u64 = 0;- let mut qa_confidence_count: usize = 0;- let mut cursor_exact_matches: usize = 0;- let mut cursor_total: usize = 0;- let mut cursor_distance_sum: usize = 0;- let mut cursor_distance_count: usize = 0;- let mut wrong_editable_region_count: usize = 0;- let mut wrong_editable_region_total: usize = 0;- let mut isolated_whitespace_count: usize = 0;- let mut kept_rate_sum: f64 = 0.0;- let mut kept_rate_count: usize = 0;- let mut kept_chars_total: usize = 0;- let mut correctly_deleted_chars_total: usize = 0;- let mut discarded_chars_total: usize = 0;- let mut recall_rate_sum: f64 = 0.0;- let mut recall_rate_count: usize = 0;- let mut editable_context_coverage_count: usize = 0;- let mut editable_context_lines_precision_sum = 0.0;- let mut editable_context_lines_recall_sum = 0.0;- let mut editable_context_lines_f1_sum = 0.0;- let mut editable_context_files_precision_sum = 0.0;- let mut editable_context_files_recall_sum = 0.0;- let mut editable_context_files_f1_sum = 0.0;- let mut total_editable_context_lines = ClassificationMetrics::default();- let mut total_editable_context_files = ClassificationMetrics::default();let mut patch_inserted_tokens: Vec<usize> = Vec::new();let mut patch_deleted_tokens: Vec<usize> = Vec::new();let mut predictions_with_patch: usize = 0;- let mut retrieved_context_bytes_sum = 0.0;- let mut retrieved_context_bytes_count = 0;let mut printed_lines: usize = 0;let mut skipped_lines: usize = 0;for example in examples {- if let Some(bytes) =- retrieved_context_bytes(example, retrieved_context_byte_limit, context_source_filter)- {- retrieved_context_bytes_sum += bytes as f64;- retrieved_context_bytes_count += 1;- }-for (score_idx, score) in example.score.iter().enumerate() {let exact_lines = score.exact_lines_counts();@@ -418,82 +383,8 @@ pub fn print_report(skipped_lines += 1;}- all_delta_chr_f_scores.push(score.delta_chr_f);- all_reversal_ratios.push(score.reversal_ratio);- total_scores += 1;- braces_disbalance_sum += score.braces_disbalance;- total_delta_chr_f.accumulate(&score.delta_chr_f_counts());- total_delta_chr_f_precision += score.delta_chr_f_precision;- total_delta_chr_f_recall += score.delta_chr_f_recall;- delta_chr_f_beta = score.delta_chr_f_beta;- total_exact_lines.accumulate(&score.exact_lines_counts());-- // Accumulate QA metrics- if let Some(qa) = qa_result {- if let Some(reverts) = qa.reverts_edits {- qa_reverts_total += 1;- if reverts {- qa_reverts_count += 1;- }- }- if let Some(conf) = qa.confidence {- qa_confidence_sum += conf as u64;- qa_confidence_count += 1;- }- }-- // Accumulate wrong editable region metrics- if let Some(wrong) = score.wrong_editable_region {- wrong_editable_region_total += 1;- if wrong {- wrong_editable_region_count += 1;- }- }-- // Accumulate isolated whitespace metrics- if score.has_isolated_whitespace_changes {- isolated_whitespace_count += 1;- }-- // Accumulate kept and recall rate metrics- if let Some(kr) = score.kept_rate {- kept_rate_sum += kr;- kept_rate_count += 1;- }- if let Some(kept_chars) = score.kept_chars {- kept_chars_total += kept_chars;- }- if let Some(correctly_deleted_chars) = score.correctly_deleted_chars {- correctly_deleted_chars_total += correctly_deleted_chars;- }- if let Some(discarded_chars) = score.discarded_chars {- discarded_chars_total += discarded_chars;- }- if let Some(rr) = score.recall_rate {- recall_rate_sum += rr;- recall_rate_count += 1;- }- if let Some(coverage) = &score.editable_context_coverage {- editable_context_coverage_count += 1;- editable_context_lines_precision_sum += coverage.lines_precision;- editable_context_lines_recall_sum += coverage.lines_recall;- editable_context_lines_f1_sum += coverage.lines_f1;- editable_context_files_precision_sum += coverage.files_precision;- editable_context_files_recall_sum += coverage.files_recall;- editable_context_files_f1_sum += coverage.files_f1;- total_editable_context_lines.accumulate(&ClassificationMetrics {- true_positives: coverage.lines_tp,- false_positives: coverage.lines_fp,- false_negatives: coverage.lines_fn,- });- total_editable_context_files.accumulate(&ClassificationMetrics {- true_positives: coverage.files_tp,- false_positives: coverage.files_fp,- false_negatives: coverage.files_fn,- });- }-- // Accumulate token change metrics (only for predictions that produced a patch)+ // Token change percentiles need the raw per-prediction values, so+ // they are collected here rather than in `compute_summary`.let has_patch = example.predictions.get(score_idx)@@ -504,18 +395,6 @@ pub fn print_report(patch_inserted_tokens.push(score.inserted_tokens);patch_deleted_tokens.push(score.deleted_tokens);}-- // Accumulate cursor metrics- if let Some(exact_match) = score.cursor_exact_match {- cursor_total += 1;- if exact_match {- cursor_exact_matches += 1;- }- }- if let Some(dist) = score.cursor_distance {- cursor_distance_sum += dist;- cursor_distance_count += 1;- }}}@@ -528,68 +407,33 @@ pub fn print_report(}println!("{}", separator);- if !all_delta_chr_f_scores.is_empty() {- let avg_delta_chr_f: f32 =- all_delta_chr_f_scores.iter().sum::<f32>() / all_delta_chr_f_scores.len() as f32;- let avg_reversal_ratio: f32 =- all_reversal_ratios.iter().sum::<f32>() / all_reversal_ratios.len() as f32;- let braces_disbalance_avg: f32 = braces_disbalance_sum as f32 / total_scores as f32;-- let qa_reverts_str = if qa_reverts_total > 0 {- format!(- "{:.1}%",- qa_reverts_count as f32 / qa_reverts_total as f32 * 100.0- )- } else {- "-".to_string()- };- let qa_conf_str = if qa_confidence_count > 0 {- format!(- "{:.1}",- qa_confidence_sum as f32 / qa_confidence_count as f32- )- } else {- "-".to_string()- };- let cursor_str = if cursor_total > 0 {- format!(- "{:.0}%",- cursor_exact_matches as f32 / cursor_total as f32 * 100.0- )- } else {- "-".to_string()- };- let wrong_er_str = if wrong_editable_region_total > 0 {- format!(- "{:.2}%",- wrong_editable_region_count as f32 / wrong_editable_region_total as f32 * 100.0- )- } else {- "-".to_string()- };- let isolated_ws_str = if total_scores > 0 {- format!(- "{}/{} ({:.1}%)",- isolated_whitespace_count,- total_scores,- isolated_whitespace_count as f32 / total_scores as f32 * 100.0- )- } else {- "-".to_string()- };- let avg_cursor_distance = if cursor_distance_count > 0 {- Some(cursor_distance_sum as f32 / cursor_distance_count as f32)- } else {- None+ let summary = compute_summary(+ examples,+ retrieved_context_byte_limit,+ context_source_filter,+ );++ if summary.total_examples > 0 {+ let total_scores = summary.total_examples;+ let format_rate = |rate: Option<f32>, precision: usize| {+ rate.map(|rate| format!("{:.*}%", precision, rate * 100.0))+ .unwrap_or_else(|| "-".to_string())};+ let qa_reverts_str = format_rate(summary.qa_avg_reverts_edits, 1);+ let qa_conf_str = summary+ .qa_avg_confidence+ .map(|confidence| format!("{:.1}", confidence))+ .unwrap_or_else(|| "-".to_string());+ let cursor_str = format_rate(summary.cursor_exact_match_rate, 0);+ let wrong_er_str = format_rate(summary.wrong_editable_region_rate, 2);println!("{:<40} {:>8.2} {:>5.1} {:>6.1}% {:>6.1}% {:>7} {:>7} {:>6} {:>5}","TOTAL / AVERAGE",- avg_delta_chr_f,- braces_disbalance_avg,- total_exact_lines.f1() * 100.0,- avg_reversal_ratio * 100.0,+ summary.avg_delta_chr_f,+ summary.avg_braces_disbalance,+ summary.exact_lines_f1 * 100.0,+ summary.avg_reversal_ratio * 100.0,qa_reverts_str,qa_conf_str,cursor_str,@@ -598,81 +442,108 @@ pub fn print_report(println!("{}", separator);println!("Delta chrF (β={:.1}): TP={}, FP={}, FN={}, P={:.1}%, R={:.1}%",- delta_chr_f_beta,- total_delta_chr_f.true_positives,- total_delta_chr_f.false_positives,- total_delta_chr_f.false_negatives,- total_delta_chr_f_precision / total_scores as f64 * 100.0,- total_delta_chr_f_recall / total_scores as f64 * 100.0+ summary.delta_chr_f_beta,+ summary.delta_chr_f_true_positives,+ summary.delta_chr_f_false_positives,+ summary.delta_chr_f_false_negatives,+ summary.delta_chr_f_precision * 100.0,+ summary.delta_chr_f_recall * 100.0);- // Print additional cursor metrics if available- if let Some(avg_dist) = avg_cursor_distance {+ if let Some(avg_distance) = summary.cursor_avg_distance {println!("Cursor: {}/{} exact matches ({:.0}%), avg distance: {:.1} bytes",- cursor_exact_matches,- cursor_total,- cursor_exact_matches as f32 / cursor_total as f32 * 100.0,- avg_dist+ summary.cursor_exact_matches.unwrap_or(0),+ summary.cursor_total_evaluated.unwrap_or(0),+ summary.cursor_exact_match_rate.unwrap_or(0.0) * 100.0,+ avg_distance);}- // Print isolated whitespace metrics- if total_scores > 0 {- println!("Isolated whitespace changes: {}", isolated_ws_str);+ if let (Some(count), Some(rate)) = (+ summary.isolated_whitespace_count,+ summary.isolated_whitespace_rate,+ ) {+ println!(+ "Isolated whitespace changes: {}/{} ({:.1}%)",+ count,+ total_scores,+ rate * 100.0+ );}- // Print kept and recall rate metrics- if kept_rate_count > 0 {- let avg_kept_rate = kept_rate_sum / kept_rate_count as f64;+ if let (Some(avg_kept_rate), Some(evaluated)) =+ (summary.avg_kept_rate, summary.kept_rate_examples)+ {println!("Kept rate: {:.1}% avg ({} evaluated, kept chars: {}, correctly deleted chars: {}, discarded chars: {})",avg_kept_rate * 100.0,- kept_rate_count,- kept_chars_total,- correctly_deleted_chars_total,- discarded_chars_total+ evaluated,+ summary.total_kept_chars.unwrap_or(0),+ summary.total_correctly_deleted_chars.unwrap_or(0),+ summary.total_discarded_chars.unwrap_or(0));}- if recall_rate_count > 0 {- let avg_recall_rate = recall_rate_sum / recall_rate_count as f64;+ if let (Some(avg_recall_rate), Some(evaluated)) =+ (summary.avg_recall_rate, summary.recall_rate_examples)+ {println!("Recall rate: {:.1}% avg ({} evaluated)",avg_recall_rate * 100.0,- recall_rate_count+ evaluated);}- if retrieved_context_bytes_count > 0 {+ if let (Some(avg_bytes), Some(example_count)) = (+ summary.avg_retrieved_context_bytes,+ summary.retrieved_context_examples,+ ) {println!("Retrieved context size: {:.0} bytes avg ({} examples)",- retrieved_context_bytes_sum / retrieved_context_bytes_count as f64,- retrieved_context_bytes_count- );- }- if editable_context_coverage_count > 0 {- let count = editable_context_coverage_count as f64;- println!(- "Editable context lines: P={:.1}%, R={:.1}%, F1={:.1}% avg ({} evaluated, TP={}, FP={}, FN={})",- editable_context_lines_precision_sum / count * 100.0,- editable_context_lines_recall_sum / count * 100.0,- editable_context_lines_f1_sum / count * 100.0,- editable_context_coverage_count,- total_editable_context_lines.true_positives,- total_editable_context_lines.false_positives,- total_editable_context_lines.false_negatives- );- println!(- "Editable context files: P={:.1}%, R={:.1}%, F1={:.1}% avg ({} evaluated, TP={}, FP={}, FN={})",- editable_context_files_precision_sum / count * 100.0,- editable_context_files_recall_sum / count * 100.0,- editable_context_files_f1_sum / count * 100.0,- editable_context_coverage_count,- total_editable_context_files.true_positives,- total_editable_context_files.false_positives,- total_editable_context_files.false_negatives+ avg_bytes, example_count);}+ print_prf_line(+ "Editable context lines",+ summary.editable_context_examples,+ summary.avg_editable_context_lines_precision,+ summary.avg_editable_context_lines_recall,+ summary.avg_editable_context_lines_f1,+ summary.editable_context_lines_tp,+ summary.editable_context_lines_fp,+ summary.editable_context_lines_fn,+ );+ print_prf_line(+ "Editable context files",+ summary.editable_context_examples,+ summary.avg_editable_context_files_precision,+ summary.avg_editable_context_files_recall,+ summary.avg_editable_context_files_f1,+ summary.editable_context_files_tp,+ summary.editable_context_files_fp,+ summary.editable_context_files_fn,+ );+ print_prf_line(+ "Jump location lines",+ summary.jump_location_examples,+ summary.avg_jump_location_lines_precision,+ summary.avg_jump_location_lines_recall,+ summary.avg_jump_location_lines_f1,+ summary.jump_location_lines_tp,+ summary.jump_location_lines_fp,+ summary.jump_location_lines_fn,+ );+ print_prf_line(+ "Jump location files",+ summary.jump_location_examples,+ summary.avg_jump_location_files_precision,+ summary.avg_jump_location_files_recall,+ summary.avg_jump_location_files_f1,+ summary.jump_location_files_tp,+ summary.jump_location_files_fp,+ summary.jump_location_files_fn,+ );+// Print token change percentile summary (only for predictions with a patch)if !patch_inserted_tokens.is_empty() {patch_inserted_tokens.sort_unstable();@@ -737,8 +608,6 @@ fn print_context_coverage_report(const MAX_EXAMPLES_DEFAULT: usize = 20;const LINE_WIDTH: usize = 120;- use crate::metrics::ClassificationMetrics;-let separator = "─".repeat(LINE_WIDTH);println!("{}", separator);println!(@@ -759,28 +628,10 @@ fn print_context_coverage_report();println!("{}", separator);- let mut total_lines = ClassificationMetrics::default();- let mut total_files = ClassificationMetrics::default();- let mut line_precision_sum = 0.0;- let mut line_recall_sum = 0.0;- let mut line_f1_sum = 0.0;- let mut file_precision_sum = 0.0;- let mut file_recall_sum = 0.0;- let mut file_f1_sum = 0.0;- let mut total_scores = 0;- let mut retrieved_context_bytes_sum = 0.0;- let mut retrieved_context_bytes_count = 0;let mut printed_lines = 0;let mut skipped_lines = 0;for example in examples {- if let Some(bytes) =- retrieved_context_bytes(example, retrieved_context_byte_limit, context_source_filter)- {- retrieved_context_bytes_sum += bytes as f64;- retrieved_context_bytes_count += 1;- }-for score in &example.score {let Some(coverage) = &score.editable_context_coverage else {continue;@@ -807,24 +658,6 @@ fn print_context_coverage_report(} else {skipped_lines += 1;}-- total_scores += 1;- line_precision_sum += coverage.lines_precision;- line_recall_sum += coverage.lines_recall;- line_f1_sum += coverage.lines_f1;- file_precision_sum += coverage.files_precision;- file_recall_sum += coverage.files_recall;- file_f1_sum += coverage.files_f1;- total_lines.accumulate(&ClassificationMetrics {- true_positives: coverage.lines_tp,- false_positives: coverage.lines_fp,- false_negatives: coverage.lines_fn,- });- total_files.accumulate(&ClassificationMetrics {- true_positives: coverage.files_tp,- false_positives: coverage.files_fp,- false_negatives: coverage.files_fn,- });}}@@ -838,34 +671,41 @@ fn print_context_coverage_report(println!("{}", separator);- if total_scores > 0 {- let count = total_scores as f64;+ let summary = compute_summary(+ examples,+ retrieved_context_byte_limit,+ context_source_filter,+ );++ if let Some(total_scores) = summary.editable_context_examples {println!("{:<40} {:>5.1}% {:>5.1}% {:>5.1}% {:>5} {:>5} {:>5} {:>5.1}% {:>5.1}% {:>5.1}% {:>5} {:>5} {:>5}","TOTAL / AVERAGE",- line_precision_sum / count * 100.0,- line_recall_sum / count * 100.0,- line_f1_sum / count * 100.0,- total_lines.true_positives,- total_lines.false_positives,- total_lines.false_negatives,- file_precision_sum / count * 100.0,- file_recall_sum / count * 100.0,- file_f1_sum / count * 100.0,- total_files.true_positives,- total_files.false_positives,- total_files.false_negatives+ summary.avg_editable_context_lines_precision.unwrap_or(0.0) * 100.0,+ summary.avg_editable_context_lines_recall.unwrap_or(0.0) * 100.0,+ summary.avg_editable_context_lines_f1.unwrap_or(0.0) * 100.0,+ summary.editable_context_lines_tp.unwrap_or(0),+ summary.editable_context_lines_fp.unwrap_or(0),+ summary.editable_context_lines_fn.unwrap_or(0),+ summary.avg_editable_context_files_precision.unwrap_or(0.0) * 100.0,+ summary.avg_editable_context_files_recall.unwrap_or(0.0) * 100.0,+ summary.avg_editable_context_files_f1.unwrap_or(0.0) * 100.0,+ summary.editable_context_files_tp.unwrap_or(0),+ summary.editable_context_files_fp.unwrap_or(0),+ summary.editable_context_files_fn.unwrap_or(0));println!("{}", separator);println!("Evaluated editable context coverage for {} examples",total_scores);- if retrieved_context_bytes_count > 0 {+ if let (Some(avg_bytes), Some(example_count)) = (+ summary.avg_retrieved_context_bytes,+ summary.retrieved_context_examples,+ ) {println!("Retrieved context size: {:.0} bytes avg ({} examples)",- retrieved_context_bytes_sum / retrieved_context_bytes_count as f64,- retrieved_context_bytes_count+ avg_bytes, example_count);}}@@ -873,6 +713,51 @@ fn print_context_coverage_report(println!("\n");}+/// Print one "P/R/F1 avg + pooled TP/FP/FN" summary line, or nothing when+/// the metric was never evaluated.+fn print_prf_line(+ label: &str,+ examples: Option<usize>,+ precision: Option<f64>,+ recall: Option<f64>,+ f1: Option<f64>,+ true_positives: Option<usize>,+ false_positives: Option<usize>,+ false_negatives: Option<usize>,+) {+ let (+ Some(examples),+ Some(precision),+ Some(recall),+ Some(f1),+ Some(true_positives),+ Some(false_positives),+ Some(false_negatives),+ ) = (+ examples,+ precision,+ recall,+ f1,+ true_positives,+ false_positives,+ false_negatives,+ )+ else {+ return;+ };+ println!(+ "{}: P={:.1}%, R={:.1}%, F1={:.1}% avg ({} evaluated, TP={}, FP={}, FN={})",+ label,+ precision * 100.0,+ recall * 100.0,+ f1 * 100.0,+ examples,+ true_positives,+ false_positives,+ false_negatives+ );+}+fn percentile(sorted_values: &[usize], p: usize) -> usize {if sorted_values.is_empty() {return 0;diff --git a/crates/edit_prediction_context/src/edit_prediction_context.rs b/crates/edit_prediction_context/src/edit_prediction_context.rsindex 44e295908af..3069c13b65d 100644--- a/crates/edit_prediction_context/src/edit_prediction_context.rs+++ b/crates/edit_prediction_context/src/edit_prediction_context.rs@@ -29,7 +29,8 @@ mod fake_definition_lsp;mod git_log_context;pub use editable_context::{- EditHistoryContextEntry, collect_editable_context, limit_retrieved_context_to_bytes,+ EditHistoryContextEntry, OracleTarget, collect_editable_context,+ limit_retrieved_context_to_bytes,};pub use zeta_prompt::{ContextSource, RelatedExcerpt, RelatedFile};diff --git a/crates/edit_prediction_context/src/editable_context.rs b/crates/edit_prediction_context/src/editable_context.rsindex 1aede0ce65a..f985b5dbb07 100644--- a/crates/edit_prediction_context/src/editable_context.rs+++ b/crates/edit_prediction_context/src/editable_context.rs@@ -9,7 +9,7 @@ use std::{};use text::Anchor;use util::{paths::PathStyle, rel_path::RelPath};-use zeta_prompt::{ContextSource, RelatedExcerpt, RelatedFile};+use zeta_prompt::{ContextSource, RelatedExcerpt, RelatedFile, multi_region::is_good_block_start};use crate::{bm25_context::{Bm25ContextCandidate, collect_bm25_context},@@ -22,6 +22,15 @@ const CURSOR_CONTEXT_LINE_COUNT: u32 = 20;const EDIT_HISTORY_CONTEXT_LINE_COUNT: u32 = 20;const GIT_LOG_CONTEXT_LINE_COUNT: u32 = 10000;const GIT_LOG_CONTEXT_FILE_COUNT: usize = 10;+const ORACLE_SNIPPET_MIN_CONTEXT_LINE_COUNT: u32 = 10;+const ORACLE_SNIPPET_MAX_CONTEXT_LINE_COUNT: u32 = 40;+/// How far excerpt boundaries may be nudged to land on a natural block+/// boundary, mirroring `zeta_prompt::multi_region`'s marker placement.+const BOUNDARY_SNAP_LINE_COUNT: u32 = 5;+/// Maximum number of rows between two excerpts of the same buffer that get+/// bridged into one contiguous excerpt instead of rendering an elision+/// marker between them.+const BRIDGED_GAP_LINE_COUNT: u32 = 3;type RangesByBuffer = HashMap<EntityId, (Entity<Buffer>, Vec<EditableContextRange>)>;@@ -31,6 +40,16 @@ pub struct EditHistoryContextEntry {pub edited_range: Range<Anchor>,}+/// A file known (from expected patches) to be edited next, used by the+/// oracle context sources when generating training data.+#[derive(Clone, Debug)]+pub struct OracleTarget {+ pub path: Arc<Path>,+ /// 0-based, end-exclusive row ranges of the expected edit hunks.+ /// Only used by `ContextSource::OracleSnippet`.+ pub row_ranges: Vec<Range<u32>>,+}+struct EditableContextRange {range: Range<Anchor>,order: usize,@@ -48,7 +67,7 @@ pub async fn collect_editable_context(active_buffer: Entity<Buffer>,cursor_position: Anchor,edit_history: Vec<EditHistoryContextEntry>,- oracle_paths: Vec<Arc<Path>>,+ oracle_targets: Vec<OracleTarget>,context_sources: Vec<ContextSource>,cx: &mut AsyncApp,) -> anyhow::Result<Vec<RelatedFile>> {@@ -81,6 +100,14 @@ pub async fn collect_editable_context(.await;}+ // Collected before bm25 so that, under a related-files byte budget, the+ // small snippets containing the expected edits are never trimmed away in+ // favor of bm25 excerpts.+ if context_sources.contains(&ContextSource::OracleSnippet) {+ collect_oracle_snippet_context(&mut ranges_by_buffer, project.clone(), &oracle_targets, cx)+ .await;+ }+if context_sources.contains(&ContextSource::Bm25) {collect_bm25_context_ranges(&mut ranges_by_buffer,@@ -94,7 +121,8 @@ pub async fn collect_editable_context(}if context_sources.contains(&ContextSource::OracleFile) {- collect_oracle_file_context(&mut ranges_by_buffer, project.clone(), oracle_paths, cx).await;+ collect_oracle_file_context(&mut ranges_by_buffer, project.clone(), &oracle_targets, cx)+ .await;}Ok(cx.update(|cx| {@@ -405,24 +433,24 @@ fn anchor_range_for_row_range(async fn collect_oracle_file_context(ranges_by_buffer: &mut RangesByBuffer,project: Entity<Project>,- oracle_paths: Vec<Arc<Path>>,+ oracle_targets: &[OracleTarget],cx: &mut AsyncApp,) {let next_order = next_context_order(ranges_by_buffer);let mut seen_buffers = HashSet::default();let mut index = 0;- for path in oracle_paths {- let buffer = match open_buffer_for_path(&project, &path, cx).await {+ for target in oracle_targets {+ let buffer = match open_buffer_for_path(&project, &target.path, cx).await {Ok(Some(buffer)) => buffer,Ok(None) => {- log::debug!("failed to find oracle file path: {}", path.display());+ log::debug!("failed to find oracle file path: {}", target.path.display());continue;}Err(error) => {log::debug!("failed to open oracle file path {}: {error:#}",- path.display()+ target.path.display());continue;}@@ -443,6 +471,89 @@ async fn collect_oracle_file_context(}}+async fn collect_oracle_snippet_context(+ ranges_by_buffer: &mut RangesByBuffer,+ project: Entity<Project>,+ oracle_targets: &[OracleTarget],+ cx: &mut AsyncApp,+) {+ let next_order = next_context_order(ranges_by_buffer);+ let mut index = 0;++ for target in oracle_targets {+ if target.row_ranges.is_empty() {+ continue;+ }++ let buffer = match open_buffer_for_path(&project, &target.path, cx).await {+ Ok(Some(buffer)) => buffer,+ Ok(None) => {+ log::debug!(+ "failed to find oracle snippet path: {}",+ target.path.display()+ );+ continue;+ }+ Err(error) => {+ log::debug!(+ "failed to open oracle snippet path {}: {error:#}",+ target.path.display()+ );+ continue;+ }+ };++ for row_range in &target.row_ranges {+ let Some(range) = buffer.read_with(cx, |buffer, _cx| {+ let snapshot = buffer.snapshot();+ let padding_above = oracle_snippet_padding(&target.path, row_range.start, 0);+ let padding_below = oracle_snippet_padding(&target.path, row_range.end, 1);+ let start_row = row_range.start.saturating_sub(padding_above);+ // Empty hunk row ranges (pure insertions) still cover one row.+ let core_end_row = row_range.end.max(row_range.start + 1);+ let end_row = core_end_row.saturating_add(padding_below);+ let start_row =+ snap_start_row_to_block_boundary(&snapshot, start_row, row_range.start);+ // `end_row` is exclusive while snapping operates on the last+ // included row.+ let end_row = snap_end_row_to_block_boundary(+ &snapshot,+ end_row.saturating_sub(1),+ core_end_row.saturating_sub(1),+ ) + 1;+ anchor_range_for_row_range(&snapshot, start_row..end_row)+ }) else {+ continue;+ };++ push_context_range(+ ranges_by_buffer,+ buffer.clone(),+ range,+ next_order + index,+ ContextSource::OracleSnippet,+ );+ index += 1;+ }+ }+}++/// Deterministic pseudo-random padding, so that the expected edit is not+/// always centered in the snippet (a student model could otherwise learn the+/// excerpt center as a position prior), while keeping context retrieval+/// reproducible across runs.+fn oracle_snippet_padding(path: &Path, row: u32, salt: u32) -> u32 {+ use std::hash::{Hash as _, Hasher as _};++ let mut hasher = collections::FxHasher::default();+ path.hash(&mut hasher);+ row.hash(&mut hasher);+ salt.hash(&mut hasher);+ let span =+ u64::from(ORACLE_SNIPPET_MAX_CONTEXT_LINE_COUNT - ORACLE_SNIPPET_MIN_CONTEXT_LINE_COUNT);+ ORACLE_SNIPPET_MIN_CONTEXT_LINE_COUNT + (hasher.finish() % (span + 1)) as u32+}+async fn open_buffer_for_path(project: &Entity<Project>,path: &Path,@@ -588,11 +699,71 @@ fn expanded_anchor_range(.row.saturating_add(context_line_count).min(snapshot.max_point().row);+ let start_row = snap_start_row_to_block_boundary(snapshot, start_row, start.row);+ let end_row = snap_end_row_to_block_boundary(snapshot, end_row, end.row);let start = snapshot.anchor_before(Point::new(start_row, 0));let end = snapshot.anchor_after(Point::new(end_row, snapshot.line_len(end_row)));start..end}+/// Nudge an excerpt's first row forward (up to `BOUNDARY_SNAP_LINE_COUNT`+/// lines, never past `core_row`) so the excerpt starts at a natural block+/// boundary: preferably a good block start right after blank line(s), or+/// failing that any good block start. Mirrors the marker placement+/// heuristics of `zeta_prompt::multi_region`.+fn snap_start_row_to_block_boundary(+ snapshot: &text::BufferSnapshot,+ row: u32,+ core_row: u32,+) -> u32 {+ let limit = core_row+ .min(row.saturating_add(BOUNDARY_SNAP_LINE_COUNT))+ .min(snapshot.max_point().row);+ let mut first_good_start = None;+ for candidate in row..=limit {+ if snapshot.is_line_blank(candidate) {+ continue;+ }+ if !is_good_block_start(line_text(snapshot, candidate).trim()) {+ continue;+ }+ if candidate > 0 && snapshot.is_line_blank(candidate - 1) {+ return candidate;+ }+ if first_good_start.is_none() {+ first_good_start = Some(candidate);+ }+ }+ first_good_start.unwrap_or(row)+}++/// Nudge an excerpt's last row backward (up to `BOUNDARY_SNAP_LINE_COUNT`+/// lines, never before `core_row`) so the excerpt ends at the last non-blank+/// line before a blank line or at the end of the file.+fn snap_end_row_to_block_boundary(snapshot: &text::BufferSnapshot, row: u32, core_row: u32) -> u32 {+ let max_row = snapshot.max_point().row;+ let row = row.min(max_row);+ if row == max_row {+ return row;+ }+ let limit = core_row.max(row.saturating_sub(BOUNDARY_SNAP_LINE_COUNT));+ for candidate in (limit..=row).rev() {+ if snapshot.is_line_blank(candidate) {+ continue;+ }+ if snapshot.is_line_blank(candidate + 1) {+ return candidate;+ }+ }+ row+}++fn line_text(snapshot: &text::BufferSnapshot, row: u32) -> String {+ snapshot+ .text_for_range(Point::new(row, 0)..Point::new(row, snapshot.line_len(row)))+ .collect()+}+fn push_context_range(ranges_by_buffer: &mut RangesByBuffer,buffer: Entity<Buffer>,@@ -628,9 +799,10 @@ fn related_file_for_ranges()).into();- let ranges = resolved_context_ranges(ranges, &snapshot);+ let mut ranges = resolved_context_ranges(ranges, &snapshot);+ split_overlapping_ranges(&mut ranges);- let mut excerpts = ranges+ let excerpts = ranges.into_iter().map(|range| RelatedExcerpt {row_range: range.range.start.row..range.range.end.row,@@ -642,7 +814,6 @@ fn related_file_for_ranges(context_source: range.context_source,}).collect::<Vec<_>>();- excerpts.sort_by_key(|excerpt| excerpt.order);Some(RelatedFile {path,@@ -674,29 +845,124 @@ fn resolved_context_ranges(.collect()}-#[allow(dead_code)]-fn merge_overlapping_ranges(ranges: &mut Vec<ResolvedEditableContextRange>) {+/// Split overlapping ranges into disjoint segments instead of merging them+/// into one range. Each segment keeps the minimum order and highest-priority+/// source among the ranges covering it, and adjacent segments with equal+/// order are coalesced. This preserves priority granularity: a small+/// high-priority snippet inside a large low-priority range remains its own+/// excerpt, so byte-budget selection can retain it even when the surrounding+/// range doesn't fit. The resulting segments are disjoint and sorted by+/// position.+///+/// Ranges separated by at most `BRIDGED_GAP_LINE_COUNT` rows are bridged:+/// the small gap is attached to the preceding segment so the excerpts render+/// as one contiguous block instead of being separated by an elision marker.+fn split_overlapping_ranges(ranges: &mut Vec<ResolvedEditableContextRange>) {ranges.sort_by_key(|range| (range.range.start, range.range.end));- let mut merged: Vec<ResolvedEditableContextRange> = Vec::new();+ let mut output: Vec<ResolvedEditableContextRange> = Vec::new();+ let mut cluster: Vec<ResolvedEditableContextRange> = Vec::new();+ let mut cluster_end = Point::zero();for range in ranges.drain(..) {- if let Some(last_range) = merged.last_mut()- && range.range.start <= last_range.range.end- {- if context_source_order(range.context_source)- < context_source_order(last_range.context_source)+ let bridge_limit_row = row_aligned_end(cluster_end)+ .row+ .saturating_add(BRIDGED_GAP_LINE_COUNT);+ if cluster.is_empty() || range.range.start.row <= bridge_limit_row {+ cluster_end = cluster_end.max(range.range.end);+ cluster.push(range);+ } else {+ split_cluster(std::mem::take(&mut cluster), cluster_end, &mut output);+ cluster_end = range.range.end;+ cluster.push(range);+ }+ }+ if !cluster.is_empty() {+ split_cluster(cluster, cluster_end, &mut output);+ }++ *ranges = output;+}++fn split_cluster(+ cluster: Vec<ResolvedEditableContextRange>,+ cluster_end: Point,+ output: &mut Vec<ResolvedEditableContextRange>,+) {+ if cluster.len() == 1 {+ output.extend(cluster);+ return;+ }++ let cluster_start = cluster[0].range.start;+ let mut boundaries = Vec::with_capacity(cluster.len() * 2 + 1);+ boundaries.push(cluster_start);+ for range in &cluster {+ boundaries.push(range.range.start);+ boundaries.push(row_aligned_end(range.range.end));+ }+ boundaries.retain(|boundary| *boundary >= cluster_start && *boundary < cluster_end);+ boundaries.sort();+ boundaries.dedup();+ boundaries.push(cluster_end);++ for window in boundaries.windows(2) {+ let segment = window[0]..window[1];+ // The segment is attributed to the lowest-order covering range+ // (breaking ties by source priority), so that its source label is+ // consistent with the order that drives budget selection.+ let mut order_and_source: Option<(usize, ContextSource)> = None;+ for range in &cluster {+ if range.range.start <= segment.start && row_aligned_end(range.range.end) >= segment.end{- last_range.context_source = range.context_source;+ let candidate = (range.order, range.context_source);+ if order_and_source.is_none_or(|(order, context_source)| {+ (candidate.0, context_source_order(candidate.1))+ < (order, context_source_order(context_source))+ }) {+ order_and_source = Some(candidate);+ }+ }+ }+ let Some((order, context_source)) = order_and_source else {+ // A bridged gap between two nearby ranges: no range covers it, so+ // attach it to the preceding segment to form contiguous output.+ if let Some(last) = output.last_mut()+ && last.range.end == segment.start+ {+ last.range.end = segment.end;+ }+ continue;+ };++ if let Some(last) = output.last_mut()+ && last.range.end == segment.start+ && last.order == order+ {+ last.range.end = segment.end;+ if context_source_order(context_source) < context_source_order(last.context_source) {+ last.context_source = context_source;}- last_range.range.end = last_range.range.end.max(range.range.end);- last_range.order = last_range.order.min(range.order);continue;}- merged.push(range);+ output.push(ResolvedEditableContextRange {+ range: segment,+ order,+ context_source,+ });}+}- *ranges = merged;+/// Context ranges always cover whole lines: their ends sit either at a line+/// start (column 0) or at the end of a line's content. Cutting a segment at+/// the end of a line's content would attribute the trailing newline to the+/// next segment, so nudge such ends forward to the next line start.+fn row_aligned_end(point: Point) -> Point {+ if point.column > 0 {+ Point::new(point.row + 1, 0)+ } else {+ point+ }}#[allow(dead_code)]@@ -716,6 +982,257 @@ fn context_source_order(context_source: ContextSource) -> usize {ContextSource::EditHistoryFile => 4,ContextSource::GitLog => 5,ContextSource::Bm25 => 6,- ContextSource::OracleFile => 7,+ ContextSource::OracleSnippet => 7,+ ContextSource::OracleFile => 8,+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn text_snapshot(text: &str) -> text::BufferSnapshot {+ text::Buffer::new(+ text::ReplicaId::LOCAL,+ text::BufferId::new(1).unwrap(),+ text,+ )+ .snapshot()+ .clone()+ }++ fn resolved_range(+ start_row: u32,+ end_row: u32,+ order: usize,+ context_source: ContextSource,+ ) -> ResolvedEditableContextRange {+ ResolvedEditableContextRange {+ range: Point::new(start_row, 0)..Point::new(end_row, 0),+ order,+ context_source,+ }+ }++ #[test]+ fn test_split_overlapping_ranges_coalesces_equal_orders() {+ // A full-file current-file range plus edit-history windows inside it:+ // every segment is covered by the order-0 full-file range, so they+ // coalesce back into a single range with the highest-priority source.+ let mut ranges = vec![+ resolved_range(6, 46, 1, ContextSource::EditHistory),+ resolved_range(0, 281, 0, ContextSource::CurrentFile),+ resolved_range(17, 79, 2, ContextSource::EditHistory),+ resolved_range(85, 125, 3, ContextSource::EditHistory),+ ];+ split_overlapping_ranges(&mut ranges);+ assert_eq!(ranges.len(), 1);+ assert_eq!(ranges[0].range, Point::new(0, 0)..Point::new(281, 0));+ assert_eq!(ranges[0].order, 0);+ assert_eq!(ranges[0].context_source, ContextSource::CurrentFile);+ }++ #[test]+ fn test_split_overlapping_ranges_keeps_disjoint_ranges() {+ let mut ranges = vec![+ resolved_range(50, 60, 1, ContextSource::EditHistory),+ resolved_range(0, 10, 0, ContextSource::CursorExcerpt),+ resolved_range(5, 12, 2, ContextSource::EditHistory),+ ];+ split_overlapping_ranges(&mut ranges);+ assert_eq!(ranges.len(), 3);+ assert_eq!(ranges[0].range, Point::new(0, 0)..Point::new(10, 0));+ assert_eq!(ranges[0].order, 0);+ assert_eq!(ranges[0].context_source, ContextSource::CursorExcerpt);+ assert_eq!(ranges[1].range, Point::new(10, 0)..Point::new(12, 0));+ assert_eq!(ranges[1].order, 2);+ assert_eq!(ranges[1].context_source, ContextSource::EditHistory);+ assert_eq!(ranges[2].range, Point::new(50, 0)..Point::new(60, 0));+ assert_eq!(ranges[2].order, 1);+ }++ #[test]+ fn test_split_overlapping_ranges_keeps_adjacent_ranges_with_distinct_orders() {+ let mut ranges = vec![+ resolved_range(0, 10, 0, ContextSource::EditHistory),+ resolved_range(10, 20, 1, ContextSource::EditHistory),+ ];+ split_overlapping_ranges(&mut ranges);+ assert_eq!(ranges.len(), 2);+ assert_eq!(ranges[0].range, Point::new(0, 0)..Point::new(10, 0));+ assert_eq!(ranges[0].order, 0);+ assert_eq!(ranges[1].range, Point::new(10, 0)..Point::new(20, 0));+ assert_eq!(ranges[1].order, 1);+ }++ #[test]+ fn test_split_overlapping_ranges_preserves_high_priority_snippet_inside_large_range() {+ // A small high-priority snippet inside a large low-priority range+ // stays its own segment, so byte-budget selection can keep it even+ // when the surrounding range doesn't fit.+ let mut ranges = vec![+ resolved_range(0, 200, 8, ContextSource::GitLog),+ resolved_range(50, 60, 2, ContextSource::OracleSnippet),+ ];+ split_overlapping_ranges(&mut ranges);+ assert_eq!(ranges.len(), 3);+ assert_eq!(ranges[0].range, Point::new(0, 0)..Point::new(50, 0));+ assert_eq!(ranges[0].order, 8);+ assert_eq!(ranges[0].context_source, ContextSource::GitLog);+ assert_eq!(ranges[1].range, Point::new(50, 0)..Point::new(60, 0));+ assert_eq!(ranges[1].order, 2);+ assert_eq!(ranges[1].context_source, ContextSource::OracleSnippet);+ assert_eq!(ranges[2].range, Point::new(60, 0)..Point::new(200, 0));+ assert_eq!(ranges[2].order, 8);+ assert_eq!(ranges[2].context_source, ContextSource::GitLog);+ }++ #[test]+ fn test_split_overlapping_ranges_aligns_mid_line_ends_to_row_starts() {+ // Ranges ending at a line's content end (column > 0) are treated as+ // covering through that whole line, so equal-order overlapping ranges+ // still coalesce into one segment.+ let mut ranges = vec![+ ResolvedEditableContextRange {+ range: Point::new(0, 0)..Point::new(10, 5),+ order: 1,+ context_source: ContextSource::EditHistory,+ },+ resolved_range(5, 20, 1, ContextSource::EditHistory),+ ];+ split_overlapping_ranges(&mut ranges);+ assert_eq!(ranges.len(), 1);+ assert_eq!(ranges[0].range, Point::new(0, 0)..Point::new(20, 0));+ assert_eq!(ranges[0].order, 1);+ }++ #[test]+ fn test_split_overlapping_ranges_bridges_small_gaps() {+ // Ranges separated by a few rows are bridged: the gap is attached to+ // the preceding segment, producing contiguous excerpts.+ let mut ranges = vec![+ resolved_range(0, 10, 0, ContextSource::EditHistory),+ resolved_range(13, 20, 1, ContextSource::Bm25),+ ];+ split_overlapping_ranges(&mut ranges);+ assert_eq!(ranges.len(), 2);+ assert_eq!(ranges[0].range, Point::new(0, 0)..Point::new(13, 0));+ assert_eq!(ranges[0].order, 0);+ assert_eq!(ranges[0].context_source, ContextSource::EditHistory);+ assert_eq!(ranges[1].range, Point::new(13, 0)..Point::new(20, 0));+ assert_eq!(ranges[1].order, 1);+ assert_eq!(ranges[1].context_source, ContextSource::Bm25);+ }++ #[test]+ fn test_split_overlapping_ranges_bridged_equal_orders_coalesce() {+ let mut ranges = vec![+ resolved_range(0, 10, 1, ContextSource::EditHistory),+ resolved_range(12, 20, 1, ContextSource::EditHistory),+ ];+ split_overlapping_ranges(&mut ranges);+ assert_eq!(ranges.len(), 1);+ assert_eq!(ranges[0].range, Point::new(0, 0)..Point::new(20, 0));+ assert_eq!(ranges[0].order, 1);+ }++ #[test]+ fn test_split_overlapping_ranges_does_not_bridge_large_gaps() {+ let mut ranges = vec![+ resolved_range(0, 10, 0, ContextSource::EditHistory),+ resolved_range(14, 20, 1, ContextSource::Bm25),+ ];+ split_overlapping_ranges(&mut ranges);+ assert_eq!(ranges.len(), 2);+ assert_eq!(ranges[0].range, Point::new(0, 0)..Point::new(10, 0));+ assert_eq!(ranges[1].range, Point::new(14, 0)..Point::new(20, 0));+ }++ #[test]+ fn test_snap_start_row_prefers_block_start_after_blank_line() {+ let snapshot = text_snapshot(concat!(+ " body\n", // 0+ "}\n", // 1+ "\n", // 2+ "fn bar() {\n", // 3+ " body\n", // 4+ " core\n", // 5+ ));+ // Row 1 is a structural tail and row 2 is blank; row 3 follows a+ // blank line and is a good block start.+ assert_eq!(snap_start_row_to_block_boundary(&snapshot, 1, 5), 3);+ // Snapping never moves past the core row.+ assert_eq!(snap_start_row_to_block_boundary(&snapshot, 1, 2), 1);+ // A row that is already a good start after a blank line stays put.+ assert_eq!(snap_start_row_to_block_boundary(&snapshot, 3, 5), 3);+ }++ #[test]+ fn test_snap_start_row_falls_back_to_first_good_start() {+ let snapshot = text_snapshot(concat!(+ "}\n", // 0+ "let x = 1;\n", // 1+ "let y = 2;\n", // 2+ "core\n", // 3+ ));+ // No after-blank boundary in the window; the first good start wins.+ assert_eq!(snap_start_row_to_block_boundary(&snapshot, 0, 3), 1);+ }++ #[test]+ fn test_snap_end_row_ends_before_blank_line() {+ let snapshot = text_snapshot(concat!(+ "core\n", // 0+ "a\n", // 1+ "b\n", // 2+ "\n", // 3+ "c\n", // 4+ "d\n", // 5+ ));+ // Row 4 is followed by non-blank row 5, so scan back to row 2 which+ // precedes the blank row 3.+ assert_eq!(snap_end_row_to_block_boundary(&snapshot, 4, 0), 2);+ // Snapping never moves before the core row.+ assert_eq!(snap_end_row_to_block_boundary(&snapshot, 4, 4), 4);+ // The last row of the file stays put.+ let max_row = snapshot.max_point().row;+ assert_eq!(+ snap_end_row_to_block_boundary(&snapshot, max_row, 0),+ max_row+ );+ }++ #[test]+ fn test_limit_retrieved_context_keeps_high_priority_snippet_under_tight_budget() {+ let line = "0123456789\n";+ let excerpt = |row_range: Range<u32>, order: usize, context_source: ContextSource| {+ let text = line.repeat((row_range.end - row_range.start) as usize);+ RelatedExcerpt {+ row_range,+ text: text.into(),+ order,+ context_source,+ }+ };+ let related_files = vec![RelatedFile {+ path: Path::new("root/file.rs").into(),+ max_row: 200,+ excerpts: vec![+ excerpt(0..50, 8, ContextSource::GitLog),+ excerpt(50..60, 2, ContextSource::OracleSnippet),+ excerpt(60..200, 8, ContextSource::GitLog),+ ],+ in_open_source_repo: false,+ }];++ // Budget fits the snippet but not the surrounding segments.+ let limited = limit_retrieved_context_to_bytes(&related_files, 20 * line.len());+ assert_eq!(limited.len(), 1);+ assert_eq!(limited[0].excerpts.len(), 1);+ assert_eq!(limited[0].excerpts[0].row_range, 50..60);+ assert_eq!(+ limited[0].excerpts[0].context_source,+ ContextSource::OracleSnippet+ );}}diff --git a/crates/edit_prediction_metrics/src/edit_prediction_metrics.rs b/crates/edit_prediction_metrics/src/edit_prediction_metrics.rsindex 205075e2f5b..62b8207c420 100644--- a/crates/edit_prediction_metrics/src/edit_prediction_metrics.rs+++ b/crates/edit_prediction_metrics/src/edit_prediction_metrics.rs@@ -9,7 +9,10 @@ mod tokenize;#[cfg(feature = "tree-sitter")]mod tree_sitter;-pub use jumps::{EditableContextCoverage, Excerpt, editable_context_coverage};+pub use jumps::{+ EditableContextCoverage, Excerpt, LineFileClassification, PatchLocationMatch,+ editable_context_coverage, patch_location_match,+};pub use kept_rate::AnnotatedToken;pub use kept_rate::KeptRateResult;pub use kept_rate::TokenAnnotation;diff --git a/crates/edit_prediction_metrics/src/jumps.rs b/crates/edit_prediction_metrics/src/jumps.rsindex 8cfeafcc97a..607cfab5949 100644--- a/crates/edit_prediction_metrics/src/jumps.rs+++ b/crates/edit_prediction_metrics/src/jumps.rs@@ -4,6 +4,7 @@ use std::{};use crate::patch::{Patch, PatchLine};+use crate::patch_metrics::ClassificationMetrics;const LINE_RELEVANCE_WINDOW: u32 = 20;@@ -14,8 +15,13 @@ pub struct Excerpt {pub content: String,}+/// Line- and file-level precision/recall/F1 over TP/FP/FN counts.+///+/// Shared shape for two metrics with the same structure: how much expected+/// edit context was retrieved (`EditableContextCoverage`) and how well+/// predicted edit locations match expected ones (`PatchLocationMatch`).#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]-pub struct EditableContextCoverage {+pub struct LineFileClassification {pub lines_tp: usize,pub lines_fp: usize,pub lines_fn: usize,@@ -31,7 +37,10 @@ pub struct EditableContextCoverage {pub files_f1: f64,}-impl EditableContextCoverage {+pub type EditableContextCoverage = LineFileClassification;+pub type PatchLocationMatch = LineFileClassification;++impl LineFileClassification {pub fn new(lines_tp: usize,lines_fp: usize,@@ -55,6 +64,43 @@ impl EditableContextCoverage {files_f1: f1(files_tp, files_fp, files_fn),}}++ pub fn lines_counts(&self) -> ClassificationMetrics {+ ClassificationMetrics {+ true_positives: self.lines_tp,+ false_positives: self.lines_fp,+ false_negatives: self.lines_fn,+ }+ }++ pub fn files_counts(&self) -> ClassificationMetrics {+ ClassificationMetrics {+ true_positives: self.files_tp,+ false_positives: self.files_fp,+ false_negatives: self.files_fn,+ }+ }+}++/// Measures how well an actual patch's edited file/line locations match an expected patch.+pub fn patch_location_match(expected_patch: &str, actual_patch: &str) -> PatchLocationMatch {+ let expected_patch = Patch::parse_unified_diff(expected_patch);+ let actual_patch = Patch::parse_unified_diff(actual_patch);+ let (expected_files, expected_anchor_lines, _) = expected_context(&expected_patch);+ let (actual_files, mut actual_anchor_lines, _) = expected_context(&actual_patch);++ normalize_actual_paths(+ &expected_files,+ &mut actual_anchor_lines,+ actual_files.iter().cloned().collect(),+ );+ let actual_files = actual_anchor_lines.keys().cloned().collect();++ let (lines_tp, lines_fp, lines_fn) =+ match_anchor_rows(&expected_anchor_lines, &actual_anchor_lines);+ let (files_tp, files_fp, files_fn) = classify_values(&expected_files, &actual_files);++ PatchLocationMatch::new(lines_tp, lines_fp, lines_fn, files_tp, files_fp, files_fn)}/// Measures how much expected edit context was retrieved and how much unrelated context was retrieved.@@ -201,6 +247,87 @@ fn retrieved_context(context: &[Excerpt]) -> (BTreeSet<String>, BTreeMap<String,(retrieved_files, retrieved_lines)}+fn normalize_actual_paths(+ expected_files: &BTreeSet<String>,+ actual_anchor_lines: &mut BTreeMap<String, BTreeSet<u32>>,+ actual_files: BTreeSet<String>,+) {+ let mut normalized = BTreeMap::new();++ for actual_path in actual_files {+ let normalized_path = normalize_actual_path(expected_files, &actual_path);+ if let Some(rows) = actual_anchor_lines.remove(&actual_path) {+ normalized+ .entry(normalized_path)+ .or_insert_with(BTreeSet::new)+ .extend(rows);+ }+ }++ *actual_anchor_lines = normalized;+}++fn normalize_actual_path(expected_files: &BTreeSet<String>, actual_path: &str) -> String {+ if expected_files.contains(actual_path) {+ return actual_path.to_string();+ }++ if let Some(stripped_path) = strip_first_path_component(actual_path) {+ if expected_files.contains(stripped_path) {+ return stripped_path.to_string();+ }+ }++ actual_path.to_string()+}++fn strip_first_path_component(path: &str) -> Option<&str> {+ path.split_once('/')+ .map(|(_, rest)| rest)+ .filter(|rest| !rest.is_empty())+}++fn match_anchor_rows(+ expected: &BTreeMap<String, BTreeSet<u32>>,+ actual: &BTreeMap<String, BTreeSet<u32>>,+) -> (usize, usize, usize) {+ let mut true_positives = 0;+ let mut false_positives = 0;+ let mut false_negatives = 0;+ let paths = expected+ .keys()+ .chain(actual.keys())+ .cloned()+ .collect::<BTreeSet<_>>();++ for path in paths {+ let expected_rows = expected.get(&path).cloned().unwrap_or_default();+ let actual_rows = actual.get(&path).cloned().unwrap_or_default();+ let matched = match_rows_with_window(&expected_rows, &actual_rows);+ true_positives += matched;+ false_positives += actual_rows.len().saturating_sub(matched);+ false_negatives += expected_rows.len().saturating_sub(matched);+ }++ (true_positives, false_positives, false_negatives)+}++fn match_rows_with_window(expected: &BTreeSet<u32>, actual: &BTreeSet<u32>) -> usize {+ let mut unmatched_expected = expected.clone();+ let mut matched = 0;++ for actual_row in actual {+ let start = actual_row.saturating_sub(LINE_RELEVANCE_WINDOW);+ let end = actual_row.saturating_add(LINE_RELEVANCE_WINDOW);+ if let Some(expected_row) = unmatched_expected.range(start..=end).next().copied() {+ unmatched_expected.remove(&expected_row);+ matched += 1;+ }+ }++ matched+}+fn insert_anchor_row(anchor_lines_by_file: &mut BTreeMap<String, BTreeSet<u32>>,relevant_lines_by_file: &mut BTreeMap<String, BTreeSet<u32>>,@@ -502,4 +629,70 @@ mod tests {assert_eq!(score, EditableContextCoverage::new(0, 0, 0, 0, 0, 0));}++ #[test]+ fn patch_location_match_counts_file_and_nearby_line_matches() {+ let expected = indoc! {"+ --- a/src/main.rs+ +++ b/src/main.rs+ @@ -50,1 +50,1 @@+ -let value = 1;+ +let value = 2;+ "};+ let actual = indoc! {"+ --- a/src/main.rs+ +++ b/src/main.rs+ @@ -55,1 +55,1 @@+ -let value = 1;+ +let value = 2;+ "};++ let score = patch_location_match(expected, actual);++ assert_eq!(score, PatchLocationMatch::new(1, 0, 0, 1, 0, 0));+ }++ #[test]+ fn patch_location_match_counts_missing_and_extra_files() {+ let expected = indoc! {"+ --- a/src/main.rs+ +++ b/src/main.rs+ @@ -1,1 +1,1 @@+ -let value = 1;+ +let value = 2;+ "};+ let actual = indoc! {"+ --- a/src/lib.rs+ +++ b/src/lib.rs+ @@ -1,1 +1,1 @@+ -let value = 1;+ +let value = 2;+ "};++ let score = patch_location_match(expected, actual);++ assert_eq!(score, PatchLocationMatch::new(0, 1, 1, 0, 1, 1));+ }++ #[test]+ fn patch_location_match_normalizes_project_prefixed_actual_path() {+ let expected = indoc! {"+ --- a/src/main.rs+ +++ b/src/main.rs+ @@ -1,1 +1,1 @@+ -let value = 1;+ +let value = 2;+ "};+ let actual = indoc! {"+ --- a/project/src/main.rs+ +++ b/project/src/main.rs+ @@ -1,1 +1,1 @@+ -let value = 1;+ +let value = 2;+ "};++ let score = patch_location_match(expected, actual);++ assert_eq!(score, PatchLocationMatch::new(1, 0, 0, 1, 0, 0));+ }}diff --git a/crates/edit_prediction_metrics/src/prediction_score.rs b/crates/edit_prediction_metrics/src/prediction_score.rsindex 684a93ba9de..041d19ae771 100644--- a/crates/edit_prediction_metrics/src/prediction_score.rs+++ b/crates/edit_prediction_metrics/src/prediction_score.rs@@ -1,4 +1,5 @@use serde::{Deserialize, Serialize};+use std::collections::{BTreeMap, BTreeSet};use std::error::Error;use std::fmt;use std::path::Path;@@ -7,7 +8,11 @@ use zeta_prompt::udiff::{apply_diff_to_string, apply_diff_to_string_with_hunk_ofuse crate::reversal::compute_prediction_reversal_ratio_from_history;use crate::{- jumps::{EditableContextCoverage, Excerpt, editable_context_coverage},+ jumps::{+ EditableContextCoverage, Excerpt, PatchLocationMatch, editable_context_coverage,+ patch_location_match,+ },+ patch::{Hunk, Patch, PatchLine},patch_metrics::{ClassificationMetrics, DeltaChrFMetrics, braces_disbalance, count_patch_token_changes,delta_chr_f, delta_chr_f_beta, exact_lines_match, has_isolated_whitespace_changes,@@ -66,6 +71,8 @@ pub struct PredictionScore {pub avg_logprob: Option<f64>,#[serde(default, skip_serializing_if = "Option::is_none")]pub editable_context_coverage: Option<EditableContextCoverage>,+ #[serde(default, skip_serializing_if = "Option::is_none")]+ pub jump_location: Option<PatchLocationMatch>,}impl PredictionScore {@@ -97,6 +104,7 @@ impl PredictionScore {cumulative_logprob: None,avg_logprob: None,editable_context_coverage: None,+ jump_location: None,}}@@ -215,51 +223,164 @@ pub fn score_prediction(input: PredictionScoringInput<'_>) -> PredictionScore {})});- let Some(actual_patch) = input.actual_patch else {- let mut score = PredictionScore::zero();- score.editable_context_coverage = editable_context_coverage;- return score;- };-+ let actual_patch = input.actual_patch.unwrap_or("");let token_changes = count_patch_token_changes(actual_patch);- let actual_text = match apply_diff_to_string(actual_patch, input.original_text) {- Ok(text) => text,- Err(_) => {- let mut score = PredictionScore::zero();- score.inserted_tokens = token_changes.inserted_tokens;- score.deleted_tokens = token_changes.deleted_tokens;- score.editable_context_coverage = editable_context_coverage;- return score;- }+ let mut best = input+ .expected_patches+ .iter()+ .map(|expected| score_against_expected_patch(input, expected, actual_patch))+ .max_by(|left, right| {+ left.delta_chr_f_metrics+ .score+ .total_cmp(&right.delta_chr_f_metrics.score)+ .then_with(|| left.exact_lines.f1().total_cmp(&right.exact_lines.f1()))+ .then_with(|| {+ left.jump_location+ .lines_f1+ .total_cmp(&right.jump_location.lines_f1)+ })+ })+ .unwrap_or_else(|| score_against_no_expected_patch(input, actual_patch));++ let (cursor_distance, cursor_exact_match) =+ compute_cursor_metrics(best.expected_cursor, input.actual_cursor);++ let wrong_editable_region = input+ .actual_patch+ .map(|actual_patch| !is_editable_region_correct(actual_patch));+ let has_isolated_whitespace_changes = input.actual_patch.is_some_and(|actual_patch| {+ has_isolated_whitespace_changes(actual_patch, input.actual_cursor.map(|cursor| cursor.row))+ });++ best.score.cumulative_logprob = input.cumulative_logprob;+ best.score.avg_logprob = input.avg_logprob;+ best.score.editable_context_coverage = editable_context_coverage;+ best.score.inserted_tokens = token_changes.inserted_tokens;+ best.score.deleted_tokens = token_changes.deleted_tokens;+ best.score.cursor_distance = cursor_distance;+ best.score.cursor_exact_match = cursor_exact_match;+ best.score.wrong_editable_region = wrong_editable_region;+ best.score.has_isolated_whitespace_changes = has_isolated_whitespace_changes;+ best.score+}++struct ExpectedPatchScore {+ score: PredictionScore,+ delta_chr_f_metrics: DeltaChrFMetrics,+ exact_lines: ClassificationMetrics,+ jump_location: PatchLocationMatch,+ expected_cursor: Option<usize>,+}++struct ContentScore {+ delta_chr_f_metrics: DeltaChrFMetrics,+ braces_disbalance: usize,+ reversal_ratio: f32,+ kept_rate: Option<f64>,+ recall_rate: Option<f64>,+ kept_chars: Option<usize>,+ correctly_deleted_chars: Option<usize>,+ discarded_chars: Option<usize>,+}++fn score_against_expected_patch(+ input: PredictionScoringInput<'_>,+ expected: &PreparedExpectedPatch,+ actual_patch: &str,+) -> ExpectedPatchScore {+ let exact_lines = exact_lines_match(&expected.patch, actual_patch);+ let jump_location = patch_location_match(&expected.patch, actual_patch);+ let content_score = if let Some(context) = input.context {+ score_content_on_context(input, expected, actual_patch, context).unwrap_or_else(|| {+ if expected.patch.trim().is_empty() && actual_patch.trim().is_empty() {+ score_content_on_cursor_excerpt(input, expected, actual_patch)+ } else {+ zero_content_score()+ }+ })+ } else {+ score_content_on_cursor_excerpt(input, expected, actual_patch)+ };+ let delta_chr_f_metrics = content_score.delta_chr_f_metrics.clone();++ let score = PredictionScore {+ delta_chr_f: delta_chr_f_metrics.score as f32,+ delta_chr_f_true_positives: delta_chr_f_metrics.counts.true_positives,+ delta_chr_f_false_positives: delta_chr_f_metrics.counts.false_positives,+ delta_chr_f_false_negatives: delta_chr_f_metrics.counts.false_negatives,+ delta_chr_f_precision: delta_chr_f_metrics.precision,+ delta_chr_f_recall: delta_chr_f_metrics.recall,+ delta_chr_f_beta: delta_chr_f_metrics.beta,+ braces_disbalance: content_score.braces_disbalance,+ exact_lines_tp: exact_lines.true_positives,+ exact_lines_fp: exact_lines.false_positives,+ exact_lines_fn: exact_lines.false_negatives,+ reversal_ratio: content_score.reversal_ratio,+ cursor_distance: None,+ cursor_exact_match: None,+ wrong_editable_region: None,+ has_isolated_whitespace_changes: false,+ inserted_tokens: 0,+ deleted_tokens: 0,+ kept_rate: content_score.kept_rate,+ recall_rate: content_score.recall_rate,+ kept_chars: content_score.kept_chars,+ correctly_deleted_chars: content_score.correctly_deleted_chars,+ discarded_chars: content_score.discarded_chars,+ cumulative_logprob: None,+ avg_logprob: None,+ editable_context_coverage: None,+ jump_location: Some(jump_location.clone()),};- let mut best_delta_chr_f_metrics = DeltaChrFMetrics::default();- let mut best_expected_cursor = None;- let mut best_expected_text = None;-- for expected in input.expected_patches {- let delta_chr_f_metrics = delta_chr_f(input.original_text, &expected.text, &actual_text);- if best_expected_text.is_none()- || delta_chr_f_metrics.score > best_delta_chr_f_metrics.score- {- best_delta_chr_f_metrics = delta_chr_f_metrics;- best_expected_cursor = expected.cursor_editable_region_offset;- best_expected_text = Some(expected.text.as_str());- }+ ExpectedPatchScore {+ score,+ delta_chr_f_metrics,+ exact_lines,+ jump_location,+ expected_cursor: expected.cursor_editable_region_offset,}+}- let disbalance_before = braces_disbalance(input.original_text);- let disbalance_after = braces_disbalance(&actual_text);- let braces_disbalance = disbalance_after.saturating_sub(disbalance_before);+fn score_against_no_expected_patch(+ input: PredictionScoringInput<'_>,+ actual_patch: &str,+) -> ExpectedPatchScore {+ let expected = PreparedExpectedPatch {+ patch: String::new(),+ text: input.original_text.to_string(),+ cursor_editable_region_offset: None,+ };+ score_against_expected_patch(input, &expected, actual_patch)+}- let best_exact_lines = input- .expected_patches- .iter()- .map(|expected| exact_lines_match(&expected.patch, actual_patch))- .max_by_key(|metrics| metrics.true_positives)- .unwrap_or_default();+fn zero_content_score() -> ContentScore {+ ContentScore {+ delta_chr_f_metrics: DeltaChrFMetrics {+ beta: delta_chr_f_beta(),+ ..DeltaChrFMetrics::default()+ },+ braces_disbalance: 0,+ reversal_ratio: 0.0,+ kept_rate: None,+ recall_rate: None,+ kept_chars: None,+ correctly_deleted_chars: None,+ discarded_chars: None,+ }+}+fn score_content_on_cursor_excerpt(+ input: PredictionScoringInput<'_>,+ expected: &PreparedExpectedPatch,+ actual_patch: &str,+) -> ContentScore {+ let actual_text = apply_diff_to_string(actual_patch, input.original_text)+ .unwrap_or_else(|_| input.original_text.to_string());+ let delta_chr_f_metrics = delta_chr_f(input.original_text, &expected.text, &actual_text);+ let braces_disbalance =+ braces_disbalance(&actual_text).saturating_sub(braces_disbalance(input.original_text));let reversal_ratio = input.reversal_context.map(|context| {@@ -272,60 +393,222 @@ pub fn score_prediction(input: PredictionScoringInput<'_>) -> PredictionScore {)}).unwrap_or(0.0);+ let kept_rate =+ crate::kept_rate::compute_kept_rate(input.original_text, &actual_text, &expected.text);- let (cursor_distance, cursor_exact_match) =- compute_cursor_metrics(best_expected_cursor, input.actual_cursor);-- let wrong_editable_region = Some(!is_editable_region_correct(actual_patch));- let has_isolated_whitespace_changes =- has_isolated_whitespace_changes(actual_patch, input.actual_cursor.map(|cursor| cursor.row));-- let (kept_rate, recall_rate, kept_chars, correctly_deleted_chars, discarded_chars) =- best_expected_text- .map(|reference_text| {- let result = crate::kept_rate::compute_kept_rate(- input.original_text,- &actual_text,- reference_text,- );- (- Some(result.kept_rate),- Some(result.recall_rate),- Some(result.kept_chars),- Some(result.correctly_deleted_chars),- Some(result.discarded_chars),- )- })- .unwrap_or((None, None, None, None, None));-- PredictionScore {- delta_chr_f: best_delta_chr_f_metrics.score as f32,- delta_chr_f_true_positives: best_delta_chr_f_metrics.counts.true_positives,- delta_chr_f_false_positives: best_delta_chr_f_metrics.counts.false_positives,- delta_chr_f_false_negatives: best_delta_chr_f_metrics.counts.false_negatives,- delta_chr_f_precision: best_delta_chr_f_metrics.precision,- delta_chr_f_recall: best_delta_chr_f_metrics.recall,- delta_chr_f_beta: best_delta_chr_f_metrics.beta,+ ContentScore {+ delta_chr_f_metrics,braces_disbalance,- exact_lines_tp: best_exact_lines.true_positives,- exact_lines_fp: best_exact_lines.false_positives,- exact_lines_fn: best_exact_lines.false_negatives,reversal_ratio,- cursor_distance,- cursor_exact_match,- wrong_editable_region,- has_isolated_whitespace_changes,- inserted_tokens: token_changes.inserted_tokens,- deleted_tokens: token_changes.deleted_tokens,- kept_rate,- recall_rate,- kept_chars,- correctly_deleted_chars,- discarded_chars,- cumulative_logprob: input.cumulative_logprob,- avg_logprob: input.avg_logprob,- editable_context_coverage,+ kept_rate: Some(kept_rate.kept_rate),+ recall_rate: Some(kept_rate.recall_rate),+ kept_chars: Some(kept_rate.kept_chars),+ correctly_deleted_chars: Some(kept_rate.correctly_deleted_chars),+ discarded_chars: Some(kept_rate.discarded_chars),+ }+}++fn score_content_on_context(+ input: PredictionScoringInput<'_>,+ expected: &PreparedExpectedPatch,+ actual_patch: &str,+ context: &[Excerpt],+) -> Option<ContentScore> {+ let expected_documents = apply_patch_to_documents(&expected.patch, context);+ let actual_documents = apply_patch_to_documents(actual_patch, context);+ let document_indices = expected_documents+ .keys()+ .chain(actual_documents.keys())+ .copied()+ .collect::<BTreeSet<_>>();++ if document_indices.is_empty() {+ return None;}++ let mut original_text = String::new();+ let mut expected_text = String::new();+ let mut actual_text = String::new();+ let mut braces_disbalance_before = 0;+ let mut braces_disbalance_after = 0;+ let mut cursor_actual_text = None;++ for document_ix in document_indices {+ let document = context.get(document_ix)?;+ let expected_document_text = expected_documents+ .get(&document_ix)+ .map(String::as_str)+ .unwrap_or(document.content.as_str());+ let actual_document_text = actual_documents+ .get(&document_ix)+ .map(String::as_str)+ .unwrap_or(document.content.as_str());++ if !original_text.is_empty() {+ original_text.push('\n');+ expected_text.push('\n');+ actual_text.push('\n');+ }+ original_text.push_str(&document.content);+ expected_text.push_str(expected_document_text);+ actual_text.push_str(actual_document_text);++ braces_disbalance_before += braces_disbalance(&document.content);+ braces_disbalance_after += braces_disbalance(actual_document_text);++ if input.reversal_context.is_some_and(|reversal_context| {+ path_matches(+ &reversal_context.cursor_path.to_string_lossy(),+ &document.path,+ )+ }) {+ cursor_actual_text = Some(actual_document_text.to_string());+ }+ }++ let delta_chr_f_metrics = delta_chr_f(&original_text, &expected_text, &actual_text);+ let kept_rate =+ crate::kept_rate::compute_kept_rate(&original_text, &actual_text, &expected_text);+ let reversal_ratio = if let (Some(reversal_context), Some(cursor_actual_text)) =+ (input.reversal_context, cursor_actual_text.as_deref())+ {+ compute_prediction_reversal_ratio_from_history(+ input.original_text,+ reversal_context.edit_history,+ reversal_context.excerpt_start_row,+ cursor_actual_text,+ reversal_context.cursor_path,+ )+ } else {+ 0.0+ };++ Some(ContentScore {+ delta_chr_f_metrics,+ braces_disbalance: braces_disbalance_after.saturating_sub(braces_disbalance_before),+ reversal_ratio,+ kept_rate: Some(kept_rate.kept_rate),+ recall_rate: Some(kept_rate.recall_rate),+ kept_chars: Some(kept_rate.kept_chars),+ correctly_deleted_chars: Some(kept_rate.correctly_deleted_chars),+ discarded_chars: Some(kept_rate.discarded_chars),+ })+}++fn apply_patch_to_documents(patch: &str, context: &[Excerpt]) -> BTreeMap<usize, String> {+ let patch = Patch::parse_unified_diff(patch);+ let mut hunks_by_document: BTreeMap<usize, Vec<Hunk>> = BTreeMap::new();++ for hunk in patch.hunks.into_iter().filter(hunk_has_change) {+ if let Some(document_ix) = find_hunk_document(&hunk, context) {+ hunks_by_document.entry(document_ix).or_default().push(hunk);+ }+ }++ hunks_by_document+ .into_iter()+ .filter_map(|(document_ix, hunks)| {+ let document = context.get(document_ix)?;+ let document_patch = diff_for_document_hunks(document, &hunks);+ let text = apply_diff_to_string(&document_patch, &document.content).ok()?;+ Some((document_ix, text))+ })+ .collect()+}++fn find_hunk_document(hunk: &Hunk, context: &[Excerpt]) -> Option<usize> {+ context+ .iter()+ .enumerate()+ .find_map(|(document_ix, document)| {+ if !path_matches(&hunk.filename, &document.path) {+ return None;+ }++ let document_patch = diff_for_document_hunks(document, std::slice::from_ref(hunk));+ apply_diff_to_string(&document_patch, &document.content)+ .is_ok()+ .then_some(document_ix)+ })+}++fn diff_for_document_hunks(document: &Excerpt, hunks: &[Hunk]) -> String {+ let mut diff = String::new();+ diff.push_str(&format!("--- a/{}\n", document.path));+ diff.push_str(&format!("+++ b/{}\n", document.path));++ for hunk in hunks {+ let old_start = adjust_hunk_start(hunk.old_start, &document.row_range);+ let new_start = adjust_hunk_start(hunk.new_start, &document.row_range);+ let old_count = hunk+ .lines+ .iter()+ .filter(|line| matches!(line, PatchLine::Context(_) | PatchLine::Deletion(_)))+ .count();+ let new_count = hunk+ .lines+ .iter()+ .filter(|line| matches!(line, PatchLine::Context(_) | PatchLine::Addition(_)))+ .count();+ diff.push_str(&format!(+ "@@ -{},{} +{},{} @@\n",+ old_start, old_count, new_start, new_count+ ));+ for line in &hunk.lines {+ match line {+ PatchLine::Context(text) => {+ diff.push(' ');+ diff.push_str(text);+ diff.push('\n');+ }+ PatchLine::Addition(text) => {+ diff.push('+');+ diff.push_str(text);+ diff.push('\n');+ }+ PatchLine::Deletion(text) => {+ diff.push('-');+ diff.push_str(text);+ diff.push('\n');+ }+ PatchLine::Garbage(text) => {+ diff.push_str(text);+ diff.push('\n');+ }+ }+ }+ }++ diff+}++fn adjust_hunk_start(start: isize, row_range: &std::ops::Range<u32>) -> isize {+ let Ok(start_row) = u32::try_from(start.saturating_sub(1)) else {+ return start;+ };++ if row_range.start <= start_row && start_row <= row_range.end {+ start.saturating_sub(row_range.start as isize)+ } else {+ start+ }+}++fn hunk_has_change(hunk: &Hunk) -> bool {+ hunk.lines+ .iter()+ .any(|line| matches!(line, PatchLine::Addition(_) | PatchLine::Deletion(_)))+}++fn path_matches(patch_path: &str, document_path: &str) -> bool {+ patch_path == document_path+ || strip_first_path_component(patch_path).is_some_and(|stripped| stripped == document_path)+}++fn strip_first_path_component(path: &str) -> Option<&str> {+ path.split_once('/')+ .map(|(_, rest)| rest)+ .filter(|rest| !rest.is_empty())}fn compute_cursor_metrics(@@ -372,4 +655,76 @@ mod tests {assert_eq!(score.delta_chr_f, 0.0);assert_eq!(score.kept_rate, Some(0.0));}++ #[test]+ fn test_scores_related_file_patch_against_context_document() {+ let original_text = "fn main() {}\n";+ let expected_patch = "--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -11,3 +11,3 @@\n fn value() {\n- 1\n+ 2\n }\n";+ let actual_patch = "--- a/project/src/lib.rs\n+++ b/project/src/lib.rs\n@@ -11,3 +11,3 @@\n fn value() {\n- 1\n+ 2\n }\n";+ let expected_patches = [PreparedExpectedPatch {+ patch: expected_patch.to_string(),+ text: original_text.to_string(),+ cursor_editable_region_offset: None,+ }];+ let context = [+ Excerpt {+ path: "src/main.rs".to_string(),+ row_range: 0..1,+ content: original_text.to_string(),+ },+ Excerpt {+ path: "src/lib.rs".to_string(),+ row_range: 10..13,+ content: "fn value() {\n 1\n}\n".to_string(),+ },+ ];++ let score = score_prediction(PredictionScoringInput {+ original_text,+ expected_patches: &expected_patches,+ actual_patch: Some(actual_patch),+ actual_cursor: None,+ reversal_context: None,+ cumulative_logprob: None,+ avg_logprob: None,+ context: Some(&context),+ });++ assert_eq!(score.delta_chr_f, 100.0);+ assert_eq!(score.exact_lines_tp, 2);+ assert_eq!(score.jump_location.unwrap().files_f1, 1.0);+ }++ #[test]+ fn test_missing_related_file_prediction_counts_as_false_negative() {+ let original_text = "fn main() {}\n";+ let expected_patch = "--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -11,3 +11,3 @@\n fn value() {\n- 1\n+ 2\n }\n";+ let expected_patches = [PreparedExpectedPatch {+ patch: expected_patch.to_string(),+ text: original_text.to_string(),+ cursor_editable_region_offset: None,+ }];+ let context = [Excerpt {+ path: "src/lib.rs".to_string(),+ row_range: 10..13,+ content: "fn value() {\n 1\n}\n".to_string(),+ }];++ let score = score_prediction(PredictionScoringInput {+ original_text,+ expected_patches: &expected_patches,+ actual_patch: None,+ actual_cursor: None,+ reversal_context: None,+ cumulative_logprob: None,+ avg_logprob: None,+ context: Some(&context),+ });++ assert!(score.delta_chr_f < 100.0);+ assert_eq!(score.exact_lines_fn, 2);+ let location = score.jump_location.unwrap();+ assert_eq!(location.files_fn, 1);+ assert_eq!(location.lines_fn, 1);+ }}diff --git a/crates/edit_prediction_metrics/src/reversal.rs b/crates/edit_prediction_metrics/src/reversal.rsindex 05b82f0c9cc..8b1a6e5e634 100644--- a/crates/edit_prediction_metrics/src/reversal.rs+++ b/crates/edit_prediction_metrics/src/reversal.rs@@ -1181,8 +1181,8 @@ mod tests {-old+new"}.into(),- old_range: 0..1,- new_range: 0..1,+ old_range: 0..0,+ new_range: 0..0,predicted: false,in_open_source_repo: true,}),@@ -1194,8 +1194,8 @@ mod tests {-a+b"}.into(),- old_range: 0..1,- new_range: 0..1,+ old_range: 0..0,+ new_range: 0..0,predicted: false,in_open_source_repo: true,}),@@ -1207,8 +1207,8 @@ mod tests {-x+y"}.into(),- old_range: 0..1,- new_range: 0..1,+ old_range: 0..0,+ new_range: 0..0,predicted: false,in_open_source_repo: true,}),@@ -1937,8 +1937,8 @@ mod tests {line2"}.into(),- old_range: 0..1,- new_range: 0..1,+ old_range: 0..0,+ new_range: 0..0,predicted: false,in_open_source_repo: false,})],@@ -1977,8 +1977,8 @@ mod tests {line11"}.into(),- old_range: 0..1,- new_range: 0..1,+ old_range: 0..0,+ new_range: 0..0,predicted: false,in_open_source_repo: false,})],@@ -2039,8 +2039,8 @@ mod tests {line2"}.into(),- old_range: 0..1,- new_range: 0..1,+ old_range: 0..0,+ new_range: 0..0,predicted: false,in_open_source_repo: false,})],@@ -2078,8 +2078,8 @@ mod tests {more_wrong"}.into(),- old_range: 0..1,- new_range: 0..1,+ old_range: 0..0,+ new_range: 0..0,predicted: false,in_open_source_repo: false,})],@@ -2148,8 +2148,8 @@ mod tests {line2"}.into(),- old_range: 0..1,- new_range: 0..1,+ old_range: 0..0,+ new_range: 0..0,predicted: false,in_open_source_repo: false,}),@@ -2163,8 +2163,8 @@ mod tests {line2"}.into(),- old_range: 0..1,- new_range: 0..1,+ old_range: 0..0,+ new_range: 0..0,predicted: false,in_open_source_repo: false,}),diff --git a/crates/edit_prediction_metrics/src/summary.rs b/crates/edit_prediction_metrics/src/summary.rsindex f927a027d76..54190d78bc3 100644--- a/crates/edit_prediction_metrics/src/summary.rs+++ b/crates/edit_prediction_metrics/src/summary.rs@@ -1,5 +1,6 @@use serde::Serialize;+use crate::jumps::LineFileClassification;use crate::patch_metrics::ClassificationMetrics;use crate::prediction_score::PredictionScore;@@ -41,6 +42,8 @@ pub struct SummaryJson {#[serde(skip_serializing_if = "Option::is_none")]pub cursor_exact_match_rate: Option<f32>,#[serde(skip_serializing_if = "Option::is_none")]+ pub cursor_exact_matches: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]pub cursor_avg_distance: Option<f32>,#[serde(skip_serializing_if = "Option::is_none")]pub cursor_total_evaluated: Option<usize>,@@ -48,16 +51,24 @@ pub struct SummaryJson {pub wrong_editable_region_rate: Option<f32>,pub isolated_whitespace_rate: Option<f32>,#[serde(skip_serializing_if = "Option::is_none")]+ pub isolated_whitespace_count: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]pub avg_kept_rate: Option<f64>,#[serde(skip_serializing_if = "Option::is_none")]+ pub kept_rate_examples: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]pub avg_recall_rate: Option<f64>,#[serde(skip_serializing_if = "Option::is_none")]+ pub recall_rate_examples: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]pub total_kept_chars: Option<usize>,#[serde(skip_serializing_if = "Option::is_none")]pub total_correctly_deleted_chars: Option<usize>,#[serde(skip_serializing_if = "Option::is_none")]pub total_discarded_chars: Option<usize>,#[serde(skip_serializing_if = "Option::is_none")]+ pub editable_context_examples: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]pub avg_editable_context_lines_precision: Option<f64>,#[serde(skip_serializing_if = "Option::is_none")]pub avg_editable_context_lines_recall: Option<f64>,@@ -82,6 +93,32 @@ pub struct SummaryJson {#[serde(skip_serializing_if = "Option::is_none")]pub editable_context_files_fn: Option<usize>,#[serde(skip_serializing_if = "Option::is_none")]+ pub jump_location_examples: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub avg_jump_location_lines_precision: Option<f64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub avg_jump_location_lines_recall: Option<f64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub avg_jump_location_lines_f1: Option<f64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub jump_location_lines_tp: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub jump_location_lines_fp: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub jump_location_lines_fn: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub avg_jump_location_files_precision: Option<f64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub avg_jump_location_files_recall: Option<f64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub avg_jump_location_files_f1: Option<f64>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub jump_location_files_tp: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub jump_location_files_fp: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub jump_location_files_fn: Option<usize>,+ #[serde(skip_serializing_if = "Option::is_none")]pub avg_retrieved_context_bytes: Option<f64>,#[serde(skip_serializing_if = "Option::is_none")]pub total_retrieved_context_bytes: Option<usize>,@@ -89,314 +126,257 @@ pub struct SummaryJson {pub retrieved_context_examples: Option<usize>,}+/// Mean of observed values; `None` when nothing was observed.+#[derive(Default)]+struct MeanAggregate {+ sum: f64,+ count: usize,+}++impl MeanAggregate {+ fn add(&mut self, value: f64) {+ self.sum += value;+ self.count += 1;+ }++ fn mean(&self) -> Option<f64> {+ (self.count > 0).then(|| self.sum / self.count as f64)+ }++ fn mean_f32(&self) -> Option<f32> {+ self.mean().map(|mean| mean as f32)+ }++ fn count(&self) -> Option<usize> {+ (self.count > 0).then_some(self.count)+ }+}++/// Fraction of observations that were hits; `None` when nothing was observed.+#[derive(Default)]+struct RateAggregate {+ hits: usize,+ total: usize,+}++impl RateAggregate {+ fn add(&mut self, hit: bool) {+ self.total += 1;+ if hit {+ self.hits += 1;+ }+ }++ fn rate(&self) -> Option<f32> {+ (self.total > 0).then(|| self.hits as f32 / self.total as f32)+ }++ fn hits(&self) -> Option<usize> {+ (self.total > 0).then_some(self.hits)+ }++ fn total(&self) -> Option<usize> {+ (self.total > 0).then_some(self.total)+ }+}++/// Sum of observed values; `None` when nothing was observed.+#[derive(Default)]+struct SumAggregate {+ sum: usize,+ count: usize,+}++impl SumAggregate {+ fn add(&mut self, value: usize) {+ self.sum += value;+ self.count += 1;+ }++ fn total(&self) -> Option<usize> {+ (self.count > 0).then_some(self.sum)+ }++ fn mean(&self) -> Option<f64> {+ (self.count > 0).then(|| self.sum as f64 / self.count as f64)+ }++ fn count(&self) -> Option<usize> {+ (self.count > 0).then_some(self.count)+ }+}++/// Macro-averaged precision/recall/F1 plus pooled (micro) TP/FP/FN counts.+#[derive(Default)]+struct PrfAggregate {+ count: usize,+ precision_sum: f64,+ recall_sum: f64,+ f1_sum: f64,+ counts: ClassificationMetrics,+}++impl PrfAggregate {+ fn add(&mut self, precision: f64, recall: f64, f1: f64, counts: &ClassificationMetrics) {+ self.count += 1;+ self.precision_sum += precision;+ self.recall_sum += recall;+ self.f1_sum += f1;+ self.counts.accumulate(counts);+ }++ fn avg_precision(&self) -> Option<f64> {+ (self.count > 0).then(|| self.precision_sum / self.count as f64)+ }++ fn avg_recall(&self) -> Option<f64> {+ (self.count > 0).then(|| self.recall_sum / self.count as f64)+ }++ fn avg_f1(&self) -> Option<f64> {+ (self.count > 0).then(|| self.f1_sum / self.count as f64)+ }++ fn true_positives(&self) -> Option<usize> {+ (self.count > 0).then_some(self.counts.true_positives)+ }++ fn false_positives(&self) -> Option<usize> {+ (self.count > 0).then_some(self.counts.false_positives)+ }++ fn false_negatives(&self) -> Option<usize> {+ (self.count > 0).then_some(self.counts.false_negatives)+ }++ fn count(&self) -> Option<usize> {+ (self.count > 0).then_some(self.count)+ }+}++/// Aggregates the line- and file-level halves of a [`LineFileClassification`].+#[derive(Default)]+struct LineFileAggregate {+ lines: PrfAggregate,+ files: PrfAggregate,+}++impl LineFileAggregate {+ fn add(&mut self, classification: &LineFileClassification) {+ self.lines.add(+ classification.lines_precision,+ classification.lines_recall,+ classification.lines_f1,+ &classification.lines_counts(),+ );+ self.files.add(+ classification.files_precision,+ classification.files_recall,+ classification.files_f1,+ &classification.files_counts(),+ );+ }++ fn count(&self) -> Option<usize> {+ self.lines.count()+ }+}+pub fn compute_summary<'a>(predictions: impl IntoIterator<Item = PredictionSummaryInput<'a>>,) -> SummaryJson {- let mut all_delta_chr_f_scores = Vec::new();- let mut all_reversal_ratios = Vec::new();+ let mut total_scores: usize = 0;+ let mut delta_chr_f_sum: f32 = 0.0;+ let mut reversal_ratio_sum: f32 = 0.0;let mut braces_disbalance_sum: usize = 0;let mut total_delta_chr_f = ClassificationMetrics::default();- let mut total_delta_chr_f_precision = 0.0;- let mut total_delta_chr_f_recall = 0.0;+ let mut delta_chr_f_precision_sum = 0.0;+ let mut delta_chr_f_recall_sum = 0.0;let mut delta_chr_f_beta = 0.0;let mut total_exact_lines = ClassificationMetrics::default();- let mut total_scores: usize = 0;- let mut qa_reverts_count: usize = 0;- let mut qa_reverts_total: usize = 0;- let mut qa_confidence_sum: u64 = 0;- let mut qa_confidence_count: usize = 0;- let mut cursor_exact_matches: usize = 0;- let mut cursor_total: usize = 0;- let mut cursor_distance_sum: usize = 0;- let mut cursor_distance_count: usize = 0;- let mut wrong_editable_region_count: usize = 0;- let mut wrong_editable_region_total: usize = 0;- let mut isolated_whitespace_count: usize = 0;- let mut kept_rate_sum: f64 = 0.0;- let mut kept_rate_count: usize = 0;- let mut kept_chars_total: usize = 0;- let mut kept_chars_count: usize = 0;- let mut correctly_deleted_chars_total: usize = 0;- let mut correctly_deleted_chars_count: usize = 0;- let mut discarded_chars_total: usize = 0;- let mut discarded_chars_count: usize = 0;- let mut recall_rate_sum: f64 = 0.0;- let mut recall_rate_count: usize = 0;- let mut editable_context_lines_precision_sum: f64 = 0.0;- let mut editable_context_lines_recall_sum: f64 = 0.0;- let mut editable_context_lines_f1_sum: f64 = 0.0;- let mut editable_context_files_precision_sum: f64 = 0.0;- let mut editable_context_files_recall_sum: f64 = 0.0;- let mut editable_context_files_f1_sum: f64 = 0.0;- let mut editable_context_coverage_count: usize = 0;- let mut editable_context_lines_tp: usize = 0;- let mut editable_context_lines_fp: usize = 0;- let mut editable_context_lines_fn: usize = 0;- let mut editable_context_files_tp: usize = 0;- let mut editable_context_files_fp: usize = 0;- let mut editable_context_files_fn: usize = 0;- let mut retrieved_context_bytes_total: usize = 0;- let mut retrieved_context_bytes_count: usize = 0;+ let mut qa_reverts = RateAggregate::default();+ let mut qa_confidence = MeanAggregate::default();+ let mut cursor_exact = RateAggregate::default();+ let mut cursor_distance = MeanAggregate::default();+ let mut wrong_editable_region = RateAggregate::default();+ let mut isolated_whitespace = RateAggregate::default();+ let mut kept_rate = MeanAggregate::default();+ let mut recall_rate = MeanAggregate::default();+ let mut kept_chars = SumAggregate::default();+ let mut correctly_deleted_chars = SumAggregate::default();+ let mut discarded_chars = SumAggregate::default();+ let mut editable_context = LineFileAggregate::default();+ let mut jump_location = LineFileAggregate::default();+ let mut retrieved_context_bytes = SumAggregate::default();for prediction in predictions {let score = prediction.score;- all_delta_chr_f_scores.push(score.delta_chr_f);- all_reversal_ratios.push(score.reversal_ratio);total_scores += 1;+ delta_chr_f_sum += score.delta_chr_f;+ reversal_ratio_sum += score.reversal_ratio;braces_disbalance_sum += score.braces_disbalance;total_delta_chr_f.accumulate(&score.delta_chr_f_counts());- total_delta_chr_f_precision += score.delta_chr_f_precision;- total_delta_chr_f_recall += score.delta_chr_f_recall;+ delta_chr_f_precision_sum += score.delta_chr_f_precision;+ delta_chr_f_recall_sum += score.delta_chr_f_recall;delta_chr_f_beta = score.delta_chr_f_beta;total_exact_lines.accumulate(&score.exact_lines_counts());if let Some(qa) = prediction.qa {if let Some(reverts) = qa.reverts_edits {- qa_reverts_total += 1;- if reverts {- qa_reverts_count += 1;- }+ qa_reverts.add(reverts);}if let Some(confidence) = qa.confidence {- qa_confidence_sum += confidence as u64;- qa_confidence_count += 1;+ qa_confidence.add(confidence as f64);}}if let Some(wrong) = score.wrong_editable_region {- wrong_editable_region_total += 1;- if wrong {- wrong_editable_region_count += 1;- }+ wrong_editable_region.add(wrong);}+ isolated_whitespace.add(score.has_isolated_whitespace_changes);- if score.has_isolated_whitespace_changes {- isolated_whitespace_count += 1;+ if let Some(value) = score.kept_rate {+ kept_rate.add(value);}-- if let Some(kept_rate) = score.kept_rate {- kept_rate_sum += kept_rate;- kept_rate_count += 1;- }- if let Some(kept_chars) = score.kept_chars {- kept_chars_total += kept_chars;- kept_chars_count += 1;+ if let Some(value) = score.recall_rate {+ recall_rate.add(value);}- if let Some(correctly_deleted_chars) = score.correctly_deleted_chars {- correctly_deleted_chars_total += correctly_deleted_chars;- correctly_deleted_chars_count += 1;+ if let Some(value) = score.kept_chars {+ kept_chars.add(value);}- if let Some(discarded_chars) = score.discarded_chars {- discarded_chars_total += discarded_chars;- discarded_chars_count += 1;+ if let Some(value) = score.correctly_deleted_chars {+ correctly_deleted_chars.add(value);}- if let Some(recall_rate) = score.recall_rate {- recall_rate_sum += recall_rate;- recall_rate_count += 1;+ if let Some(value) = score.discarded_chars {+ discarded_chars.add(value);}- if let Some(retrieved_context_bytes) = prediction.retrieved_context_bytes {- retrieved_context_bytes_total += retrieved_context_bytes;- retrieved_context_bytes_count += 1;+ if let Some(value) = prediction.retrieved_context_bytes {+ retrieved_context_bytes.add(value);}if let Some(coverage) = &score.editable_context_coverage {- editable_context_lines_precision_sum += coverage.lines_precision;- editable_context_lines_recall_sum += coverage.lines_recall;- editable_context_lines_f1_sum += coverage.lines_f1;- editable_context_files_precision_sum += coverage.files_precision;- editable_context_files_recall_sum += coverage.files_recall;- editable_context_files_f1_sum += coverage.files_f1;- editable_context_coverage_count += 1;- editable_context_lines_tp += coverage.lines_tp;- editable_context_lines_fp += coverage.lines_fp;- editable_context_lines_fn += coverage.lines_fn;- editable_context_files_tp += coverage.files_tp;- editable_context_files_fp += coverage.files_fp;- editable_context_files_fn += coverage.files_fn;+ editable_context.add(coverage);+ }+ if let Some(location) = &score.jump_location {+ jump_location.add(location);}if let Some(exact_match) = score.cursor_exact_match {- cursor_total += 1;- if exact_match {- cursor_exact_matches += 1;- }+ cursor_exact.add(exact_match);}if let Some(distance) = score.cursor_distance {- cursor_distance_sum += distance;- cursor_distance_count += 1;+ cursor_distance.add(distance as f64);}}- let avg_delta_chr_f = if all_delta_chr_f_scores.is_empty() {- 0.0- } else {- all_delta_chr_f_scores.iter().sum::<f32>() / all_delta_chr_f_scores.len() as f32- };-- let avg_reversal_ratio = if all_reversal_ratios.is_empty() {- 0.0- } else {- all_reversal_ratios.iter().sum::<f32>() / all_reversal_ratios.len() as f32- };-- let avg_braces_disbalance = if total_scores == 0 {- 0.0- } else {- braces_disbalance_sum as f32 / total_scores as f32- };-- let qa_avg_reverts_edits = if qa_reverts_total > 0 {- Some(qa_reverts_count as f32 / qa_reverts_total as f32)- } else {- None- };-- let qa_avg_confidence = if qa_confidence_count > 0 {- Some(qa_confidence_sum as f32 / qa_confidence_count as f32)- } else {- None- };-- let cursor_exact_match_rate = if cursor_total > 0 {- Some(cursor_exact_matches as f32 / cursor_total as f32)- } else {- None- };-- let cursor_avg_distance = if cursor_distance_count > 0 {- Some(cursor_distance_sum as f32 / cursor_distance_count as f32)- } else {- None- };-- let cursor_total_evaluated = if cursor_total > 0 {- Some(cursor_total)- } else {- None- };-- let wrong_editable_region_rate = if wrong_editable_region_total > 0 {- Some(wrong_editable_region_count as f32 / wrong_editable_region_total as f32)- } else {- None- };-- let isolated_whitespace_rate = if total_scores > 0 {- Some(isolated_whitespace_count as f32 / total_scores as f32)- } else {- None- };-- let avg_kept_rate = if kept_rate_count > 0 {- Some(kept_rate_sum / kept_rate_count as f64)- } else {- None- };-- let avg_recall_rate = if recall_rate_count > 0 {- Some(recall_rate_sum / recall_rate_count as f64)- } else {- None- };-- let total_kept_chars = if kept_chars_count > 0 {- Some(kept_chars_total)- } else {- None- };-- let total_correctly_deleted_chars = if correctly_deleted_chars_count > 0 {- Some(correctly_deleted_chars_total)- } else {- None- };-- let total_discarded_chars = if discarded_chars_count > 0 {- Some(discarded_chars_total)- } else {- None- };-- let avg_editable_context_lines_precision = if editable_context_coverage_count > 0 {- Some(editable_context_lines_precision_sum / editable_context_coverage_count as f64)- } else {- None- };- let avg_editable_context_lines_recall = if editable_context_coverage_count > 0 {- Some(editable_context_lines_recall_sum / editable_context_coverage_count as f64)- } else {- None- };- let avg_editable_context_lines_f1 = if editable_context_coverage_count > 0 {- Some(editable_context_lines_f1_sum / editable_context_coverage_count as f64)- } else {- None- };- let editable_context_lines_tp = if editable_context_coverage_count > 0 {- Some(editable_context_lines_tp)- } else {- None- };- let editable_context_lines_fp = if editable_context_coverage_count > 0 {- Some(editable_context_lines_fp)- } else {- None- };- let editable_context_lines_fn = if editable_context_coverage_count > 0 {- Some(editable_context_lines_fn)- } else {- None- };- let avg_editable_context_files_precision = if editable_context_coverage_count > 0 {- Some(editable_context_files_precision_sum / editable_context_coverage_count as f64)- } else {- None- };- let avg_editable_context_files_recall = if editable_context_coverage_count > 0 {- Some(editable_context_files_recall_sum / editable_context_coverage_count as f64)- } else {- None- };- let avg_editable_context_files_f1 = if editable_context_coverage_count > 0 {- Some(editable_context_files_f1_sum / editable_context_coverage_count as f64)- } else {- None- };- let editable_context_files_tp = if editable_context_coverage_count > 0 {- Some(editable_context_files_tp)- } else {- None- };- let editable_context_files_fp = if editable_context_coverage_count > 0 {- Some(editable_context_files_fp)- } else {- None- };- let editable_context_files_fn = if editable_context_coverage_count > 0 {- Some(editable_context_files_fn)- } else {- None- };- let avg_retrieved_context_bytes = if retrieved_context_bytes_count > 0 {- Some(retrieved_context_bytes_total as f64 / retrieved_context_bytes_count as f64)- } else {- None- };- let total_retrieved_context_bytes = if retrieved_context_bytes_count > 0 {- Some(retrieved_context_bytes_total)- } else {- None- };- let retrieved_context_examples = if retrieved_context_bytes_count > 0 {- Some(retrieved_context_bytes_count)- } else {- None- };-SummaryJson {total_examples: total_scores,- avg_delta_chr_f,+ avg_delta_chr_f: if total_scores == 0 {+ 0.0+ } else {+ delta_chr_f_sum / total_scores as f32+ },delta_chr_f_beta,delta_chr_f_true_positives: total_delta_chr_f.true_positives,delta_chr_f_false_positives: total_delta_chr_f.false_positives,@@ -404,47 +384,73 @@ pub fn compute_summary<'a>(delta_chr_f_precision: if total_scores == 0 {0.0} else {- total_delta_chr_f_precision / total_scores as f64+ delta_chr_f_precision_sum / total_scores as f64},delta_chr_f_recall: if total_scores == 0 {0.0} else {- total_delta_chr_f_recall / total_scores as f64+ delta_chr_f_recall_sum / total_scores as f64+ },+ avg_braces_disbalance: if total_scores == 0 {+ 0.0+ } else {+ braces_disbalance_sum as f32 / total_scores as f32},- avg_braces_disbalance,exact_lines_true_positives: total_exact_lines.true_positives,exact_lines_false_positives: total_exact_lines.false_positives,exact_lines_false_negatives: total_exact_lines.false_negatives,exact_lines_precision: total_exact_lines.precision(),exact_lines_recall: total_exact_lines.recall(),exact_lines_f1: total_exact_lines.f1(),- avg_reversal_ratio,- qa_avg_reverts_edits,- qa_avg_confidence,- cursor_exact_match_rate,- cursor_avg_distance,- cursor_total_evaluated,- wrong_editable_region_rate,- isolated_whitespace_rate,- avg_kept_rate,- avg_recall_rate,- total_kept_chars,- total_correctly_deleted_chars,- total_discarded_chars,- avg_editable_context_lines_precision,- avg_editable_context_lines_recall,- avg_editable_context_lines_f1,- editable_context_lines_tp,- editable_context_lines_fp,- editable_context_lines_fn,- avg_editable_context_files_precision,- avg_editable_context_files_recall,- avg_editable_context_files_f1,- editable_context_files_tp,- editable_context_files_fp,- editable_context_files_fn,- avg_retrieved_context_bytes,- total_retrieved_context_bytes,- retrieved_context_examples,+ avg_reversal_ratio: if total_scores == 0 {+ 0.0+ } else {+ reversal_ratio_sum / total_scores as f32+ },+ qa_avg_reverts_edits: qa_reverts.rate(),+ qa_avg_confidence: qa_confidence.mean_f32(),+ cursor_exact_match_rate: cursor_exact.rate(),+ cursor_exact_matches: cursor_exact.hits(),+ cursor_avg_distance: cursor_distance.mean_f32(),+ cursor_total_evaluated: cursor_exact.total(),+ wrong_editable_region_rate: wrong_editable_region.rate(),+ isolated_whitespace_rate: isolated_whitespace.rate(),+ isolated_whitespace_count: isolated_whitespace.hits(),+ avg_kept_rate: kept_rate.mean(),+ kept_rate_examples: kept_rate.count(),+ avg_recall_rate: recall_rate.mean(),+ recall_rate_examples: recall_rate.count(),+ total_kept_chars: kept_chars.total(),+ total_correctly_deleted_chars: correctly_deleted_chars.total(),+ total_discarded_chars: discarded_chars.total(),+ editable_context_examples: editable_context.count(),+ avg_editable_context_lines_precision: editable_context.lines.avg_precision(),+ avg_editable_context_lines_recall: editable_context.lines.avg_recall(),+ avg_editable_context_lines_f1: editable_context.lines.avg_f1(),+ editable_context_lines_tp: editable_context.lines.true_positives(),+ editable_context_lines_fp: editable_context.lines.false_positives(),+ editable_context_lines_fn: editable_context.lines.false_negatives(),+ avg_editable_context_files_precision: editable_context.files.avg_precision(),+ avg_editable_context_files_recall: editable_context.files.avg_recall(),+ avg_editable_context_files_f1: editable_context.files.avg_f1(),+ editable_context_files_tp: editable_context.files.true_positives(),+ editable_context_files_fp: editable_context.files.false_positives(),+ editable_context_files_fn: editable_context.files.false_negatives(),+ jump_location_examples: jump_location.count(),+ avg_jump_location_lines_precision: jump_location.lines.avg_precision(),+ avg_jump_location_lines_recall: jump_location.lines.avg_recall(),+ avg_jump_location_lines_f1: jump_location.lines.avg_f1(),+ jump_location_lines_tp: jump_location.lines.true_positives(),+ jump_location_lines_fp: jump_location.lines.false_positives(),+ jump_location_lines_fn: jump_location.lines.false_negatives(),+ avg_jump_location_files_precision: jump_location.files.avg_precision(),+ avg_jump_location_files_recall: jump_location.files.avg_recall(),+ avg_jump_location_files_f1: jump_location.files.avg_f1(),+ jump_location_files_tp: jump_location.files.true_positives(),+ jump_location_files_fp: jump_location.files.false_positives(),+ jump_location_files_fn: jump_location.files.false_negatives(),+ avg_retrieved_context_bytes: retrieved_context_bytes.mean(),+ total_retrieved_context_bytes: retrieved_context_bytes.total(),+ retrieved_context_examples: retrieved_context_bytes.count(),}}diff --git a/crates/zeta_prompt/src/hashed_regions.rs b/crates/zeta_prompt/src/hashed_regions.rsnew file mode 100644index 00000000000..59b927e46fa--- /dev/null+++ b/crates/zeta_prompt/src/hashed_regions.rs@@ -0,0 +1,1257 @@+//! Hashed Regions (V0609HashedRegions): a variant of the Smart Regions+//! multi-region format where marker tags are identified by a short+//! content-derived hash (e.g. `<|marker_b1f8|>`) instead of a sequence+//! number.+//!+//! Hashed identifiers are self-describing: a tag can be mapped back to its+//! location without reproducing the exact rendering order of the prompt, so+//! markers can be placed across *all* prompt context, and budget-based+//! truncation of related files doesn't shift the addressing of the remaining+//! markers. All context, including the current file, lives in related files:+//! context retrieval includes the current file via `ContextSource::CurrentFile`,+//! so the cursor file is expected to be one of the related files. Inputs that+//! weren't run through current-file retrieval can be normalized with+//! [`ensure_cursor_file_excerpt`] before rendering or parsing.++use crate::{ContextSource, RelatedExcerpt, RelatedFile, ZetaPromptInput, multi_region, udiff};+use anyhow::{Context as _, Result, anyhow};+use std::{+ borrow::Cow,+ collections::{HashMap, HashSet},+ ops::Range,+ path::{Path, PathBuf},+};++pub const MARKER_TAG_PREFIX: &str = "<|marker_";+pub const MARKER_TAG_SUFFIX: &str = "|>";+pub const V0615_END_MARKER: &str = "<[end▁of▁sentence]>";+pub const NO_EDITS: &str = "NO_EDITS";+/// Number of base64 characters in a marker tag identifier.+pub const TAG_ID_LEN: usize = 4;++const BASE64_URL_SAFE_ALPHABET: &[u8; 64] =+ b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";++pub fn marker_tag(id: &str) -> String {+ format!("{MARKER_TAG_PREFIX}{id}{MARKER_TAG_SUFFIX}")+}++/// Marker tags assigned to one contiguous snippet of context.+#[derive(Debug, Clone)]+pub struct SnippetMarkers {+ pub file_ix: usize,+ pub excerpt_ix: usize,+ /// `(tag id, byte offset within the snippet text)`, sorted by offset.+ /// The first marker is at offset 0 and the last at `text.len()`.+ pub markers: Vec<(String, usize)>,+}++/// Assign hashed marker tags to every related-file excerpt of `input`.+///+/// The assignment is deterministic and independent of any later budget-based+/// truncation, so the same table can be rebuilt when parsing model output.+pub fn build_marker_table(input: &ZetaPromptInput) -> Vec<SnippetMarkers> {+ build_marker_table_with_filter(input, |_| true)+}++pub fn build_editable_marker_table(input: &ZetaPromptInput) -> Vec<SnippetMarkers> {+ build_marker_table_with_filter(input, is_hash_region_editable_context_source)+}++pub fn is_hash_region_editable_context_source(context_source: ContextSource) -> bool {+ matches!(+ context_source,+ ContextSource::CurrentFile | ContextSource::EditHistory+ )+}++fn build_marker_table_with_filter(+ input: &ZetaPromptInput,+ include_context_source: impl Fn(ContextSource) -> bool,+) -> Vec<SnippetMarkers> {+ let mut used_ids = HashSet::new();+ let mut snippets = Vec::new();+ if let Some(related_files) = input.related_files.as_deref() {+ for (file_ix, file) in related_files.iter().enumerate() {+ for (excerpt_ix, excerpt) in file.excerpts.iter().enumerate() {+ if include_context_source(excerpt.context_source) {+ snippets.push(SnippetMarkers {+ file_ix,+ excerpt_ix,+ markers: assign_tags(&excerpt.text, &mut used_ids),+ });+ }+ }+ }+ }+ snippets+}++pub fn markers_for_text(text: &str) -> Vec<(String, usize)> {+ let mut used_ids = HashSet::new();+ assign_tags(text, &mut used_ids)+}++fn assign_tags(text: &str, used_ids: &mut HashSet<String>) -> Vec<(String, usize)> {+ let offsets = multi_region::compute_marker_offsets_v0618(text);+ offsets+ .iter()+ .enumerate()+ .map(|(i, &offset)| {+ let block = match offsets.get(i + 1) {+ Some(&next_offset) => &text[offset..next_offset],+ // The final marker has no following block; hash the preceding+ // one. This collides with the previous marker's tag by+ // construction, which `unique_tag_id` resolves by reseeding.+ None => {+ let previous_offset = if i == 0 { 0 } else { offsets[i - 1] };+ &text[previous_offset..offset]+ }+ };+ (unique_tag_id(block, used_ids), offset)+ })+ .collect()+}++fn unique_tag_id(content: &str, used_ids: &mut HashSet<String>) -> String {+ let mut seed = 0u64;+ loop {+ let id = encode_tag_id(hash_with_seed(content, seed));+ if used_ids.insert(id.clone()) {+ return id;+ }+ seed += 1;+ }+}++/// FNV-1a, with the seed folded in ahead of the content.+fn hash_with_seed(content: &str, seed: u64) -> u64 {+ const FNV_OFFSET: u64 = 0xcbf29ce484222325;+ const FNV_PRIME: u64 = 0x100000001b3;+ let mut hash = FNV_OFFSET;+ for byte in seed.to_le_bytes() {+ hash ^= byte as u64;+ hash = hash.wrapping_mul(FNV_PRIME);+ }+ for byte in content.as_bytes() {+ hash ^= *byte as u64;+ hash = hash.wrapping_mul(FNV_PRIME);+ }+ hash+}++fn encode_tag_id(hash: u64) -> String {+ (0..TAG_ID_LEN)+ .map(|i| BASE64_URL_SAFE_ALPHABET[((hash >> (6 * i)) & 0x3f) as usize] as char)+ .collect()+}++/// Write `text` into `output`, inserting marker tags at the given offsets.+/// When `cursor` is provided, its marker string is inserted at the given byte+/// offset within `text`.+pub fn write_snippet_with_markers(+ output: &mut String,+ text: &str,+ markers: &[(String, usize)],+ cursor: Option<(usize, &str)>,+) {+ let mut cursor_placed = false;+ for (i, (id, offset)) in markers.iter().enumerate() {+ if !output.is_empty() && !output.ends_with('\n') {+ output.push('\n');+ }+ output.push_str(&marker_tag(id));++ if let Some((_, next_offset)) = markers.get(i + 1) {+ output.push('\n');+ let block = &text[*offset..*next_offset];+ match cursor {+ Some((cursor_offset, cursor_marker))+ if !cursor_placed+ && cursor_offset >= *offset+ && cursor_offset <= *next_offset =>+ {+ cursor_placed = true;+ let cursor_in_block = cursor_offset - offset;+ output.push_str(&block[..cursor_in_block]);+ output.push_str(cursor_marker);+ output.push_str(&block[cursor_in_block..]);+ }+ _ => output.push_str(block),+ }+ }+ }+}++/// Extract a marker-bounded span from a model-output codeblock.+///+/// Returns `(start tag id, end tag id, content)` where `content` is the text+/// between the first and last marker tags, with any intermediate marker tags+/// stripped.+pub fn extract_marker_span(text: &str) -> Result<(String, String, String)> {+ let (start_id, end_id, content) = extract_marker_span_allow_same(text)?;+ if start_id == end_id {+ return Err(anyhow!(+ "start and end markers are the same (marker {start_id})"+ ));+ }+ Ok((start_id, end_id, content))+}++pub fn extract_marker_span_allow_same(text: &str) -> Result<(String, String, String)> {+ let first_tag_start = text+ .find(MARKER_TAG_PREFIX)+ .context("no start marker found in output")?;+ let first_id_start = first_tag_start + MARKER_TAG_PREFIX.len();+ let first_id_end = text[first_id_start..]+ .find(MARKER_TAG_SUFFIX)+ .map(|i| i + first_id_start)+ .context("malformed start marker tag")?;+ let start_id = &text[first_id_start..first_id_end];+ let first_tag_end = first_id_end + MARKER_TAG_SUFFIX.len();++ let last_tag_start = text+ .rfind(MARKER_TAG_PREFIX)+ .context("no end marker found in output")?;+ if last_tag_start == first_tag_start {+ return Err(anyhow!("output span must be bounded by two marker tags"));+ }+ let last_id_start = last_tag_start + MARKER_TAG_PREFIX.len();+ let last_id_end = text[last_id_start..]+ .find(MARKER_TAG_SUFFIX)+ .map(|i| i + last_id_start)+ .context("malformed end marker tag")?;+ let end_id = &text[last_id_start..last_id_end];++ let mut content_start = first_tag_end;+ if text.as_bytes().get(content_start) == Some(&b'\n') {+ content_start += 1;+ }+ let content_end = last_tag_start;+ let content = &text[content_start..content_end.max(content_start)];+ let content = multi_region::strip_marker_tags(content);+ Ok((start_id.to_string(), end_id.to_string(), content))+}++pub struct RelatedFileCursor {+ pub file_ix: usize,+ pub excerpt_ix: usize,+ pub offset_in_excerpt: usize,+}++struct ParseSnippet<'a> {+ file_ix: usize,+ first_excerpt_ix: usize,+ last_excerpt_ix: usize,+ end_row: u32,+ text: Cow<'a, str>,+ markers: Vec<(String, usize)>,+}++pub fn related_file_patch_path(cursor_path: &Path, related_path: &Path) -> PathBuf {+ let stripped: PathBuf = related_path.iter().skip(1).collect();+ if stripped == cursor_path {+ return stripped;+ }++ let cursor_first_component = cursor_path.components().next();+ let related_first_component = related_path.components().next();+ if related_first_component.is_some()+ && cursor_first_component != related_first_component+ && related_path.components().count() > 1+ {+ stripped+ } else {+ related_path.to_path_buf()+ }+}++fn line_start_offset(text: &str, row: usize) -> Option<usize> {+ let mut offset = 0;+ for _ in 0..row {+ offset += text[offset..].find('\n')? + 1;+ }+ Some(offset)+}++pub fn locate_cursor_in_related_files(input: &ZetaPromptInput) -> Option<RelatedFileCursor> {+ let related_files = input.related_files.as_deref()?;+ let excerpt_start_row = input.excerpt_start_row?;+ let cursor_offset = input.cursor_offset_in_excerpt;+ let excerpt_prefix = input.cursor_excerpt.get(..cursor_offset)?;+ let cursor_row = excerpt_start_row + excerpt_prefix.matches('\n').count() as u32;+ let cursor_column = cursor_offset - excerpt_prefix.rfind('\n').map_or(0, |pos| pos + 1);++ for (file_ix, file) in related_files.iter().enumerate() {+ if related_file_patch_path(&input.cursor_path, &file.path) != input.cursor_path.as_ref() {+ continue;+ }++ for (excerpt_ix, excerpt) in file.excerpts.iter().enumerate() {+ if cursor_row < excerpt.row_range.start || cursor_row > excerpt.row_range.end {+ continue;+ }+ let row_in_excerpt = (cursor_row - excerpt.row_range.start) as usize;+ let line_start = line_start_offset(&excerpt.text, row_in_excerpt)?;+ let line_len = excerpt.text[line_start..]+ .lines()+ .next()+ .unwrap_or("")+ .len();+ if cursor_column <= line_len {+ return Some(RelatedFileCursor {+ file_ix,+ excerpt_ix,+ offset_in_excerpt: line_start + cursor_column,+ });+ }+ }+ }++ None+}++/// Ensure the cursor file is represented by a related-file excerpt that covers+/// the cursor, synthesizing one from `cursor_excerpt` when it isn't.+///+/// All hashed-region context — including the current file — is addressed+/// through `related_files` (see module docs), so a prompt built from a+/// `ZetaPromptInput` whose `related_files` don't cover the cursor cannot be+/// rendered or parsed. Inputs produced by current-file context retrieval+/// (`ContextSource::CurrentFile`) are already covered and left untouched; this+/// normalizes the rest (e.g. raw settled-data samples, or any caller that+/// didn't run current-file retrieval) from the `cursor_excerpt` the input+/// already carries, so the format is usable without re-running context+/// collection.+///+/// When synthesis is needed, any pre-existing excerpts of the cursor file are+/// **replaced** by the synthesized window: the renderer emits excerpts verbatim+/// without coalescing, so keeping overlapping fragments would duplicate lines+/// with conflicting markers. Other related files are left untouched.+///+/// Returns whether the cursor file is covered after the call (already, or via+/// the synthesized excerpt). Returns `false` only when coverage couldn't be+/// established — e.g. a missing `excerpt_start_row` or an empty+/// `cursor_excerpt` — in which case the input is left unchanged.+pub fn ensure_cursor_file_excerpt(input: &mut ZetaPromptInput) -> bool {+ if locate_cursor_in_related_files(input).is_some() {+ return true;+ }+ let Some(excerpt_start_row) = input.excerpt_start_row else {+ return false;+ };+ if input.cursor_excerpt.is_empty() {+ return false;+ }++ let cursor_excerpt = input.cursor_excerpt.clone();+ let end_row = excerpt_start_row + cursor_excerpt.matches('\n').count() as u32;+ let synthesized = RelatedExcerpt {+ row_range: excerpt_start_row..end_row,+ text: cursor_excerpt,+ order: 0,+ context_source: ContextSource::CurrentFile,+ };++ let cursor_path = input.cursor_path.clone();+ let in_open_source_repo = input.in_open_source_repo;+ let related_files = input.related_files.get_or_insert_with(Vec::new);+ if let Some(file) = related_files+ .iter_mut()+ .find(|file| related_file_patch_path(&cursor_path, &file.path) == cursor_path.as_ref())+ {+ file.max_row = file.max_row.max(end_row);+ file.excerpts = vec![synthesized];+ } else {+ related_files.insert(+ 0,+ RelatedFile {+ path: cursor_path,+ max_row: end_row,+ excerpts: vec![synthesized],+ in_open_source_repo,+ },+ );+ }++ // Confirm the synthesized excerpt actually covers the cursor (guards against+ // a cursor offset that lies outside the excerpt text).+ locate_cursor_in_related_files(input).is_some()+}++pub fn marker_table_for_excerpt(+ marker_table: &[SnippetMarkers],+ file_ix: usize,+ excerpt_ix: usize,+) -> Option<&[(String, usize)]> {+ marker_table.iter().find_map(|snippet| {+ (snippet.file_ix == file_ix && snippet.excerpt_ix == excerpt_ix)+ .then_some(snippet.markers.as_slice())+ })+}++fn merge_contiguous_snippets(+ input: &ZetaPromptInput,+ marker_table: Vec<SnippetMarkers>,+) -> Result<Vec<ParseSnippet<'_>>> {+ let related_files = input+ .related_files+ .as_deref()+ .context("prompt inputs are missing related files")?;+ let mut snippets: Vec<ParseSnippet> = Vec::new();+ for snippet in marker_table {+ let file = related_files+ .get(snippet.file_ix)+ .context("related file index out of range")?;+ let excerpt = file+ .excerpts+ .get(snippet.excerpt_ix)+ .context("related excerpt index out of range")?;+ if let Some(last) = snippets.last_mut()+ && last.file_ix == snippet.file_ix+ && last.last_excerpt_ix + 1 == snippet.excerpt_ix+ && last.end_row == excerpt.row_range.start+ {+ let text = last.text.to_mut();+ if !text.is_empty() && !text.ends_with('\n') {+ text.push('\n');+ }+ let base = text.len();+ text.push_str(&excerpt.text);+ last.markers.extend(+ snippet+ .markers+ .into_iter()+ .map(|(id, offset)| (id, base + offset)),+ );+ last.last_excerpt_ix = snippet.excerpt_ix;+ last.end_row = excerpt.row_range.end;+ } else {+ snippets.push(ParseSnippet {+ file_ix: snippet.file_ix,+ first_excerpt_ix: snippet.excerpt_ix,+ last_excerpt_ix: snippet.excerpt_ix,+ end_row: excerpt.row_range.end,+ text: Cow::Borrowed(excerpt.text.as_ref()),+ markers: snippet.markers,+ });+ }+ }+ Ok(snippets)+}++fn snippet_path_and_start_row(+ input: &ZetaPromptInput,+ snippet: &ParseSnippet<'_>,+) -> Result<(PathBuf, u32)> {+ let related_files = input+ .related_files+ .as_deref()+ .context("prompt inputs are missing related files")?;+ let file = related_files+ .get(snippet.file_ix)+ .context("related file index out of range")?;+ let excerpt = file+ .excerpts+ .get(snippet.first_excerpt_ix)+ .context("related excerpt index out of range")?;+ Ok((+ related_file_patch_path(&input.cursor_path, &file.path),+ excerpt.row_range.start,+ ))+}++fn common_prefix_suffix(a: &[u8], b: &[u8]) -> (usize, usize) {+ let prefix = a.iter().zip(b.iter()).take_while(|(x, y)| x == y).count();+ let remaining_a = a.len() - prefix;+ let remaining_b = b.len() - prefix;+ let max_suffix = remaining_a.min(remaining_b);+ let suffix = a[a.len() - max_suffix..]+ .iter()+ .rev()+ .zip(b[b.len() - max_suffix..].iter().rev())+ .take_while(|(x, y)| x == y)+ .count();+ (prefix, suffix)+}++fn nearest_marker_id(markers: &[(String, usize)], cursor_offset: Option<usize>) -> &str {+ let cursor = cursor_offset.unwrap_or(0);+ markers+ .iter()+ .min_by_key(|(_, offset)| (*offset as isize - cursor as isize).unsigned_abs())+ .map(|(id, _)| id.as_str())+ .unwrap_or("unknown")+}++/// Encode a single marker-bounded edit block for one snippet, given its old and+/// new text. The returned block starts and ends with a marker tag and does+/// **not** include the output end marker; callers concatenate blocks and append+/// [`V0615_END_MARKER`] once after the last block.+pub fn encode_from_old_and_new(+ old_text: &str,+ new_text: &str,+ markers: &[(String, usize)],+ cursor_offset_in_new: Option<usize>,+ cursor_marker: &str,+) -> Result<String> {+ let no_edit_id = nearest_marker_id(markers, cursor_offset_in_new);+ if old_text == new_text {+ let tag = marker_tag(no_edit_id);+ return Ok(format!("{tag}{tag}"));+ }++ let (common_prefix, common_suffix) =+ common_prefix_suffix(old_text.as_bytes(), new_text.as_bytes());+ let change_end_in_old = old_text.len() - common_suffix;+ let mut start_marker_ix = markers+ .iter()+ .rposition(|(_, offset)| *offset <= common_prefix)+ .unwrap_or(0);+ let mut end_marker_ix = markers+ .iter()+ .position(|(_, offset)| *offset >= change_end_in_old)+ .unwrap_or_else(|| markers.len().saturating_sub(1));++ if start_marker_ix == end_marker_ix {+ if end_marker_ix < markers.len().saturating_sub(1) {+ end_marker_ix += 1;+ } else if start_marker_ix > 0 {+ start_marker_ix -= 1;+ }+ }++ let old_start = markers+ .get(start_marker_ix)+ .map(|(_, offset)| *offset)+ .context("start marker out of range")?;+ let old_end = markers+ .get(end_marker_ix)+ .map(|(_, offset)| *offset)+ .context("end marker out of range")?;+ let new_start = old_start;+ let new_end = new_text+ .len()+ .saturating_sub(old_text.len().saturating_sub(old_end));+ let new_span = &new_text[new_start..new_end];++ let mut result = String::new();+ result.push_str(&marker_tag(&markers[start_marker_ix].0));+ result.push('\n');+ if let Some(cursor_offset) = cursor_offset_in_new {+ if cursor_offset >= new_start && cursor_offset <= new_end {+ let cursor_in_span = cursor_offset - new_start;+ result.push_str(&new_span[..cursor_in_span]);+ result.push_str(cursor_marker);+ result.push_str(&new_span[cursor_in_span..]);+ } else {+ result.push_str(new_span);+ }+ } else {+ result.push_str(new_span);+ }+ if !result.ends_with('\n') {+ result.push('\n');+ }+ result.push_str(&marker_tag(&markers[end_marker_ix].0));+ Ok(result)+}++/// Parse student model output (raw marker spans, no markdown code fences) into+/// a unified patch.+///+/// The output is a run of marker tags with no fences, so blocks are delimited by+/// pairing tags two at a time: `(1, 2), (3, 4), ...`. This matches the encoder,+/// which emits exactly two tags per block and no intermediate tags. Any+/// unpaired trailing tag is ignored.+pub fn parse_output_as_patch(+ input: &ZetaPromptInput,+ output: &str,+ cursor_marker: &str,+) -> Result<String> {+ let output = output.strip_suffix(V0615_END_MARKER).unwrap_or(output);+ if output.trim() == NO_EDITS {+ return Ok(String::new());+ }++ let spans = pair_marker_spans(output)?;+ let (patch, _cursor) = build_patch_from_spans(input, &spans, cursor_marker)?;+ Ok(patch)+}++/// A cursor position resolved while turning marker-span edits into a patch.+pub struct HashRegionCursor {+ pub path: PathBuf,+ /// Byte offset of the cursor within `new_text`.+ pub cursor_offset_in_new_text: usize,+ /// Full new text of the edited snippet, after applying all of its edits.+ pub new_text: String,+ /// Original text of the edited snippet.+ pub old_text: String,+ /// 0-based row where the snippet starts in its file.+ pub start_row: u32,+}++/// One marker-bounded edit resolved against a parse snippet.+struct ParsedSpanEdit {+ snippet_ix: usize,+ range: Range<usize>,+ new_text: String,+ cursor_offset_in_new_text: Option<usize>,+}++/// Split raw model output into marker-bounded spans by pairing marker tags two+/// at a time. Returns `(start_id, end_id, raw_new_span)` per pair, where+/// `raw_new_span` may still contain the cursor marker.+fn pair_marker_spans(output: &str) -> Result<Vec<(String, String, String)>> {+ let tags = find_all_marker_tags(output);+ if tags.len() < 2 {+ return Err(anyhow!("output does not contain a marker-bounded span"));+ }+ let mut spans = Vec::new();+ let mut i = 0;+ while i + 1 < tags.len() {+ let (start_id, _, start_tag_end) = &tags[i];+ let (end_id, end_tag_start, _) = &tags[i + 1];+ let content = &output[*start_tag_end..*end_tag_start];+ let content = content.strip_prefix('\n').unwrap_or(content);+ let content = multi_region::strip_marker_tags(content);+ spans.push((start_id.clone(), end_id.clone(), content));+ i += 2;+ }+ Ok(spans)+}++/// Find every marker tag in `text`, in order, as `(id, tag_start, tag_end)`.+fn find_all_marker_tags(text: &str) -> Vec<(String, usize, usize)> {+ let mut tags = Vec::new();+ let mut search = 0;+ while let Some(rel) = text[search..].find(MARKER_TAG_PREFIX) {+ let tag_start = search + rel;+ let id_start = tag_start + MARKER_TAG_PREFIX.len();+ let Some(suffix_rel) = text[id_start..].find(MARKER_TAG_SUFFIX) else {+ break;+ };+ let id_end = id_start + suffix_rel;+ let tag_end = id_end + MARKER_TAG_SUFFIX.len();+ tags.push((text[id_start..id_end].to_string(), tag_start, tag_end));+ search = tag_end;+ }+ tags+}++/// Resolve a list of marker spans into per-snippet edits and assemble a unified+/// patch.+///+/// `spans` is a list of `(start_id, end_id, raw_new_span)` where `raw_new_span`+/// may still contain `cursor_marker`. This is shared by the student parser+/// (which pairs raw marker tags) and the teacher parser (which extracts spans+/// from markdown code fences). Edits that overlap an already-accepted edit in+/// the same snippet are skipped (lenient). The cursor marker is honored in+/// every region that contains it; the returned [`HashRegionCursor`] reports the+/// first such position.+pub fn build_patch_from_spans(+ input: &ZetaPromptInput,+ spans: &[(String, String, String)],+ cursor_marker: &str,+) -> Result<(String, Option<HashRegionCursor>)> {+ let marker_table = build_marker_table(input);+ let snippets = merge_contiguous_snippets(input, marker_table)?;+ let mut marker_index: HashMap<&str, (usize, usize)> = HashMap::new();+ for (snippet_ix, snippet) in snippets.iter().enumerate() {+ for (id, offset) in &snippet.markers {+ marker_index.insert(id.as_str(), (snippet_ix, *offset));+ }+ }++ let mut edits: Vec<ParsedSpanEdit> = Vec::new();+ for (start_id, end_id, raw_new_span) in spans {+ let &(start_snippet, start_byte) = marker_index+ .get(start_id.as_str())+ .with_context(|| format!("unknown start marker `{start_id}`"))?;+ let &(end_snippet, end_byte) = marker_index+ .get(end_id.as_str())+ .with_context(|| format!("unknown end marker `{end_id}`"))?;++ if start_snippet != end_snippet {+ return Err(anyhow!(+ "markers `{start_id}` and `{end_id}` belong to different context snippets \+ that are not contiguous excerpts of the same file"+ ));+ }+ if start_byte > end_byte {+ return Err(anyhow!(+ "start marker `{start_id}` must come before end marker `{end_id}`"+ ));+ }++ let old_text = snippets[start_snippet].text.as_ref();+ let old_span = &old_text[start_byte..end_byte];++ let cursor_in_span = raw_new_span.find(cursor_marker);+ let mut new_span = raw_new_span.replace(cursor_marker, "");+ if old_span.is_empty() {+ if !new_span.is_empty() && !new_span.ends_with('\n') {+ new_span.push('\n');+ }+ } else {+ if old_span.ends_with('\n') && !new_span.ends_with('\n') && !new_span.is_empty() {+ new_span.push('\n');+ }+ if !old_span.ends_with('\n') && new_span.ends_with('\n') {+ new_span.pop();+ }+ }++ if !new_span.is_empty()+ && let Some(dropped) = detect_trailing_deletion(old_span, &new_span)+ {+ return Err(anyhow!(+ "edit span `{start_id}`..`{end_id}` looks truncated: the replacement \+ stops before the end marker, which would silently delete:\n{dropped}"+ ));+ }++ // `cursor_in_span` was located in `raw_new_span` before the trailing+ // newline normalization above, which can drop a byte. Clamp it to the+ // finalized replacement so the offset never points past `new_span`+ // (downstream cursor mapping byte-slices `new_text` by this offset).+ let cursor_offset_in_new_text = cursor_in_span.map(|offset| offset.min(new_span.len()));+ edits.push(ParsedSpanEdit {+ snippet_ix: start_snippet,+ range: start_byte..end_byte,+ new_text: new_span,+ cursor_offset_in_new_text,+ });+ }++ assemble_patch_from_edits(input, &snippets, edits)+}++/// Apply resolved edits to their snippets and emit one diff section per edited+/// snippet, in the order snippets first appear in the edit sequence.+fn assemble_patch_from_edits(+ input: &ZetaPromptInput,+ snippets: &[ParseSnippet<'_>],+ edits: Vec<ParsedSpanEdit>,+) -> Result<(String, Option<HashRegionCursor>)> {+ let mut snippet_order: Vec<usize> = Vec::new();+ for edit in &edits {+ if !snippet_order.contains(&edit.snippet_ix) {+ snippet_order.push(edit.snippet_ix);+ }+ }++ let mut diff_output = String::new();+ let mut cursor = None;++ for &snippet_ix in &snippet_order {+ let snippet = &snippets[snippet_ix];+ let mut snippet_edits: Vec<&ParsedSpanEdit> = edits+ .iter()+ .filter(|edit| edit.snippet_ix == snippet_ix)+ .collect();+ snippet_edits.sort_by_key(|edit| edit.range.start);++ // Lenient overlap handling: keep edits in line order, dropping any whose+ // range starts before the previous accepted edit ended.+ let mut accepted: Vec<&ParsedSpanEdit> = Vec::new();+ let mut last_end = 0usize;+ for edit in snippet_edits {+ if !accepted.is_empty() && edit.range.start < last_end {+ continue;+ }+ last_end = edit.range.end;+ accepted.push(edit);+ }++ let old_text = snippet.text.as_ref();+ let (path, start_row) = snippet_path_and_start_row(input, snippet)?;++ let mut new_text = String::new();+ let mut position = 0;+ let mut cursor_in_new_text = None;+ for edit in &accepted {+ new_text.push_str(&old_text[position..edit.range.start]);+ if let Some(cursor_offset) = edit.cursor_offset_in_new_text {+ cursor_in_new_text = Some(new_text.len() + cursor_offset);+ }+ new_text.push_str(&edit.new_text);+ position = edit.range.end;+ }+ new_text.push_str(&old_text[position..]);++ let diff = udiff::unified_diff_with_context(old_text, &new_text, start_row, start_row, 3);+ if !diff.is_empty() {+ let path_str = path+ .iter()+ .map(|component| component.to_string_lossy())+ .collect::<Vec<_>>()+ .join("/");+ diff_output.push_str(&format!("--- a/{path_str}\n+++ b/{path_str}\n"));+ diff_output.push_str(&diff);+ if !diff_output.ends_with('\n') {+ diff_output.push('\n');+ }+ }++ if cursor.is_none()+ && let Some(cursor_offset) = cursor_in_new_text+ {+ cursor = Some(HashRegionCursor {+ path: path.clone(),+ cursor_offset_in_new_text: cursor_offset,+ new_text: new_text.clone(),+ old_text: old_text.to_string(),+ start_row,+ });+ }+ }++ Ok((diff_output, cursor))+}++/// Detects a span replacement that ends in a pure deletion of the span's tail,+/// the signature of a model that stopped writing before reaching its end+/// marker.+///+/// Returns the deleted tail if the line diff between `old_span` and `new_span`+/// ends with a deletion-only group that reaches the last line of `old_span` and+/// drops more than `MAX_TRAILING_DELETED_LINES` non-blank lines.+fn detect_trailing_deletion(old_span: &str, new_span: &str) -> Option<String> {+ const MAX_TRAILING_DELETED_LINES: usize = 3;++ fn flag_if_large(deleted_tail: &str) -> Option<String> {+ let non_blank_deleted = deleted_tail+ .lines()+ .filter(|line| !line.trim().is_empty())+ .count();+ (non_blank_deleted > MAX_TRAILING_DELETED_LINES)+ .then(|| deleted_tail.trim_end().to_string())+ }++ // A verbatim prefix is checked at the byte level so that a replacement+ // stopping mid-line is caught too; the line diff below would see that as a+ // trailing replace group rather than a pure deletion.+ if let Some(deleted_tail) = old_span.strip_prefix(new_span) {+ return flag_if_large(deleted_tail);+ }++ // With zero context lines, hunks contain only `-` and `+` lines, and within+ // a hunk deletions precede insertions, so a diff whose final line is a+ // deletion ends with a deletion-only group.+ let diff = udiff::unified_diff_with_context(old_span, new_span, 0, 0, 0);+ let lines: Vec<&str> = diff.lines().collect();+ let mut deletion_start = lines.len();+ while deletion_start > 0 && lines[deletion_start - 1].starts_with('-') {+ deletion_start -= 1;+ }+ let deleted: Vec<&str> = lines[deletion_start..]+ .iter()+ .map(|line| line.strip_prefix('-').unwrap_or(line))+ .collect();+ if deleted.is_empty() {+ return None;+ }++ // The trailing `-` run is preceded by its hunk header exactly when the hunk+ // is deletion-only (a replacement group would interpose `+` lines).+ let header = lines.get(deletion_start.checked_sub(1)?)?;+ let old_range_start: usize = header+ .strip_prefix("@@ -")?+ .split(',')+ .next()?+ .parse()+ .ok()?;++ // Only flag deletions that reach the end of the span; a deletion in the+ // middle is followed by reproduced context, so the model demonstrably kept+ // writing past it.+ if old_range_start + deleted.len() - 1 != old_span.lines().count() {+ return None;+ }++ flag_if_large(&deleted.join("\n"))+}++/// Encode an expected unified patch into the training output for a student.+///+/// Emits **one marker-bounded block per diff hunk**, in patch order (which+/// preserves the teacher's cross-file ordering), with no markdown code fences,+/// terminated by a single [`V0615_END_MARKER`]. Blocks are separated by a+/// newline; the parser re-pairs marker tags two at a time, so the separator is+/// not significant.+///+/// Reachability is per hunk: a hunk whose file is absent from the prompt+/// context, or whose location can't be resolved within its snippet, is skipped.+/// If at least one hunk is reachable, the remaining hunks are still encoded+/// (partial edit). If no hunk is reachable, the output is `NO_EDITS`.+pub fn encode_patch_as_output(+ input: &ZetaPromptInput,+ patch: &str,+ cursor_offset: Option<usize>,+ cursor_marker: &str,+) -> Result<String> {+ if patch.lines().count() <= 3 {+ return Ok(format!("{NO_EDITS}{V0615_END_MARKER}"));+ }++ let marker_table = build_marker_table(input);+ let snippets = merge_contiguous_snippets(input, marker_table)?;+ let mut parser = udiff::DiffParser::new(patch);+ let mut blocks: Vec<String> = Vec::new();++ while let Some(event) = parser.next().context("failed to parse expected patch")? {+ let udiff::DiffEvent::Hunk {+ path,+ mut hunk,+ status: _,+ } = event+ else {+ continue;+ };++ // A hunk whose file isn't in the prompt context is unreachable; skip it+ // and keep any other reachable hunks (partial edit).+ let Some((snippet_ix, start_row)) =+ snippets+ .iter()+ .enumerate()+ .find_map(|(snippet_ix, snippet)| {+ let (snippet_path, start_row) =+ snippet_path_and_start_row(input, snippet).ok()?;+ (snippet_path == Path::new(path.as_ref())).then_some((snippet_ix, start_row))+ })+ else {+ continue;+ };+ let snippet = &snippets[snippet_ix];+ let old_text = snippet.text.as_ref();+ let candidates = udiff::find_context_candidates(old_text, &mut hunk);+ // A hunk whose location can't be pinned down within the snippet is+ // unreachable; skip it.+ let Some(hunk_offset) =+ udiff::disambiguate_by_line_number(&candidates, hunk.start_line, &|offset| {+ start_row + old_text[..offset].matches('\n').count() as u32+ })+ else {+ continue;+ };++ let mut new_text = old_text.to_string();+ for edit in hunk.edits.iter().rev() {+ let range = (hunk_offset + edit.range.start)..(hunk_offset + edit.range.end);+ new_text.replace_range(range, &edit.text);+ }+ // The cursor marker is placed in every region whose span contains it.+ // The extracted `cursor_offset` is hunk-relative, so map it through each+ // hunk's offset; `encode_from_old_and_new` inserts it only when it lands+ // within that block's span.+ let cursor_in_new = cursor_offset.map(|cursor| (hunk_offset + cursor).min(new_text.len()));+ blocks.push(encode_from_old_and_new(+ old_text,+ &new_text,+ &snippet.markers,+ cursor_in_new,+ cursor_marker,+ )?);+ }++ if blocks.is_empty() {+ return Ok(format!("{NO_EDITS}{V0615_END_MARKER}"));+ }++ let mut output = blocks.join("\n");+ output.push_str(V0615_END_MARKER);+ Ok(output)+}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::{ContextSource, RelatedExcerpt, RelatedFile};+ use std::path::PathBuf;+ use std::sync::Arc;++ fn make_input(cursor_excerpt: &str, related: &[(&str, &[&str])]) -> ZetaPromptInput {+ ZetaPromptInput {+ cursor_path: PathBuf::from("src/main.rs").into(),+ cursor_excerpt: cursor_excerpt.into(),+ cursor_offset_in_excerpt: 0,+ excerpt_start_row: Some(0),+ events: Vec::new(),+ related_files: Some(+ related+ .iter()+ .map(|(path, excerpts)| {+ let mut row = 0;+ RelatedFile {+ path: Arc::from(PathBuf::from(path).as_path()),+ max_row: 1000,+ excerpts: excerpts+ .iter()+ .map(|text| {+ let row_count = text.matches('\n').count() as u32;+ let excerpt = RelatedExcerpt {+ row_range: row..row + row_count,+ text: Arc::from(*text),+ order: 0,+ context_source: ContextSource::CurrentFile,+ };+ row += row_count + 10;+ excerpt+ })+ .collect(),+ in_open_source_repo: false,+ }+ })+ .collect(),+ ),+ active_buffer_diagnostics: Vec::new(),+ excerpt_ranges: crate::ExcerptRanges::default(),+ syntax_ranges: None,+ in_open_source_repo: false,+ can_collect_data: false,+ repo_url: None,+ }+ }++ #[test]+ fn test_ensure_cursor_file_excerpt_synthesizes_when_uncovered() {+ // The cursor file's only related excerpt is a fragment elsewhere in the+ // file (rows 40..42), not covering the cursor at row 1.+ let mut input = make_input(+ "fn main() {\n let x = 1;\n}\n",+ &[("src/main.rs", &["// unrelated\n// fragment\n"])],+ );+ input.cursor_offset_in_excerpt = 16; // inside " let x = 1;"+ input.related_files.as_mut().unwrap()[0].excerpts[0].row_range = 40..42;++ assert!(locate_cursor_in_related_files(&input).is_none());+ assert!(ensure_cursor_file_excerpt(&mut input));++ let cursor =+ locate_cursor_in_related_files(&input).expect("cursor covered after synthesis");+ let file = &input.related_files.as_ref().unwrap()[cursor.file_ix];+ // The fragment was replaced by the synthesized full window, so the file+ // content isn't duplicated with overlapping markers.+ assert_eq!(file.excerpts.len(), 1);+ assert_eq!(file.excerpts[0].context_source, ContextSource::CurrentFile);+ assert_eq!(file.excerpts[0].row_range, 0..3);+ assert_eq!(+ file.excerpts[0].text.as_ref(),+ "fn main() {\n let x = 1;\n}\n"+ );+ }++ #[test]+ fn test_ensure_cursor_file_excerpt_noop_when_covered() {+ // make_input places the cursor file's excerpt at rows 0..3, covering the+ // cursor at row 1.+ let mut input = make_input(+ "fn main() {\n let x = 1;\n}\n",+ &[("src/main.rs", &["fn main() {\n let x = 1;\n}\n"])],+ );+ input.cursor_offset_in_excerpt = 16;+ let before = input.clone();+ assert!(ensure_cursor_file_excerpt(&mut input));+ assert_eq!(input, before);+ }++ #[test]+ fn test_tag_ids_are_unique_even_for_identical_blocks() {+ let mut used = HashSet::new();+ let id_a = unique_tag_id("same content", &mut used);+ let id_b = unique_tag_id("same content", &mut used);+ assert_ne!(id_a, id_b);+ assert_eq!(id_a.len(), TAG_ID_LEN);+ assert_eq!(id_b.len(), TAG_ID_LEN);+ }++ #[test]+ fn test_tag_ids_are_deterministic() {+ let mut used_a = HashSet::new();+ let mut used_b = HashSet::new();+ assert_eq!(+ unique_tag_id("hello\nworld\n", &mut used_a),+ unique_tag_id("hello\nworld\n", &mut used_b)+ );+ }++ #[test]+ fn test_build_marker_table_covers_all_context() {+ let input = make_input(+ "fn main() {\n println!();\n}\n",+ &[+ ("src/a.rs", &["struct A;\n", "impl A {}\n"]),+ ("src/b.rs", &["struct B;\n"]),+ ],+ );+ let table = build_marker_table(&input);+ assert_eq!(table.len(), 3);+ assert_eq!((table[0].file_ix, table[0].excerpt_ix), (0, 0));+ assert_eq!((table[1].file_ix, table[1].excerpt_ix), (0, 1));+ assert_eq!((table[2].file_ix, table[2].excerpt_ix), (1, 0));++ let mut all_ids = HashSet::new();+ for snippet in &table {+ assert!(snippet.markers.len() >= 2);+ assert_eq!(snippet.markers.first().map(|(_, offset)| *offset), Some(0));+ for (id, _) in &snippet.markers {+ assert!(all_ids.insert(id.clone()), "duplicate tag id {id}");+ }+ }+ }++ #[test]+ fn test_write_snippet_with_markers_and_cursor() {+ let text = "fn main() {\n let x = 1;\n}\n";+ let markers = vec![("aaaa".to_string(), 0), ("bbbb".to_string(), text.len())];+ let mut output = String::new();+ write_snippet_with_markers(&mut output, text, &markers, Some((16, "<|user_cursor|>")));+ assert_eq!(+ output,+ "<|marker_aaaa|>\nfn main() {\n <|user_cursor|>let x = 1;\n}\n<|marker_bbbb|>"+ );+ }++ #[test]+ fn test_extract_marker_span_round_trip() {+ let codeblock = "<|marker_aaaa|>\nnew content\n<|marker_bbbb|>";+ let (start, end, content) = extract_marker_span(codeblock).unwrap();+ assert_eq!(start, "aaaa");+ assert_eq!(end, "bbbb");+ assert_eq!(content, "new content\n");+ }++ #[test]+ fn test_extract_marker_span_strips_intermediate_tags() {+ let codeblock = "<|marker_aaaa|>\nline one\n<|marker_cccc|>\nline two\n<|marker_bbbb|>";+ let (start, end, content) = extract_marker_span(codeblock).unwrap();+ assert_eq!(start, "aaaa");+ assert_eq!(end, "bbbb");+ assert_eq!(content, "line one\nline two\n");+ }++ #[test]+ fn test_extract_marker_span_rejects_single_marker() {+ assert!(extract_marker_span("<|marker_aaaa|>\ncontent\n").is_err());+ }++ #[test]+ fn test_extract_marker_span_rejects_same_marker() {+ assert!(extract_marker_span("<|marker_aaaa|>\ncontent\n<|marker_aaaa|>").is_err());+ }++ const MULTI_FN_EXCERPT: &str = "fn alpha() {\n one();\n}\n\nfn beta() {\n two();\n}\n\nfn gamma() {\n three();\n}\n";++ const TWO_HUNK_PATCH: &str = concat!(+ "--- a/src/main.rs\n",+ "+++ b/src/main.rs\n",+ "@@ -1,3 +1,3 @@\n",+ " fn alpha() {\n",+ "- one();\n",+ "+ uno();\n",+ " }\n",+ "@@ -9,3 +9,3 @@\n",+ " fn gamma() {\n",+ "- three();\n",+ "+ tres();\n",+ " }\n",+ );++ #[test]+ fn test_encode_multi_hunk_emits_multiple_blocks() {+ let input = make_input(MULTI_FN_EXCERPT, &[("src/main.rs", &[MULTI_FN_EXCERPT])]);+ let output =+ encode_patch_as_output(&input, TWO_HUNK_PATCH, None, "<|user_cursor|>").unwrap();++ assert!(output.ends_with(V0615_END_MARKER), "output: {output}");+ // Two blocks => four marker tags, exactly one end marker.+ assert_eq!(+ output.matches(MARKER_TAG_PREFIX).count(),+ 4,+ "output: {output}"+ );+ assert_eq!(+ output.matches(V0615_END_MARKER).count(),+ 1,+ "output: {output}"+ );+ assert!(output.contains("uno();"), "output: {output}");+ assert!(output.contains("tres();"), "output: {output}");+ }++ #[test]+ fn test_round_trip_multi_hunk() {+ let input = make_input(MULTI_FN_EXCERPT, &[("src/main.rs", &[MULTI_FN_EXCERPT])]);+ let output =+ encode_patch_as_output(&input, TWO_HUNK_PATCH, None, "<|user_cursor|>").unwrap();+ let patch = parse_output_as_patch(&input, &output, "<|user_cursor|>").unwrap();++ assert!(patch.contains("- one();"), "patch: {patch}");+ assert!(patch.contains("+ uno();"), "patch: {patch}");+ assert!(patch.contains("- three();"), "patch: {patch}");+ assert!(patch.contains("+ tres();"), "patch: {patch}");+ }++ #[test]+ fn test_encode_partial_skips_unreachable_hunk() {+ // Second hunk targets a file that is not in the prompt context, so it+ // is unreachable. The first (reachable) hunk is still encoded.+ let patch = format!(+ "{TWO_HUNK_PATCH}--- a/other.rs\n+++ b/other.rs\n@@ -1,1 +1,1 @@\n-gone();\n+kept();\n"+ );+ let input = make_input(MULTI_FN_EXCERPT, &[("src/main.rs", &[MULTI_FN_EXCERPT])]);+ let output = encode_patch_as_output(&input, &patch, None, "<|user_cursor|>").unwrap();++ assert_ne!(output.trim_end_matches(V0615_END_MARKER), NO_EDITS);+ assert!(output.contains("uno();"), "output: {output}");+ assert!(output.contains("tres();"), "output: {output}");+ assert!(!output.contains("kept();"), "output: {output}");+ }++ #[test]+ fn test_encode_no_edits_when_all_hunks_unreachable() {+ let patch = "--- a/other.rs\n+++ b/other.rs\n@@ -1,3 +1,3 @@\n fn x() {\n- gone();\n+ kept();\n }\n";+ let input = make_input(MULTI_FN_EXCERPT, &[("src/main.rs", &[MULTI_FN_EXCERPT])]);+ let output = encode_patch_as_output(&input, patch, None, "<|user_cursor|>").unwrap();++ assert_eq!(output, format!("{NO_EDITS}{V0615_END_MARKER}"));+ }++ #[test]+ fn test_parse_multiple_direct_marker_blocks() {+ // The student emits raw marker spans with no code fences; blocks are+ // delimited by pairing tags two at a time.+ let input = make_input(MULTI_FN_EXCERPT, &[("src/main.rs", &[MULTI_FN_EXCERPT])]);+ let markers = build_marker_table(&input)[0].markers.clone();+ assert!(markers.len() >= 3, "expected internal markers: {markers:?}");++ let tag = |ix: usize| marker_tag(&markers[ix].0);+ let old_first = &MULTI_FN_EXCERPT[markers[0].1..markers[1].1];+ let old_second = &MULTI_FN_EXCERPT[markers[1].1..markers[markers.len() - 1].1];+ let new_first = old_first.replace("one()", "uno()");+ let new_second = old_second.replace("three()", "tres()");++ let output = format!(+ "{}\n{}{}\n{}\n{}{}{}",+ tag(0),+ new_first,+ tag(1),+ tag(1),+ new_second,+ tag(markers.len() - 1),+ V0615_END_MARKER,+ );++ let patch = parse_output_as_patch(&input, &output, "<|user_cursor|>").unwrap();+ assert!(patch.contains("+ uno();"), "patch: {patch}");+ assert!(patch.contains("+ tres();"), "patch: {patch}");+ assert_eq!(+ patch.matches("--- a/src/main.rs").count(),+ 1,+ "patch: {patch}"+ );+ }+}diff --git a/crates/zeta_prompt/src/multi_region.rs b/crates/zeta_prompt/src/multi_region.rsindex be416b513fa..540bc2db11a 100644--- a/crates/zeta_prompt/src/multi_region.rs+++ b/crates/zeta_prompt/src/multi_region.rs@@ -7,6 +7,8 @@ const V0316_MIN_BLOCK_LINES: usize = 3;const V0316_MAX_BLOCK_LINES: usize = 8;const V0318_MIN_BLOCK_LINES: usize = 6;const V0318_MAX_BLOCK_LINES: usize = 16;+const V0618_MIN_BLOCK_LINES: usize = 3;+const V0618_MAX_BLOCK_LINES: usize = 32;const MAX_NUDGE_LINES: usize = 5;pub const V0316_END_MARKER: &str = "<[end▁of▁sentence]>";pub const V0317_END_MARKER: &str = "<[end▁of▁sentence]>";@@ -55,6 +57,13 @@ fn collect_line_info(text: &str) -> Vec<LineInfo> {lines}+/// Whether a trimmed line is a model-friendly place to start a block: it has+/// content and isn't a structural tail. Exposed for reuse by context+/// retrieval when snapping excerpt boundaries to block boundaries.+pub fn is_good_block_start(trimmed_line: &str) -> bool {+ !trimmed_line.is_empty() && !is_structural_tail(trimmed_line)+}+fn is_structural_tail(trimmed_line: &str) -> bool {if trimmed_line.starts_with(&['}', ']', ')']) {return true;@@ -144,6 +153,10 @@ pub fn compute_marker_offsets_v0318(editable_text: &str) -> Vec<usize> {compute_marker_offsets_with_limits(editable_text, V0318_MIN_BLOCK_LINES, V0318_MAX_BLOCK_LINES)}+pub fn compute_marker_offsets_v0618(editable_text: &str) -> Vec<usize> {+ compute_marker_offsets_with_limits(editable_text, V0618_MIN_BLOCK_LINES, V0618_MAX_BLOCK_LINES)+}+fn line_start_at_or_before(text: &str, offset: usize) -> usize {let bounded_offset = text.floor_char_boundary(offset.min(text.len()));text[..bounded_offset]@@ -290,7 +303,7 @@ pub fn write_editable_with_markers(////// When a marker tag sits on its own line (followed by `\n`), the trailing/// newline is also removed so the surrounding lines stay joined naturally.-fn strip_marker_tags(text: &str) -> String {+pub(crate) fn strip_marker_tags(text: &str) -> String {let mut result = String::with_capacity(text.len());let mut pos = 0;let bytes = text.as_bytes();diff --git a/crates/zeta_prompt/src/zeta_prompt.rs b/crates/zeta_prompt/src/zeta_prompt.rsindex b610e193c72..5f106d01b5e 100644--- a/crates/zeta_prompt/src/zeta_prompt.rs+++ b/crates/zeta_prompt/src/zeta_prompt.rs@@ -1,4 +1,5 @@pub mod excerpt_ranges;+pub mod hashed_regions;pub mod multi_region;pub mod udiff;@@ -120,6 +121,11 @@ pub enum ZetaFormat {V0420Diagnostics,/// V0318-style multi-region format using Qwen FIM tokens and PSM ordering.V0608QwenMultiRegions,++ /// V0318-style marker-span output, but with content-hashed marker tags over rendered+ /// related-file context so the model can target jump edits. There is no cursor-centered+ /// editable region for this format.+ V0615HashRegions,}impl std::fmt::Display for ZetaFormat {@@ -266,6 +272,7 @@ pub enum ContextSource {GitLog,Bm25,OracleFile,+ OracleSnippet,}pub fn prompt_input_contains_special_tokens(input: &ZetaPromptInput, format: ZetaFormat) -> bool {@@ -296,6 +303,7 @@ pub fn format_zeta_prompt(input: &ZetaPromptInput, format: ZetaFormat) -> Option| ZetaFormat::V0331SeedCoderModelPy| ZetaFormat::V0318SeedMultiRegions| ZetaFormat::V0608QwenMultiRegions => 4096,+ ZetaFormat::V0615HashRegions => 8000,ZetaFormat::V0420Diagnostics => 8192,ZetaFormat::V0327SingleFile => 16384,};@@ -365,6 +373,18 @@ pub fn special_tokens_for_format(format: ZetaFormat) -> &'static [&'static str]];TOKENS}+ ZetaFormat::V0615HashRegions => {+ static TOKENS: &[&str] = &[+ seed_coder::FIM_SUFFIX,+ seed_coder::FIM_PREFIX,+ seed_coder::FIM_MIDDLE,+ seed_coder::FILE_MARKER,+ hashed_regions::V0615_END_MARKER,+ CURSOR_MARKER,+ hashed_regions::MARKER_TAG_PREFIX,+ ];+ TOKENS+ }ZetaFormat::V0327SingleFile => {static TOKENS: &[&str] = &[seed_coder::FIM_SUFFIX,@@ -413,6 +433,7 @@ pub fn token_limits_for_format(format: ZetaFormat) -> (usize, usize) {| ZetaFormat::V0317SeedMultiRegions| ZetaFormat::V0327SingleFile| ZetaFormat::V0304SeedNoEdits => (350, 150),+ ZetaFormat::V0615HashRegions => (8000, 0),ZetaFormat::V0304VariableEdit => (1024, 0),}@@ -439,6 +460,7 @@ pub fn stop_tokens_for_format(format: ZetaFormat) -> &'static [&'static str] {ZetaFormat::V0608QwenMultiRegions => &[qwen::END_MARKER],ZetaFormat::V0317SeedMultiRegions => &[multi_region::V0317_END_MARKER],ZetaFormat::V0327SingleFile => &[multi_region::V0327_END_MARKER],+ ZetaFormat::V0615HashRegions => &[hashed_regions::V0615_END_MARKER],}}@@ -468,7 +490,8 @@ pub fn training_delimiters_for_format(format: ZetaFormat) -> TrainingDelimiters| ZetaFormat::V0317SeedMultiRegions| ZetaFormat::V0318SeedMultiRegions| ZetaFormat::V0327SingleFile- | ZetaFormat::V0420Diagnostics => TrainingDelimiters {+ | ZetaFormat::V0420Diagnostics+ | ZetaFormat::V0615HashRegions => TrainingDelimiters {instruction_part: seed_coder::FIM_SUFFIX,response_part: seed_coder::FIM_MIDDLE,},@@ -538,6 +561,16 @@ pub fn excerpt_ranges_for_format(ranges.editable_350_context_150.clone(),),),+ ZetaFormat::V0615HashRegions => (+ ranges+ .context_8192+ .clone()+ .unwrap_or_else(|| ranges.editable_350_context_150.clone()),+ ranges+ .context_8192+ .clone()+ .unwrap_or_else(|| ranges.editable_350_context_150.clone()),+ ),ZetaFormat::V0304VariableEdit => {let context = ranges@@ -661,6 +694,14 @@ pub fn write_cursor_excerpt_section_for_format(seed_coder::FILE_MARKER,));}+ ZetaFormat::V0615HashRegions => {+ prompt.push_str(&build_v0615_cursor_prefix(+ path,+ context,+ editable_range,+ cursor_offset,+ ));+ }}}@@ -748,6 +789,34 @@ fn build_v0318_cursor_prefix(section}+fn build_v0615_cursor_prefix(+ path: &Path,+ context: &str,+ editable_range: &Range<usize>,+ cursor_offset: usize,+) -> String {+ let mut section = String::new();+ let path_str = path.to_string_lossy();+ write!(section, "{}{}\n", seed_coder::FILE_MARKER, path_str).ok();++ section.push_str(&context[..editable_range.start]);++ let editable_text = &context[editable_range.clone()];+ let cursor_in_editable = cursor_offset - editable_range.start;+ let markers = hashed_regions::markers_for_text(editable_text);+ hashed_regions::write_snippet_with_markers(+ &mut section,+ editable_text,+ &markers,+ Some((cursor_in_editable, CURSOR_MARKER)),+ );++ if !section.ends_with('\n') {+ section.push('\n');+ }+ section+}+fn build_v0317_cursor_prefix(path: &Path,context: &str,@@ -818,6 +887,172 @@ fn assemble_single_file_fim_prompt(prompt}+fn format_hash_region_related_files_within_budget(+ input: &ZetaPromptInput,+ marker_table: &[hashed_regions::SnippetMarkers],+ cursor: &hashed_regions::RelatedFileCursor,+ max_tokens: usize,+) -> Option<String> {+ let related_files = input.related_files.as_deref()?;++ struct RenderedExcerpt {+ file_ix: usize,+ excerpt_ix: usize,+ order: usize,+ rendered: String,+ }++ let mut candidates = Vec::new();+ let mut required_candidate_ix = None;+ for (file_ix, file) in related_files.iter().enumerate() {+ for (excerpt_ix, excerpt) in file.excerpts.iter().enumerate() {+ let markers =+ hashed_regions::marker_table_for_excerpt(marker_table, file_ix, excerpt_ix);+ let mut rendered = String::new();+ if let Some(markers) = markers {+ let cursor_in_excerpt = (file_ix == cursor.file_ix+ && excerpt_ix == cursor.excerpt_ix)+ .then_some((cursor.offset_in_excerpt, CURSOR_MARKER));+ hashed_regions::write_snippet_with_markers(+ &mut rendered,+ &excerpt.text,+ markers,+ cursor_in_excerpt,+ );+ } else {+ rendered.push_str(&excerpt.text);+ }+ if !rendered.ends_with('\n') {+ rendered.push('\n');+ }++ if file_ix == cursor.file_ix && excerpt_ix == cursor.excerpt_ix {+ required_candidate_ix = Some(candidates.len());+ }++ candidates.push(RenderedExcerpt {+ file_ix,+ excerpt_ix,+ order: excerpt.order,+ rendered,+ });+ }+ }++ let required_candidate_ix = required_candidate_ix?;+ let file_headers: Vec<String> = related_files+ .iter()+ .map(|file| {+ let path = hashed_regions::related_file_patch_path(&input.cursor_path, &file.path)+ .iter()+ .map(|component| component.to_string_lossy())+ .collect::<Vec<_>>()+ .join("/");+ format!("{}{path}\n", seed_coder::FILE_MARKER)+ })+ .collect();++ let mut total_tokens = 0;+ let mut included = vec![false; candidates.len()];+ let mut file_included = vec![false; related_files.len()];++ let required = &candidates[required_candidate_ix];+ let required_cost =+ estimate_tokens(file_headers[required.file_ix].len() + required.rendered.len());+ if required_cost > max_tokens {+ return None;+ }+ total_tokens += required_cost;+ included[required_candidate_ix] = true;+ file_included[required.file_ix] = true;++ let mut selection_order: Vec<usize> = (0..candidates.len()).collect();+ selection_order.sort_by_key(|&candidate_ix| {+ let candidate = &candidates[candidate_ix];+ (candidate.order, candidate.file_ix, candidate.excerpt_ix)+ });++ for candidate_ix in selection_order {+ if included[candidate_ix] {+ continue;+ }+ let candidate = &candidates[candidate_ix];+ let header_cost = if file_included[candidate.file_ix] {+ 0+ } else {+ estimate_tokens(file_headers[candidate.file_ix].len())+ };+ let excerpt_cost = estimate_tokens(candidate.rendered.len());+ if total_tokens + header_cost + excerpt_cost > max_tokens {+ continue;+ }+ total_tokens += header_cost + excerpt_cost;+ included[candidate_ix] = true;+ file_included[candidate.file_ix] = true;+ }++ let mut result = String::new();+ let mut last_file_ix = None;+ for (candidate_ix, candidate) in candidates.iter().enumerate() {+ if !included[candidate_ix] {+ continue;+ }+ if last_file_ix != Some(candidate.file_ix) {+ result.push_str(&file_headers[candidate.file_ix]);+ last_file_ix = Some(candidate.file_ix);+ }+ result.push_str(&candidate.rendered);++ let file = &related_files[candidate.file_ix];+ let excerpt = &file.excerpts[candidate.excerpt_ix];+ let next_excerpt_start = candidates+ .iter()+ .enumerate()+ .skip(candidate_ix + 1)+ .find(|(next_ix, next)| included[*next_ix] && next.file_ix == candidate.file_ix)+ .map(|(_, next)| file.excerpts[next.excerpt_ix].row_range.start);+ if rows_omitted_after_excerpt(excerpt, next_excerpt_start, file.max_row) {+ result.push_str("...\n");+ }+ }++ Some(result)+}++fn format_hash_regions_prompt_with_budget(+ input: &ZetaPromptInput,+ max_tokens: usize,+) -> Option<String> {+ let marker_table = hashed_regions::build_marker_table(input);+ let cursor = hashed_regions::locate_cursor_in_related_files(input)?;+ hashed_regions::marker_table_for_excerpt(&marker_table, cursor.file_ix, cursor.excerpt_ix)?;++ let fixed_tokens = estimate_tokens(+ seed_coder::FIM_SUFFIX.len()+ + "\n".len()+ + seed_coder::FIM_PREFIX.len()+ + seed_coder::FIM_MIDDLE.len(),+ );+ let related_files_budget = max_tokens.saturating_sub(fixed_tokens);+ let related_files_section = format_hash_region_related_files_within_budget(+ input,+ &marker_table,+ &cursor,+ related_files_budget,+ )?;++ let mut prompt = String::new();+ prompt.push_str(seed_coder::FIM_SUFFIX);+ prompt.push('\n');+ prompt.push_str(seed_coder::FIM_PREFIX);+ prompt.push_str(&related_files_section);+ if !prompt.ends_with('\n') {+ prompt.push('\n');+ }+ prompt.push_str(seed_coder::FIM_MIDDLE);+ Some(prompt)+}+pub fn format_prompt_with_budget_for_format(input: &ZetaPromptInput,format: ZetaFormat,@@ -829,7 +1064,9 @@ pub fn format_prompt_with_budget_for_format(let empty_files = Vec::new();let input_related_files = input.related_files.as_deref().unwrap_or(&empty_files);- let filtered_related_files = if let Some(cursor_excerpt_start_row) = input.excerpt_start_row {+ let filtered_related_files = if format == ZetaFormat::V0615HashRegions {+ input_related_files.to_vec()+ } else if let Some(cursor_excerpt_start_row) = input.excerpt_start_row {let relative_row_range =offset_range_to_row_range(&input.cursor_excerpt, context_range.clone());let row_range = relative_row_range.start + cursor_excerpt_start_row@@ -845,6 +1082,9 @@ pub fn format_prompt_with_budget_for_format(let related_files = filtered_related_files.as_slice();let prompt = match format {+ ZetaFormat::V0615HashRegions => {+ format_hash_regions_prompt_with_budget(input, apply_prompt_budget_margin(max_tokens))?+ }ZetaFormat::V0211SeedCoder| ZetaFormat::V0331SeedCoderModelPy| ZetaFormat::V0304SeedNoEdits@@ -1079,7 +1319,8 @@ pub fn max_edit_event_count_for_format(format: &ZetaFormat) -> usize {| ZetaFormat::V0317SeedMultiRegions| ZetaFormat::V0420Diagnostics| ZetaFormat::V0608QwenMultiRegions- | ZetaFormat::V0327SingleFile => 6,+ | ZetaFormat::V0327SingleFile+ | ZetaFormat::V0615HashRegions => 6,}}@@ -1106,7 +1347,8 @@ pub fn get_prefill_for_format(| ZetaFormat::V0317SeedMultiRegions| ZetaFormat::V0420Diagnostics| ZetaFormat::V0608QwenMultiRegions- | ZetaFormat::V0327SingleFile => String::new(),+ | ZetaFormat::V0327SingleFile+ | ZetaFormat::V0615HashRegions => String::new(),}}@@ -1125,6 +1367,7 @@ pub fn output_end_marker_for_format(format: ZetaFormat) -> Option<&'static str>ZetaFormat::V0608QwenMultiRegions => Some(qwen::END_MARKER),ZetaFormat::V0317SeedMultiRegions => Some(multi_region::V0317_END_MARKER),ZetaFormat::V0327SingleFile => Some(multi_region::V0327_END_MARKER),+ ZetaFormat::V0615HashRegions => Some(hashed_regions::V0615_END_MARKER),ZetaFormat::V0112MiddleAtEnd| ZetaFormat::V0113Ordered@@ -1225,6 +1468,7 @@ pub fn encode_patch_as_output_for_format(Ok(None)}}+ ZetaFormat::V0615HashRegions => Ok(None),_ => Ok(None),}}@@ -1237,6 +1481,10 @@ pub fn format_expected_output(patch: &str,cursor_offset: Option<usize>,) -> Result<String> {+ if format == ZetaFormat::V0615HashRegions {+ return hashed_regions::encode_patch_as_output(input, patch, cursor_offset, CURSOR_MARKER);+ }+let (context, editable_range, _, _) = resolve_cursor_region(input, format);let mut old_editable = context[editable_range].to_string();if !old_editable.is_empty() && !old_editable.ends_with('\n') {@@ -1329,7 +1577,8 @@ pub fn format_expected_output(| ZetaFormat::V0304VariableEdit| ZetaFormat::V0304SeedNoEdits| ZetaFormat::V0331SeedCoderModelPy- | ZetaFormat::V0306SeedMultiRegions => {+ | ZetaFormat::V0306SeedMultiRegions+ | ZetaFormat::V0615HashRegions => {let (mut result, first_hunk_offset) = if empty_patch {(old_editable.clone(), None)} else {@@ -1471,6 +1720,11 @@ pub fn parse_zeta2_model_output(editable_range_in_context,multi_region::apply_marker_span_v0318(old_editable_region, output)?,),+ ZetaFormat::V0615HashRegions => {+ anyhow::bail!(+ "V0615HashRegions output addresses related-file context; use parse_zeta2_model_output_as_patch"+ )+ }_ => (editable_range_in_context, output.to_string()),};@@ -1485,6 +1739,10 @@ pub fn parse_zeta2_model_output_as_patch(format: ZetaFormat,prompt_inputs: &ZetaPromptInput,) -> Result<String> {+ if format == ZetaFormat::V0615HashRegions {+ return hashed_regions::parse_output_as_patch(prompt_inputs, output, CURSOR_MARKER);+ }+let parsed = parse_zeta2_model_output(output, format, prompt_inputs)?;parsed_output_to_patch(prompt_inputs, parsed)}@@ -1620,7 +1878,7 @@ pub fn get_prefill(input: &ZetaPromptInput, format: ZetaFormat) -> String {get_prefill_for_format(format, context, &editable_range)}-fn format_edit_history_within_budget(+pub fn format_edit_history_within_budget(events: &[Arc<Event>],file_marker: &str,edit_history_name: &str,@@ -1735,7 +1993,7 @@ pub fn format_related_files_within_budget(// Render all of the files that fit within the token budget, in the original order.let mut result = String::new();let mut last_file_ix = None;- for candidate in &excerpt_candidates {+ for (candidate_ix, candidate) in excerpt_candidates.iter().enumerate() {if last_file_ix != Some(candidate.file_ix) {if last_file_ix.is_some() {result.push_str(file_suffix);@@ -1749,7 +2007,11 @@ pub fn format_related_files_within_budget(if !result.ends_with('\n') {result.push('\n');}- if excerpt.row_range.end < file.max_row {+ let next_excerpt_start = excerpt_candidates+ .get(candidate_ix + 1)+ .filter(|next| next.file_ix == candidate.file_ix)+ .map(|next| file.excerpts[next.excerpt_ix].row_range.start);+ if rows_omitted_after_excerpt(excerpt, next_excerpt_start, file.max_row) {result.push_str("...\n");}}@@ -1757,6 +2019,20 @@ pub fn format_related_files_within_budget(result}+/// Whether rows are omitted between this excerpt and the next rendered+/// excerpt of the same file (or the end of the file), in which case an+/// ellipsis line should be rendered.+pub fn rows_omitted_after_excerpt(+ excerpt: &RelatedExcerpt,+ next_excerpt_start: Option<u32>,+ file_max_row: u32,+) -> bool {+ match next_excerpt_start {+ Some(next_start) => excerpt.row_range.end < next_start,+ None => excerpt.row_range.end < file_max_row,+ }+}+pub fn write_related_files(prompt: &mut String,related_files: &[RelatedFile],@@ -1766,12 +2042,16 @@ pub fn write_related_files(let start = prompt.len();let path_str = file.path.to_string_lossy();write!(prompt, "<|file_sep|>{}\n", path_str).ok();- for excerpt in &file.excerpts {+ for (excerpt_ix, excerpt) in file.excerpts.iter().enumerate() {prompt.push_str(&excerpt.text);if !prompt.ends_with('\n') {prompt.push('\n');}- if excerpt.row_range.end < file.max_row {+ let next_excerpt_start = file+ .excerpts+ .get(excerpt_ix + 1)+ .map(|next| next.row_range.start);+ if rows_omitted_after_excerpt(excerpt, next_excerpt_start, file.max_row) {prompt.push_str("...\n");}}@@ -5217,13 +5497,13 @@ mod tests {context_source: ContextSource::Lsp,},RelatedExcerpt {- row_range: 10..20,+ row_range: 11..20,text: "second excerpt\n".into(),order: 0,context_source: ContextSource::Lsp,},RelatedExcerpt {- row_range: 20..30,+ row_range: 21..30,text: "third excerpt\n".into(),order: 0,context_source: ContextSource::Lsp,@@ -5268,6 +5548,55 @@ mod tests {);}+ #[test]+ fn test_contiguous_excerpts_render_without_ellipsis() {+ // Excerpts whose row ranges touch (end == next start) are contiguous+ // segments of the same region and must render seamlessly, without an+ // ellipsis line between them.+ let input = make_input(+ "x",+ 0..1,+ 0,+ vec![],+ vec![RelatedFile {+ path: Path::new("big.rs").into(),+ max_row: 30,+ in_open_source_repo: false,+ excerpts: vec![+ RelatedExcerpt {+ row_range: 0..10,+ text: "first segment\n".into(),+ order: 1,+ context_source: ContextSource::GitLog,+ },+ RelatedExcerpt {+ row_range: 10..20,+ text: "second segment\n".into(),+ order: 0,+ context_source: ContextSource::OracleSnippet,+ },+ ],+ }],+ );++ assert_eq!(+ format_with_budget(&input, 10000).unwrap(),+ indoc! {r#"+ <|file_sep|>big.rs+ first segment+ second segment+ ...+ <|file_sep|>test.rs+ <|fim_prefix|>+ <|fim_middle|>current+ <|user_cursor|>x+ <|fim_suffix|>+ <|fim_middle|>updated+ "#}+ .to_string()+ );+ }+#[test]fn test_truncation_prioritizes_lower_order_excerpts() {// Two files: file_a has a high-order excerpt, file_b has a low-order one.@@ -5365,13 +5694,13 @@ mod tests {context_source: ContextSource::Lsp,},RelatedExcerpt {- row_range: 5..15,+ row_range: 6..15,text: "important fn\n".into(),order: 1,context_source: ContextSource::Lsp,},RelatedExcerpt {- row_range: 15..30,+ row_range: 16..30,text: "less important fn\n".into(),order: 3,context_source: ContextSource::Lsp,@@ -5723,6 +6052,208 @@ mod tests {assert!(editable_range.end < excerpt.len());}+ #[test]+ fn test_v0615_formats_hashed_markers_for_rendered_related_context() {+ let current_text = "fn main() {\n let value = 1;\n}\n";+ let cursor_offset = current_text.find("let value").unwrap();+ let input = ZetaPromptInput {+ cursor_path: Path::new("test.rs").into(),+ cursor_excerpt: current_text.into(),+ cursor_offset_in_excerpt: cursor_offset,+ excerpt_start_row: Some(0),+ events: Vec::new(),+ related_files: Some(vec![+ RelatedFile {+ path: Path::new("test.rs").into(),+ max_row: 3,+ excerpts: vec![RelatedExcerpt {+ row_range: 0..3,+ text: current_text.into(),+ order: 0,+ context_source: ContextSource::CurrentFile,+ }],+ in_open_source_repo: false,+ },+ RelatedFile {+ path: Path::new("helper.rs").into(),+ max_row: 3,+ excerpts: vec![RelatedExcerpt {+ row_range: 10..13,+ text: "fn helper() {\n one();\n}\n".into(),+ order: 1,+ context_source: ContextSource::EditHistory,+ }],+ in_open_source_repo: false,+ },+ RelatedFile {+ path: Path::new("readonly.rs").into(),+ max_row: 1,+ excerpts: vec![RelatedExcerpt {+ row_range: 0..1,+ text: "pub fn readonly() {}\n".into(),+ order: 2,+ context_source: ContextSource::Lsp,+ }],+ in_open_source_repo: false,+ },+ ]),+ active_buffer_diagnostics: vec![],+ excerpt_ranges: ExcerptRanges::default(),+ syntax_ranges: None,+ in_open_source_repo: false,+ can_collect_data: false,+ repo_url: None,+ };++ let prompt = format_zeta_prompt(&input, ZetaFormat::V0615HashRegions).unwrap();+ let marker_table = hashed_regions::build_marker_table(&input);+ let marker_count: usize = marker_table+ .iter()+ .map(|snippet| snippet.markers.len())+ .sum();++ assert!(prompt.starts_with("<[fim-suffix]>\n<[fim-prefix]><filename>test.rs\n"));+ assert!(prompt.ends_with("<[fim-middle]>"));+ assert!(prompt.contains(CURSOR_MARKER));+ assert!(prompt.contains("<filename>helper.rs\n"));+ assert!(prompt.contains("<filename>readonly.rs\n"));+ assert!(prompt.contains("<filename>readonly.rs\n<|marker_"));+ assert!(prompt.contains("pub fn readonly() {}"));+ assert_eq!(+ prompt.matches(hashed_regions::MARKER_TAG_PREFIX).count(),+ marker_count+ );+ assert!(!prompt.contains(seed_coder::START_MARKER));+ }++ #[test]+ fn test_v0615_parse_related_file_jump_as_patch() {+ let current_text = "fn main() {\n helper();\n}\n";+ let helper_text = "fn helper() {\n one();\n}\n";+ let input = ZetaPromptInput {+ cursor_path: Path::new("test.rs").into(),+ cursor_excerpt: current_text.into(),+ cursor_offset_in_excerpt: current_text.find("helper").unwrap(),+ excerpt_start_row: Some(0),+ events: Vec::new(),+ related_files: Some(vec![+ RelatedFile {+ path: Path::new("test.rs").into(),+ max_row: 3,+ excerpts: vec![RelatedExcerpt {+ row_range: 0..3,+ text: current_text.into(),+ order: 0,+ context_source: ContextSource::CurrentFile,+ }],+ in_open_source_repo: false,+ },+ RelatedFile {+ path: Path::new("helper.rs").into(),+ max_row: 13,+ excerpts: vec![RelatedExcerpt {+ row_range: 10..13,+ text: helper_text.into(),+ order: 1,+ context_source: ContextSource::EditHistory,+ }],+ in_open_source_repo: false,+ },+ ]),+ active_buffer_diagnostics: vec![],+ excerpt_ranges: ExcerptRanges::default(),+ syntax_ranges: None,+ in_open_source_repo: false,+ can_collect_data: false,+ repo_url: None,+ };+ let marker_table = hashed_regions::build_marker_table(&input);+ let helper_markers = &marker_table[1].markers;+ let start_tag = hashed_regions::marker_tag(&helper_markers[0].0);+ let end_tag = hashed_regions::marker_tag(&helper_markers[helper_markers.len() - 1].0);+ let output = format!(+ "{start_tag}\nfn helper() {{\n two();\n}}\n{end_tag}{}",+ hashed_regions::V0615_END_MARKER+ );++ let patch =+ parse_zeta2_model_output_as_patch(&output, ZetaFormat::V0615HashRegions, &input)+ .unwrap();++ assert!(patch.contains("--- a/helper.rs"), "patch: {patch}");+ assert!(patch.contains("@@ -11,"), "patch: {patch}");+ assert!(patch.contains("- one();"), "patch: {patch}");+ assert!(patch.contains("+ two();"), "patch: {patch}");+ }++ #[test]+ fn test_v0615_expected_output_round_trips_to_patch() {+ let current_text = "fn main() {\n helper();\n}\n";+ let helper_text = "fn helper() {\n one();\n}\n";+ let input = ZetaPromptInput {+ cursor_path: Path::new("test.rs").into(),+ cursor_excerpt: current_text.into(),+ cursor_offset_in_excerpt: current_text.find("helper").unwrap(),+ excerpt_start_row: Some(0),+ events: Vec::new(),+ related_files: Some(vec![+ RelatedFile {+ path: Path::new("test.rs").into(),+ max_row: 3,+ excerpts: vec![RelatedExcerpt {+ row_range: 0..3,+ text: current_text.into(),+ order: 0,+ context_source: ContextSource::CurrentFile,+ }],+ in_open_source_repo: false,+ },+ RelatedFile {+ path: Path::new("helper.rs").into(),+ max_row: 13,+ excerpts: vec![RelatedExcerpt {+ row_range: 10..13,+ text: helper_text.into(),+ order: 1,+ context_source: ContextSource::EditHistory,+ }],+ in_open_source_repo: false,+ },+ ]),+ active_buffer_diagnostics: vec![],+ excerpt_ranges: ExcerptRanges::default(),+ syntax_ranges: None,+ in_open_source_repo: false,+ can_collect_data: false,+ repo_url: None,+ };+ let patch = indoc! {"+ --- a/helper.rs+ +++ b/helper.rs+ @@ -11,3 +11,3 @@+ fn helper() {+ - one();+ + two();+ }+ "};++ let output =+ format_expected_output(&input, ZetaFormat::V0615HashRegions, patch, None).unwrap();+ let parsed_patch =+ parse_zeta2_model_output_as_patch(&output, ZetaFormat::V0615HashRegions, &input)+ .unwrap();++ assert!(output.contains(hashed_regions::MARKER_TAG_PREFIX));+ assert!(+ parsed_patch.contains("- one();"),+ "patch: {parsed_patch}"+ );+ assert!(+ parsed_patch.contains("+ two();"),+ "patch: {parsed_patch}"+ );+ }+#[test]fn test_seed_coder_no_context() {let input = make_input("before\nmiddle\nafter", 7..13, 10, vec![], vec![]);