assay-cli 3.10.2

CLI for Assay
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use crate::exit_codes;
use anyhow::{bail, Context, Result};
use assay_evidence::bundle::BundleWriter;
use assay_evidence::types::{EvidenceEvent, ProducerMeta};
use chrono::{DateTime, SecondsFormat, Utc};
use clap::Args;
use serde_json::{json, Map, Value};
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
use std::path::{Path, PathBuf};

const EVENT_TYPE: &str = "assay.receipt.mastra.score_event.v1";
const EVENT_SOURCE: &str = "urn:assay:external:mastra:score-event";
const RECEIPT_SCHEMA: &str = "assay.receipt.mastra.score_event.v1";
const SOURCE_SYSTEM: &str = "mastra";
const SOURCE_SURFACE: &str = "observability.score_event";
const REDUCER_VERSION: &str = "assay-mastra-score-event@0.1.0";
const INPUT_SCHEMA: &str = "mastra.score-event.export.v1";
const DEFAULT_RUN_ID: &str = "import-mastra-score-event";
const MAX_BOUNDARY_STRING_CHARS: usize = 160;
const MAX_REASON_CHARS: usize = 240;

#[derive(Debug, Args, Clone)]
pub struct MastraScoreEventArgs {
    /// Mastra reduced ScoreEvent JSONL artifact file
    #[arg(long, value_name = "PATH")]
    pub input: PathBuf,

    /// Output Assay evidence bundle path (.tar.gz)
    #[arg(long, alias = "out", value_name = "PATH")]
    pub bundle_out: PathBuf,

    /// Reviewer-safe source artifact reference stored in receipts
    #[arg(long)]
    pub source_artifact_ref: Option<String>,

    /// Assay import run id used for receipt provenance and event ids
    #[arg(long, default_value = DEFAULT_RUN_ID)]
    pub run_id: String,

    /// Import timestamp for deterministic fixtures (RFC3339 UTC recommended)
    #[arg(long)]
    pub import_time: Option<String>,
}

pub fn cmd_mastra_score_event(args: MastraScoreEventArgs) -> Result<i32> {
    let import_time = parse_import_time(args.import_time.as_deref())?;
    let source_artifact_ref = args
        .source_artifact_ref
        .unwrap_or_else(|| default_source_artifact_ref(&args.input));
    // The receipt is a narrow score projection, but provenance binds back to
    // the exact reduced JSONL artifact bytes.
    let source_artifact_digest = sha256_file(&args.input)
        .with_context(|| format!("failed to digest input {}", args.input.display()))?;
    let producer = ProducerMeta {
        name: "assay-cli".to_string(),
        version: env!("CARGO_PKG_VERSION").to_string(),
        git: option_env!("ASSAY_GIT_SHA").map(str::to_string),
    };

    let events = read_mastra_score_events(
        &args.input,
        &source_artifact_ref,
        &source_artifact_digest,
        &args.run_id,
        import_time,
        &producer,
    )?;

    let out_file = File::create(&args.bundle_out)
        .with_context(|| format!("failed to create bundle {}", args.bundle_out.display()))?;
    let mut writer = BundleWriter::new(out_file).with_producer(producer);
    for event in events {
        writer.add_event(event);
    }
    writer
        .finish()
        .with_context(|| format!("failed to write bundle {}", args.bundle_out.display()))?;

    eprintln!(
        "Imported Mastra ScoreEvent receipts to {}",
        args.bundle_out.display()
    );

    Ok(exit_codes::OK)
}

fn read_mastra_score_events(
    input: &Path,
    source_artifact_ref: &str,
    source_artifact_digest: &str,
    run_id: &str,
    import_time: DateTime<Utc>,
    producer: &ProducerMeta,
) -> Result<Vec<EvidenceEvent>> {
    if run_id.contains(':') {
        bail!("run_id cannot contain ':' because event ids use run_id:seq");
    }

    let file =
        File::open(input).with_context(|| format!("failed to open input {}", input.display()))?;
    let reader = BufReader::new(file);
    let mut events = Vec::new();
    let mut saw_jsonl_row = false;

    for (line_index, line_result) in reader.lines().enumerate() {
        let line_number = line_index + 1;
        let line = line_result.with_context(|| format!("failed to read line {line_number}"))?;
        if line.trim().is_empty() {
            continue;
        }
        saw_jsonl_row = true;
        let row: Value = serde_json::from_str(&line)
            .with_context(|| format!("invalid JSONL object at line {line_number}"))?;
        let seq = events.len() as u64;
        let payload = reduce_score_event(
            &row,
            source_artifact_ref,
            source_artifact_digest,
            import_time,
            line_number,
        )?;
        let event = EvidenceEvent::new(EVENT_TYPE, EVENT_SOURCE, run_id, seq, payload)
            .with_time(import_time)
            .with_producer(producer);
        events.push(event);
    }

    if !saw_jsonl_row {
        bail!("input contains no JSONL rows");
    }

    Ok(events)
}

fn reduce_score_event(
    row: &Value,
    source_artifact_ref: &str,
    source_artifact_digest: &str,
    import_time: DateTime<Utc>,
    line_number: usize,
) -> Result<Value> {
    let record = row
        .as_object()
        .ok_or_else(|| anyhow::anyhow!("line {line_number} must be a JSON object"))?;
    validate_top_level(record, line_number)?;

    let timestamp = normalized_timestamp(record.get("timestamp"), "timestamp", line_number)?;
    let target_ref = bounded_string(
        record.get("target_ref"),
        "target_ref",
        MAX_BOUNDARY_STRING_CHARS,
        line_number,
    )?;
    let score = record
        .get("score")
        .and_then(Value::as_number)
        .ok_or_else(|| anyhow::anyhow!("line {line_number} score must be a number"))?
        .clone();

    let score_id_ref = optional_bounded_string(
        record.get("score_id_ref"),
        "score_id_ref",
        MAX_BOUNDARY_STRING_CHARS,
        line_number,
    )?;
    let scorer_id = optional_bounded_string(
        record.get("scorer_id"),
        "scorer_id",
        MAX_BOUNDARY_STRING_CHARS,
        line_number,
    )?;
    let scorer_name = optional_bounded_string(
        record.get("scorer_name"),
        "scorer_name",
        MAX_BOUNDARY_STRING_CHARS,
        line_number,
    )?;

    if scorer_id.is_none() && scorer_name.is_none() {
        bail!("line {line_number} missing scorer identity; expected scorer_id or scorer_name");
    }

    let mut score_event = Map::new();
    score_event.insert("score".to_string(), Value::Number(score));
    score_event.insert("target_ref".to_string(), Value::String(target_ref));
    score_event.insert("timestamp".to_string(), Value::String(timestamp));

    if let Some(score_id_ref) = score_id_ref {
        score_event.insert("score_id_ref".to_string(), Value::String(score_id_ref));
    }
    if let Some(scorer_id) = scorer_id {
        score_event.insert("scorer_id".to_string(), Value::String(scorer_id));
    }
    if let Some(scorer_name) = scorer_name {
        score_event.insert("scorer_name".to_string(), Value::String(scorer_name));
    }

    for field in [
        "scorer_version",
        "score_source",
        "trace_id_ref",
        "span_id_ref",
        "score_trace_id_ref",
        "target_entity_type",
        "metadata_ref",
    ] {
        if let Some(value) = optional_bounded_string(
            record.get(field),
            field,
            MAX_BOUNDARY_STRING_CHARS,
            line_number,
        )? {
            score_event.insert(field.to_string(), Value::String(value));
        }
    }

    if let Some(reason) = optional_bounded_string(
        record.get("reason"),
        "reason",
        MAX_REASON_CHARS,
        line_number,
    )? {
        score_event.insert("reason".to_string(), Value::String(reason));
    }

    Ok(json!({
        "schema": RECEIPT_SCHEMA,
        "source_system": SOURCE_SYSTEM,
        "source_surface": SOURCE_SURFACE,
        "source_artifact_ref": source_artifact_ref,
        "source_artifact_digest": source_artifact_digest,
        "reducer_version": REDUCER_VERSION,
        "imported_at": import_time.to_rfc3339_opts(SecondsFormat::Secs, true),
        "score_event": Value::Object(score_event),
    }))
}

fn validate_top_level(record: &Map<String, Value>, line_number: usize) -> Result<()> {
    let allowed = [
        "schema",
        "framework",
        "surface",
        "timestamp",
        "score",
        "target_ref",
        "score_id_ref",
        "scorer_id",
        "scorer_name",
        "scorer_version",
        "score_source",
        "reason",
        "trace_id_ref",
        "span_id_ref",
        "score_trace_id_ref",
        "target_entity_type",
        "metadata_ref",
    ];
    if let Some(key) = record.keys().find(|key| !allowed.contains(&key.as_str())) {
        bail!(
            "line {line_number} contains unsupported top-level key {key:?}; v1 imports reduced score-event artifacts and excludes raw metadata, correlationContext, traces, logs, metrics, and feedback"
        );
    }

    string_equals(record, "schema", INPUT_SCHEMA, line_number)?;
    string_equals(record, "framework", "mastra", line_number)?;
    string_equals(record, "surface", SOURCE_SURFACE, line_number)?;
    Ok(())
}

fn string_equals(
    record: &Map<String, Value>,
    key: &str,
    expected: &str,
    line_number: usize,
) -> Result<()> {
    match record.get(key).and_then(Value::as_str) {
        Some(actual) if actual == expected => Ok(()),
        Some(actual) => bail!("line {line_number} {key} must be {expected:?}, got {actual:?}"),
        None => bail!("line {line_number} missing string {key}"),
    }
}

fn bounded_string(
    value: Option<&Value>,
    field_name: &str,
    max_chars: usize,
    line_number: usize,
) -> Result<String> {
    let value = value
        .and_then(Value::as_str)
        .ok_or_else(|| anyhow::anyhow!("line {line_number} {field_name} must be a string"))?;
    validate_bounded_string(value, field_name, max_chars, line_number)
}

fn optional_bounded_string(
    value: Option<&Value>,
    field_name: &str,
    max_chars: usize,
    line_number: usize,
) -> Result<Option<String>> {
    let Some(value) = value else {
        return Ok(None);
    };
    if value.is_null() {
        return Ok(None);
    }
    Ok(Some(validate_bounded_string(
        value.as_str().ok_or_else(|| {
            anyhow::anyhow!("line {line_number} {field_name} must be a string or null")
        })?,
        field_name,
        max_chars,
        line_number,
    )?))
}

fn validate_bounded_string(
    value: &str,
    field_name: &str,
    max_chars: usize,
    line_number: usize,
) -> Result<String> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        bail!("line {line_number} {field_name} must not be empty");
    }
    if trimmed.chars().count() > max_chars {
        bail!("line {line_number} {field_name} must be at most {max_chars} characters");
    }
    if trimmed.contains('\n')
        || trimmed.contains('\r')
        || trimmed.contains('"')
        || trimmed.contains('`')
        || trimmed.contains('{')
        || trimmed.contains('}')
    {
        bail!("line {line_number} {field_name} is not reviewer-safe for v1");
    }
    Ok(trimmed.to_string())
}

fn normalized_timestamp(
    value: Option<&Value>,
    field_name: &str,
    line_number: usize,
) -> Result<String> {
    let value = value
        .and_then(Value::as_str)
        .ok_or_else(|| anyhow::anyhow!("line {line_number} {field_name} must be a string"))?;
    Ok(DateTime::parse_from_rfc3339(value)
        .with_context(|| format!("line {line_number} {field_name} must be RFC3339 with timezone"))?
        .with_timezone(&Utc)
        .to_rfc3339_opts(SecondsFormat::Millis, true))
}

fn parse_import_time(value: Option<&str>) -> Result<DateTime<Utc>> {
    match value {
        Some(value) => Ok(DateTime::parse_from_rfc3339(value)
            .with_context(|| format!("invalid --import-time {value:?}; expected RFC3339"))?
            .with_timezone(&Utc)),
        None => Ok(Utc::now()),
    }
}

fn default_source_artifact_ref(input: &Path) -> String {
    input
        .file_name()
        .and_then(|name| name.to_str())
        .filter(|name| !name.is_empty())
        .unwrap_or("mastra-score-events.jsonl")
        .to_string()
}

fn sha256_file(path: &Path) -> Result<String> {
    let file = File::open(path)?;
    let mut reader = BufReader::new(file);
    let mut hasher = Sha256::new();
    let mut buffer = [0_u8; 8192];
    loop {
        let read = reader.read(&mut buffer)?;
        if read == 0 {
            break;
        }
        hasher.update(&buffer[..read]);
    }
    Ok(format!("sha256:{}", hex::encode(hasher.finalize())))
}

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

    #[test]
    fn import_writes_verifiable_score_event_bundle() {
        let dir = tempfile::tempdir().unwrap();
        let input = dir.path().join("mastra-score-events.jsonl");
        let output = dir.path().join("mastra-score-events.tar.gz");
        fs::write(
            &input,
            concat!(
                r#"{"schema":"mastra.score-event.export.v1","framework":"mastra","surface":"observability.score_event","timestamp":"2026-04-30T10:31:38.858Z","score_id_ref":"f6605b31-af00-4b17-ae00-ed6262f4f411","scorer_id":"assay-scoreid-proof-scorer","score":0.91,"target_ref":"span:span-proof-001","trace_id_ref":"trace-proof-001","span_id_ref":"span-proof-001","score_trace_id_ref":"score-trace-proof-001","score_source":"live","metadata_ref":"metadata:scoreid-proof"}"#,
                "\n",
                r#"{"schema":"mastra.score-event.export.v1","framework":"mastra","surface":"observability.score_event","timestamp":"2026-04-15T18:58:12.297Z","scorer_name":"P14 Live Capture Scorer","score":0.18,"target_ref":"span:c4b7f4a58f2d90e1","trace_id_ref":"9f5bbab9073de1205f4a1de4925ad2b","span_id_ref":"c4b7f4a58f2d90e1","metadata_ref":"metadata:p14-live-capture"}"#,
                "\n"
            ),
        )
        .unwrap();

        let code = cmd_mastra_score_event(MastraScoreEventArgs {
            input: input.clone(),
            bundle_out: output.clone(),
            source_artifact_ref: Some("mastra-score-events.jsonl".to_string()),
            run_id: "mastra_test".to_string(),
            import_time: Some("2026-04-28T12:00:00Z".to_string()),
        })
        .unwrap();
        assert_eq!(code, exit_codes::OK);

        let reader = BundleReader::open(File::open(output).unwrap()).unwrap();
        assert_eq!(reader.manifest().event_count, 2);
        let events = reader.events().collect::<Result<Vec<_>>>().unwrap();
        assert_eq!(events[0].type_, EVENT_TYPE);
        assert_eq!(events[0].source, EVENT_SOURCE);
        assert_eq!(events[0].payload["source_surface"], SOURCE_SURFACE);
        assert_eq!(
            events[0].payload["score_event"]["scorer_id"],
            "assay-scoreid-proof-scorer"
        );
        assert_eq!(events[0].payload["score_event"]["score"], 0.91);
        assert_eq!(
            events[0].payload["score_event"]["timestamp"],
            "2026-04-30T10:31:38.858Z"
        );
        assert_eq!(
            events[0].payload["score_event"]["score_id_ref"],
            "f6605b31-af00-4b17-ae00-ed6262f4f411"
        );
        assert_eq!(
            events[0].payload["score_event"]["score_trace_id_ref"],
            "score-trace-proof-001"
        );
        assert_eq!(
            events[0].payload["score_event"]["metadata_ref"],
            "metadata:scoreid-proof"
        );
        assert_eq!(
            events[1].payload["score_event"]["scorer_name"],
            "P14 Live Capture Scorer"
        );
        assert_eq!(
            events[1].payload["score_event"]["metadata_ref"],
            "metadata:p14-live-capture"
        );

        let serialized = serde_json::to_string(&events).unwrap();
        assert!(!serialized.contains("correlationContext"));
        assert!(!serialized.contains("\"metadata\":"));
        assert!(!serialized.contains("exportedSpan"));
        assert!(!serialized.contains("feedback"));
    }

    #[test]
    fn import_rejects_raw_metadata_and_correlation_context() {
        let dir = tempfile::tempdir().unwrap();
        let input = dir.path().join("mastra-score-events.jsonl");
        let output = dir.path().join("mastra-score-events.tar.gz");
        fs::write(
            &input,
            r#"{"schema":"mastra.score-event.export.v1","framework":"mastra","surface":"observability.score_event","timestamp":"2026-04-15T18:53:12.297Z","scorer_id":"p14-live-capture-scorer","score":0.92,"target_ref":"span:7c4180655970aca2","metadata":{"traceDepth":2},"correlationContext":{"entityType":"agent"}}"#,
        )
        .unwrap();

        let err = cmd_mastra_score_event(MastraScoreEventArgs {
            input,
            bundle_out: output,
            source_artifact_ref: None,
            run_id: "mastra_test".to_string(),
            import_time: Some("2026-04-28T12:00:00Z".to_string()),
        })
        .unwrap_err();
        assert!(err
            .to_string()
            .contains("unsupported top-level key \"metadata\""));
    }

    #[test]
    fn import_rejects_raw_callback_score_object() {
        let dir = tempfile::tempdir().unwrap();
        let input = dir.path().join("mastra-score-events.jsonl");
        let output = dir.path().join("mastra-score-events.tar.gz");
        fs::write(
            &input,
            r#"{"schema":"mastra.score-event.export.v1","framework":"mastra","surface":"observability.score_event","timestamp":"2026-04-15T18:53:12.297Z","scorer_id":"p14-live-capture-scorer","score":{"score":0.92},"target_ref":"span:7c4180655970aca2"}"#,
        )
        .unwrap();

        let err = cmd_mastra_score_event(MastraScoreEventArgs {
            input,
            bundle_out: output,
            source_artifact_ref: None,
            run_id: "mastra_test".to_string(),
            import_time: Some("2026-04-28T12:00:00Z".to_string()),
        })
        .unwrap_err();
        assert!(err.to_string().contains("score must be a number"));
    }

    #[test]
    fn import_rejects_missing_scorer_identity() {
        let dir = tempfile::tempdir().unwrap();
        let input = dir.path().join("mastra-score-events.jsonl");
        let output = dir.path().join("mastra-score-events.tar.gz");
        fs::write(
            &input,
            r#"{"schema":"mastra.score-event.export.v1","framework":"mastra","surface":"observability.score_event","timestamp":"2026-04-15T18:53:12.297Z","score":0.92,"target_ref":"span:7c4180655970aca2"}"#,
        )
        .unwrap();

        let err = cmd_mastra_score_event(MastraScoreEventArgs {
            input,
            bundle_out: output,
            source_artifact_ref: None,
            run_id: "mastra_test".to_string(),
            import_time: Some("2026-04-28T12:00:00Z".to_string()),
        })
        .unwrap_err();
        assert!(err.to_string().contains("missing scorer identity"));
    }

    #[test]
    fn import_rejects_legacy_underscore_surface() {
        let dir = tempfile::tempdir().unwrap();
        let input = dir.path().join("mastra-score-events.jsonl");
        let output = dir.path().join("mastra-score-events.tar.gz");
        fs::write(
            &input,
            r#"{"schema":"mastra.score-event.export.v1","framework":"mastra","surface":"observability_score_event","timestamp":"2026-04-15T18:53:12.297Z","scorer_id":"p14-live-capture-scorer","score":0.92,"target_ref":"span:7c4180655970aca2"}"#,
        )
        .unwrap();

        let err = cmd_mastra_score_event(MastraScoreEventArgs {
            input,
            bundle_out: output,
            source_artifact_ref: None,
            run_id: "mastra_test".to_string(),
            import_time: Some("2026-04-28T12:00:00Z".to_string()),
        })
        .unwrap_err();
        assert!(err
            .to_string()
            .contains("surface must be \"observability.score_event\""));
    }
}