969 lines · 31.9 KB · Rust
RawDownload
use crate::{    anthropic_client::PlainLlmClient,    git::{ensure_repo_cloned, run_git},    paths::{FAILED_EXAMPLES_DIR, LATEST_FAILED_EXAMPLES_DIR, SYNTHESIZE_STATE_FILE},    progress::{InfoStyle, Progress, Step, StepProgress},};use anthropic::ResponseContent;use anyhow::{Context as _, Result};use chrono::Local;use collections::{HashMap, HashSet};use edit_prediction::{    data_collection::format_cursor_excerpt,    example_spec::ExampleSpec,    udiff::{apply_diff_to_string, edits_for_diff},};use futures::stream::{FuturesUnordered, StreamExt};use indoc::indoc;use serde::{Deserialize, Serialize};use std::{    path::{Path, PathBuf},    sync::Arc,}; #[derive(Debug, Clone)]pub struct SynthesizeConfig {    pub repo_urls: Vec<String>,    /// Number of examples to generate per repository    pub count: usize,    pub max_commits: usize,    pub output_dir: PathBuf,    pub fresh: bool,} #[derive(Debug, Default, Serialize, Deserialize)]struct SynthesizeState {    repositories: HashMap<String, RepoState>,} #[derive(Debug, Default, Serialize, Deserialize)]struct RepoState {    processed_commits: HashSet<String>,    examples_generated: usize,} impl SynthesizeState {    fn load() -> Self {        if SYNTHESIZE_STATE_FILE.exists() {            std::fs::read_to_string(&*SYNTHESIZE_STATE_FILE)                .ok()                .and_then(|s| serde_json::from_str(&s).ok())                .unwrap_or_default()        } else {            Self::default()        }    }     fn save(&self) -> Result<()> {        let content = serde_json::to_string_pretty(self)?;        std::fs::write(&*SYNTHESIZE_STATE_FILE, content)?;        Ok(())    }     fn take_repo_state(&mut self, repo_url: &str) -> RepoState {        self.repositories.remove(repo_url).unwrap_or_default()    }     fn merge_repo_state(&mut self, repo_url: String, repo_state: RepoState) {        self.repositories.insert(repo_url, repo_state);    }} impl RepoState {    fn is_processed(&self, commit_sha: &str) -> bool {        self.processed_commits.contains(commit_sha)    }     fn mark_processed(&mut self, commit_sha: &str, examples_count: usize) {        self.processed_commits.insert(commit_sha.to_string());        self.examples_generated += examples_count;    }} #[derive(Debug)]struct CommitInfo {    sha: String,    parent_sha: String,    message: String,    diff: String,    expanded_diff: String,} /// Claude's response parsed into structured form#[derive(Debug)]struct ClaudeResponse {    name: String,    reasoning: String,    edit_history_hunks: Vec<String>,    expected_patch_hunks: Vec<String>,} pub async fn run_synthesize(config: SynthesizeConfig) -> Result<()> {    let mut state = if config.fresh {        SynthesizeState::default()    } else {        SynthesizeState::load()    };     std::fs::create_dir_all(&config.output_dir)?;    std::fs::create_dir_all(&*FAILED_EXAMPLES_DIR)?;     // Create "latest_failed" symlink pointing to this run's failed directory    if LATEST_FAILED_EXAMPLES_DIR.is_symlink() {        std::fs::remove_file(&*LATEST_FAILED_EXAMPLES_DIR)?;    }    #[cfg(unix)]    std::os::unix::fs::symlink(&*FAILED_EXAMPLES_DIR, &*LATEST_FAILED_EXAMPLES_DIR)?;    #[cfg(windows)]    std::os::windows::fs::symlink_dir(&*FAILED_EXAMPLES_DIR, &*LATEST_FAILED_EXAMPLES_DIR)?;     let progress = Progress::global();    let total_examples = config.count * config.repo_urls.len();    progress.set_total_examples(total_examples);     let client = Arc::new(PlainLlmClient::new()?);    let config = Arc::new(config);     let mut futures: FuturesUnordered<_> = config        .repo_urls        .iter()        .map(|repo_url| {            let client = client.clone();            let repo_state = state.take_repo_state(repo_url);            let config = config.clone();            let repo_url = repo_url.clone();            async move {                let result = synthesize_repo(&client, repo_state, &config, &repo_url).await;                (repo_url, result)            }        })        .collect();     let mut errors = Vec::new();    while let Some((repo_url, result)) = futures.next().await {        match result {            Ok(repo_state) => {                state.merge_repo_state(repo_url, repo_state);            }            Err(e) => {                errors.push(e);            }        }    }     state.save()?;     progress.finalize();     if let Some(first_error) = errors.into_iter().next() {        return Err(first_error);    }     Ok(())} async fn synthesize_repo(    client: &PlainLlmClient,    mut repo_state: RepoState,    config: &SynthesizeConfig,    repo_url: &str,) -> Result<RepoState> {    let progress = Progress::global();    let batch_size = config.max_commits;     let clone_progress = progress.start(Step::Synthesize, &format!("clone {}", repo_url));    let repo_path = ensure_repo_cloned(repo_url).await?;    drop(clone_progress);     let mut examples_generated = 0;    let mut commits_skipped = 0;     'outer: loop {        let list_progress = progress.start(            Step::Synthesize,            &format!("{}: list-commits", repo_name_from_url(repo_url)),        );        let commits = list_commits(&repo_path, batch_size, commits_skipped).await?;        drop(list_progress);         if commits.is_empty() {            break;        }         commits_skipped += commits.len();         for commit in commits {            if examples_generated >= config.count {                break 'outer;            }             if !config.fresh && repo_state.is_processed(&commit.sha) {                continue;            }             if should_skip_commit(&commit) {                continue;            }             let repo_name = repo_name_from_url(repo_url);            let commit_label = format!(                "{}: {} {}",                repo_name,                &commit.sha[..8],                truncate_message(&commit.message, 40)            );            let step_progress = Arc::new(progress.start(Step::Synthesize, &commit_label));             // Single Claude call to identify and copy hunks            step_progress.set_substatus("analyzing...");            let claude_response =                match analyze_commit(client, repo_url, &commit, step_progress.clone()).await {                    Ok(Some(response)) => response,                    Ok(None) => {                        step_progress.set_info("no pattern", InfoStyle::Normal);                        repo_state.mark_processed(&commit.sha, 0);                        continue;                    }                    Err(e) => {                        step_progress.set_info(format!("error: {:?}", e), InfoStyle::Warning);                        repo_state.mark_processed(&commit.sha, 0);                        continue;                    }                };             // Validate and build the example            step_progress.set_substatus("validating...");            match build_example(repo_url, &commit, &repo_path, &claude_response).await {                Ok(spec) => {                    let timestamp = Local::now().format("%Y-%m-%d--%H-%M-%S");                    let filename = format!("{}--{}.md", repo_name, timestamp);                    let path = config.output_dir.join(&filename);                    std::fs::write(&path, spec.to_markdown())?;                    examples_generated += 1;                    step_progress.set_info(filename, InfoStyle::Normal);                }                Err(rejection_reason) => {                    log::debug!("Example rejected: {}", rejection_reason);                    let timestamp = Local::now().format("%Y-%m-%d--%H-%M-%S%.3f");                    let filename = format!("{}--{}.md", repo_name, timestamp);                    let path = FAILED_EXAMPLES_DIR.join(&filename);                    let content = format_rejected_example(&claude_response, &rejection_reason);                    if let Err(e) = std::fs::write(&path, content) {                        log::warn!("Failed to write rejected example: {:?}", e);                    }                    step_progress.set_info(format!("rejected: {}", filename), InfoStyle::Warning);                }            }             repo_state.mark_processed(&commit.sha, 1);        }    }     Ok(repo_state)} fn repo_name_from_url(url: &str) -> String {    url.rsplit('/')        .next()        .unwrap_or(url)        .trim_end_matches(".git")        .to_string()} fn truncate_message(msg: &str, max_len: usize) -> String {    let first_line = msg.lines().next().unwrap_or("");    if first_line.len() <= max_len {        first_line.to_string()    } else {        format!("{}...", &first_line[..max_len - 3])    }} fn should_skip_commit(commit: &CommitInfo) -> bool {    let lines_changed = commit        .diff        .lines()        .filter(|l| l.starts_with('+') || l.starts_with('-'))        .count();    lines_changed < 30        || lines_changed > 1000        || is_non_code_commit(commit)        || is_rename_commit(commit)} fn is_non_code_commit(commit: &CommitInfo) -> bool {    let non_code_extensions = [        ".md", ".txt", ".json", ".yaml", ".yml", ".toml", ".lock", ".svg", ".png", ".jpg", ".gif",        ".ico", ".woff", ".ttf", ".eot",    ];     let diff_files: Vec<&str> = commit        .diff        .lines()        .filter(|l| l.starts_with("+++ b/") || l.starts_with("--- a/"))        .filter_map(|l| {            l.strip_prefix("+++ b/")                .or_else(|| l.strip_prefix("--- a/"))        })        .collect();     if diff_files.is_empty() {        return false;    }     diff_files        .iter()        .all(|f| non_code_extensions.iter().any(|ext| f.ends_with(ext)))} fn is_rename_commit(commit: &CommitInfo) -> bool {    commit.diff.contains("similarity index")        || commit.diff.contains("rename from")        || commit.diff.contains("rename to")} async fn list_commits(    repo_path: &Path,    max_commits: usize,    skip: usize,) -> Result<Vec<CommitInfo>> {    let output = run_git(        repo_path,        &[            "log",            "--no-merges",            &format!("--skip={}", skip),            &format!("-{}", max_commits),            "--format=%H|%P|%s",        ],    )    .await?;     let mut commits = Vec::new();    for line in output.lines() {        let parts: Vec<&str> = line.splitn(3, '|').collect();        if parts.len() < 3 {            continue;        }        let sha = parts[0].to_string();        let parent_sha = parts[1].split_whitespace().next().unwrap_or("").to_string();        if parent_sha.is_empty() {            continue;        }         // Get standard diff (for skip checks)        let diff = run_git(repo_path, &["show", "--format=", &sha])            .await            .unwrap_or_default();         // Get expanded diff with 30 lines of context        let expanded_diff = run_git(repo_path, &["show", "-U30", "--format=", &sha])            .await            .unwrap_or_default();         commits.push(CommitInfo {            sha,            parent_sha,            message: parts[2].to_string(),            diff,            expanded_diff,        });    }     Ok(commits)} fn build_prompt(repo_url: &str, commit: &CommitInfo) -> String {    format!(        indoc! {r#"            You are analyzing a git commit to construct a realistic edit prediction example.             Your goal is to tell the story of a programmer's editing session: what sequence            of changes did they make, and what change logically comes next? We use these examples            to train a model to predict edits, so the quality of the EDIT HISTORY is what matters most.             An edit prediction example consists of:            1. **Edit History**: 2-6 hunks showing what the programmer did BEFORE making the expected patch.               This is the most important part - it must tell a coherent story of the changes leading up to the prediction.            2. **Expected Patch**: One small hunk that logically follows from the edit history.             Both single-file and multi-file patterns are acceptable.             ## What Makes a Good Example             The edit history should read like a story: "First the programmer changed X, then Y, then Z, and now they need to change W."             GOOD examples (rich sequences with 3+ steps):            - Removing a parameter: docstring update → constructor change → field removal → (predict) usage site update            - Adding a feature: type definition → first usage → second usage → (predict) third usage            - Bug fix pattern: fix in file A → fix in file B → fix in file C → (predict) fix in file D             BAD examples (respond NO_PATTERN):            - Commits where all changes are independent (no narrative thread)            - Simple find-and-replace (renaming, version bumps)            - Documentation-only or config-only changes            - Changes where you can only find 1-2 hunks for the edit history             ## Commit Information             Repository: {repo_url}            Commit: {sha}            Message: {message}             ## Diff (30 lines context)             ```diff            {expanded_diff}            ```             ## Your Task             First, THINK through whether this commit can support a good example:             1. What is the high-level pattern in this commit?            2. Can you identify at least 3 related hunks (2 or more for edit history + 1 for expected patch)?            3. What would be the narrative? (First... then... then... finally predict...)            4. Which specific hunk should be the expected patch (the "punchline")?             If you cannot construct a coherent 3+ hunk story, respond with just:            NO_PATTERN: <brief reason>             If you CAN construct a good example, respond in this format:             ANALYSIS:            Pattern: <one sentence describing the pattern>            Steps:            1. <file:line-range> - <what this hunk does>            2. <file:line-range> - <what this hunk does>            3. <file:line-range> - <what this hunk does>            4. [EXPECTED PATCH] <file:line-range> - <what this hunk does>             NAME: <short description, like a commit message, under 60 chars>             EDIT_HISTORY:             Hunk 1:            ```diff            --- a/src/models/user.py            +++ b/src/models/user.py            @@ -15,7 +15,6 @@ class User:                 """A user in the system.                  Attributes:            -        email: The user's email address.                     name: The user's display name.                 """            ```             Hunk 2:            ```diff            --- a/src/models/user.py            +++ b/src/models/user.py            @@ -25,10 +24,9 @@ class User:                 def __init__(                     self,                     name: str,            -        email: str,                     created_at: datetime,                 ):                     self.name = name            -        self.email = email                     self.created_at = created_at            ```             Hunk 3:            ```diff            --- a/src/api/handlers.py            +++ b/src/api/handlers.py            @@ -42,7 +42,6 @@ def create_user(request):                 data = request.json()                 user = User(                     name=data["name"],            -        email=data["email"],                     created_at=datetime.now(),                 )                 return user.save()            ```             EXPECTED_PATCH:            ```diff            --- a/src/api/handlers.py            +++ b/src/api/handlers.py            @@ -58,7 +57,6 @@ def update_user(request, user_id):                 user = User.get(user_id)                 user.name = data.get("name", user.name)            -    user.email = data.get("email", user.email)                 user.save()                 return user            ```             ## Requirements for the diffs

Lines 1–500 of 969.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t