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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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use std::path::PathBuf;
use std::sync::{Arc, LazyLock};

use anyhow::Context;
use bytes::Bytes;
use forge_app::domain::{
    ExecuteRule, Fetch, Permission, PermissionOperation, Policy, PolicyConfig, PolicyEngine,
    ReadRule, Rule, WriteRule,
};
use forge_app::{
    DirectoryReaderInfra, EnvironmentInfra, FileInfoInfra, FileReaderInfra, FileWriterInfra,
    PolicyDecision, PolicyService, UserInfra,
};
use strum_macros::{Display, EnumIter};

/// User response for permission confirmation requests
#[derive(Debug, Clone, PartialEq, Eq, Display, EnumIter, strum_macros::EnumString)]
pub enum PolicyPermission {
    /// Accept the operation
    #[strum(to_string = "Accept")]
    Accept,
    /// Reject the operation
    #[strum(to_string = "Reject")]
    Reject,
    /// Accept the operation and remember this choice for similar operations
    #[strum(to_string = "Accept and Remember")]
    AcceptAndRemember,
}

#[derive(Clone)]
pub struct ForgePolicyService<I> {
    infra: Arc<I>,
}
/// Default policies loaded once at startup from the embedded YAML file
static DEFAULT_POLICIES: LazyLock<PolicyConfig> = LazyLock::new(|| {
    let yaml_content = include_str!("./permissions.default.yaml");
    serde_yml::from_str(yaml_content).expect(
        "Failed to parse default policies YAML. This should never happen as the YAML is embedded.",
    )
});

impl<I> ForgePolicyService<I>
where
    I: FileReaderInfra + FileWriterInfra + FileInfoInfra + EnvironmentInfra + DirectoryReaderInfra,
{
    pub fn new(infra: Arc<I>) -> Self {
        Self { infra }
    }

    fn permissions_path(&self) -> PathBuf {
        self.infra.get_environment().permissions_path()
    }

    /// Create a policies collection with sensible defaults
    /// Returns a clone of the preloaded default policies
    fn load_default_policies() -> PolicyConfig {
        DEFAULT_POLICIES.clone()
    }

    /// Add a policy for a specific operation type
    async fn add_policy_for_operation(
        &self,
        operation: &PermissionOperation,
    ) -> anyhow::Result<Option<PathBuf>>
    where
        I: UserInfra,
    {
        if let Some(new_policy) = create_policy_for_operation(operation, None) {
            // TODO: Can return a diff later
            self.modify_policy(new_policy).await?;
            Ok(Some(self.permissions_path()))
        } else {
            Ok(None)
        }
    }

    /// Load all policy definitions from the forge/policies directory
    async fn read_policies(&self) -> anyhow::Result<Option<PolicyConfig>> {
        let policies_path = self.permissions_path();
        if !self.infra.exists(&policies_path).await? {
            return Ok(None);
        }

        let content = self.infra.read_utf8(&policies_path).await?;
        let policies = serde_yml::from_str(&content)
            .with_context(|| format!("Failed to parse policy {}", policies_path.display()))?;

        Ok(Some(policies))
    }

    /// Add or modify a policy in the policies file
    async fn modify_policy(&self, policy: Policy) -> anyhow::Result<()> {
        let policies_path = self.permissions_path();
        let mut policies = self.read_policies().await?.unwrap_or_default();

        // Add the new policy to the collection
        policies = policies.add_policy(policy);

        // Serialize the updated policies to YAML
        let new_content = serde_yml::to_string(&policies)
            .with_context(|| "Failed to serialize policies to YAML")?;

        // Write the updated content
        self.infra
            .write(&policies_path, Bytes::from(new_content.to_owned()))
            .await?;

        Ok(())
    }

    /// Create a default policies file if it does not exist
    async fn init_policies(&self) -> anyhow::Result<()> {
        let policies_path = self.permissions_path();

        // Check if the file already exists
        if self.infra.exists(&policies_path).await? {
            return Ok(());
        }

        // Get the default policies content
        let default_policies = Self::load_default_policies();
        let content = serde_yml::to_string(&default_policies)
            .with_context(|| "Failed to serialize default policies to YAML")?;

        // Write the default policies to the file
        self.infra
            .write(&policies_path, Bytes::from(content))
            .await?;

        Ok(())
    }

    /// Get or create policies, prompting user if needed
    #[async_recursion::async_recursion]
    async fn get_or_create_policies(&self) -> anyhow::Result<(PolicyConfig, Option<PathBuf>)>
    where
        I: UserInfra,
    {
        if let Some(policies) = self.read_policies().await? {
            Ok((policies, None))
        } else {
            self.init_policies().await?;
            let (policies, _) = self.get_or_create_policies().await?;
            Ok((policies, Some(self.permissions_path())))
        }
    }
}

#[async_trait::async_trait]
impl<I> PolicyService for ForgePolicyService<I>
where
    I: FileReaderInfra
        + FileWriterInfra
        + FileInfoInfra
        + EnvironmentInfra
        + DirectoryReaderInfra
        + UserInfra,
{
    /// Check if an operation is allowed based on policies and handle user
    /// confirmation
    async fn check_operation_permission(
        &self,
        operation: &PermissionOperation,
    ) -> anyhow::Result<PolicyDecision> {
        let (policies, path) = self.get_or_create_policies().await?;

        let engine = PolicyEngine::new(&policies);
        let permission = engine.can_perform(operation);

        match permission {
            Permission::Deny => Ok(PolicyDecision { allowed: false, path }),
            Permission::Allow => Ok(PolicyDecision { allowed: true, path }),
            Permission::Confirm => {
                // Request user confirmation using UserInfra
                let confirmation_msg = match operation {
                    PermissionOperation::Read { message, .. } => {
                        format!("{message}. How would you like to proceed?")
                    }
                    PermissionOperation::Write { message, .. } => {
                        format!("{message}. How would you like to proceed?")
                    }
                    PermissionOperation::Execute { .. } => {
                        "How would you like to proceed?".to_string()
                    }
                    PermissionOperation::Fetch { message, .. } => {
                        format!("{message}. How would you like to proceed?")
                    }
                };

                match self
                    .infra
                    .select_one_enum::<PolicyPermission>(&confirmation_msg)
                    .await?
                {
                    Some(PolicyPermission::Accept) => Ok(PolicyDecision { allowed: true, path }),
                    Some(PolicyPermission::AcceptAndRemember) => {
                        let update_path = self.add_policy_for_operation(operation).await?;
                        Ok(PolicyDecision { allowed: true, path: update_path.or(path) })
                    }
                    Some(PolicyPermission::Reject) | None => {
                        Ok(PolicyDecision { allowed: false, path })
                    }
                }
            }
        }
    }
}

/// Create a policy for an operation based on its type
fn create_policy_for_operation(
    operation: &PermissionOperation,
    dir: Option<String>,
) -> Option<Policy> {
    fn create_file_policy(
        path: &std::path::Path,
        rule_constructor: fn(String) -> Rule,
    ) -> Option<Policy> {
        path.extension()
            .and_then(|ext| ext.to_str())
            .map(|extension| Policy::Simple {
                permission: Permission::Allow,
                rule: rule_constructor(format!("*.{extension}")),
            })
    }

    match operation {
        PermissionOperation::Read { path, cwd: _, message: _ } => {
            create_file_policy(path, |pattern| {
                Rule::Read(ReadRule { read: pattern, dir: None })
            })
        }
        PermissionOperation::Write { path, cwd: _, message: _ } => {
            create_file_policy(path, |pattern| {
                Rule::Write(WriteRule { write: pattern, dir: None })
            })
        }

        PermissionOperation::Fetch { url, cwd: _, message: _ } => {
            if let Ok(parsed_url) = url::Url::parse(url) {
                parsed_url.host_str().map(|host| Policy::Simple {
                    permission: Permission::Allow,
                    rule: Rule::Fetch(Fetch { url: format!("{host}*"), dir: None }),
                })
            } else {
                Some(Policy::Simple {
                    permission: Permission::Allow,
                    rule: Rule::Fetch(Fetch { url: url.to_string(), dir: None }),
                })
            }
        }
        PermissionOperation::Execute { command, cwd: _ } => {
            let parts: Vec<&str> = command.split_whitespace().collect();
            match parts.as_slice() {
                [] => None,
                [cmd] => Some(Policy::Simple {
                    permission: Permission::Allow,
                    rule: Rule::Execute(ExecuteRule { command: format!("{cmd}*"), dir }),
                }),
                [cmd, subcmd, ..] => Some(Policy::Simple {
                    permission: Permission::Allow,
                    rule: Rule::Execute(ExecuteRule { command: format!("{cmd} {subcmd}*"), dir }),
                }),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn test_create_policy_for_read_operation() {
        let path = PathBuf::from("/path/to/file.rs");
        let operation = PermissionOperation::Read {
            path,
            cwd: std::path::PathBuf::from("/test/cwd"),
            message: "Read file: /path/to/file.rs".to_string(),
        };

        let actual = create_policy_for_operation(&operation, None);

        let expected = Some(Policy::Simple {
            permission: Permission::Allow,
            rule: Rule::Read(ReadRule { read: "*.rs".to_string(), dir: None }),
        });

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_create_policy_for_write_operation() {
        let path = PathBuf::from("/path/to/file.json");
        let operation = PermissionOperation::Write {
            path,
            cwd: std::path::PathBuf::from("/test/cwd"),
            message: "Create/overwrite file: /path/to/file.json".to_string(),
        };

        let actual = create_policy_for_operation(&operation, None);

        let expected = Some(Policy::Simple {
            permission: Permission::Allow,
            rule: Rule::Write(WriteRule { write: "*.json".to_string(), dir: None }),
        });

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_create_policy_for_write_patch_operation() {
        let path = PathBuf::from("/path/to/file.toml");
        let operation = PermissionOperation::Write {
            path,
            cwd: std::path::PathBuf::from("/test/cwd"),
            message: "Modify file: /path/to/file.toml".to_string(),
        };

        let actual = create_policy_for_operation(&operation, None);

        let expected = Some(Policy::Simple {
            permission: Permission::Allow,
            rule: Rule::Write(WriteRule { write: "*.toml".to_string(), dir: None }),
        });

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_create_policy_for_net_fetch_operation() {
        let url = "https://example.com/api/data".to_string();
        let operation = PermissionOperation::Fetch {
            url,
            cwd: std::path::PathBuf::from("/test/cwd"),
            message: "Fetch content from URL: https://example.com/api/data".to_string(),
        };

        let actual = create_policy_for_operation(&operation, None);

        let expected = Some(Policy::Simple {
            permission: Permission::Allow,
            rule: Rule::Fetch(Fetch { url: "example.com*".to_string(), dir: None }),
        });

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_create_policy_for_execute_operation_with_subcommand() {
        let command = "git push origin main".to_string();
        let operation =
            PermissionOperation::Execute { command, cwd: std::path::PathBuf::from("/test/cwd") };

        let actual = create_policy_for_operation(&operation, None);

        let expected = Some(Policy::Simple {
            permission: Permission::Allow,
            rule: Rule::Execute(ExecuteRule { command: "git push*".to_string(), dir: None }),
        });

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_create_policy_for_execute_operation_single_command() {
        let command = "ls".to_string();
        let operation =
            PermissionOperation::Execute { command, cwd: std::path::PathBuf::from("/test/cwd") };

        let actual = create_policy_for_operation(&operation, None);

        let expected = Some(Policy::Simple {
            permission: Permission::Allow,
            rule: Rule::Execute(ExecuteRule { command: "ls*".to_string(), dir: None }),
        });

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_create_policy_for_file_without_extension() {
        let path = PathBuf::from("/path/to/file");
        let operation = PermissionOperation::Read {
            path,
            cwd: std::path::PathBuf::from("/test/cwd"),
            message: "Read file: /path/to/file".to_string(),
        };

        let actual = create_policy_for_operation(&operation, None);

        let expected = None;

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_create_policy_for_invalid_url() {
        let url = "not-a-valid-url".to_string();
        let operation = PermissionOperation::Fetch {
            url,
            cwd: std::path::PathBuf::from("/test/cwd"),
            message: "Fetch content from URL: not-a-valid-url".to_string(),
        };

        let actual = create_policy_for_operation(&operation, None);

        let expected = Some(Policy::Simple {
            permission: Permission::Allow,
            rule: Rule::Fetch(Fetch { url: "not-a-valid-url".to_string(), dir: None }),
        });

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_create_policy_for_empty_execute_command() {
        let command = "".to_string();
        let operation =
            PermissionOperation::Execute { command, cwd: std::path::PathBuf::from("/test/cwd") };

        let actual = create_policy_for_operation(&operation, None);

        let expected = None;

        assert_eq!(actual, expected);
    }

    #[test]
    fn test_create_policy_for_execute_operation_with_working_directory() {
        let command = "ls".to_string();
        let operation =
            PermissionOperation::Execute { command, cwd: std::path::PathBuf::from("/test/cwd") };
        let working_directory = Some("/home/user/project".to_string());

        let actual = create_policy_for_operation(&operation, working_directory.clone());

        let expected = Some(Policy::Simple {
            permission: Permission::Allow,
            rule: Rule::Execute(ExecuteRule { command: "ls*".to_string(), dir: working_directory }),
        });

        assert_eq!(actual, expected);
    }
}