git-paw 0.3.0

Parallel AI Worktrees — orchestrate multiple AI coding CLI sessions across git worktrees
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
//! OpenSpec-format backend for spec scanning.
//!
//! Scans `openspec/changes/` for pending changes, extracts prompt content
//! from `tasks.md` and supplementary `specs/` files, and parses optional
//! frontmatter fields and file ownership declarations.

use std::fmt::Write;
use std::fs;
use std::path::Path;

use crate::error::PawError;
use crate::specs::{SpecBackend, SpecEntry};

/// Backend for the `OpenSpec` directory-based spec format.
///
/// Each pending change lives in its own subdirectory under the scanned
/// directory. The `tasks.md` file provides the primary prompt, and any
/// `specs/<capability>/spec.md` files are appended as supplementary content.
#[derive(Debug)]
pub struct OpenSpecBackend;

impl SpecBackend for OpenSpecBackend {
    fn scan(&self, dir: &Path) -> Result<Vec<SpecEntry>, PawError> {
        let entries = fs::read_dir(dir).map_err(|e| {
            PawError::SpecError(format!("cannot read directory {}: {e}", dir.display()))
        })?;

        let mut specs = Vec::new();

        for entry in entries {
            let entry = entry
                .map_err(|e| PawError::SpecError(format!("error reading directory entry: {e}")))?;

            let path = entry.path();
            if !path.is_dir() {
                continue;
            }

            let name = entry.file_name();
            let id = name.to_string_lossy().to_string();

            // Skip archive directory.
            if id == "archive" {
                continue;
            }

            let tasks_path = path.join("tasks.md");
            if !tasks_path.exists() {
                eprintln!("warning: skipping change {id}: no tasks.md found");
                continue;
            }

            let tasks_content = fs::read_to_string(&tasks_path).map_err(|e| {
                PawError::SpecError(format!("cannot read {}: {e}", tasks_path.display()))
            })?;

            let (frontmatter, body) = parse_frontmatter(&tasks_content);
            let cli = frontmatter
                .iter()
                .find(|(k, _)| k == "paw_cli")
                .map(|(_, v)| v.clone());

            let owned_files = extract_owned_files(&tasks_content);

            let mut prompt = body.to_string();

            // Append supplementary specs if present.
            let specs_dir = path.join("specs");
            if specs_dir.is_dir()
                && let Ok(spec_entries) = fs::read_dir(&specs_dir)
            {
                let mut cap_dirs: Vec<_> = spec_entries
                    .filter_map(Result::ok)
                    .filter(|e| e.path().is_dir())
                    .collect();
                cap_dirs.sort_by_key(std::fs::DirEntry::file_name);

                for cap_entry in cap_dirs {
                    let spec_file = cap_entry.path().join("spec.md");
                    if spec_file.exists() {
                        let cap_name = cap_entry.file_name().to_string_lossy().to_string();
                        if let Ok(spec_content) = fs::read_to_string(&spec_file) {
                            let _ = write!(prompt, "\n\n## Spec: {cap_name}\n\n{spec_content}");
                        }
                    }
                }
            }

            specs.push(SpecEntry {
                id,
                branch: String::new(), // Filled in by scan_specs.
                cli,
                prompt,
                owned_files,
            });
        }

        Ok(specs)
    }
}

/// Parses YAML-style frontmatter delimited by `---` lines.
///
/// Returns a list of key-value pairs and the remaining content after
/// the closing `---`. If no frontmatter is present, returns an empty
/// list and the original content.
fn parse_frontmatter(content: &str) -> (Vec<(String, String)>, &str) {
    let trimmed = content.trim_start();
    if !trimmed.starts_with("---") {
        return (vec![], content);
    }

    // Find the opening delimiter and start searching for the closing one.
    let after_open = match trimmed.strip_prefix("---") {
        Some(rest) => rest.trim_start_matches('-'),
        None => return (vec![], content),
    };

    // Skip the newline after the opening ---.
    let after_open = after_open.strip_prefix('\n').unwrap_or(after_open);

    let Some(close_pos) = after_open.find("\n---") else {
        return (vec![], content);
    };

    let front = &after_open[..close_pos];
    let rest_start = close_pos + 4; // skip "\n---"
    let rest = &after_open[rest_start..];
    // Skip the newline after the closing ---.
    let rest = rest.strip_prefix('\n').unwrap_or(rest);

    let mut fields = Vec::new();
    for line in front.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        if let Some((key, value)) = line.split_once(':') {
            fields.push((key.trim().to_string(), value.trim().to_string()));
        }
    }

    (fields, rest)
}

/// Extracts file ownership declarations from content.
///
/// Looks for a line containing `Files owned:` or `Owned files:` (case-insensitive)
/// followed by a markdown list of file paths (lines starting with `- `).
fn extract_owned_files(content: &str) -> Option<Vec<String>> {
    let lower = content.to_lowercase();
    let pattern_pos = lower
        .find("files owned:")
        .or_else(|| lower.find("owned files:"))?;

    // Find the end of the header line.
    let after_header = &content[pattern_pos..];
    let newline_pos = after_header.find('\n')?;
    let list_start = &after_header[newline_pos + 1..];

    let mut files = Vec::new();
    for line in list_start.lines() {
        let trimmed = line.trim();
        if let Some(path) = trimmed.strip_prefix("- ") {
            files.push(path.trim().trim_matches('`').to_string());
        } else if !trimmed.is_empty() {
            break;
        }
    }

    if files.is_empty() { None } else { Some(files) }
}

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

    // --- parse_frontmatter tests ---

    #[test]
    fn frontmatter_with_paw_cli() {
        let content = "---\npaw_cli: gemini\n---\nBody here";
        let (fields, body) = parse_frontmatter(content);
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0], ("paw_cli".to_string(), "gemini".to_string()));
        assert_eq!(body, "Body here");
    }

    #[test]
    fn frontmatter_without_paw_cli() {
        let content = "---\ntitle: my change\n---\nBody here";
        let (fields, body) = parse_frontmatter(content);
        assert_eq!(fields.len(), 1);
        assert_eq!(fields[0].0, "title");
        assert!(fields.iter().all(|(k, _)| k != "paw_cli"));
        assert_eq!(body, "Body here");
    }

    #[test]
    fn no_frontmatter() {
        let content = "Just a body\nwith lines";
        let (fields, body) = parse_frontmatter(content);
        assert!(fields.is_empty());
        assert_eq!(body, content);
    }

    #[test]
    fn frontmatter_multiple_fields() {
        let content = "---\npaw_cli: claude\ntitle: test\n---\nContent";
        let (fields, body) = parse_frontmatter(content);
        assert_eq!(fields.len(), 2);
        assert_eq!(body, "Content");
    }

    // --- extract_owned_files tests ---

    #[test]
    fn owned_files_present() {
        let content = "Some text\n\nFiles owned:\n- src/auth.rs\n- src/login.rs\n\nMore text";
        let files = extract_owned_files(content).unwrap();
        assert_eq!(files, vec!["src/auth.rs", "src/login.rs"]);
    }

    #[test]
    fn owned_files_alternate_pattern() {
        let content = "Owned files:\n- `src/main.rs`\n";
        let files = extract_owned_files(content).unwrap();
        assert_eq!(files, vec!["src/main.rs"]);
    }

    #[test]
    fn no_owned_files() {
        let content = "No file ownership here";
        assert!(extract_owned_files(content).is_none());
    }

    // --- OpenSpecBackend integration tests ---

    #[test]
    fn scan_empty_directory() {
        let tmp = tempfile::tempdir().unwrap();
        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn scan_skips_files() {
        let tmp = tempfile::tempdir().unwrap();
        fs::write(tmp.path().join("not-a-dir.md"), "content").unwrap();
        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn scan_skips_archive() {
        let tmp = tempfile::tempdir().unwrap();
        let archive = tmp.path().join("archive");
        fs::create_dir(&archive).unwrap();
        fs::write(archive.join("tasks.md"), "archived task").unwrap();
        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn scan_skips_missing_tasks_md() {
        let tmp = tempfile::tempdir().unwrap();
        fs::create_dir(tmp.path().join("no-tasks")).unwrap();
        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn scan_basic_change() {
        let tmp = tempfile::tempdir().unwrap();
        let change = tmp.path().join("add-auth");
        fs::create_dir(&change).unwrap();
        fs::write(change.join("tasks.md"), "implement auth").unwrap();

        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].id, "add-auth");
        assert_eq!(result[0].prompt, "implement auth");
        assert!(result[0].cli.is_none());
        assert!(result[0].owned_files.is_none());
    }

    #[test]
    fn scan_with_frontmatter() {
        let tmp = tempfile::tempdir().unwrap();
        let change = tmp.path().join("my-change");
        fs::create_dir(&change).unwrap();
        fs::write(
            change.join("tasks.md"),
            "---\npaw_cli: gemini\n---\nDo the thing",
        )
        .unwrap();

        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].cli.as_deref(), Some("gemini"));
        assert_eq!(result[0].prompt, "Do the thing");
    }

    #[test]
    fn scan_with_specs() {
        let tmp = tempfile::tempdir().unwrap();
        let change = tmp.path().join("feat-x");
        fs::create_dir_all(change.join("specs/auth")).unwrap();
        fs::write(change.join("tasks.md"), "Primary task").unwrap();
        fs::write(change.join("specs/auth/spec.md"), "Auth spec content").unwrap();

        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert_eq!(result.len(), 1);
        assert!(result[0].prompt.contains("Primary task"));
        assert!(result[0].prompt.contains("## Spec: auth"));
        assert!(result[0].prompt.contains("Auth spec content"));
    }

    #[test]
    fn scan_with_owned_files() {
        let tmp = tempfile::tempdir().unwrap();
        let change = tmp.path().join("change-y");
        fs::create_dir(&change).unwrap();
        fs::write(
            change.join("tasks.md"),
            "Do stuff\n\nFiles owned:\n- src/a.rs\n- src/b.rs\n",
        )
        .unwrap();

        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert_eq!(result.len(), 1);
        let files = result[0].owned_files.as_ref().unwrap();
        assert_eq!(files, &["src/a.rs", "src/b.rs"]);
    }

    #[test]
    fn scan_multiple_changes() {
        let tmp = tempfile::tempdir().unwrap();
        for name in &["alpha", "beta"] {
            let d = tmp.path().join(name);
            fs::create_dir(&d).unwrap();
            fs::write(d.join("tasks.md"), format!("task for {name}")).unwrap();
        }

        let backend = OpenSpecBackend;
        let mut result = backend.scan(tmp.path()).unwrap();
        result.sort_by(|a, b| a.id.cmp(&b.id));
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].id, "alpha");
        assert_eq!(result[1].id, "beta");
    }

    // --- Gap #12: Multiple spec files ---

    #[test]
    fn scan_with_multiple_spec_files() {
        let tmp = tempfile::tempdir().unwrap();
        let change = tmp.path().join("my-change");
        fs::create_dir_all(change.join("specs/auth")).unwrap();
        fs::create_dir_all(change.join("specs/api")).unwrap();
        fs::write(change.join("tasks.md"), "Primary task content").unwrap();
        fs::write(change.join("specs/auth/spec.md"), "Auth spec details").unwrap();
        fs::write(change.join("specs/api/spec.md"), "API spec details").unwrap();

        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert_eq!(result.len(), 1);

        let prompt = &result[0].prompt;
        assert!(
            prompt.contains("Primary task content"),
            "prompt should contain primary task"
        );
        assert!(
            prompt.contains("## Spec: api"),
            "prompt should contain api spec heading"
        );
        assert!(
            prompt.contains("API spec details"),
            "prompt should contain api spec content"
        );
        assert!(
            prompt.contains("## Spec: auth"),
            "prompt should contain auth spec heading"
        );
        assert!(
            prompt.contains("Auth spec details"),
            "prompt should contain auth spec content"
        );
    }

    #[test]
    fn frontmatter_excluded_from_prompt() {
        let tmp = tempfile::tempdir().unwrap();
        let change = tmp.path().join("fm-test");
        fs::create_dir(&change).unwrap();
        fs::write(
            change.join("tasks.md"),
            "---\npaw_cli: claude\n---\nActual prompt",
        )
        .unwrap();

        let backend = OpenSpecBackend;
        let result = backend.scan(tmp.path()).unwrap();
        assert!(!result[0].prompt.contains("---"));
        assert!(!result[0].prompt.contains("paw_cli"));
        assert_eq!(result[0].prompt, "Actual prompt");
    }
}