Skip to main content

tj_core/
artifacts.rs

1//! Artifact extraction — regex-based scrape of structured references
2//! out of free-form event text. Captures the bits that turn a journal
3//! entry into a real ledger of what shipped: commit hashes, PR URLs,
4//! ticket IDs, branch names, file paths.
5//!
6//! Intentionally regex-only and side-effect free: the classifier may
7//! still emit a richer JSON payload in the future, but those will be
8//! merged into the same shape via `Artifacts::merge`. Keeping the
9//! extractor pure means `reclassify` can run it offline over historic
10//! events without spawning the model.
11
12use regex::Regex;
13use serde::{Deserialize, Serialize};
14
15/// Structured artifacts collected from one or many events. All vectors
16/// are deduplicated (case-sensitive) by the `merge` constructor — the
17/// extractor itself emits raw matches.
18#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
19pub struct Artifacts {
20    #[serde(default, skip_serializing_if = "Vec::is_empty")]
21    pub commit_hashes: Vec<String>,
22    #[serde(default, skip_serializing_if = "Vec::is_empty")]
23    pub pr_urls: Vec<String>,
24    #[serde(default, skip_serializing_if = "Vec::is_empty")]
25    pub linked_issues: Vec<String>,
26    #[serde(default, skip_serializing_if = "Vec::is_empty")]
27    pub files: Vec<String>,
28    #[serde(default, skip_serializing_if = "Vec::is_empty")]
29    pub branch_names: Vec<String>,
30}
31
32impl Artifacts {
33    pub fn is_empty(&self) -> bool {
34        self.commit_hashes.is_empty()
35            && self.pr_urls.is_empty()
36            && self.linked_issues.is_empty()
37            && self.files.is_empty()
38            && self.branch_names.is_empty()
39    }
40
41    /// Merge another `Artifacts` into self, preserving insertion order
42    /// and deduplicating exact-match strings.
43    pub fn merge(&mut self, other: Artifacts) {
44        for (dst, src) in [
45            (&mut self.commit_hashes, other.commit_hashes),
46            (&mut self.pr_urls, other.pr_urls),
47            (&mut self.linked_issues, other.linked_issues),
48            (&mut self.files, other.files),
49            (&mut self.branch_names, other.branch_names),
50        ] {
51            for s in src {
52                if !dst.iter().any(|x| x == &s) {
53                    dst.push(s);
54                }
55            }
56        }
57    }
58}
59
60/// Extract artifacts from a single piece of text (event body, prompt,
61/// tool output — anything stringly-typed). Idempotent and free of I/O.
62pub fn extract(text: &str) -> Artifacts {
63    let mut a = Artifacts::default();
64
65    // Commit hashes — 7 to 40 hex chars surrounded by word boundaries.
66    // Word boundary on \b avoids matching inside longer non-hex tokens
67    // (e.g. ULIDs are base32, but adjacent digits + letters could
68    // technically pass — the boundary keeps matches clean).
69    static_re(
70        r"\b[0-9a-f]{7,40}\b",
71        |m| {
72            // Reject if all-digits (could be a year, an ID, a port).
73            // A real abbreviated commit always has at least one letter.
74            if m.chars().all(|c| c.is_ascii_digit()) {
75                return;
76            }
77            a.commit_hashes.push(m.to_string());
78        },
79        text,
80    );
81
82    // GitHub / GitLab PR URLs.
83    static_re(
84        r"https?://[A-Za-z0-9.\-]+/[A-Za-z0-9_./\-]+/(?:pull|merge_requests)/\d+",
85        |m| a.pr_urls.push(m.to_string()),
86        text,
87    );
88
89    // Ticket IDs: ABC-123. At least 2 letters to avoid matching version
90    // strings like v1-2 and minimum 1 digit.
91    static_re(
92        r"\b[A-Z]{2,}-\d+\b",
93        |m| a.linked_issues.push(m.to_string()),
94        text,
95    );
96
97    // File paths — heuristic: path-like tokens with at least one slash
98    // (and an extension) OR a leading ./ . Path segments allow a
99    // leading dot so `.docs/specs/auth.md`, `.github/workflows/ci.yml`
100    // etc are captured as artifacts. Tight enough to skip prose, loose
101    // enough to catch the common cases (src/foo.rs, ./bar.ts,
102    // crates/tj-core/src/db.rs).
103    static_re(
104        r"(?:\./|\.?[A-Za-z0-9_\-]+/)+[A-Za-z0-9_.\-]+\.[A-Za-z0-9]{1,8}\b",
105        |m| a.files.push(m.to_string()),
106        text,
107    );
108
109    // Branch names from `git checkout -b <name>` / `git switch -c
110    // <name>` / `branch <name>` blurbs.
111    static_re(
112        r"(?:checkout -b|switch -c|branch)\s+([A-Za-z0-9._/\-]+)",
113        |_full| {},
114        text,
115    );
116    // The closure form above fires on the whole match; capture the
117    // group separately because Regex::captures is what we actually want
118    // here. Done as a second pass to keep the extractor flat.
119    if let Ok(re) = Regex::new(r"(?:checkout -b|switch -c|branch)\s+([A-Za-z0-9._/\-]+)") {
120        for cap in re.captures_iter(text) {
121            if let Some(m) = cap.get(1) {
122                a.branch_names.push(m.as_str().to_string());
123            }
124        }
125    }
126
127    // Dedup in place — emit-time order matters for stable test output.
128    dedup(&mut a.commit_hashes);
129    dedup(&mut a.pr_urls);
130    dedup(&mut a.linked_issues);
131    dedup(&mut a.files);
132    dedup(&mut a.branch_names);
133    a
134}
135
136fn dedup(v: &mut Vec<String>) {
137    let mut seen = std::collections::HashSet::new();
138    v.retain(|x| seen.insert(x.clone()));
139}
140
141fn static_re(pat: &str, mut f: impl FnMut(&str), text: &str) {
142    if let Ok(re) = Regex::new(pat) {
143        for m in re.find_iter(text) {
144            f(m.as_str());
145        }
146    }
147}
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    fn extracts_commit_hash() {
155        let a = extract("fixed in commit abc1234 and 9012abcdef");
156        assert_eq!(a.commit_hashes, vec!["abc1234", "9012abcdef"]);
157    }
158
159    #[test]
160    fn rejects_all_digit_commit_lookalikes() {
161        // Year-like sequence, port numbers, etc.
162        let a = extract("ran tests on port 12345 in 2026");
163        assert!(a.commit_hashes.is_empty());
164    }
165
166    #[test]
167    fn extracts_github_pr_url() {
168        let a = extract("see https://github.com/Digital-Threads/Task-Journal/pull/42");
169        assert_eq!(
170            a.pr_urls,
171            vec!["https://github.com/Digital-Threads/Task-Journal/pull/42"]
172        );
173    }
174
175    #[test]
176    fn extracts_linked_issues() {
177        let a = extract("FIN-868 references JIRA-12345 and INC-7");
178        assert_eq!(a.linked_issues, vec!["FIN-868", "JIRA-12345", "INC-7"]);
179    }
180
181    #[test]
182    fn extracts_file_paths() {
183        let a = extract("edited crates/tj-core/src/db.rs and ./README.md");
184        assert!(a.files.contains(&"crates/tj-core/src/db.rs".to_string()));
185        assert!(a.files.contains(&"./README.md".to_string()));
186    }
187
188    #[test]
189    fn extracts_dot_prefixed_dirs() {
190        // .docs/specs/*.md, .github/workflows/*.yml — leading-dot dirs
191        // are spec/config holders we want surfaced as artifacts so the
192        // pack ties decisions back to the document that justified them.
193        let a = extract("see .docs/specs/auth.md and .github/workflows/ci.yml");
194        assert!(a.files.contains(&".docs/specs/auth.md".to_string()));
195        assert!(a.files.contains(&".github/workflows/ci.yml".to_string()));
196    }
197
198    #[test]
199    fn extracts_branch_names() {
200        let a = extract("git checkout -b FIN-868-fix-paygate-fee then switch -c hotfix/abc");
201        assert_eq!(
202            a.branch_names,
203            vec!["FIN-868-fix-paygate-fee", "hotfix/abc"]
204        );
205    }
206
207    #[test]
208    fn merge_dedupes() {
209        let mut a = Artifacts {
210            commit_hashes: vec!["abc1234".into()],
211            ..Default::default()
212        };
213        let b = Artifacts {
214            commit_hashes: vec!["abc1234".into(), "def5678".into()],
215            ..Default::default()
216        };
217        a.merge(b);
218        assert_eq!(a.commit_hashes, vec!["abc1234", "def5678"]);
219    }
220
221    #[test]
222    fn empty_text_yields_empty_artifacts() {
223        let a = extract("");
224        assert!(a.is_empty());
225    }
226
227    #[test]
228    fn json_round_trip() {
229        let a = Artifacts {
230            commit_hashes: vec!["abc1234".into()],
231            linked_issues: vec!["FIN-868".into()],
232            ..Default::default()
233        };
234        let s = serde_json::to_string(&a).unwrap();
235        let b: Artifacts = serde_json::from_str(&s).unwrap();
236        assert_eq!(a, b);
237    }
238}