attini 0.0.1

CLI coding agent that aims to be as autonomous as it can be, without ever leaving your control
Documentation
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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Filesystem-facing side of the permission system: load rules from
//! `.attini/{NAME}/permissions.jsonl` (session-local) and
//! `.attini/permissions.jsonl` (workspace-wide), and implement the
//! rule append used by `attini approve --grant`.
//!
//! The on-disk format is **JSONL**: one JSON object per line, one rule
//! per line. Lines beginning with `#` are comments; blank lines are
//! ignored. Each rule has a fixed field order (`type` -> `allow` ->
//! type-specific) and an explicit `allow` boolean. The layer a rule
//! belongs to is expressed by which file it lives in, not by a field.
//! See `docs/design/permissions-file.md`.

use std::collections::BTreeSet;
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};

use nojson::{DisplayJson, JsonFormatter, RawJson, RawJsonValue};

use crate::sansio::permissions::{PermissionKind, Rule};
use crate::session::{SessionPaths, session_paths, session_root};

pub const PERMISSIONS_FILENAME: &str = "permissions.jsonl";

pub struct LoadedRules {
    /// Rules from the workspace-wide file, in file order.
    pub workspace: Vec<Rule>,
    /// Rules from the session-local file, in file order.
    pub session: Vec<Rule>,
    /// Union of `read` rules from both tiers, deduplicated and sorted.
    /// Callers typically pass the resolved paths to `ToolExecutor::new`
    /// after canonicalising each entry.
    pub extra_read_paths: Vec<String>,
}

pub fn workspace_permissions_path() -> PathBuf {
    session_root().join(PERMISSIONS_FILENAME)
}

pub fn session_permissions_path(paths: &SessionPaths) -> PathBuf {
    paths.dir.join(PERMISSIONS_FILENAME)
}

/// Load session-local + workspace-wide permissions. Missing files ->
/// empty. A whole-file read failure is reported and treated as empty.
/// An individual malformed line is reported and skipped; the rest of
/// the file still loads.
pub fn load(session_name: &str) -> io::Result<LoadedRules> {
    let paths = session_paths(session_name)?;
    let workspace = load_rules_from_path(&workspace_permissions_path(), "workspace");
    let session = load_rules_from_path(&session_permissions_path(&paths), "session");
    let mut merged: BTreeSet<String> = BTreeSet::new();
    for rule in workspace.iter().chain(session.iter()) {
        if rule.kind == PermissionKind::Read && rule.allow {
            merged.insert(rule.path.clone());
        }
    }
    Ok(LoadedRules {
        workspace,
        session,
        extra_read_paths: merged.into_iter().collect(),
    })
}

fn load_rules_from_path(path: &Path, scope_label: &str) -> Vec<Rule> {
    let text = match fs::read_to_string(path) {
        Ok(s) => s,
        Err(e) if e.kind() == io::ErrorKind::NotFound => return Vec::new(),
        Err(e) => {
            eprintln!(
                "attini: cannot read {scope_label} permissions {}: {e}",
                path.display()
            );
            return Vec::new();
        }
    };
    parse_jsonl(&text, path, scope_label)
}

/// Parse a JSONL permissions file into rules. `#` comments and blank
/// lines are skipped; a malformed line is reported and skipped.
fn parse_jsonl(text: &str, path: &Path, scope_label: &str) -> Vec<Rule> {
    let mut rules = Vec::new();
    for (i, line) in text.lines().enumerate() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        match parse_rule_line(trimmed) {
            Ok(rule) => rules.push(rule),
            Err(reason) => eprintln!(
                "attini: {scope_label} permissions {} line {}: skipping ({reason})",
                path.display(),
                i + 1
            ),
        }
    }
    rules
}

fn parse_rule_line(line: &str) -> Result<Rule, String> {
    let json = RawJson::parse(line).map_err(|e| e.to_string())?;
    let value = json.value();
    let type_str = required_string(value, "type")?;
    match type_str.as_str() {
        "command" => {
            let allow = required_bool(value, "allow")?;
            let args_prefix = required_string_array(value, "args_prefix")?;
            if args_prefix.is_empty() {
                return Err("args_prefix is empty".to_string());
            }
            if args_prefix.iter().any(|s| s.is_empty()) {
                return Err("args_prefix contains an empty element".to_string());
            }
            Ok(Rule::command(allow, args_prefix))
        }
        "read" => {
            let path = required_string(value, "path")?;
            if path.trim().is_empty() {
                return Err("path is empty".to_string());
            }
            // Read rules are allow-only: `allow` may be omitted (or
            // `true`), but `allow:false` is rejected because a read deny
            // cannot be enforced (the model can always read via the
            // `command` tool).
            match optional_bool(value, "allow")? {
                None | Some(true) => Ok(Rule::read(path)),
                Some(false) => {
                    Err("read rules are allow-only; `allow:false` is not supported".to_string())
                }
            }
        }
        "write" => {
            let allow = required_bool(value, "allow")?;
            let path = required_string(value, "path")?;
            if path.trim().is_empty() {
                return Err("path is empty".to_string());
            }
            Ok(Rule::write(allow, path))
        }
        other => Err(format!(
            "unknown type {other:?} (expected \"command\", \"read\" or \"write\")"
        )),
    }
}

fn required_string(value: RawJsonValue<'_, '_>, key: &str) -> Result<String, String> {
    let member = value
        .to_member(key)
        .and_then(|m| m.required())
        .map_err(|e| format!("{key}: {e}"))?;
    member
        .to_unquoted_string_str()
        .map(|s| s.into_owned())
        .map_err(|e| format!("{key}: {e}"))
}

fn required_bool(value: RawJsonValue<'_, '_>, key: &str) -> Result<bool, String> {
    let member = value
        .to_member(key)
        .and_then(|m| m.required())
        .map_err(|e| format!("{key}: {e}"))?;
    let raw = member.as_boolean_str().map_err(|e| format!("{key}: {e}"))?;
    match raw {
        "true" => Ok(true),
        "false" => Ok(false),
        other => Err(format!("{key}: expected a boolean (got {other})")),
    }
}

/// Read an optional boolean member. Returns `Ok(None)` when the key is
/// absent, `Ok(Some(b))` when present and boolean, and an error when
/// the key is present but not a boolean.
fn optional_bool(value: RawJsonValue<'_, '_>, key: &str) -> Result<Option<bool>, String> {
    let member = match value.to_member(key).and_then(|m| m.required()) {
        Ok(m) => m,
        Err(_) => return Ok(None),
    };
    let raw = member.as_boolean_str().map_err(|e| format!("{key}: {e}"))?;
    match raw {
        "true" => Ok(Some(true)),
        "false" => Ok(Some(false)),
        other => Err(format!("{key}: expected a boolean (got {other})")),
    }
}

fn required_string_array(value: RawJsonValue<'_, '_>, key: &str) -> Result<Vec<String>, String> {
    let member = value
        .to_member(key)
        .and_then(|m| m.required())
        .map_err(|e| format!("{key}: {e}"))?;
    let array = member.to_array().map_err(|e| format!("{key}: {e}"))?;
    let mut out = Vec::new();
    for item in array {
        let s: String = item
            .to_unquoted_string_str()
            .map_err(|e| format!("{key}: {e}"))?
            .into_owned();
        out.push(s);
    }
    Ok(out)
}

// -------------------------------------------------------------------
// Writable representation (JSONL appends)
// -------------------------------------------------------------------

impl DisplayJson for Rule {
    fn fmt(&self, f: &mut JsonFormatter<'_, '_>) -> std::fmt::Result {
        f.object(|f| {
            f.member("type", self.kind.as_str())?;
            // Read rules are allow-only, so `allow` is omitted (implied
            // `true`). Command and write rules always carry `allow`.
            if self.kind != PermissionKind::Read {
                f.member("allow", self.allow)?;
            }
            match self.kind {
                PermissionKind::Command => {
                    f.member("args_prefix", &self.args_prefix)?;
                }
                PermissionKind::Read => {
                    f.member("path", &self.path)?;
                }
                PermissionKind::Write => {
                    f.member("path", &self.path)?;
                }
            }
            Ok(())
        })
    }
}

fn render_rule_line(rule: &Rule) -> String {
    // `Json(...)` renders one compact JSON object; prefix with a newline
    // when appending.
    nojson::Json(rule).to_string()
}

// -------------------------------------------------------------------
// grant (command prefix)
// -------------------------------------------------------------------

pub enum GrantOutcome {
    Appended(PathBuf),
    AlreadyGranted(PathBuf),
}

#[derive(Debug)]
pub enum GrantError {
    ArgsEmpty,
    SessionMissing(PathBuf),
    ReadError(PathBuf, io::Error),
    ExistingDenyConflict(PathBuf, Vec<String>),
    ExistingWriteDenyConflict(PathBuf, String),
    Io(io::Error),
}

impl From<io::Error> for GrantError {
    fn from(e: io::Error) -> Self {
        Self::Io(e)
    }
}

impl std::fmt::Display for GrantError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ArgsEmpty => write!(f, "args_prefix is empty or contains an empty element"),
            Self::SessionMissing(p) => write!(f, "session directory not found: {}", p.display()),
            Self::ReadError(p, e) => write!(f, "cannot read {}: {e}", p.display()),
            Self::ExistingDenyConflict(p, args_prefix) => write!(
                f,
                "command rule for args_prefix {args_prefix:?} already exists as `allow:false` in {}; edit manually to resolve",
                p.display()
            ),
            Self::ExistingWriteDenyConflict(p, path) => write!(
                f,
                "write rule for path {path:?} already exists as `allow:false` in {}; edit manually to resolve",
                p.display()
            ),
            Self::Io(e) => write!(f, "{e}"),
        }
    }
}

pub enum GrantScope<'a> {
    Session(&'a str),
    Workspace,
}

fn resolve_target(scope: GrantScope<'_>) -> Result<PathBuf, GrantError> {
    match scope {
        GrantScope::Session(name) => {
            let paths = session_paths(name).map_err(GrantError::Io)?;
            if !paths.dir.try_exists()? {
                return Err(GrantError::SessionMissing(paths.dir.clone()));
            }
            Ok(session_permissions_path(&paths))
        }
        GrantScope::Workspace => {
            let root = session_root();
            if !root.try_exists()? {
                fs::create_dir_all(&root)?;
            }
            Ok(workspace_permissions_path())
        }
    }
}

pub fn grant(scope: GrantScope<'_>, args_prefix: &[String]) -> Result<GrantOutcome, GrantError> {
    if args_prefix.is_empty() || args_prefix.iter().any(|s| s.is_empty()) {
        return Err(GrantError::ArgsEmpty);
    }
    let args_prefix: Vec<String> = args_prefix.to_vec();
    let target = resolve_target(scope)?;
    let existing = load_rules_from_path(&target, "grant");
    for rule in &existing {
        if rule.kind != PermissionKind::Command {
            continue;
        }
        if rule.args_prefix != args_prefix {
            continue;
        }
        return match rule.allow {
            true => Ok(GrantOutcome::AlreadyGranted(target)),
            false => Err(GrantError::ExistingDenyConflict(target, args_prefix)),
        };
    }
    let rule = Rule::command(true, args_prefix);
    append_rule_line(&target, &rule)?;
    Ok(GrantOutcome::Appended(target))
}

/// Persist a `read` rule for `path` at the given scope. Mirrors
/// [`grant`] but for the path matcher: `read` rules are allow-only, so
/// an existing identical rule is a no-op and a fresh rule is appended
/// (there is no read deny to conflict with).
pub fn grant_read(scope: GrantScope<'_>, path: &str) -> Result<GrantOutcome, GrantError> {
    if path.trim().is_empty() {
        return Err(GrantError::ArgsEmpty);
    }
    let target = resolve_target(scope)?;
    let existing = load_rules_from_path(&target, "grant");
    for rule in &existing {
        if rule.kind != PermissionKind::Read {
            continue;
        }
        if rule.path != path {
            continue;
        }
        return Ok(GrantOutcome::AlreadyGranted(target));
    }
    let rule = Rule::read(path.to_string());
    append_rule_line(&target, &rule)?;
    Ok(GrantOutcome::Appended(target))
}

/// Persist an `allow:true` `write` rule for `path` at the given scope.
/// Mirrors [`grant`] and [`grant_read`] but for the write path matcher:
/// an existing identical `allow:true` rule is a no-op, an existing
/// identical `allow:false` rule is a conflict (edit manually), and a
/// fresh rule is appended.
pub fn grant_write(scope: GrantScope<'_>, path: &str) -> Result<GrantOutcome, GrantError> {
    if path.trim().is_empty() {
        return Err(GrantError::ArgsEmpty);
    }
    let target = resolve_target(scope)?;
    let existing = load_rules_from_path(&target, "grant");
    for rule in &existing {
        if rule.kind != PermissionKind::Write {
            continue;
        }
        if rule.path != path {
            continue;
        }
        return match rule.allow {
            true => Ok(GrantOutcome::AlreadyGranted(target)),
            false => Err(GrantError::ExistingWriteDenyConflict(
                target,
                path.to_string(),
            )),
        };
    }
    let rule = Rule::write(true, path.to_string());
    append_rule_line(&target, &rule)?;
    Ok(GrantOutcome::Appended(target))
}

// -------------------------------------------------------------------
// Atomic append helper
// -------------------------------------------------------------------

/// Append a rendered rule as a new line, atomically (read whole file,
/// append, write tmp, rename). Appending rather than rewriting keeps
/// any hand-written comments in the file intact except that the file is
/// rewritten byte-for-byte with one extra trailing line.
fn append_rule_line(path: &Path, rule: &Rule) -> io::Result<()> {
    let mut body = match fs::read_to_string(path) {
        Ok(s) => s,
        Err(e) if e.kind() == io::ErrorKind::NotFound => String::new(),
        Err(e) => return Err(e),
    };
    if !body.is_empty() && !body.ends_with('\n') {
        body.push('\n');
    }
    body.push_str(&render_rule_line(rule));
    body.push('\n');
    atomic_write(path, body.as_bytes())
}

fn atomic_write(path: &Path, content: &[u8]) -> io::Result<()> {
    let tmp = tmp_path_for(path);
    {
        let mut file = fs::File::create(&tmp)?;
        file.write_all(content)?;
        file.sync_all()?;
    }
    fs::rename(&tmp, path)
}

fn tmp_path_for(target: &Path) -> PathBuf {
    let pid = std::process::id();
    let mut buf = target.as_os_str().to_os_string();
    buf.push(format!(".grant-tmp-{pid}"));
    PathBuf::from(buf)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn tempdir(name: &str) -> PathBuf {
        let base = std::env::temp_dir().join(format!(
            "attini-permissions-test-{}-{}",
            name,
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&base);
        fs::create_dir_all(&base).expect("mkdir");
        base
    }

    #[test]
    fn parse_jsonl_reads_command_and_read_rules_skipping_comments() {
        let text = r#"
# allow cargo test
{"type":"command","allow":true,"args_prefix":["cargo","test"]}

# deny rm
{"type":"command","allow":false,"args_prefix":["rm"]}
{"type":"read","allow":true,"path":"../docs/"}
"#;
        let dir = tempdir("jsonl_basic");
        let path = dir.join(PERMISSIONS_FILENAME);
        let rules = parse_jsonl(text, &path, "workspace");
        assert_eq!(rules.len(), 3);
        assert_eq!(rules[0].kind, PermissionKind::Command);
        assert!(rules[0].allow);
        assert_eq!(
            rules[0].args_prefix,
            vec!["cargo".to_string(), "test".to_string()]
        );
        assert_eq!(rules[1].kind, PermissionKind::Command);
        assert!(!rules[1].allow);
        assert_eq!(rules[2].kind, PermissionKind::Read);
        assert_eq!(rules[2].path, "../docs/");
    }

    #[test]
    fn parse_jsonl_skips_malformed_line_and_keeps_rest() {
        let text = r#"{"type":"command","allow":true,"args_prefix":["ls"]}
not json
{"type":"command","allow":true,"args_prefix":["cat"]}
"#;
        let dir = tempdir("jsonl_bad_line");
        let path = dir.join(PERMISSIONS_FILENAME);
        let rules = parse_jsonl(text, &path, "workspace");
        assert_eq!(rules.len(), 2);
        assert_eq!(rules[0].args_prefix, vec!["ls".to_string()]);
        assert_eq!(rules[1].args_prefix, vec!["cat".to_string()]);
    }

    #[test]
    fn rule_without_allow_is_skipped() {
        let text = r#"{"type":"command","args_prefix":["ls"]}
"#;
        let dir = tempdir("jsonl_no_allow");
        let path = dir.join(PERMISSIONS_FILENAME);
        let rules = parse_jsonl(text, &path, "workspace");
        assert!(rules.is_empty());
    }

    #[test]
    fn read_rule_may_omit_allow() {
        let text = r#"{"type":"read","path":"../docs/"}
"#;
        let dir = tempdir("jsonl_read_no_allow");
        let path = dir.join(PERMISSIONS_FILENAME);
        let rules = parse_jsonl(text, &path, "workspace");
        assert_eq!(rules.len(), 1);
        assert_eq!(rules[0].kind, PermissionKind::Read);
        assert!(rules[0].allow);
        assert_eq!(rules[0].path, "../docs/");
    }

    #[test]
    fn read_rule_with_allow_false_is_skipped() {
        // Read rules are allow-only; `allow:false` is not supported and
        // the line is rejected (reported and skipped).
        let text = r#"{"type":"read","allow":false,"path":"secret/"}
"#;
        let dir = tempdir("jsonl_read_deny");
        let path = dir.join(PERMISSIONS_FILENAME);
        let rules = parse_jsonl(text, &path, "workspace");
        assert!(rules.is_empty());
    }

    #[test]
    fn unknown_type_is_skipped() {
        let text = r#"{"type":"bogus","allow":true,"path":"src/"}
"#;
        let dir = tempdir("jsonl_unknown_type");
        let path = dir.join(PERMISSIONS_FILENAME);
        let rules = parse_jsonl(text, &path, "workspace");
        assert!(rules.is_empty());
    }

    #[test]
    fn write_rule_parses_and_round_trips() {
        let text = r#"{"type":"write","allow":true,"path":"src/"}
{"type":"write","allow":false,"path":"src/generated"}
"#;
        let dir = tempdir("jsonl_write");
        let path = dir.join(PERMISSIONS_FILENAME);
        let rules = parse_jsonl(text, &path, "workspace");
        assert_eq!(rules.len(), 2);
        assert_eq!(rules[0].kind, PermissionKind::Write);
        assert!(rules[0].allow);
        assert_eq!(rules[0].path, "src/");
        assert_eq!(rules[1].kind, PermissionKind::Write);
        assert!(!rules[1].allow);
        assert_eq!(
            render_rule_line(&rules[0]),
            r#"{"type":"write","allow":true,"path":"src/"}"#
        );
    }

    #[test]
    fn render_rule_line_matches_canonical_field_order() {
        let cmd = Rule::command(true, vec!["cargo".to_string(), "test".to_string()]);
        assert_eq!(
            render_rule_line(&cmd),
            r#"{"type":"command","allow":true,"args_prefix":["cargo","test"]}"#
        );
        // Read rules are allow-only: `allow` is omitted from the line.
        let read = Rule::read("secret/".to_string());
        assert_eq!(
            render_rule_line(&read),
            r#"{"type":"read","path":"secret/"}"#
        );
        let write = Rule::write(true, "src/".to_string());
        assert_eq!(
            render_rule_line(&write),
            r#"{"type":"write","allow":true,"path":"src/"}"#
        );
    }

    #[test]
    fn append_rule_line_preserves_comments_and_appends_one_line() {
        let dir = tempdir("append_preserve");
        let path = dir.join(PERMISSIONS_FILENAME);
        fs::write(
            &path,
            "# my rules\n{\"type\":\"command\",\"allow\":true,\"args_prefix\":[\"ls\"]}\n",
        )
        .expect("write");
        let rule = Rule::command(true, vec!["cargo".to_string(), "test".to_string()]);
        append_rule_line(&path, &rule).expect("append");
        let written = fs::read_to_string(&path).expect("read back");
        assert!(written.starts_with("# my rules\n"));
        assert!(written.contains(r#"{"type":"command","allow":true,"args_prefix":["ls"]}"#));
        assert!(written.ends_with(
            r#"{"type":"command","allow":true,"args_prefix":["cargo","test"]}
"#
        ));
    }

    #[test]
    fn load_merges_read_paths_from_both_tiers() {
        // Directly exercise the union logic without touching the real
        // `.attini/` tree: build the LoadedRules shape by hand.
        let mut set: BTreeSet<String> = BTreeSet::new();
        set.insert("../docs/".to_string());
        set.insert("/opt/shared".to_string());
        set.insert("../docs/".to_string());
        assert_eq!(set.len(), 2);
    }
}