promptjar 0.1.0

Query a Git repo of Markdown prompt archives like a database
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
//! The data model and the file-level parser: one Markdown file in, one
//! [`Parsed`] out (a thread if the frontmatter is valid, plus diagnostics).

use crate::{frontmatter, records};

/// One Markdown file with valid frontmatter.
#[derive(Debug, Clone)]
pub struct Thread {
    /// Path relative to the archive root, `/`-separated.
    pub rel_path: String,
    /// First path component of `rel_path`, or `.` for root-level files.
    pub project: String,
    pub date: jiff::civil::Date,
    pub models: Vec<String>,
    /// Frontmatter keys other than `date` and `model`, converted to JSON.
    pub extra: serde_json::Map<String, serde_json::Value>,
    /// Raw file body (everything after the closing frontmatter delimiter).
    pub body: String,
    pub records: Vec<Record>,
}

impl Thread {
    /// Total whitespace-delimited words over all records.
    pub fn words(&self) -> usize {
        self.records.iter().map(|r| r.words).sum()
    }

    /// The `project/file.md:N` address of one record.
    pub fn address(&self, index: usize) -> String {
        format!("{}:{index}", self.rel_path)
    }
}

/// One prompt block: a chunk of the body between top-level dash-style
/// thematic breaks. Indices are 1-based and stable even for empty chunks.
#[derive(Debug, Clone)]
pub struct Record {
    pub index: usize,
    /// 1-based line where the record's content starts.
    pub line: usize,
    /// Chunk text with surrounding whitespace trimmed.
    pub text: String,
    pub words: usize,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
    Error,
    Warning,
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Severity::Error => f.write_str("error"),
            Severity::Warning => f.write_str("warning"),
        }
    }
}

/// One lint finding, anchored to a 1-based line in the source file.
#[derive(Debug, Clone)]
pub struct Diagnostic {
    pub line: usize,
    pub severity: Severity,
    /// Reported only under `lint --strict` (unknown frontmatter keys).
    pub strict_only: bool,
    pub message: String,
}

impl Diagnostic {
    pub fn error(line: usize, message: String) -> Self {
        Diagnostic {
            line,
            severity: Severity::Error,
            strict_only: false,
            message,
        }
    }

    pub fn warning(line: usize, message: String) -> Self {
        Diagnostic {
            line,
            severity: Severity::Warning,
            strict_only: false,
            message,
        }
    }

    pub fn strict_warning(line: usize, message: String) -> Self {
        Diagnostic {
            line,
            severity: Severity::Warning,
            strict_only: true,
            message,
        }
    }
}

/// The result of parsing one Markdown file.
#[derive(Debug)]
pub struct Parsed {
    /// `Some` only when the file has valid frontmatter.
    pub thread: Option<Thread>,
    pub diagnostics: Vec<Diagnostic>,
}

impl Parsed {
    fn not_thread(diagnostic: Diagnostic) -> Self {
        Parsed {
            thread: None,
            diagnostics: vec![diagnostic],
        }
    }

    /// True when any diagnostic is an error (the file is not a valid thread
    /// for a reason other than simply having no frontmatter).
    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|d| d.severity == Severity::Error)
    }
}

/// The project a root-relative path belongs to.
pub fn project_of(rel_path: &str) -> String {
    match rel_path.split_once('/') {
        Some((project, _)) => project.to_string(),
        None => ".".to_string(),
    }
}

/// Parse one Markdown file. `rel_path` is the `/`-separated path relative to
/// the archive root, used for the project name and record addresses.
pub fn parse_file(rel_path: &str, raw: &str) -> Parsed {
    // Editors add UTF-8 BOMs invisibly; tolerate one (SPEC.md Questions 4).
    let content = raw.strip_prefix('\u{feff}').unwrap_or(raw);

    let (yaml, yaml_line, body_offset) = match frontmatter::extract(content) {
        frontmatter::Extract::None => {
            return Parsed::not_thread(Diagnostic::warning(
                1,
                "no YAML frontmatter; not a thread".to_string(),
            ));
        }
        frontmatter::Extract::Displaced { line } => {
            return Parsed::not_thread(Diagnostic::error(
                line,
                "frontmatter must start at byte 0 of the file".to_string(),
            ));
        }
        frontmatter::Extract::Unclosed => {
            return Parsed::not_thread(Diagnostic::error(
                1,
                "unclosed frontmatter: missing closing `---` line".to_string(),
            ));
        }
        frontmatter::Extract::Found {
            yaml,
            yaml_line,
            body_offset,
        } => (yaml, yaml_line, body_offset),
    };

    let fm = match frontmatter::parse_yaml(yaml, yaml_line) {
        Ok(fm) => fm,
        Err(diagnostics) => {
            return Parsed {
                thread: None,
                diagnostics,
            };
        }
    };

    let mut diagnostics: Vec<Diagnostic> = fm
        .extra_lines
        .iter()
        .map(|(key, line)| {
            Diagnostic::strict_warning(*line, format!("unknown frontmatter key `{key}`"))
        })
        .collect();

    let body = &content[body_offset..];
    let mut recs = Vec::new();
    for (i, span) in records::split(body).into_iter().enumerate() {
        let chunk = &body[span.clone()];
        let text = chunk.trim();
        let leading = chunk.len() - chunk.trim_start().len();
        let anchor = body_offset + span.start + if text.is_empty() { 0 } else { leading };
        let line = line_of(content, anchor);
        if text.is_empty() {
            diagnostics.push(Diagnostic::warning(line, format!("empty record {}", i + 1)));
        }
        recs.push(Record {
            index: i + 1,
            line,
            words: text.split_whitespace().count(),
            text: text.to_string(),
        });
    }

    Parsed {
        thread: Some(Thread {
            rel_path: rel_path.to_string(),
            project: project_of(rel_path),
            date: fm.date,
            models: fm.models,
            extra: fm.extra,
            body: body.to_string(),
            records: recs,
        }),
        diagnostics,
    }
}

/// 1-based line number of a byte offset.
fn line_of(content: &str, offset: usize) -> usize {
    1 + content.as_bytes()[..offset]
        .iter()
        .filter(|&&b| b == b'\n')
        .count()
}

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

    const FM: &str = "---\ndate: 2026-08-11\nmodel: Claude Fable 5 Extra\n---\n";

    fn parse(content: &str) -> Parsed {
        parse_file("proj/file.md", content)
    }

    fn thread(content: &str) -> Thread {
        let parsed = parse(content);
        parsed
            .thread
            .unwrap_or_else(|| panic!("expected a thread: {:?}", parsed.diagnostics))
    }

    fn errors(content: &str) -> Vec<Diagnostic> {
        let parsed = parse(content);
        assert!(parsed.thread.is_none(), "expected no thread");
        assert!(
            parsed.has_errors(),
            "expected errors: {:?}",
            parsed.diagnostics
        );
        parsed.diagnostics
    }

    #[test]
    fn separator_splits_records() {
        let t = thread(&format!("{FM}\nOne.\n\n---\n\nTwo.\n"));
        assert_eq!(t.records.len(), 2);
        assert_eq!(t.records[0].text, "One.");
        assert_eq!(t.records[1].text, "Two.");
        assert_eq!(t.address(2), "proj/file.md:2");
    }

    #[test]
    fn dashes_in_fenced_code_do_not_split() {
        let t = thread(&format!(
            "{FM}\nBefore.\n\n```yaml\n---\ndate: 2026-08-11\n---\n```\n\nAfter.\n"
        ));
        assert_eq!(t.records.len(), 1);
        assert!(t.records[0].text.contains("```yaml"));
    }

    #[test]
    fn setext_heading_underline_does_not_split() {
        let t = thread(&format!(
            "{FM}\nA heading\n---\n\nBody under the heading.\n"
        ));
        assert_eq!(t.records.len(), 1);
    }

    #[test]
    fn dashes_in_block_quote_and_list_do_not_split() {
        let t = thread(&format!(
            "{FM}\n> quoted\n> ---\n\n1. item\n\n   ---\n\n   more\n"
        ));
        assert_eq!(t.records.len(), 1);
    }

    #[test]
    fn asterisk_and_underscore_rules_are_content() {
        let t = thread(&format!("{FM}\nOne.\n\n***\n\n___\n\nStill one.\n"));
        assert_eq!(t.records.len(), 1);
    }

    #[test]
    fn extra_keys_preserved() {
        let t = thread("---\ndate: 2026-08-11\nmodel: M\ntags: [a, b]\nnote: hi\n---\n\nx\n");
        assert_eq!(t.extra["tags"], serde_json::json!(["a", "b"]));
        assert_eq!(t.extra["note"], serde_json::json!("hi"));
    }

    #[test]
    fn unknown_keys_reported_strict_only() {
        let parsed = parse("---\ndate: 2026-08-11\nmodel: M\ntags: [a]\n---\n\nx\n");
        let diag = parsed
            .diagnostics
            .iter()
            .find(|d| d.message.contains("unknown frontmatter key `tags`"))
            .expect("unknown-key diagnostic");
        assert!(diag.strict_only);
        assert_eq!(diag.severity, Severity::Warning);
        assert_eq!(diag.line, 4);
    }

    #[test]
    fn model_scalar_and_sequence_forms() {
        let scalar = thread(FM);
        assert_eq!(scalar.models, ["Claude Fable 5 Extra"]);
        let flow =
            thread("---\ndate: 2026-08-08\nmodel: [Claude Fable 5 Extra, GPT-5.6 Sol Pro]\n---\n");
        assert_eq!(flow.models, ["Claude Fable 5 Extra", "GPT-5.6 Sol Pro"]);
        let block = thread("---\ndate: 2026-08-08\nmodel:\n  - A\n  - B\n---\n");
        assert_eq!(block.models, ["A", "B"]);
    }

    #[test]
    fn quoted_and_unquoted_dates_agree() {
        let plain = thread("---\ndate: 2026-08-11\nmodel: M\n---\n");
        let quoted = thread("---\ndate: \"2026-08-11\"\nmodel: M\n---\n");
        assert_eq!(plain.date, quoted.date);
        assert_eq!(plain.date.to_string(), "2026-08-11");
    }

    #[test]
    fn invalid_dates_error() {
        for bad in [
            "2026-8-1",
            "2026-13-01",
            "2026-02-30",
            "20260811",
            "not a date",
        ] {
            let diags = errors(&format!("---\ndate: {bad}\nmodel: M\n---\n"));
            assert!(
                diags.iter().any(|d| d.message.contains("date")),
                "{bad}: {diags:?}"
            );
        }
    }

    #[test]
    fn missing_or_empty_model_errors() {
        for fm in [
            "---\ndate: 2026-08-11\n---\n",
            "---\ndate: 2026-08-11\nmodel:\n---\n",
            "---\ndate: 2026-08-11\nmodel: []\n---\n",
        ] {
            let diags = errors(fm);
            assert!(
                diags.iter().any(|d| d.message.contains("model")),
                "{fm}: {diags:?}"
            );
        }
    }

    #[test]
    fn empty_record_between_separators_warns_but_counts() {
        let parsed = parse(&format!("{FM}\nOne.\n\n---\n\n---\n\nThree.\n"));
        let t = parsed.thread.expect("thread");
        assert_eq!(t.records.len(), 3);
        assert_eq!(t.records[1].text, "");
        assert_eq!(t.records[1].words, 0);
        assert!(
            parsed
                .diagnostics
                .iter()
                .any(|d| { d.severity == Severity::Warning && d.message == "empty record 2" })
        );
    }

    #[test]
    fn frontmatter_only_file_has_zero_records() {
        let parsed = parse(FM);
        let t = parsed.thread.expect("thread");
        assert!(t.records.is_empty());
        assert!(parsed.diagnostics.is_empty());
    }

    #[test]
    fn crlf_line_endings_tolerated() {
        let t =
            thread("---\r\ndate: 2026-08-11\r\nmodel: M\r\n---\r\nOne.\r\n\r\n---\r\n\r\nTwo.\r\n");
        assert_eq!(t.date.to_string(), "2026-08-11");
        assert_eq!(t.records.len(), 2);
        assert_eq!(t.records[1].text, "Two.");
    }

    #[test]
    fn missing_trailing_newline_tolerated() {
        let t = thread(&format!("{FM}\nOne.\n\n---\n\nTwo"));
        assert_eq!(t.records.len(), 2);
        assert_eq!(t.records[1].text, "Two");
    }

    #[test]
    fn bom_is_stripped() {
        let t = thread(&format!("\u{feff}{FM}\nx\n"));
        assert_eq!(t.records.len(), 1);
    }

    #[test]
    fn no_frontmatter_is_a_warning() {
        let parsed = parse("# README\n\nJust a file.\n");
        assert!(parsed.thread.is_none());
        assert_eq!(parsed.diagnostics.len(), 1);
        assert_eq!(parsed.diagnostics[0].severity, Severity::Warning);
        assert_eq!(parsed.diagnostics[0].line, 1);
    }

    #[test]
    fn leading_blank_line_before_frontmatter_is_an_error() {
        let diags = errors(&format!("\n{FM}"));
        assert!(diags[0].message.contains("byte 0"), "{diags:?}");
        assert_eq!(diags[0].line, 2);
    }

    #[test]
    fn unclosed_frontmatter_is_an_error() {
        let diags = errors("---\ndate: 2026-08-11\nmodel: M\n");
        assert!(diags[0].message.contains("unclosed"), "{diags:?}");
    }

    #[test]
    fn unparseable_yaml_is_an_error() {
        let diags = errors("---\ndate: [\nmodel: M\n---\n");
        assert!(
            diags.iter().any(|d| d.message.contains("unparseable YAML")),
            "{diags:?}"
        );
    }

    #[test]
    fn words_are_counted_per_record_and_summed() {
        let t = thread(&format!("{FM}\nOne two three.\n\n---\n\nFour five.\n"));
        assert_eq!(t.records[0].words, 3);
        assert_eq!(t.records[1].words, 2);
        assert_eq!(t.words(), 5);
    }

    #[test]
    fn projects_derive_from_the_first_path_component() {
        assert_eq!(project_of("okr/prompts.md"), "okr");
        assert_eq!(project_of("okr/sub/deep.md"), "okr");
        assert_eq!(project_of("rootfile.md"), ".");
    }
}