Skip to main content

llm_git/
map_reduce.rs

1//! Map-reduce pattern for large diff analysis
2//!
3//! When diffs exceed the token threshold, this module splits analysis across
4//! files, then synthesizes results for accurate classification.
5
6use std::{borrow::Cow, cmp::Reverse, fmt::Write as _, path::Path};
7
8use futures::stream::{self, StreamExt};
9use serde::{Deserialize, Serialize};
10
11use crate::{
12   api::{OneShotSpec, run_oneshot, strict_json_schema},
13   config::CommitConfig,
14   diff::{FileDiff, parse_diff, reconstruct_diff},
15   error::{CommitGenError, Result},
16   templates,
17   tokens::TokenCounter,
18   types::ConventionalAnalysis,
19};
20
21/// Observation from a single file during map phase
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct FileObservation {
24   pub file:         String,
25   pub observations: Vec<String>,
26   pub additions:    usize,
27   pub deletions:    usize,
28}
29
30/// Maximum tokens per file in map phase (leave headroom for prompt template +
31/// context)
32const MAX_FILE_TOKENS: usize = 50_000;
33const MAP_PHASE_CONCURRENCY: usize = 16;
34
35const fn map_phase_model(config: &CommitConfig) -> &str {
36   config.summary_model.as_str()
37}
38
39fn build_file_batches(
40   files: &[FileDiff],
41   counter: &TokenCounter,
42   budget: usize,
43) -> Vec<Vec<usize>> {
44   build_file_batches_for_indices(files, 0..files.len(), counter, budget)
45}
46
47fn build_llm_file_batches(
48   files: &[FileDiff],
49   counter: &TokenCounter,
50   budget: usize,
51) -> Vec<Vec<usize>> {
52   if files.iter().all(|file| !file.is_binary) {
53      return build_file_batches(files, counter, budget);
54   }
55
56   build_file_batches_for_indices(
57      files,
58      files
59         .iter()
60         .enumerate()
61         .filter_map(|(idx, file)| (!file.is_binary).then_some(idx)),
62      counter,
63      budget,
64   )
65}
66
67fn build_file_batches_for_indices<I>(
68   files: &[FileDiff],
69   indices: I,
70   counter: &TokenCounter,
71   budget: usize,
72) -> Vec<Vec<usize>>
73where
74   I: IntoIterator<Item = usize>,
75{
76   let budget = budget.max(1);
77   let mut batches = Vec::new();
78   let mut current_batch = Vec::new();
79   let mut current_tokens = 0usize;
80
81   for idx in indices {
82      let file_tokens = files[idx].token_estimate(counter);
83      if file_tokens > budget {
84         if !current_batch.is_empty() {
85            batches.push(std::mem::take(&mut current_batch));
86            current_tokens = 0;
87         }
88         batches.push(vec![idx]);
89         continue;
90      }
91
92      if !current_batch.is_empty() && current_tokens.saturating_add(file_tokens) > budget {
93         batches.push(std::mem::take(&mut current_batch));
94         current_tokens = 0;
95      }
96
97      current_batch.push(idx);
98      current_tokens = current_tokens.saturating_add(file_tokens);
99   }
100
101   if !current_batch.is_empty() {
102      batches.push(current_batch);
103   }
104
105   batches
106}
107
108/// Check if map-reduce should be used.
109///
110/// Route only when the included diff is large enough to benefit from
111/// per-file analysis, or when a single included file would be truncated in the
112/// map phase.
113#[tracing::instrument(target = "lgit", name = "map_reduce.should_use", skip_all, fields(diff_bytes = diff.len(), threshold = config.map_reduce_threshold))]
114pub fn should_use_map_reduce(diff: &str, config: &CommitConfig, counter: &TokenCounter) -> bool {
115   if !config.map_reduce_enabled {
116      return false;
117   }
118
119   let files = parse_diff(diff);
120   let mut has_included_file = false;
121   let mut total_tokens = 0usize;
122
123   for file in files.iter().filter(|file| {
124      !config
125         .excluded_files
126         .iter()
127         .any(|excluded| file.filename.ends_with(excluded))
128   }) {
129      has_included_file = true;
130
131      let file_tokens = file.token_estimate(counter);
132      if file_tokens > MAX_FILE_TOKENS {
133         return true;
134      }
135
136      total_tokens = total_tokens.saturating_add(file_tokens);
137      if total_tokens >= config.map_reduce_threshold {
138         return true;
139      }
140   }
141
142   has_included_file && total_tokens >= config.map_reduce_threshold
143}
144
145/// Maximum files to include in context header (prevent token explosion)
146const MAX_CONTEXT_FILES: usize = 20;
147
148/// Precomputed context header metadata for cross-file awareness.
149struct ContextFile<'a> {
150   filename:     &'a str,
151   summary_line: String,
152   change_size:  usize,
153}
154
155struct ContextHeaders<'a> {
156   files:               Vec<ContextFile<'a>>,
157   ranked_indices:      Vec<usize>,
158   large_commit_header: Option<String>,
159}
160
161impl<'a> ContextHeaders<'a> {
162   fn new(files: &'a [FileDiff]) -> Self {
163      // Skip detailed context for very large commits (diminishing returns).
164      if files.len() > 100 {
165         return Self {
166            files:               Vec::new(),
167            ranked_indices:      Vec::new(),
168            large_commit_header: Some(format!("(Large commit with {} total files)", files.len())),
169         };
170      }
171
172      let files: Vec<_> = files
173         .iter()
174         .map(|file| {
175            let change_size = file.additions + file.deletions;
176            let description = infer_file_description(&file.filename, &file.content);
177            ContextFile {
178               filename: &file.filename,
179               summary_line: format!(
180                  "- {} ({} lines): {}",
181                  file.filename, change_size, description
182               ),
183               change_size,
184            }
185         })
186         .collect();
187
188      let mut ranked_indices = Vec::new();
189      if files.len() > MAX_CONTEXT_FILES {
190         ranked_indices = (0..files.len()).collect();
191         ranked_indices.sort_by_key(|&idx| Reverse(files[idx].change_size));
192      }
193
194      Self { files, ranked_indices, large_commit_header: None }
195   }
196
197   fn header_for_files(&self, current_files: &[&str]) -> Cow<'_, str> {
198      if let Some(header) = &self.large_commit_header {
199         return Cow::Borrowed(header.as_str());
200      }
201
202      let current_count = self
203         .files
204         .iter()
205         .filter(|file| is_current_context_file(file.filename, current_files))
206         .count();
207      let total_other = self.files.len().saturating_sub(current_count);
208
209      if total_other == 0 {
210         return Cow::Borrowed("");
211      }
212
213      let mut header = String::with_capacity(32 + total_other.min(MAX_CONTEXT_FILES) * 80);
214      header.push_str("OTHER FILES IN THIS CHANGE:");
215
216      let mut shown = 0usize;
217      if total_other > MAX_CONTEXT_FILES {
218         for &idx in &self.ranked_indices {
219            let file = &self.files[idx];
220            if is_current_context_file(file.filename, current_files) {
221               continue;
222            }
223
224            header.push('\n');
225            header.push_str(&file.summary_line);
226            shown += 1;
227
228            if shown == MAX_CONTEXT_FILES {
229               break;
230            }
231         }
232      } else {
233         for file in &self.files {
234            if is_current_context_file(file.filename, current_files) {
235               continue;
236            }
237
238            header.push('\n');
239            header.push_str(&file.summary_line);
240            shown += 1;
241         }
242      }
243
244      if shown < total_other {
245         write!(&mut header, "\n... and {} more files", total_other - shown)
246            .expect("writing to a string cannot fail");
247      }
248
249      Cow::Owned(header)
250   }
251}
252
253fn is_current_context_file(filename: &str, current_files: &[&str]) -> bool {
254   current_files.contains(&filename)
255}
256
257/// Infer a brief description of what a file likely contains based on
258/// name/content
259fn infer_file_description(filename: &str, content: &str) -> &'static str {
260   let filename_lower = filename.to_lowercase();
261
262   // Check filename patterns
263   if filename_lower.contains("test") {
264      return "test file";
265   }
266   if filename_lower.contains("prompt") || filename_lower.contains("system") {
267      return "prompt template";
268   }
269   if Path::new(filename)
270      .extension()
271      .is_some_and(|e| e.eq_ignore_ascii_case("md"))
272   {
273      return "documentation";
274   }
275   let ext = Path::new(filename).extension();
276   if filename_lower.contains("config")
277      || ext.is_some_and(|e| e.eq_ignore_ascii_case("toml"))
278      || ext.is_some_and(|e| e.eq_ignore_ascii_case("yaml"))
279      || ext.is_some_and(|e| e.eq_ignore_ascii_case("yml"))
280   {
281      return "configuration";
282   }
283   if filename_lower.contains("error") {
284      return "error definitions";
285   }
286   if filename_lower.contains("type") {
287      return "type definitions";
288   }
289   if filename_lower.ends_with("mod.rs") || filename_lower.ends_with("lib.rs") {
290      return "module exports";
291   }
292   if filename_lower.ends_with("main.rs")
293      || filename_lower.ends_with("main.go")
294      || filename_lower.ends_with("main.py")
295   {
296      return "entry point";
297   }
298
299   // Check content patterns
300   if content.contains("impl ") || content.contains("fn ") {
301      return "implementation";
302   }
303   if content.contains("struct ") || content.contains("enum ") {
304      return "type definitions";
305   }
306   if content.contains("async ") || content.contains("await") {
307      return "async code";
308   }
309
310   "source code"
311}
312
313/// Map phase: analyze token-budgeted file batches and extract observations
314#[tracing::instrument(target = "lgit", name = "map_reduce.map_phase", skip_all, fields(file_count = files.len(), model = map_model_name))]
315async fn map_phase(
316   files: &[FileDiff],
317   map_model_name: &str,
318   config: &CommitConfig,
319   counter: &TokenCounter,
320) -> Result<Vec<FileObservation>> {
321   let context_headers = ContextHeaders::new(files);
322   let llm_batches = build_llm_file_batches(files, counter, config.map_batch_token_budget);
323   let total_batches = llm_batches.len();
324
325   let mut observations_by_index = vec![None; files.len()];
326   for (idx, file) in files.iter().enumerate().filter(|(_, file)| file.is_binary) {
327      observations_by_index[idx] = Some(FileObservation {
328         file:         file.filename.clone(),
329         observations: vec!["Binary file changed.".to_string()],
330         additions:    0,
331         deletions:    0,
332      });
333   }
334
335   let batch_results: Vec<Result<Vec<(usize, FileObservation)>>> =
336      stream::iter(llm_batches.into_iter().enumerate())
337         .map(|(batch_idx, batch_indices)| {
338            let context_headers = &context_headers;
339            async move {
340               let batch_files: Vec<&FileDiff> =
341                  batch_indices.iter().map(|&idx| &files[idx]).collect();
342               let current_paths: Vec<&str> = batch_files
343                  .iter()
344                  .map(|file| file.filename.as_str())
345                  .collect();
346               let context_header = context_headers.header_for_files(&current_paths);
347               let progress_label = format!(
348                  "map batch {}/{} ({} files)",
349                  batch_idx + 1,
350                  total_batches,
351                  batch_files.len()
352               );
353               let observations = map_file_batch(
354                  &batch_files,
355                  &context_header,
356                  map_model_name,
357                  config,
358                  counter,
359                  &progress_label,
360               )
361               .await?;
362
363               Ok(batch_indices.into_iter().zip(observations).collect())
364            }
365         })
366         .buffer_unordered(MAP_PHASE_CONCURRENCY)
367         .collect()
368         .await;
369
370   for result in batch_results {
371      for (idx, observation) in result? {
372         observations_by_index[idx] = Some(observation);
373      }
374   }
375
376   let mut observations = Vec::with_capacity(files.len());
377   for (idx, observation) in observations_by_index.into_iter().enumerate() {
378      let observation = observation.ok_or_else(|| {
379         CommitGenError::Other(format!("Missing map observation for {}", files[idx].filename))
380      })?;
381      observations.push(observation);
382   }
383
384   Ok(observations)
385}
386
387#[tracing::instrument(target = "lgit", name = "map_reduce.observe_diff_files", skip_all, fields(diff_bytes = diff.len(), model = map_model_name))]
388pub async fn observe_diff_files(
389   diff: &str,
390   map_model_name: &str,
391   config: &CommitConfig,
392   counter: &TokenCounter,
393) -> Result<Vec<FileObservation>> {
394   let mut files = parse_diff(diff);
395
396   files.retain(|file| {
397      !config
398         .excluded_files
399         .iter()
400         .any(|excluded| file.filename.ends_with(excluded))
401   });
402
403   if files.is_empty() {
404      return Err(CommitGenError::Other(
405         "No relevant files to summarize after filtering".to_string(),
406      ));
407   }
408
409   let llm_file_count = files.iter().filter(|file| !file.is_binary).count();
410   let batch_count = build_llm_file_batches(&files, counter, config.map_batch_token_budget).len();
411   crate::api::print_llm_progress(|| {
412      format!(
413         "Map-reduce map phase: {batch_count} batch LLM queries for {llm_file_count} files queued \
414          on {map_model_name} (max {MAP_PHASE_CONCURRENCY} parallel)"
415      )
416   });
417
418   map_phase(&files, map_model_name, config, counter).await
419}
420
421/// Analyze a token-budgeted file batch and extract per-file observations
422#[tracing::instrument(target = "lgit", name = "map_reduce.map_file_batch", skip_all, fields(file_count = files.len(), model = model_name))]
423async fn map_file_batch(
424   files: &[&FileDiff],
425   context_header: &str,
426   model_name: &str,
427   config: &CommitConfig,
428   counter: &TokenCounter,
429   progress_label: &str,
430) -> Result<Vec<FileObservation>> {
431   let rendered_diffs: Vec<String> = files
432      .iter()
433      .map(|file| render_file_diff_for_batch(file, counter))
434      .collect();
435   let prompt_files: Vec<templates::MapFile<'_>> = files
436      .iter()
437      .zip(&rendered_diffs)
438      .map(|(file, diff)| templates::MapFile { path: file.filename.as_str(), diff })
439      .collect();
440   let variant = if config.markdown_output {
441      "markdown"
442   } else {
443      "default"
444   };
445   let parts = templates::render_map_prompt(variant, &prompt_files, context_header)?;
446   let observation_schema = build_batch_observation_schema();
447   let response = run_oneshot::<BatchObservationResponse>(config, &OneShotSpec {
448      operation:        "map-reduce/map",
449      model:            model_name,
450      prompt_family:    "map",
451      prompt_variant:   variant,
452      system_prompt:    &parts.system,
453      user_prompt:      &parts.user,
454      tool_name:        "create_file_observations",
455      tool_description: "Extract observations from a batch of file changes",
456      schema:           &observation_schema,
457      progress_label:   Some(progress_label),
458      debug:            None,
459      cacheable:        true,
460   })
461   .await?;
462
463   Ok(map_batch_response_to_observations(
464      files,
465      &response.output,
466      response.text_content.as_deref(),
467      response.stop_reason.as_deref(),
468   ))
469}
470
471fn render_file_diff_for_batch(file: &FileDiff, counter: &TokenCounter) -> String {
472   let file_tokens = file.token_estimate(counter);
473   if file_tokens > MAX_FILE_TOKENS {
474      let mut file_clone = file.clone();
475      let target_size = MAX_FILE_TOKENS * 4; // Convert tokens to chars
476      file_clone.truncate(target_size);
477      eprintln!(
478         "  {} truncated {} ({} \u{2192} {} tokens)",
479         crate::style::icons::WARNING,
480         file.filename,
481         file_tokens,
482         file_clone.token_estimate(counter)
483      );
484      return reconstruct_diff(&[file_clone]);
485   }
486
487   reconstruct_single_file_diff(file)
488}
489
490fn reconstruct_single_file_diff(file: &FileDiff) -> String {
491   let mut diff = String::with_capacity(file.size());
492   diff.push_str(&file.header);
493   if !file.content.is_empty() {
494      diff.push('\n');
495      diff.push_str(&file.content);
496   }
497   diff
498}
499
500fn map_batch_response_to_observations(
501   files: &[&FileDiff],
502   response: &BatchObservationResponse,
503   text_content: Option<&str>,
504   stop_reason: Option<&str>,
505) -> Vec<FileObservation> {
506   if response.files.is_empty() && text_content.is_some_and(|text| !text.trim().is_empty()) {
507      crate::style::warn(
508         "Model returned batch observations as text; using fallback observations for every file.",
509      );
510      return files
511         .iter()
512         .map(|file| fallback_file_observation(file))
513         .collect();
514   }
515
516   let stopped_at_max_tokens = stop_reason == Some("max_tokens");
517   let mut used_entries = vec![false; response.files.len()];
518   files
519      .iter()
520      .map(|file| {
521         let Some(entry_idx) =
522            find_observation_entry(file.filename.as_str(), &response.files, &used_entries, files)
523         else {
524            return fallback_file_observation(file);
525         };
526
527         used_entries[entry_idx] = true;
528         let entry = &response.files[entry_idx];
529         let observations = if entry.observations.is_empty() && stopped_at_max_tokens {
530            vec![fallback_observation_text(&file.filename)]
531         } else {
532            entry.observations.clone()
533         };
534
535         FileObservation { file: file.filename.clone(), observations, additions: 0, deletions: 0 }
536      })
537      .collect()
538}
539
540fn find_observation_entry(
541   filename: &str,
542   entries: &[FileObservationEntry],
543   used_entries: &[bool],
544   batch_files: &[&FileDiff],
545) -> Option<usize> {
546   find_entry_by(entries, used_entries, |entry| entry.path == filename)
547      .or_else(|| {
548         let filename_basename = path_basename(filename);
549         let basename_is_unique = batch_files
550            .iter()
551            .filter(|file| path_basename(file.filename.as_str()) == filename_basename)
552            .count()
553            == 1;
554         basename_is_unique
555            .then(|| {
556               find_entry_by(entries, used_entries, |entry| {
557                  path_basename(&entry.path) == filename_basename
558               })
559            })
560            .flatten()
561      })
562      .or_else(|| {
563         find_entry_by(entries, used_entries, |entry| path_suffix_matches(&entry.path, filename))
564      })
565}
566
567fn find_entry_by<F>(
568   entries: &[FileObservationEntry],
569   used_entries: &[bool],
570   mut matches: F,
571) -> Option<usize>
572where
573   F: FnMut(&FileObservationEntry) -> bool,
574{
575   entries
576      .iter()
577      .enumerate()
578      .find_map(|(idx, entry)| (!used_entries[idx] && matches(entry)).then_some(idx))
579}
580
581fn path_basename(path: &str) -> &str {
582   Path::new(path)
583      .file_name()
584      .and_then(|name| name.to_str())
585      .unwrap_or(path)
586}
587
588fn path_suffix_matches(left: &str, right: &str) -> bool {
589   path_has_suffix(left, right) || path_has_suffix(right, left)
590}
591
592fn path_has_suffix(path: &str, suffix: &str) -> bool {
593   if path == suffix {
594      return true;
595   }
596
597   path
598      .strip_suffix(suffix)
599      .is_some_and(|prefix| prefix.ends_with('/') || prefix.ends_with('\\'))
600}
601
602fn fallback_file_observation(file: &FileDiff) -> FileObservation {
603   FileObservation {
604      file:         file.filename.clone(),
605      observations: vec![fallback_observation_text(&file.filename)],
606      additions:    0,
607      deletions:    0,
608   }
609}
610
611fn fallback_observation_text(filename: &str) -> String {
612   let fallback_target = path_basename(filename);
613   format!("Updated {fallback_target}.")
614}
615
616/// Reduce phase: synthesize all observations into final analysis
617#[tracing::instrument(target = "lgit", name = "map_reduce.reduce_phase", skip_all, fields(observation_count = observations.len(), model = model_name))]
618pub async fn reduce_phase(
619   observations: &[FileObservation],
620   stat: &str,
621   scope_candidates: &str,
622   model_name: &str,
623   config: &CommitConfig,
624) -> Result<ConventionalAnalysis> {
625   let type_enum: Vec<&str> = config.types.keys().map(|s| s.as_str()).collect();
626   let observations_json =
627      serde_json::to_string_pretty(observations).unwrap_or_else(|_| "[]".to_string());
628
629   let types_description = crate::api::format_types_description(config);
630   let variant = if config.markdown_output {
631      "markdown"
632   } else {
633      "default"
634   };
635   let parts = templates::render_reduce_prompt(
636      variant,
637      &observations_json,
638      stat,
639      scope_candidates,
640      Some(&types_description),
641   )?;
642
643   let analysis_schema = build_analysis_schema(&type_enum, config);
644   let response = run_oneshot::<ConventionalAnalysis>(config, &OneShotSpec {
645      operation:        "map-reduce/reduce",
646      model:            model_name,
647      prompt_family:    "reduce",
648      prompt_variant:   variant,
649      system_prompt:    &parts.system,
650      user_prompt:      &parts.user,
651      tool_name:        "create_conventional_analysis",
652      tool_description: "Analyze changes and classify as conventional commit with type, scope, \
653                         summary, details, and metadata",
654      schema:           &analysis_schema,
655      progress_label:   Some("reduce file observations"),
656      debug:            None,
657      cacheable:        true,
658   })
659   .await?;
660
661   Ok(response.output)
662}
663
664/// Run full map-reduce pipeline for large diffs
665#[tracing::instrument(target = "lgit", name = "map_reduce.run", skip_all, fields(diff_bytes = diff.len(), model = model_name))]
666pub async fn run_map_reduce(
667   diff: &str,
668   stat: &str,
669   scope_candidates: &str,
670   model_name: &str,
671   config: &CommitConfig,
672   counter: &TokenCounter,
673) -> Result<ConventionalAnalysis> {
674   let map_model_name = map_phase_model(config);
675   let observations = observe_diff_files(diff, map_model_name, config, counter).await?;
676   let file_count = observations.len();
677   crate::api::print_llm_progress(|| {
678      format!("Map-reduce reduce phase: synthesizing {file_count} file observations")
679   });
680
681   reduce_phase(&observations, stat, scope_candidates, model_name, config).await
682}
683
684#[derive(Debug, Deserialize, Serialize)]
685struct BatchObservationResponse {
686   #[serde(default)]
687   files: Vec<FileObservationEntry>,
688}
689
690#[derive(Debug, Deserialize, Serialize)]
691struct FileObservationEntry {
692   path:         String,
693   #[serde(default, deserialize_with = "deserialize_observations")]
694   observations: Vec<String>,
695}
696
697/// Deserialize observations flexibly: handles array, stringified array, or
698/// bullet string
699fn deserialize_observations<'de, D>(deserializer: D) -> std::result::Result<Vec<String>, D::Error>
700where
701   D: serde::Deserializer<'de>,
702{
703   use std::fmt;
704
705   use serde::de::{self, Visitor};
706
707   struct ObservationsVisitor;
708
709   impl<'de> Visitor<'de> for ObservationsVisitor {
710      type Value = Vec<String>;
711
712      fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
713         formatter.write_str("an array of strings, a JSON array string, or a bullet-point string")
714      }
715
716      fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
717      where
718         A: de::SeqAccess<'de>,
719      {
720         let mut vec = Vec::new();
721         while let Some(item) = seq.next_element::<String>()? {
722            vec.push(item);
723         }
724         Ok(vec)
725      }
726
727      fn visit_str<E>(self, s: &str) -> std::result::Result<Self::Value, E>
728      where
729         E: de::Error,
730      {
731         Ok(parse_string_to_observations(s))
732      }
733   }
734
735   deserializer.deserialize_any(ObservationsVisitor)
736}
737
738/// Parse a string into observations: handles JSON array string or bullet-point
739/// string
740fn parse_string_to_observations(s: &str) -> Vec<String> {
741   let trimmed = s.trim();
742   if trimmed.is_empty() {
743      return Vec::new();
744   }
745
746   // Try parsing as JSON array first
747   if trimmed.starts_with('[')
748      && let Ok(arr) = serde_json::from_str::<Vec<String>>(trimmed)
749   {
750      return arr;
751   }
752
753   // Fall back to bullet-point parsing
754   trimmed
755      .lines()
756      .map(str::trim)
757      .filter(|line| !line.is_empty())
758      .map(|line| {
759         line
760            .strip_prefix("- ")
761            .or_else(|| line.strip_prefix("* "))
762            .or_else(|| line.strip_prefix("• "))
763            .unwrap_or(line)
764            .trim()
765            .to_string()
766      })
767      .filter(|line| !line.is_empty())
768      .collect()
769}
770
771fn build_batch_observation_schema() -> serde_json::Value {
772   strict_json_schema(
773      serde_json::json!({
774         "files": {
775            "type": "array",
776            "description": "Per-file observations for every file in the map batch.",
777            "items": {
778               "type": "object",
779               "properties": {
780                  "path": {
781                     "type": "string",
782                     "description": "The exact input file path this observation set describes."
783                  },
784                  "observations": {
785                     "type": "array",
786                     "description": "Factual observations about what changed in this file.",
787                     "items": {
788                        "type": "string"
789                     }
790                  }
791               },
792               "required": ["path", "observations"],
793               "additionalProperties": false
794            }
795         }
796      }),
797      &["files"],
798   )
799}
800
801fn build_analysis_schema(type_enum: &[&str], config: &CommitConfig) -> serde_json::Value {
802   strict_json_schema(
803      serde_json::json!({
804         "type": {
805            "type": "string",
806            "enum": type_enum,
807            "description": "Commit type based on combined changes"
808         },
809         "scope": {
810            "type": "string",
811            "description": "Optional scope (module/component). Omit if unclear or multi-component."
812         },
813         "summary": {
814            "type": "string",
815            "description": format!(
816               "Concise past-tense commit summary without type/scope prefix or trailing period; target {} chars, hard limit {}.",
817               config.summary_guideline,
818               config.summary_hard_limit
819            ),
820            "maxLength": config.summary_hard_limit
821         },
822         "details": {
823            "type": "array",
824            "description": "Array of 0-6 detail items with changelog metadata.",
825            "items": {
826               "type": "object",
827               "properties": {
828                  "text": {
829                     "type": "string",
830                     "description": "Detail about change, starting with past-tense verb, ending with period"
831                  },
832                  "changelog_category": {
833                     "type": "string",
834                     "enum": ["Added", "Changed", "Fixed", "Deprecated", "Removed", "Security"],
835                     "description": "Changelog category if user-visible. Omit for internal changes."
836                  },
837                  "user_visible": {
838                     "type": "boolean",
839                     "description": "True if this change affects users/API and should appear in changelog"
840                  }
841               },
842               "required": ["text", "user_visible"]
843            }
844         },
845         "issue_refs": {
846            "type": "array",
847            "description": "Issue numbers from context (e.g., ['#123', '#456']). Empty if none.",
848            "items": {
849               "type": "string"
850            }
851         }
852      }),
853      &["type", "details", "issue_refs"],
854   )
855}
856
857#[cfg(test)]
858mod tests {
859   use super::*;
860   use crate::tokens::TokenCounter;
861
862   fn test_counter() -> TokenCounter {
863      TokenCounter::new("http://localhost:4000", None, "claude-sonnet-4.5")
864   }
865
866   fn file_with_tokens(filename: &str, token_estimate: usize) -> FileDiff {
867      FileDiff {
868         filename:  filename.to_string(),
869         header:    String::new(),
870         content:   "x".repeat(token_estimate * 4),
871         additions: 0,
872         deletions: 0,
873         is_binary: false,
874      }
875   }
876
877   #[test]
878   fn test_map_phase_model_uses_summary_model() {
879      let config = CommitConfig {
880         summary_model: "claude-haiku-4-5".to_string(),
881         analysis_model: "claude-opus-4.1".to_string(),
882         ..Default::default()
883      };
884
885      assert_eq!(map_phase_model(&config), "claude-haiku-4-5");
886      assert_eq!(MAP_PHASE_CONCURRENCY, 16);
887   }
888
889   #[test]
890   fn test_build_file_batches_single_batch_when_under_budget() {
891      let counter = test_counter();
892      let files = vec![
893         file_with_tokens("a.rs", 4),
894         file_with_tokens("b.rs", 4),
895         file_with_tokens("c.rs", 1),
896      ];
897
898      assert_eq!(build_file_batches(&files, &counter, 10), vec![vec![0, 1, 2]]);
899   }
900
901   #[test]
902   fn test_build_file_batches_splits_when_budget_exceeded() {
903      let counter = test_counter();
904      let files = vec![
905         file_with_tokens("a.rs", 4),
906         file_with_tokens("b.rs", 4),
907         file_with_tokens("c.rs", 4),
908      ];
909
910      assert_eq!(build_file_batches(&files, &counter, 10), vec![vec![0, 1], vec![2]]);
911   }
912
913   #[test]
914   fn test_build_file_batches_preserves_order_and_every_file_once() {
915      let counter = test_counter();
916      let files = vec![
917         file_with_tokens("a.rs", 3),
918         file_with_tokens("b.rs", 8),
919         file_with_tokens("c.rs", 2),
920         file_with_tokens("d.rs", 9),
921         file_with_tokens("e.rs", 1),
922      ];
923
924      let batches = build_file_batches(&files, &counter, 10);
925      let flattened: Vec<usize> = batches.into_iter().flatten().collect();
926      assert_eq!(flattened, vec![0, 1, 2, 3, 4]);
927   }
928
929   #[test]
930   fn test_build_file_batches_isolates_oversized_files() {
931      let counter = test_counter();
932      let files = vec![
933         file_with_tokens("a.rs", 2),
934         file_with_tokens("b.rs", 2),
935         file_with_tokens("huge.rs", 12),
936         file_with_tokens("c.rs", 2),
937      ];
938
939      assert_eq!(build_file_batches(&files, &counter, 10), vec![vec![0, 1], vec![2], vec![3]]);
940   }
941
942   #[test]
943   fn test_batch_response_mapping_matches_paths_and_falls_back_for_omissions() {
944      let exact = file_with_tokens("src/lib.rs", 1);
945      let basename = file_with_tokens("src/main.rs", 1);
946      let omitted = file_with_tokens("crates/core/mod.rs", 1);
947      let files = [&exact, &basename, &omitted];
948      let response = BatchObservationResponse {
949         files: vec![
950            FileObservationEntry {
951               path:         "src/lib.rs".to_string(),
952               observations: vec!["updated library entrypoint".to_string()],
953            },
954            FileObservationEntry {
955               path:         "main.rs".to_string(),
956               observations: vec!["changed CLI wiring".to_string()],
957            },
958         ],
959      };
960
961      let result = map_batch_response_to_observations(&files, &response, None, None);
962
963      assert_eq!(result[0].file, "src/lib.rs");
964      assert_eq!(result[0].observations, vec!["updated library entrypoint".to_string()]);
965      assert_eq!(result[1].file, "src/main.rs");
966      assert_eq!(result[1].observations, vec!["changed CLI wiring".to_string()]);
967      assert_eq!(result[2].file, "crates/core/mod.rs");
968      assert_eq!(result[2].observations, vec!["Updated mod.rs.".to_string()]);
969   }
970
971   #[test]
972   fn test_batch_response_mapping_falls_back_for_text_only_response() {
973      let first = file_with_tokens("src/lib.rs", 1);
974      let second = file_with_tokens("src/main.rs", 1);
975      let files = [&first, &second];
976      let response = BatchObservationResponse { files: Vec::new() };
977
978      let result = map_batch_response_to_observations(
979         &files,
980         &response,
981         Some("- unstructured observation"),
982         None,
983      );
984
985      assert_eq!(result[0].observations, vec!["Updated lib.rs.".to_string()]);
986      assert_eq!(result[1].observations, vec!["Updated main.rs.".to_string()]);
987   }
988
989   #[test]
990   fn test_should_use_map_reduce_disabled() {
991      let config = CommitConfig { map_reduce_enabled: false, ..Default::default() };
992      let counter = test_counter();
993      // Even with many files, disabled means no map-reduce
994      let diff = r"diff --git a/a.rs b/a.rs
995@@ -0,0 +1 @@
996+a
997diff --git a/b.rs b/b.rs
998@@ -0,0 +1 @@
999+b
1000diff --git a/c.rs b/c.rs
1001@@ -0,0 +1 @@
1002+c
1003diff --git a/d.rs b/d.rs
1004@@ -0,0 +1 @@
1005+d";
1006      assert!(!should_use_map_reduce(diff, &config, &counter));
1007   }
1008
1009   #[test]
1010   fn test_should_use_map_reduce_few_files() {
1011      let config = CommitConfig::default();
1012      let counter = test_counter();
1013      // Only 2 files - below threshold
1014      let diff = r"diff --git a/a.rs b/a.rs
1015@@ -0,0 +1 @@
1016+a
1017diff --git a/b.rs b/b.rs
1018@@ -0,0 +1 @@
1019+b";
1020      assert!(!should_use_map_reduce(diff, &config, &counter));
1021   }
1022
1023   #[test]
1024   fn test_should_use_map_reduce_many_tiny_files_below_threshold() {
1025      let config = CommitConfig { map_reduce_threshold: 1_000, ..Default::default() };
1026      let counter = test_counter();
1027      let diff = r"diff --git a/a.rs b/a.rs
1028@@ -0,0 +1 @@
1029+a
1030diff --git a/b.rs b/b.rs
1031@@ -0,0 +1 @@
1032+b
1033diff --git a/c.rs b/c.rs
1034@@ -0,0 +1 @@
1035+c
1036diff --git a/d.rs b/d.rs
1037@@ -0,0 +1 @@
1038+d
1039diff --git a/e.rs b/e.rs
1040@@ -0,0 +1 @@
1041+e";
1042      assert!(!should_use_map_reduce(diff, &config, &counter));
1043   }
1044
1045   #[test]
1046   fn test_should_use_map_reduce_large_total_diff() {
1047      let config = CommitConfig { map_reduce_threshold: 20, ..Default::default() };
1048      let counter = test_counter();
1049      let payload = "a".repeat(200);
1050      let diff = format!("diff --git a/a.rs b/a.rs\n@@ -0,0 +1 @@\n+{payload}");
1051
1052      assert!(should_use_map_reduce(&diff, &config, &counter));
1053   }
1054
1055   #[test]
1056   fn test_should_use_map_reduce_single_oversized_file() {
1057      let config = CommitConfig { map_reduce_threshold: usize::MAX, ..Default::default() };
1058      let counter = test_counter();
1059      let payload = "a".repeat((MAX_FILE_TOKENS + 1) * 4);
1060      let diff = format!("diff --git a/a.rs b/a.rs\n@@ -0,0 +1 @@\n+{payload}");
1061
1062      assert!(should_use_map_reduce(&diff, &config, &counter));
1063   }
1064
1065   #[test]
1066   fn test_generate_context_header_empty() {
1067      let files = vec![FileDiff {
1068         filename:  "only.rs".to_string(),
1069         header:    String::new(),
1070         content:   String::new(),
1071         additions: 10,
1072         deletions: 5,
1073         is_binary: false,
1074      }];
1075      let context_headers = ContextHeaders::new(&files);
1076      let header = context_headers.header_for_files(&["only.rs"]);
1077      assert!(header.is_empty());
1078   }
1079
1080   #[test]
1081   fn test_generate_context_header_multiple() {
1082      let files = vec![
1083         FileDiff {
1084            filename:  "src/main.rs".to_string(),
1085            header:    String::new(),
1086            content:   "fn main() {}".to_string(),
1087            additions: 10,
1088            deletions: 5,
1089            is_binary: false,
1090         },
1091         FileDiff {
1092            filename:  "src/lib.rs".to_string(),
1093            header:    String::new(),
1094            content:   "mod test;".to_string(),
1095            additions: 3,
1096            deletions: 1,
1097            is_binary: false,
1098         },
1099         FileDiff {
1100            filename:  "tests/test.rs".to_string(),
1101            header:    String::new(),
1102            content:   "#[test]".to_string(),
1103            additions: 20,
1104            deletions: 0,
1105            is_binary: false,
1106         },
1107      ];
1108
1109      let context_headers = ContextHeaders::new(&files);
1110      let header = context_headers.header_for_files(&["src/main.rs"]);
1111      assert!(header.contains("OTHER FILES IN THIS CHANGE:"));
1112      assert!(header.contains("src/lib.rs"));
1113      assert!(header.contains("tests/test.rs"));
1114      assert!(!header.contains("src/main.rs")); // Current file excluded
1115   }
1116
1117   #[test]
1118   fn test_infer_file_description() {
1119      assert_eq!(infer_file_description("src/test_utils.rs", ""), "test file");
1120      assert_eq!(infer_file_description("README.md", ""), "documentation");
1121      assert_eq!(infer_file_description("prompts/analysis/default.md", ""), "prompt template");
1122      assert_eq!(infer_file_description("system/analysis/default.md", ""), "prompt template");
1123      assert_eq!(infer_file_description("config.toml", ""), "configuration");
1124      assert_eq!(infer_file_description("src/error.rs", ""), "error definitions");
1125      assert_eq!(infer_file_description("src/types.rs", ""), "type definitions");
1126      assert_eq!(infer_file_description("src/mod.rs", ""), "module exports");
1127      assert_eq!(infer_file_description("src/main.rs", ""), "entry point");
1128      assert_eq!(infer_file_description("src/api.rs", "fn call()"), "implementation");
1129      assert_eq!(infer_file_description("src/models.rs", "struct Foo"), "type definitions");
1130      assert_eq!(infer_file_description("src/unknown.xyz", ""), "source code");
1131   }
1132
1133   #[test]
1134   fn test_parse_string_to_observations_json_array() {
1135      let input = r#"["item one", "item two", "item three"]"#;
1136      let result = parse_string_to_observations(input);
1137      assert_eq!(result, vec!["item one", "item two", "item three"]);
1138   }
1139
1140   #[test]
1141   fn test_parse_string_to_observations_bullet_points() {
1142      let input = "- added new function\n- fixed bug in parser\n- updated tests";
1143      let result = parse_string_to_observations(input);
1144      assert_eq!(result, vec!["added new function", "fixed bug in parser", "updated tests"]);
1145   }
1146
1147   #[test]
1148   fn test_parse_string_to_observations_asterisk_bullets() {
1149      let input = "* first change\n* second change";
1150      let result = parse_string_to_observations(input);
1151      assert_eq!(result, vec!["first change", "second change"]);
1152   }
1153
1154   #[test]
1155   fn test_parse_string_to_observations_empty() {
1156      assert!(parse_string_to_observations("").is_empty());
1157      assert!(parse_string_to_observations("   ").is_empty());
1158   }
1159
1160   #[test]
1161   fn test_deserialize_observations_array() {
1162      let json = r#"{"path": "src/lib.rs", "observations": ["a", "b", "c"]}"#;
1163      let result: FileObservationEntry =
1164         serde_json::from_str(json).expect("valid observation array JSON should deserialize");
1165      assert_eq!(result.path, "src/lib.rs");
1166      assert_eq!(result.observations, vec!["a", "b", "c"]);
1167   }
1168
1169   #[test]
1170   fn test_deserialize_observations_stringified_array() {
1171      let json = r#"{"path": "src/lib.rs", "observations": "[\"a\", \"b\", \"c\"]"}"#;
1172      let result: FileObservationEntry = serde_json::from_str(json)
1173         .expect("valid stringified observation array JSON should deserialize");
1174      assert_eq!(result.path, "src/lib.rs");
1175      assert_eq!(result.observations, vec!["a", "b", "c"]);
1176   }
1177
1178   #[test]
1179   fn test_deserialize_observations_bullet_string() {
1180      let json = r#"{"path": "src/lib.rs", "observations": "- updated function\n- fixed bug"}"#;
1181      let result: FileObservationEntry =
1182         serde_json::from_str(json).expect("valid bullet observation JSON should deserialize");
1183      assert_eq!(result.path, "src/lib.rs");
1184      assert_eq!(result.observations, vec!["updated function", "fixed bug"]);
1185   }
1186}