File size: 10,224 Bytes
e5034c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::sync::Arc;

use forge_domain::{Agent, ContextMessage, Conversation, Role, TextMessage};
use forge_template::Element;

use crate::utils::format_display_path;
use crate::{EnvironmentInfra, FsReadService};

/// Service responsible for detecting externally changed files and rendering
/// notifications
pub struct ChangedFiles<S> {
    services: Arc<S>,
    agent: Agent,
}

impl<S> ChangedFiles<S> {
    /// Creates a new ChangedFiles
    pub fn new(services: Arc<S>, agent: Agent) -> Self {
        Self { services, agent }
    }
}

impl<S: FsReadService + EnvironmentInfra<Config = forge_config::ForgeConfig>> ChangedFiles<S> {
    /// Detects externally changed files and renders a notification if changes
    /// are found. Updates file hashes in conversation metrics to prevent
    /// duplicate notifications.
    pub async fn update_file_stats(&self, mut conversation: Conversation) -> Conversation {
        use crate::file_tracking::FileChangeDetector;
        let parallel_file_reads = self
            .services
            .get_config()
            .map(|c| c.max_parallel_file_reads)
            .unwrap_or(4);
        let changes = FileChangeDetector::new(self.services.clone())
            .detect(&conversation.metrics, parallel_file_reads)
            .await;

        if changes.is_empty() {
            return conversation;
        }

        // Update file hashes to prevent duplicate notifications
        let mut updated_metrics = conversation.metrics.clone();
        for change in &changes {
            if let Some(path_str) = change.path.to_str()
                && let Some(metrics) = updated_metrics.file_operations.get_mut(path_str)
            {
                // Update the file hash
                metrics.content_hash = change.content_hash.clone();
            }
        }
        conversation.metrics = updated_metrics;

        let cwd = self.services.get_environment().cwd;
        let file_elements: Vec<Element> = changes
            .iter()
            .map(|change| {
                let display_path = format_display_path(&change.path, &cwd);
                Element::new("file").text(display_path)
            })
            .collect();

        let notification = Element::new("information")
            .append(
                Element::new("critical")
                    .text("The following files have been modified externally. Please re-read them if its relevant for the task."),
            )
            .append(Element::new("files").append(file_elements))
            .to_string();

        let context = conversation.context.take().unwrap_or_default();

        let message = TextMessage::new(Role::User, notification)
            .droppable(true)
            .model(self.agent.model.clone());

        conversation = conversation.context(context.add_message(ContextMessage::from(message)));

        conversation
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::path::PathBuf;

    use forge_domain::{
        Agent, AgentId, Context, Conversation, ConversationId, Environment, FileOperation, Metrics,
        ModelId, ProviderId, ToolKind,
    };
    use pretty_assertions::assert_eq;

    use super::*;
    use crate::services::Content;
    use crate::{FsReadService, ReadOutput, compute_hash};

    #[derive(Clone, Default)]
    struct TestServices {
        files: HashMap<String, String>,
        cwd: Option<PathBuf>,
    }

    #[async_trait::async_trait]
    impl FsReadService for TestServices {
        async fn read(
            &self,
            path: String,
            _: Option<u64>,
            _: Option<u64>,
        ) -> anyhow::Result<ReadOutput> {
            self.files
                .get(&path)
                .map(|content| {
                    let hash = compute_hash(content);
                    ReadOutput {
                        content: Content::file(content.clone()),
                        info: forge_domain::FileInfo::new(1, 1, 1, hash),
                    }
                })
                .ok_or_else(|| anyhow::anyhow!(std::io::Error::from(std::io::ErrorKind::NotFound)))
        }
    }

    impl EnvironmentInfra for TestServices {
        type Config = forge_config::ForgeConfig;

        fn get_environment(&self) -> Environment {
            use fake::{Fake, Faker};
            let mut env: Environment = Faker.fake();
            if let Some(cwd) = &self.cwd {
                env.cwd = cwd.clone();
            } else {
                // Use a deterministic cwd that won't match any test paths
                env.cwd = PathBuf::from("/deterministic/test/cwd");
            }
            env
        }

        fn get_config(&self) -> anyhow::Result<forge_config::ForgeConfig> {
            Ok(forge_config::ForgeConfig { max_parallel_file_reads: 4, ..Default::default() })
        }

        async fn update_environment(
            &self,
            _ops: Vec<forge_domain::ConfigOperation>,
        ) -> anyhow::Result<()> {
            unimplemented!()
        }

        fn get_env_var(&self, _key: &str) -> Option<String> {
            None
        }

        fn get_env_vars(&self) -> std::collections::BTreeMap<String, String> {
            std::collections::BTreeMap::new()
        }
    }

    fn fixture(
        files: HashMap<String, String>,
        tracked_files: HashMap<String, Option<String>>,
    ) -> (ChangedFiles<TestServices>, Conversation) {
        fixture_with_cwd(files, tracked_files, None)
    }

    fn fixture_with_cwd(
        files: HashMap<String, String>,
        tracked_files: HashMap<String, Option<String>>,
        cwd: Option<PathBuf>,
    ) -> (ChangedFiles<TestServices>, Conversation) {
        let services = Arc::new(TestServices { files, cwd });
        let agent = Agent::new(
            AgentId::new("test"),
            ProviderId::ANTHROPIC,
            ModelId::new("test-model"),
        );
        let changed_files = ChangedFiles::new(services, agent);

        let mut metrics = Metrics::default();
        for (path, hash) in tracked_files {
            metrics
                .file_operations
                .insert(path, FileOperation::new(ToolKind::Write).content_hash(hash));
        }

        let conversation = Conversation::new(ConversationId::generate()).metrics(metrics);

        (changed_files, conversation)
    }

    #[tokio::test]
    async fn test_no_changes_detected() {
        let content = "hello world";
        let hash = crate::compute_hash(content);

        let (service, mut conversation) = fixture(
            [("/test/file.txt".into(), content.into())].into(),
            [("/test/file.txt".into(), Some(hash))].into(),
        );

        conversation.context = Some(Context::default().add_message(ContextMessage::user(
            "Hey, there!",
            Some(ModelId::new("test")),
        )));

        let actual = service.update_file_stats(conversation.clone()).await;

        assert_eq!(actual.context.clone().unwrap_or_default().messages.len(), 1);
        assert_eq!(actual.context, conversation.context);
    }

    #[tokio::test]
    async fn test_changes_detected_adds_notification() {
        let old_hash = crate::compute_hash("old content");
        let new_content = "new content";

        let (service, conversation) = fixture(
            [("/test/file.txt".into(), new_content.into())].into(),
            [("/test/file.txt".into(), Some(old_hash))].into(),
        );

        let actual = service.update_file_stats(conversation).await;

        let messages = &actual.context.unwrap().messages;
        assert_eq!(messages.len(), 1);
        let message = messages[0].content().unwrap().to_string();
        assert!(message.contains("/test/file.txt"));
        assert!(message.contains("modified externally"));
    }

    #[tokio::test]
    async fn test_updates_content_hash() {
        let old_hash = crate::compute_hash("old content");
        let new_content = "new content";
        let new_hash = crate::compute_hash(new_content);

        let (service, conversation) = fixture(
            [("/test/file.txt".into(), new_content.into())].into(),
            [("/test/file.txt".into(), Some(old_hash))].into(),
        );

        let actual = service.update_file_stats(conversation).await;

        let updated_hash = actual
            .metrics
            .file_operations
            .get("/test/file.txt")
            .and_then(|m| m.content_hash.clone());

        assert_eq!(updated_hash, Some(new_hash));
    }

    #[tokio::test]
    async fn test_multiple_files_changed() {
        let (service, conversation) = fixture(
            [
                ("/test/file1.txt".into(), "new 1".into()),
                ("/test/file2.txt".into(), "new 2".into()),
            ]
            .into(),
            [
                ("/test/file1.txt".into(), Some(crate::compute_hash("old 1"))),
                ("/test/file2.txt".into(), Some(crate::compute_hash("old 2"))),
            ]
            .into(),
        );

        let actual = service.update_file_stats(conversation).await;

        let message = actual.context.unwrap().messages[0]
            .content()
            .unwrap()
            .to_string();

        insta::assert_snapshot!(message);
    }

    #[tokio::test]
    async fn test_uses_relative_paths_within_cwd() {
        let old_hash = crate::compute_hash("old content");
        let new_content = "new content";
        let cwd = PathBuf::from("/home/user/project");
        let absolute_path = "/home/user/project/src/main.rs";

        let (service, conversation) = fixture_with_cwd(
            [(absolute_path.into(), new_content.into())].into(),
            [(absolute_path.into(), Some(old_hash))].into(),
            Some(cwd),
        );

        let actual = service.update_file_stats(conversation).await;

        let message = actual.context.unwrap().messages[0]
            .content()
            .unwrap()
            .to_string();

        let expected = "<information>\n<critical>The following files have been modified externally. Please re-read them if its relevant for the task.</critical>\n<files>\n<file>src/main.rs</file>\n</files>\n</information>";

        assert_eq!(message, expected);
    }
}