bake-agent-context 0.4.0

Install context files from Cargo dependencies for coding agents
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
// Released under the MIT License.
// Copyright, 2026, by Samuel Williams.

#[cfg(test)]
use super::test_filesystem as fs;
use bake::{Error, Result};
use std::collections::BTreeSet;
use std::ffi::OsStr;
#[cfg(not(test))]
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

const BEGIN_MARKER: &str = "# BEGIN bake-agent-context";
const END_MARKER: &str = "# END bake-agent-context";

pub(crate) struct Update {
    path: PathBuf,
    original: String,
    updated: String,
}

impl Update {
    pub(crate) fn apply(&self) -> Result<()> {
        if self.original == self.updated {
            return Ok(());
        }

        if let Some(parent) = self.path.parent() {
            fs::create_dir_all(parent).map_err(|error| {
                Error::new(format!("cannot create {}: {error}", parent.display()))
            })?;
        }

        fs::write(&self.path, &self.updated)
            .map_err(|error| Error::new(format!("cannot update {}: {error}", self.path.display())))
    }
}

/// Prepare local Git excludes for generated context and installed skills.
///
/// These rules live in `.git/info/exclude`, so projects do not need to commit
/// ignore entries for generated local files.
pub(crate) fn prepare(
    root: &Path,
    skill_names: impl IntoIterator<Item = String>,
) -> Result<Option<Update>> {
    prepare_with_locator(root, skill_names, local_exclude_path)
}

#[cfg(all(test, unix))]
fn prepare_with_executable(
    root: &Path,
    skill_names: impl IntoIterator<Item = String>,
    executable: &OsStr,
) -> Result<Option<Update>> {
    prepare_with_locator(root, skill_names, |root| {
        local_exclude_path_with(root, executable)
    })
}

fn prepare_with_locator(
    root: &Path,
    skill_names: impl IntoIterator<Item = String>,
    locate: impl FnOnce(&Path) -> Result<Option<(PathBuf, String)>>,
) -> Result<Option<Update>> {
    prepare_from_path(locate(root)?, skill_names)
}

fn prepare_from_path(
    location: Option<(PathBuf, String)>,
    skill_names: impl IntoIterator<Item = String>,
) -> Result<Option<Update>> {
    let Some((path, prefix)) = location else {
        return Ok(None);
    };

    let skill_names: BTreeSet<_> = skill_names.into_iter().collect();
    Ok(Some(prepare_local_exclude(path, &prefix, &skill_names)?))
}

fn local_exclude_path(root: &Path) -> Result<Option<(PathBuf, String)>> {
    local_exclude_path_with(root, OsStr::new("git"))
}

fn local_exclude_path_with(root: &Path, executable: &OsStr) -> Result<Option<(PathBuf, String)>> {
    let output = match Command::new(executable)
        .args(["rev-parse", "--show-prefix", "--git-path", "info/exclude"])
        .current_dir(root)
        .output()
    {
        Ok(output) => output,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            return Err(Error::new(format!(
                "cannot locate Git exclude file: {error}"
            )));
        }
    };

    if !output.status.success() {
        // Context can still be installed from a source archive or other
        // directory which is not a Git checkout.
        return Ok(None);
    }

    exclude_path_from_output(root, output.status.success(), &output.stdout)
}

fn exclude_path_from_output(
    root: &Path,
    success: bool,
    stdout: &[u8],
) -> Result<Option<(PathBuf, String)>> {
    if !success {
        // Context can still be installed from a source archive or other
        // directory which is not a Git checkout.
        return Ok(None);
    }

    let path = String::from_utf8(stdout.to_vec())
        .map_err(|error| Error::new(format!("Git returned an invalid exclude path: {error}")))?;
    let (prefix, path) = path
        .split_once('\n')
        .ok_or_else(|| Error::new("Git returned an invalid project prefix and exclude path"))?;
    let path = PathBuf::from(path.trim());
    let path = if path.is_absolute() {
        path
    } else {
        root.join(path)
    };
    Ok(Some((path, prefix.trim_end_matches('\r').to_owned())))
}

fn prepare_local_exclude(
    path: PathBuf,
    prefix: &str,
    skill_names: &BTreeSet<String>,
) -> Result<Update> {
    let original = match fs::read_to_string(&path) {
        Ok(contents) => contents,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => String::new(),
        Err(error) => {
            return Err(Error::new(format!(
                "cannot read {}: {error}",
                path.display()
            )));
        }
    };

    let mut lines = remove_managed_block(&original, &path, prefix)?
        .lines()
        .map(str::to_owned)
        .collect::<Vec<_>>();
    if !lines.is_empty() {
        lines.push(String::new());
    }

    let mut pattern = prefix.to_owned();
    for character in ['\\', '*', '?', '[', ']'] {
        pattern = pattern.replace(character, &format!("\\{character}"));
    }
    let mut block = vec![
        format!("{BEGIN_MARKER} {prefix}").trim_end().to_owned(),
        "# Generated by Bake Agent Context; do not edit this block manually.".to_owned(),
        format!("/{pattern}.agents/context/"),
    ];
    block.extend(
        skill_names
            .iter()
            .map(|name| format!("/{pattern}.agents/skills/{name}/")),
    );
    block.push(format!("{END_MARKER} {prefix}").trim_end().to_owned());

    lines.extend(block);
    let mut updated = lines.join("\n");
    updated.push('\n');

    Ok(Update {
        path,
        original,
        updated,
    })
}

fn remove_managed_block(contents: &str, path: &Path, prefix: &str) -> Result<String> {
    let begin_marker = format!("{BEGIN_MARKER} {prefix}");
    let end_marker = format!("{END_MARKER} {prefix}");
    let mut lines: Vec<String> = contents.lines().map(str::to_owned).collect();
    let begin_positions: Vec<_> = lines
        .iter()
        .enumerate()
        .filter(|(_, line)| line.trim() == begin_marker.trim_end())
        .map(|(index, _)| index)
        .collect();
    let end_positions: Vec<_> = lines
        .iter()
        .enumerate()
        .filter(|(_, line)| line.trim() == end_marker.trim_end())
        .map(|(index, _)| index)
        .collect();

    match (begin_positions.as_slice(), end_positions.as_slice()) {
        ([], []) => {}
        ([begin], [end]) if begin < end => {
            lines.drain(*begin..=*end);
        }
        _ => {
            return Err(Error::new(format!(
                "{} has an incomplete or duplicated bake-agent-context ignore block",
                path.display()
            )));
        }
    }

    while lines.last().is_some_and(|line| line.is_empty()) {
        lines.pop();
    }

    if lines.is_empty() {
        Ok(String::new())
    } else {
        let mut output = lines.join("\n");
        output.push('\n');
        Ok(output)
    }
}

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

    #[test]
    fn update_apply_is_idempotent_and_reports_parent_and_write_errors() {
        let directory = tempdir().unwrap();
        let unchanged = directory.path().join("unchanged");
        Update {
            path: unchanged.clone(),
            original: "same".to_owned(),
            updated: "same".to_owned(),
        }
        .apply()
        .unwrap();
        assert!(!unchanged.exists());

        let parent_file = directory.path().join("file");
        fs::write(&parent_file, "not a directory").unwrap();
        let error = Update {
            path: parent_file.join("exclude"),
            original: String::new(),
            updated: "generated\n".to_owned(),
        }
        .apply()
        .unwrap_err();
        assert!(error.to_string().contains("cannot create"));

        let destination = directory.path().join("directory");
        fs::create_dir(&destination).unwrap();
        let error = Update {
            path: destination.clone(),
            original: "old".to_owned(),
            updated: "new".to_owned(),
        }
        .apply()
        .unwrap_err();
        assert!(error.to_string().contains("cannot update"));

        let error = Update {
            path: PathBuf::new(),
            original: "old".to_owned(),
            updated: "new".to_owned(),
        }
        .apply()
        .unwrap_err();
        assert!(error.to_string().contains("cannot update"));
    }

    #[test]
    fn resolves_git_output_and_rejects_invalid_utf8() {
        let directory = tempdir().unwrap();
        let root = directory.path();

        assert_eq!(
            exclude_path_from_output(root, false, b"ignored").unwrap(),
            None
        );
        assert_eq!(
            exclude_path_from_output(root, true, b"\n.git/info/exclude\n").unwrap(),
            Some((root.join(".git/info/exclude"), String::new()))
        );
        let absolute = root.join("exclude");
        assert_eq!(
            exclude_path_from_output(
                root,
                true,
                format!("app/\n{}\n", absolute.display()).as_bytes()
            )
            .unwrap(),
            Some((absolute, "app/".to_owned()))
        );
        assert!(exclude_path_from_output(root, true, &[0xff]).is_err());
        assert!(exclude_path_from_output(root, true, b"missing newline").is_err());
    }

    #[test]
    fn prepares_and_applies_managed_rules_without_losing_user_rules() {
        let directory = tempdir().unwrap();
        let path = directory.path().join("exclude");
        let original = "# user rule\n*.local\n\n";
        fs::write(&path, original).unwrap();

        let update = prepare_local_exclude(
            path.clone(),
            "",
            &BTreeSet::from(["zeta-skill".to_owned(), "alpha-skill".to_owned()]),
        )
        .unwrap();
        update.apply().unwrap();
        let result = fs::read_to_string(&path).unwrap();
        assert!(result.starts_with("# user rule\n*.local\n\n# BEGIN bake-agent-context\n"));
        assert!(result.find("alpha-skill").unwrap() < result.find("zeta-skill").unwrap());

        let repeated = prepare_local_exclude(
            path.clone(),
            "",
            &BTreeSet::from(["alpha-skill".to_owned(), "zeta-skill".to_owned()]),
        )
        .unwrap();
        repeated.apply().unwrap();
        assert_eq!(fs::read_to_string(path).unwrap(), result);
    }

    #[test]
    fn handles_empty_exclude_files_and_rejects_broken_managed_blocks() {
        let directory = tempdir().unwrap();
        let path = directory.path().join("exclude");

        let empty = prepare_local_exclude(path.clone(), "", &BTreeSet::new()).unwrap();
        empty.apply().unwrap();
        let just_block = fs::read_to_string(&path).unwrap();
        assert!(just_block.starts_with(BEGIN_MARKER));

        for contents in [
            BEGIN_MARKER.to_owned(),
            END_MARKER.to_owned(),
            format!("{END_MARKER}\n{BEGIN_MARKER}\n"),
            format!("{BEGIN_MARKER}\n{END_MARKER}\n{BEGIN_MARKER}\n{END_MARKER}\n"),
        ] {
            assert!(
                remove_managed_block(&contents, &path, "").is_err(),
                "{contents:?}"
            );
        }
        assert_eq!(
            remove_managed_block(&format!("{BEGIN_MARKER}\n{END_MARKER}\n"), &path, "").unwrap(),
            ""
        );
        assert_eq!(
            remove_managed_block("user\n\n", &path, "").unwrap(),
            "user\n"
        );
    }

    #[test]
    fn scopes_exclusions_to_each_project_in_a_repository() {
        let directory = tempdir().unwrap();
        let root = directory.path();
        assert!(
            Command::new("git")
                .args(["init", "--quiet"])
                .current_dir(root)
                .status()
                .unwrap()
                .success()
        );
        for relative in ["", "apps/web [dev]", "apps/worker"] {
            let project = root.join(relative);
            fs::create_dir_all(&project).unwrap();
            prepare(&project, ["provider-old".to_owned()])
                .unwrap()
                .unwrap()
                .apply()
                .unwrap();
        }
        prepare(&root.join("apps/web [dev]"), ["provider-new".to_owned()])
            .unwrap()
            .unwrap()
            .apply()
            .unwrap();
        prepare(root, ["provider-old".to_owned()])
            .unwrap()
            .unwrap()
            .apply()
            .unwrap();

        for (path, ignored) in [
            (".agents/skills/provider-old/SKILL.md", true),
            ("apps/worker/.agents/skills/provider-old/SKILL.md", true),
            ("apps/web [dev]/.agents/context/provider/guide.md", true),
            ("apps/web [dev]/.agents/skills/provider-new/SKILL.md", true),
            ("apps/web [dev]/.agents/skills/provider-old/SKILL.md", false),
            (
                "apps/web [dev]/.agents/skills/project-local/SKILL.md",
                false,
            ),
            ("apps/web d/.agents/skills/provider-new/SKILL.md", false),
        ] {
            let status = Command::new("git")
                .args(["check-ignore", "--quiet", path])
                .current_dir(root)
                .status()
                .unwrap();
            assert_eq!(status.success(), ignored, "{path}");
        }
    }

    #[test]
    fn reports_exclude_read_errors() {
        let directory = tempdir().unwrap();
        let path = directory.path().join("exclude-directory");
        fs::create_dir(&path).unwrap();

        let error = prepare_local_exclude(path, "", &BTreeSet::new())
            .err()
            .unwrap();
        assert!(error.to_string().contains("cannot read"));
    }

    #[test]
    fn propagates_git_lookup_and_managed_block_errors_from_prepare() {
        let directory = tempdir().unwrap();
        #[cfg(unix)]
        {
            let executable = directory.path().join("not-executable");
            fs::write(&executable, "not a command").unwrap();
            let error = prepare_with_executable(
                directory.path(),
                Vec::<String>::new(),
                executable.as_os_str(),
            )
            .err()
            .unwrap();
            assert!(error.to_string().contains("cannot locate Git exclude file"));
        }

        let error = prepare_with_locator(directory.path(), Vec::<String>::new(), |_| {
            Err(Error::new("injected Git lookup failure"))
        })
        .err()
        .unwrap();
        assert!(error.to_string().contains("injected Git lookup failure"));

        let repository = tempdir().unwrap();
        let output = Command::new("git")
            .args(["init", "--quiet"])
            .current_dir(repository.path())
            .output()
            .unwrap();
        assert!(output.status.success());
        fs::write(repository.path().join(".git/info/exclude"), BEGIN_MARKER).unwrap();
        assert!(prepare(repository.path(), Vec::<String>::new()).is_err());
    }

    #[cfg(unix)]
    #[test]
    fn handles_git_command_start_errors() {
        let directory = tempdir().unwrap();
        assert_eq!(
            local_exclude_path_with(directory.path(), OsStr::new("missing-git-executable"))
                .unwrap(),
            None
        );
        let error = local_exclude_path_with(directory.path(), directory.path().as_os_str())
            .err()
            .unwrap();
        assert!(error.to_string().contains("cannot locate Git exclude file"));
    }
}