pi_agent_rust 0.1.7

High-performance AI coding agent CLI - Rust port of Pi Agent
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
#![forbid(unsafe_code)]

//! CLI binary: Build tiered corpus from validated + scored + license-screened candidates.
//!
//! Merges signals from validated-dedup, candidate pool, and license report
//! to produce fully enriched `CandidateInput` records, scores them, and
//! outputs a tiered corpus selection.
//!
//! ```text
//! cargo run --bin ext_tiered_corpus -- \
//!   --validated docs/extension-validated-dedup.json \
//!   --candidate-pool docs/extension-candidate-pool.json \
//!   --license-report docs/extension-license-report.json \
//!   --out docs/extension-tiered-corpus.json \
//!   --summary-out docs/extension-tiered-summary.json
//! ```

use std::collections::HashMap;
use std::fs;
use std::io::Write;
use std::path::PathBuf;

use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use clap::Parser;
use pi::extension_license::{ScreeningReport, VerdictStatus};
use pi::extension_popularity::{CandidateItem, CandidatePool};
use pi::extension_scoring::{
    CandidateInput, CompatStatus, Compatibility, Gates, LicenseInfo, MarketplaceSignals, Recency,
    Redistribution, RiskInfo, Signals, Tags, score_candidates,
};
use pi::extension_validation::{ValidationReport, ValidationStatus};

#[derive(Debug, Parser)]
#[command(name = "ext_tiered_corpus")]
#[command(about = "Build tiered extension corpus from merged research signals")]
struct Args {
    /// Path to validated-dedup JSON (registration + classification data).
    #[arg(long)]
    validated: PathBuf,

    /// Path to candidate pool JSON (popularity + artifact data).
    #[arg(long)]
    candidate_pool: Option<PathBuf>,

    /// Path to license screening report JSON.
    #[arg(long)]
    license_report: Option<PathBuf>,

    /// Output path for scored + tiered corpus JSON.
    #[arg(long)]
    out: PathBuf,

    /// Output path for summary JSON.
    #[arg(long)]
    summary_out: Option<PathBuf>,

    /// Output path for JSONL decision log.
    #[arg(long)]
    log_out: Option<PathBuf>,

    /// Reference date for scoring (RFC3339). Defaults to now.
    #[arg(long)]
    as_of: Option<String>,

    /// Top N for summary sections.
    #[arg(long, default_value_t = 20)]
    top_n: usize,

    /// Task ID for provenance tracking.
    #[arg(long, default_value = "bd-34io")]
    task_id: String,
}

/// Merged data for a single candidate from all sources.
struct MergedCandidate {
    canonical_id: String,
    name: String,
    source_tier: Option<String>,
    registrations: Vec<String>,
    repository_url: Option<String>,
    npm_package: Option<String>,
    pool_item: Option<CandidateItem>,
    _license_verdict: Option<VerdictStatus>,
    license_spdx: Option<String>,
}

#[allow(clippy::too_many_lines)]
fn main() -> Result<()> {
    let args = Args::parse();

    // Load validated report.
    let validated_text = fs::read_to_string(&args.validated)
        .with_context(|| format!("reading validated report from {}", args.validated.display()))?;
    let validated: ValidationReport = serde_json::from_str(&validated_text)
        .with_context(|| format!("parsing validated report from {}", args.validated.display()))?;

    // Load candidate pool (popularity + artifact data).
    let pool_map: HashMap<String, CandidateItem> = args
        .candidate_pool
        .as_ref()
        .map(|p| {
            let text = fs::read_to_string(p)
                .with_context(|| format!("reading candidate pool from {}", p.display()))?;
            let pool: CandidatePool = serde_json::from_str(&text)
                .with_context(|| format!("parsing candidate pool from {}", p.display()))?;
            let mut map = HashMap::new();
            for item in pool.items {
                map.insert(item.id.clone(), item.clone());
                map.insert(item.name.clone(), item);
            }
            Ok::<_, anyhow::Error>(map)
        })
        .transpose()?
        .unwrap_or_default();

    // Load license report.
    let license_map: HashMap<String, (VerdictStatus, String)> = args
        .license_report
        .as_ref()
        .map(|p| {
            let text = fs::read_to_string(p)
                .with_context(|| format!("reading license report from {}", p.display()))?;
            let report: ScreeningReport = serde_json::from_str(&text)
                .with_context(|| format!("parsing license report from {}", p.display()))?;
            let mut map = HashMap::new();
            for v in report.verdicts {
                map.insert(v.canonical_id, (v.verdict, v.license));
            }
            Ok::<_, anyhow::Error>(map)
        })
        .transpose()?
        .unwrap_or_default();

    // Merge all sources into enriched candidates.
    let merged: Vec<MergedCandidate> = validated
        .candidates
        .iter()
        .filter(|c| c.status == ValidationStatus::TrueExtension)
        .map(|c| {
            let pool_item = pool_map
                .get(&c.canonical_id)
                .or_else(|| pool_map.get(&c.name))
                .cloned();

            let (license_verdict, license_spdx) = license_map
                .get(&c.canonical_id)
                .map_or((None, None), |(v, s)| (Some(*v), Some(s.clone())));

            MergedCandidate {
                canonical_id: c.canonical_id.clone(),
                name: c.name.clone(),
                source_tier: c.source_tier.clone(),
                registrations: c.evidence.registrations.clone(),
                repository_url: c.repository_url.clone(),
                npm_package: c.npm_package.clone(),
                pool_item,
                _license_verdict: license_verdict,
                license_spdx,
            }
        })
        .collect();

    eprintln!("Merged {} true extension candidates", merged.len());

    // Convert to CandidateInput for scoring.
    let inputs: Vec<CandidateInput> = merged.iter().map(build_candidate_input).collect();

    let as_of = args
        .as_of
        .as_ref()
        .map(|s| DateTime::parse_from_rfc3339(s).context("parse as_of"))
        .transpose()?
        .map_or_else(Utc::now, |d| d.with_timezone(&Utc));

    let report = score_candidates(&inputs, as_of, as_of, args.top_n);

    // Write main output.
    let json = serde_json::to_string_pretty(&report).context("serializing scoring report")?;
    let json = format!("{json}\n");
    fs::write(&args.out, &json)
        .with_context(|| format!("writing output to {}", args.out.display()))?;

    // Write summary.
    if let Some(summary_path) = &args.summary_out {
        let summary_json =
            serde_json::to_string_pretty(&report.summary).context("serialize summary")?;
        fs::write(summary_path, format!("{summary_json}\n"))
            .with_context(|| format!("write {}", summary_path.display()))?;
    }

    // Write JSONL decision log.
    if let Some(log_path) = &args.log_out {
        let mut log_file = fs::File::create(log_path)
            .with_context(|| format!("creating log file {}", log_path.display()))?;
        for item in &report.items {
            let line = serde_json::to_string(item).context("serializing log entry")?;
            writeln!(log_file, "{line}").context("writing log entry")?;
        }
    }

    // Print tier summary.
    let tier0 = report.items.iter().filter(|i| i.tier == "tier-0").count();
    let tier1 = report.items.iter().filter(|i| i.tier == "tier-1").count();
    let tier2 = report.items.iter().filter(|i| i.tier == "tier-2").count();
    let excluded = report.items.iter().filter(|i| i.tier == "excluded").count();

    eprintln!("=== Tiered Corpus Selection ({}) ===", args.task_id);
    eprintln!("Total scored:   {}", report.items.len());
    eprintln!("  Tier-0:       {tier0} (official baseline)");
    eprintln!("  Tier-1:       {tier1} (must-pass, score ≥ 70)");
    eprintln!("  Tier-2:       {tier2} (stretch, score 50-69)");
    eprintln!("  Excluded:     {excluded} (score < 50 or gate fail)");
    eprintln!();

    // Type coverage summary.
    let mut type_counts: HashMap<String, usize> = HashMap::new();
    for m in &merged {
        if m.registrations.is_empty() {
            *type_counts
                .entry("(no registrations)".to_string())
                .or_insert(0) += 1;
        }
        for reg in &m.registrations {
            *type_counts.entry(reg.clone()).or_insert(0) += 1;
        }
    }
    let mut type_list: Vec<_> = type_counts.iter().collect();
    type_list.sort_by(|a, b| b.1.cmp(a.1));
    eprintln!("Extension type coverage:");
    for (ext_type, count) in &type_list {
        eprintln!("  {ext_type:<25} {count}");
    }

    eprintln!("\nOutput written to: {}", args.out.display());

    Ok(())
}

/// Build a `CandidateInput` from merged signals.
fn build_candidate_input(m: &MergedCandidate) -> CandidateInput {
    let pool = m.pool_item.as_ref();

    // Signals from pool popularity data.
    let signals = pool.map_or_else(Signals::default, |item| {
        let is_official = m
            .source_tier
            .as_deref()
            .is_some_and(|t| t == "official-pi-mono");
        Signals {
            official_listing: Some(is_official),
            pi_mono_example: Some(is_official),
            badlogic_gist: m
                .repository_url
                .as_deref()
                .map(|url| url.contains("gist.github.com") && url.contains("badlogic"))
                .or(Some(false)),
            github_stars: item.popularity.github_stars,
            github_forks: item.popularity.github_forks,
            npm_downloads_month: item.popularity.npm_downloads_monthly,
            references: item.popularity.mentions_sources.clone().unwrap_or_default(),
            marketplace: Some(MarketplaceSignals {
                rank: item.popularity.marketplace_rank,
                installs_month: item.popularity.marketplace_installs_monthly,
                featured: item.popularity.marketplace_featured,
            }),
        }
    });

    // Tags: infer from registrations and source tier.
    let interaction = registrations_to_interactions(&m.registrations);
    let capabilities = registrations_to_capabilities(&m.registrations);
    let runtime = Some(infer_runtime(m));

    let tags = Tags {
        runtime,
        interaction,
        capabilities,
    };

    // Recency from pool data.
    let recency = pool.map_or_else(Recency::default, |item| Recency {
        updated_at: item
            .popularity
            .github_last_commit
            .clone()
            .or_else(|| item.popularity.npm_last_publish.clone())
            .or_else(|| item.retrieved.clone()),
    });

    // Compatibility: vendored items with artifacts are unmodified-compatible.
    let compat_status = pool.map_or(Some(CompatStatus::RequiresShims), |item| {
        match item.status.as_str() {
            "vendored" => Some(CompatStatus::Unmodified),
            "unvendored" | "excluded" => Some(CompatStatus::Blocked),
            _ => Some(CompatStatus::RequiresShims),
        }
    });
    let compat = Compatibility {
        status: compat_status,
        ..Compatibility::default()
    };

    // License from screening report or pool.
    let spdx = m
        .license_spdx
        .clone()
        .or_else(|| pool.map(|item| item.license.clone()));
    let redistribution = spdx
        .as_deref()
        .map_or(Redistribution::Unknown, infer_redistribution);
    let license = LicenseInfo {
        spdx,
        redistribution: Some(redistribution),
        notes: None,
    };

    // Gates.
    let provenance_pinned = pool.map(|item| item.checksum.is_some());
    let deterministic = pool.map(|item| item.status != "unvendored");
    let gates = Gates {
        provenance_pinned,
        deterministic,
    };

    // Risk: default (low).
    let risk = RiskInfo::default();

    CandidateInput {
        id: m.canonical_id.clone(),
        name: Some(m.name.clone()),
        source_tier: m.source_tier.clone(),
        signals,
        tags,
        recency,
        compat,
        license,
        gates,
        risk,
        manual_override: None,
    }
}

/// Map registration types to interaction tags for coverage scoring.
fn registrations_to_interactions(registrations: &[String]) -> Vec<String> {
    let mut interactions = Vec::new();
    for reg in registrations {
        let tag = match reg.as_str() {
            "registerProvider" => "provider",
            "registerTool" => "tool_only",
            "registerCommand" | "registerSlashCommand" | "registerFlag" | "registerShortcut" => {
                "slash_command"
            }
            "registerEvent" | "registerEventHook" => "event_hook",
            "registerMessageRenderer" => "ui_integration",
            _ => continue,
        };
        if !interactions.contains(&tag.to_string()) {
            interactions.push(tag.to_string());
        }
    }
    if interactions.is_empty() {
        // Default: treat as tool extension if no specific registrations.
        interactions.push("tool_only".to_string());
    }
    interactions
}

/// Map registration types to capability tags.
fn registrations_to_capabilities(registrations: &[String]) -> Vec<String> {
    let mut caps = Vec::new();
    for reg in registrations {
        let tag = match reg.as_str() {
            "registerTool" => "exec",
            "registerProvider" => "http",
            "registerMessageRenderer" => "ui",
            "registerCommand" | "registerSlashCommand" => "session",
            _ => continue,
        };
        if !caps.contains(&tag.to_string()) {
            caps.push(tag.to_string());
        }
    }
    caps
}

/// Infer runtime tier from source data.
fn infer_runtime(m: &MergedCandidate) -> String {
    // npm packages with deps → pkg-with-deps.
    if m.npm_package.is_some() {
        return "pkg-with-deps".to_string();
    }
    // Provider extensions are often more complex.
    if m.registrations.iter().any(|r| r == "registerProvider") {
        return "provider-ext".to_string();
    }
    // Default: legacy single-file.
    "legacy-js".to_string()
}

/// Infer `Redistribution` from SPDX string.
fn infer_redistribution(spdx: &str) -> Redistribution {
    let up = spdx.trim().to_ascii_uppercase();
    if up.is_empty() || matches!(up.as_str(), "UNKNOWN" | "UNLICENSED") {
        return Redistribution::Unknown;
    }
    if up.contains("GPL") || up.contains("AGPL") {
        return Redistribution::Restricted;
    }
    Redistribution::Ok
}