-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Support remote project rules through repo metadata standing results #11460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
706ca0e
Unify local and remote project rule discovery
moirahuang 1862fbe
format name
moirahuang 34f37c3
Simplify project rule hydration
moirahuang df81fb4
wasm
moirahuang 1d7b89b
clarifying comments
moirahuang a952f94
clippy wasm
moirahuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,12 @@ | |
|
|
||
| use std::collections::{HashMap, HashSet}; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::str::FromStr; | ||
| use std::sync::Arc; | ||
|
|
||
| use ai::project_context::model::ProjectContextModel; | ||
| use parking_lot::FairMutex; | ||
| use warp_core::features::FeatureFlag; | ||
| use warp_util::local_or_remote_path::LocalOrRemotePath; | ||
| use warpui::{ | ||
| AppContext, Entity, EntityId, ModelContext, ModelHandle, SingletonEntity, WeakModelHandle, | ||
| }; | ||
|
|
@@ -393,7 +393,14 @@ impl BlocklistAIContextModel { | |
| /// Returns `AIAgentContext` for the blocks to be included in the current AI query. | ||
| /// If `is_user_query` is true, includes blocks, selected text, and images as context. | ||
| /// If false, excludes these user-specific contexts but includes everything else. | ||
| pub fn pending_context(&self, app: &AppContext, is_user_query: bool) -> Vec<AIAgentContext> { | ||
| pub fn pending_context( | ||
| &self, | ||
| app: &AppContext, | ||
| is_user_query: bool, | ||
| current_working_directory_location: Option<&LocalOrRemotePath>, | ||
| ) -> Vec<AIAgentContext> { | ||
| // `pwd` is the shell-reported path used for directory context and local indexing. | ||
| // The location is passed separately because it preserves remote host identity for rules. | ||
| let pwd = self.current_pwd(); | ||
| let is_pwd_indexed = if cfg!(feature = "agent_mode_evals") { | ||
| // In evals, we want to disable file outline based search. Full | ||
|
|
@@ -406,15 +413,8 @@ impl BlocklistAIContextModel { | |
| }) | ||
| }; | ||
|
|
||
| let project_rules = if let Some(pwd) = pwd.clone().and_then(|path| { | ||
| PathBuf::from_str(&path) | ||
| .ok() | ||
| .and_then(|s| s.canonicalize().ok()) | ||
| }) { | ||
| ProjectContextModel::as_ref(app).find_applicable_rules(&pwd) | ||
| } else { | ||
| None | ||
| }; | ||
| let project_rules = current_working_directory_location | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we're already typed, that simplifies this logic here |
||
| .and_then(|pwd| ProjectContextModel::as_ref(app).find_applicable_rules(pwd)); | ||
|
|
||
| let mut context = Vec::new(); | ||
|
|
||
|
|
@@ -451,14 +451,14 @@ impl BlocklistAIContextModel { | |
| // Always include project rules if available | ||
| if let Some(rules) = project_rules { | ||
| context.push(AIAgentContext::ProjectRules { | ||
| root_path: rules.root_path.to_string_lossy().into(), | ||
| root_path: rules.root_path.display_path(), | ||
| active_rules: rules | ||
| .active_rules | ||
| .into_iter() | ||
| .map(|rule| { | ||
| let line_count = rule.content.lines().count(); | ||
| FileContext { | ||
| file_name: rule.path.to_string_lossy().into(), | ||
| file_name: rule.path.display_path(), | ||
| content: AnyFileContent::StringContent(rule.content.clone()), | ||
| line_range: None, | ||
| last_modified: None, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| use ai::project_context::model::ProjectRuleContents; | ||
| use futures::future::{BoxFuture, FutureExt as _}; | ||
| use remote_server::proto::{ | ||
| file_context_proto, FileContextProto, ReadFileContextFile, ReadFileContextRequest, | ||
| }; | ||
| use warp_util::local_or_remote_path::LocalOrRemotePath; | ||
| use warpui::{AppContext, SingletonEntity}; | ||
|
|
||
| use crate::remote_server::manager::RemoteServerManager; | ||
|
|
||
| pub(crate) fn read_project_rule_contents( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aka ProjectRuleContentReader |
||
| rule_paths: Vec<LocalOrRemotePath>, | ||
| ctx: &AppContext, | ||
| ) -> BoxFuture<'static, anyhow::Result<ProjectRuleContents>> { | ||
| match rule_paths.first() { | ||
| None => futures::future::ready(Ok(Vec::new())).boxed(), | ||
| Some(LocalOrRemotePath::Local(_)) => async move { | ||
| let mut contents = Vec::new(); | ||
| for path in rule_paths { | ||
| let Some(local_path) = path.to_local_path().map(std::path::Path::to_path_buf) | ||
| else { | ||
| anyhow::bail!("Project rule paths mixed local and remote locations"); | ||
| }; | ||
| match async_fs::read_to_string(&local_path).await { | ||
| Ok(content) => contents.push((path, content)), | ||
| Err(error) => log::debug!( | ||
| "Failed to read project rule file {}: {error}", | ||
| local_path.display() | ||
| ), | ||
| } | ||
| } | ||
| Ok(contents) | ||
| } | ||
| .boxed(), | ||
| Some(LocalOrRemotePath::Remote(remote)) => { | ||
| let host_id = remote.host_id.clone(); | ||
| let handle = RemoteServerManager::as_ref(ctx).host_request_handle(&host_id); | ||
| async move { | ||
| if rule_paths.iter().any(|path| { | ||
| !matches!( | ||
| path, | ||
| LocalOrRemotePath::Remote(candidate) if candidate.host_id == host_id | ||
| ) | ||
| }) { | ||
| anyhow::bail!("Project rule paths span multiple locations"); | ||
| } | ||
| let response = handle | ||
| .read_file_context(remote_rule_read_request(&rule_paths)) | ||
| .await?; | ||
| Ok(pair_remote_rule_paths_with_contents( | ||
| rule_paths, | ||
| response.file_contexts, | ||
| )) | ||
| } | ||
| .boxed() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn remote_rule_read_request(rule_paths: &[LocalOrRemotePath]) -> ReadFileContextRequest { | ||
| ReadFileContextRequest { | ||
| files: rule_paths | ||
| .iter() | ||
| .filter_map(|path| match path { | ||
| LocalOrRemotePath::Remote(remote) => Some(ReadFileContextFile { | ||
| path: remote.path.as_str().to_string(), | ||
| line_ranges: Vec::new(), | ||
| }), | ||
| LocalOrRemotePath::Local(_) => None, | ||
| }) | ||
| .collect(), | ||
| max_file_bytes: None, | ||
| max_batch_bytes: None, | ||
| } | ||
| } | ||
|
|
||
| /// Pairs remote read responses with the original host-qualified paths. | ||
| /// | ||
| /// Responses may be reordered or omit unreadable files, and their file names do not include the | ||
| /// host ID. Matching by path preserves the correct host identity without relying on response order. | ||
| fn pair_remote_rule_paths_with_contents( | ||
| rule_paths: Vec<LocalOrRemotePath>, | ||
| file_contexts: Vec<FileContextProto>, | ||
| ) -> Vec<(LocalOrRemotePath, String)> { | ||
| let content_by_path = file_contexts | ||
| .into_iter() | ||
| .filter_map(|file_context| { | ||
| let file_context_proto::Content::TextContent(content) = file_context.content? else { | ||
| return None; | ||
| }; | ||
| Some((file_context.file_name, content)) | ||
| }) | ||
| .collect::<HashMap<_, _>>(); | ||
| rule_paths | ||
| .into_iter() | ||
| .filter_map(|path| { | ||
| let LocalOrRemotePath::Remote(remote) = &path else { | ||
| return None; | ||
| }; | ||
| let content = content_by_path.get(remote.path.as_str())?.clone(); | ||
| Some((path, content)) | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[path = "metadata_project_rules_tests.rs"] | ||
| mod tests; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| use remote_server::proto::{file_context_proto, FileContextProto}; | ||
| use warp_util::host_id::HostId; | ||
| use warp_util::local_or_remote_path::LocalOrRemotePath; | ||
| use warp_util::remote_path::RemotePath; | ||
| use warp_util::standardized_path::StandardizedPath; | ||
|
|
||
| use super::{pair_remote_rule_paths_with_contents, remote_rule_read_request}; | ||
|
|
||
| fn remote_rule_path(host_id: &HostId, name: &str) -> LocalOrRemotePath { | ||
| LocalOrRemotePath::Remote(RemotePath::new( | ||
| host_id.clone(), | ||
| StandardizedPath::try_new(format!("/repo/{name}").as_str()).unwrap(), | ||
| )) | ||
| } | ||
|
|
||
| fn remote_rule_file_context(path: &LocalOrRemotePath, content: &str) -> FileContextProto { | ||
| let LocalOrRemotePath::Remote(remote) = path else { | ||
| panic!("Expected a remote rule path"); | ||
| }; | ||
|
|
||
| FileContextProto { | ||
| file_name: remote.path.as_str().to_string(), | ||
| content: Some(file_context_proto::Content::TextContent( | ||
| content.to_string(), | ||
| )), | ||
| line_range_start: None, | ||
| line_range_end: None, | ||
| last_modified_epoch_millis: None, | ||
| line_count: content.lines().count() as u32, | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn remote_rule_contents_match_reordered_responses_by_path() { | ||
| let host = HostId::new("test-host".to_string()); | ||
| let first_path = remote_rule_path(&host, "WARP.md"); | ||
| let second_path = remote_rule_path(&host, "nested/AGENTS.md"); | ||
|
|
||
| let contents = pair_remote_rule_paths_with_contents( | ||
| vec![first_path.clone(), second_path.clone()], | ||
| vec![ | ||
| remote_rule_file_context(&second_path, "second rules"), | ||
| remote_rule_file_context(&first_path, "first rules"), | ||
| ], | ||
| ); | ||
|
|
||
| assert_eq!( | ||
| contents, | ||
| vec![ | ||
| (first_path, "first rules".to_string()), | ||
| (second_path, "second rules".to_string()), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn remote_rule_contents_keep_paths_aligned_after_missing_reads() { | ||
| let host = HostId::new("test-host".to_string()); | ||
| let missing_path = remote_rule_path(&host, "WARP.md"); | ||
| let present_path = remote_rule_path(&host, "nested/AGENTS.md"); | ||
|
|
||
| let contents = pair_remote_rule_paths_with_contents( | ||
| vec![missing_path, present_path.clone()], | ||
| vec![remote_rule_file_context(&present_path, "present rules")], | ||
| ); | ||
|
|
||
| assert_eq!(contents, vec![(present_path, "present rules".to_string())]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn remote_rule_read_request_preserves_discovered_paths() { | ||
| let host = HostId::new("test-host".to_string()); | ||
| let first_path = remote_rule_path(&host, "WARP.md"); | ||
| let second_path = remote_rule_path(&host, "nested/AGENTS.md"); | ||
|
|
||
| let request = remote_rule_read_request(&[first_path.clone(), second_path.clone()]); | ||
|
|
||
| assert_eq!(request.max_file_bytes, None); | ||
| assert_eq!(request.max_batch_bytes, None); | ||
| assert_eq!(request.files.len(), 2); | ||
| let LocalOrRemotePath::Remote(first_remote) = first_path else { | ||
| panic!("Expected a remote rule path"); | ||
| }; | ||
| let LocalOrRemotePath::Remote(second_remote) = second_path else { | ||
| panic!("Expected a remote rule path"); | ||
| }; | ||
| assert_eq!(request.files[0].path, first_remote.path.as_str()); | ||
| assert_eq!(request.files[1].path, second_remote.path.as_str()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.