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
//! search-and-consume — GitHub-search-driven mass-absorb.
//!
//! Composes `gh search repos <query>` with the existing consume-gh-org
//! per-repo pipeline (clone + reverse + optional render + optional
//! measure). Operator-facing:
//!
//! pleme-doc-gen search-and-consume \
//! --query "language:rust stars:>1000 cli" \
//! --limit 30 \
//! --measure-fidelity
//!
//! Opens external territory — the substrate stops being constrained
//! to pleme-io's own corpus and surfaces typed-quality against any
//! GitHub-searchable repo set. Per the operator's "leverage GitHub
//! search to feed new territories to consume" directive.
//!
//! The result is the substrate's typed-quality signal cross-applied
//! to third-party shapes: if the score is high, our extractors
//! generalize; if low, we know exactly which shapes to invest in.
use anyhow::{anyhow, Result};
use std::path::Path;
/// List repo slugs matching a GitHub search query via the `gh` CLI.
/// Returns `owner/repo` strings in result order.
///
/// Uses `gh search repos --json fullName --jq '.[].fullName'`.
pub fn search_repos(query: &str, limit: usize) -> Result<Vec<String>> {
let limit_s = limit.to_string();
// gh search repos parses qualifiers (language:, topic:, stars:) as
// separate POSITIONAL ARGS, not as a single quoted string. Pass
// each whitespace-separated token as its own arg so qualifiers
// don't collapse into bogus values.
let mut cmd = std::process::Command::new("gh");
cmd.args(["search", "repos"]);
for tok in query.split_whitespace() { cmd.arg(tok); }
cmd.args([
"--limit", &limit_s,
"--json", "fullName",
"--jq", ".[].fullName",
]);
let out = cmd.output()
.map_err(|e| anyhow!("gh search repos failed: {e}"))?;
if !out.status.success() {
let stderr = String::from_utf8_lossy(&out.stderr);
return Err(anyhow!("gh search repos non-zero: {stderr}"));
}
let text = String::from_utf8_lossy(&out.stdout);
Ok(text.lines()
.filter(|l| !l.is_empty() && l.contains('/'))
.map(|l| l.trim().to_string())
.collect())
}
/// Top-level search-and-consume operation. Lists matching repos via
/// `gh search`, then runs the same per-repo pipeline that
/// consume_gh_org uses. Reuses the existing OrgReport shape (with
/// org = the query for identification).
pub fn search_and_consume(
query: &str,
out: &Path,
work_dir: &Path,
limit: usize,
render_too: bool,
measure: bool,
) -> Result<crate::consume_gh_org::OrgReport> {
use crate::ast::Render;
std::fs::create_dir_all(out)?;
std::fs::create_dir_all(work_dir)?;
let slugs = search_repos(query, limit)?;
let mut report = crate::consume_gh_org::OrgReport {
org: format!("search:{query}"),
listed: slugs.len(),
..Default::default()
};
for slug in &slugs {
let mut outcome = crate::consume_gh_org::RepoOutcome {
slug: slug.clone(),
ecosystem: None,
caixa_path: None,
rendered_path: None,
artifact_count: 0,
error: None,
fidelity: None,
render_health: None,
};
// Step 1 — shallow clone via the existing helper. We re-clone
// each repo into work_dir; if it exists already, the helper
// returns the existing path (idempotent).
let clone_path = match shallow_clone(slug, work_dir) {
Ok(p) => p,
Err(e) => {
outcome.error = Some(format!("clone: {e}"));
report.failed += 1;
report.outcomes.push(outcome);
continue;
}
};
// Step 2 — discover.
let detected = crate::discover::detect(&clone_path);
outcome.ecosystem = detected.as_ref().map(|d| d.ecosystem.to_string());
if detected.is_none() {
outcome.error = Some("no ecosystem detected".into());
report.skipped += 1;
report.outcomes.push(outcome);
continue;
}
// Step 3 — reverse + write .caixa.lisp.
let forms = match crate::reverse::reverse_from_path(&clone_path) {
Ok(f) => f,
Err(e) => {
outcome.error = Some(format!("reverse: {e}"));
report.failed += 1;
report.outcomes.push(outcome);
continue;
}
};
let repo_name = slug.rsplit('/').next().unwrap_or(slug);
let caixa_path = out.join(format!("{repo_name}.caixa.lisp"));
if let Err(e) = std::fs::write(&caixa_path, forms.render()) {
outcome.error = Some(format!("write caixa: {e}"));
report.failed += 1;
report.outcomes.push(outcome);
continue;
}
outcome.caixa_path = Some(caixa_path.clone());
// Step 4 (optional) — render + optionally measure fidelity.
if render_too || measure {
let rendered = out.join(format!("{repo_name}-rendered"));
if let Err(e) = std::fs::create_dir_all(&rendered) {
outcome.error = Some(format!("mkdir rendered: {e}"));
report.failed += 1;
report.outcomes.push(outcome);
continue;
}
let src = forms.render();
match crate::caixa::render(&src, &rendered, true) {
Ok(files) => {
outcome.rendered_path = Some(rendered.clone());
outcome.artifact_count = files.len();
if measure {
match crate::fidelity::measure(&clone_path, &rendered) {
Ok(f) => {
report.fidelity_perfect_total += f.perfect_count;
report.fidelity_lossy_total += f.lossy_count;
report.fidelity_gap_total += f.gap_count;
report.fidelity_measured += 1;
if let Some(eco) = &f.ecosystem {
let row = report.fidelity_by_ecosystem
.entry(eco.clone()).or_default();
row.measured += 1;
row.perfect += f.perfect_count;
row.lossy += f.lossy_count;
row.gap += f.gap_count;
}
outcome.fidelity = Some(f);
}
Err(e) => {
outcome.error = Some(format!("measure: {e}"));
}
}
}
}
Err(e) => {
outcome.error = Some(format!("render: {e}"));
report.failed += 1;
report.outcomes.push(outcome);
continue;
}
}
}
report.consumed += 1;
report.outcomes.push(outcome);
}
Ok(report)
}
/// Mirror of consume_gh_org::shallow_clone — the function is private
/// in that module; rather than expose it, we duplicate the 8-line
/// helper here. (Triple-use threshold would justify extracting; we
/// re-evaluate after the next consumer appears.)
fn shallow_clone(slug: &str, work_dir: &Path) -> Result<std::path::PathBuf> {
let repo_name = slug.rsplit('/').next().unwrap_or(slug);
// Search results can have owner collisions across the work dir; key
// by the full slug to avoid one search clobbering another.
let safe_dir = slug.replace('/', "__");
let target = work_dir.join(&safe_dir).join(repo_name);
if target.is_dir() {
return Ok(target);
}
std::fs::create_dir_all(target.parent().unwrap())?;
let url = format!("https://github.com/{slug}.git");
let st = std::process::Command::new("git")
.args(["clone", "--depth", "1", "--quiet", &url, target.to_str().unwrap()])
.status()
.map_err(|e| anyhow!("git clone {slug}: {e}"))?;
if !st.success() {
return Err(anyhow!("git clone {slug} returned non-zero"));
}
Ok(target)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn search_repos_validates_slug_shape() {
// Pure unit (no network) — verify the result filter rejects
// non-slug lines (could appear in error output).
// We can't actually call gh in tests; this is a smoke check on
// the filter logic by hand-constructing a result.
let lines = vec!["", "owner/repo", "noslash", "a/b", "junk"];
let filtered: Vec<&str> = lines.into_iter()
.filter(|l| !l.is_empty() && l.contains('/'))
.collect();
assert_eq!(filtered, vec!["owner/repo", "a/b"]);
}
}