a3s-code-core 8.5.1

A3S Code Core - Embeddable AI agent library with tool execution
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
//! Typed reproducibility manifest for one research Run.

use super::{
    digest, validate_digest_field, validate_id, ResearchContractError, ResearchProvenanceReceiptV1,
    ResearchReproducibilityV1, ResearchRunV1, RESEARCH_MAX_DIGESTS,
};
use serde::{Deserialize, Serialize};

pub const RESEARCH_REPRODUCIBILITY_MANIFEST_SCHEMA_V1: &str =
    "a3s.code.reproducibility-manifest.v1";
const RESEARCH_REPRODUCIBILITY_MANIFEST_DIGEST_DOMAIN: &str =
    "a3s.code.reproducibility-manifest.identity.v1";

/// Digests and non-secret parameters that make one research Run reproducible.
///
/// The manifest never stores prompts, credentials, or raw provider payloads.
/// Hosts retain those behind digests. Code only fences identity drift against
/// the admitted Run and optional provenance receipts.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ResearchReproducibilityManifestV1 {
    pub schema: String,
    pub manifest_id: String,
    pub project_id: String,
    pub run_id: String,
    pub provider_id: String,
    pub model_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub model_revision_digest: Option<String>,
    pub environment_lock_digest: String,
    pub code_digest: String,
    pub workflow_digest: String,
    pub parameter_digest: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub random_seed: Option<u64>,
    pub tolerance_digests: Vec<String>,
    pub output_artifact_digests: Vec<String>,
    pub manifest_digest: String,
}

impl ResearchReproducibilityManifestV1 {
    #[allow(clippy::too_many_arguments)]
    pub fn new_for_run(
        manifest_id: impl Into<String>,
        run: &ResearchRunV1,
        model_revision_digest: Option<String>,
        environment_lock_digest: impl Into<String>,
        code_digest: impl Into<String>,
        workflow_digest: impl Into<String>,
        parameter_digest: impl Into<String>,
        mut tolerance_digests: Vec<String>,
        mut output_artifact_digests: Vec<String>,
    ) -> Result<Self, ResearchContractError> {
        tolerance_digests.sort();
        tolerance_digests.dedup();
        output_artifact_digests.sort();
        output_artifact_digests.dedup();
        let mut manifest = Self {
            schema: RESEARCH_REPRODUCIBILITY_MANIFEST_SCHEMA_V1.to_owned(),
            manifest_id: manifest_id.into(),
            project_id: run.project_id.clone(),
            run_id: run.run_id.clone(),
            provider_id: run.provider_id.clone(),
            model_id: run.model_id.clone(),
            model_revision_digest,
            environment_lock_digest: environment_lock_digest.into(),
            code_digest: code_digest.into(),
            workflow_digest: workflow_digest.into(),
            parameter_digest: parameter_digest.into(),
            random_seed: run.random_seed,
            tolerance_digests,
            output_artifact_digests,
            manifest_digest: String::new(),
        };
        manifest.validate_without_digest()?;
        manifest.validate_against_run(run)?;
        manifest.manifest_digest = manifest.expected_digest()?;
        Ok(manifest)
    }

    pub fn validate_for_run(&self, run: &ResearchRunV1) -> Result<(), ResearchContractError> {
        self.validate()?;
        self.validate_against_run(run)
    }

    /// Require every output artifact in this manifest to appear in the supplied
    /// provenance receipts for the same project/Run, with matching environment
    /// and workflow digests.
    pub fn validate_against_provenance(
        &self,
        receipts: &[ResearchProvenanceReceiptV1],
    ) -> Result<(), ResearchContractError> {
        self.validate()?;
        if self.output_artifact_digests.is_empty() {
            return Err(ResearchContractError::InvalidField("outputArtifactDigests"));
        }
        let mut seen = std::collections::BTreeSet::new();
        for receipt in receipts {
            receipt.validate()?;
            if receipt.project_id != self.project_id {
                return Err(ResearchContractError::InvalidField("receipt.projectId"));
            }
            if receipt.run_id != self.run_id {
                return Err(ResearchContractError::InvalidField("receipt.runId"));
            }
            if receipt.environment_digest != self.environment_lock_digest {
                return Err(ResearchContractError::InvalidField("environmentLockDigest"));
            }
            if receipt.workflow_digest != self.workflow_digest {
                return Err(ResearchContractError::InvalidField("workflowDigest"));
            }
            if receipt.code_digest != self.code_digest {
                return Err(ResearchContractError::InvalidField("codeDigest"));
            }
            if receipt.provider_id != self.provider_id {
                return Err(ResearchContractError::InvalidField("providerId"));
            }
            if receipt.random_seed != self.random_seed {
                return Err(ResearchContractError::InvalidField("randomSeed"));
            }
            if !self
                .output_artifact_digests
                .iter()
                .any(|digest| digest == &receipt.artifact_digest)
            {
                return Err(ResearchContractError::InvalidField("artifactDigest"));
            }
            if !seen.insert(receipt.artifact_digest.as_str()) {
                return Err(ResearchContractError::InvalidField("artifactDigest"));
            }
        }
        for output in &self.output_artifact_digests {
            if !seen.contains(output.as_str()) {
                return Err(ResearchContractError::InvalidField("outputArtifactDigests"));
            }
        }
        Ok(())
    }

    pub fn validate(&self) -> Result<(), ResearchContractError> {
        self.validate_without_digest()?;
        validate_digest_field("manifestDigest", &self.manifest_digest)?;
        if self.manifest_digest != self.expected_digest()? {
            return Err(ResearchContractError::DigestMismatch("manifestDigest"));
        }
        Ok(())
    }

    pub fn from_slice(bytes: &[u8]) -> Result<Self, ResearchContractError> {
        let manifest: Self = super::decode_json_slice(bytes)?;
        manifest.validate()?;
        Ok(manifest)
    }

    pub fn to_vec(&self) -> Result<Vec<u8>, ResearchContractError> {
        self.validate()?;
        super::encode_json(self)
    }

    fn validate_against_run(&self, run: &ResearchRunV1) -> Result<(), ResearchContractError> {
        if self.project_id != run.project_id {
            return Err(ResearchContractError::InvalidField("projectId"));
        }
        if self.run_id != run.run_id {
            return Err(ResearchContractError::InvalidField("runId"));
        }
        if self.provider_id != run.provider_id {
            return Err(ResearchContractError::InvalidField("providerId"));
        }
        if self.model_id != run.model_id {
            return Err(ResearchContractError::InvalidField("modelId"));
        }
        if self.random_seed != run.random_seed {
            return Err(ResearchContractError::InvalidField("randomSeed"));
        }
        if matches!(
            run.reproducibility,
            ResearchReproducibilityV1::Deterministic
        ) && self.random_seed.is_none()
        {
            return Err(ResearchContractError::InvalidField("randomSeed"));
        }
        Ok(())
    }

    fn validate_without_digest(&self) -> Result<(), ResearchContractError> {
        if self.schema != RESEARCH_REPRODUCIBILITY_MANIFEST_SCHEMA_V1 {
            return Err(ResearchContractError::UnsupportedSchema);
        }
        validate_id("manifestId", &self.manifest_id)?;
        validate_id("projectId", &self.project_id)?;
        validate_id("runId", &self.run_id)?;
        validate_id("providerId", &self.provider_id)?;
        validate_id("modelId", &self.model_id)?;
        if let Some(model_revision_digest) = &self.model_revision_digest {
            validate_digest_field("modelRevisionDigest", model_revision_digest)?;
        }
        validate_digest_field("environmentLockDigest", &self.environment_lock_digest)?;
        validate_digest_field("codeDigest", &self.code_digest)?;
        validate_digest_field("workflowDigest", &self.workflow_digest)?;
        validate_digest_field("parameterDigest", &self.parameter_digest)?;
        if self.tolerance_digests.len() > RESEARCH_MAX_DIGESTS {
            return Err(ResearchContractError::InvalidField("toleranceDigests"));
        }
        if self.output_artifact_digests.len() > RESEARCH_MAX_DIGESTS {
            return Err(ResearchContractError::InvalidField("outputArtifactDigests"));
        }
        validate_sorted_unique_digests("toleranceDigests", &self.tolerance_digests)?;
        validate_sorted_unique_digests("outputArtifactDigests", &self.output_artifact_digests)?;
        Ok(())
    }

    fn expected_digest(&self) -> Result<String, ResearchContractError> {
        #[derive(Serialize)]
        struct Identity<'a> {
            schema: &'a str,
            manifest_id: &'a str,
            project_id: &'a str,
            run_id: &'a str,
            provider_id: &'a str,
            model_id: &'a str,
            model_revision_digest: Option<&'a str>,
            environment_lock_digest: &'a str,
            code_digest: &'a str,
            workflow_digest: &'a str,
            parameter_digest: &'a str,
            random_seed: Option<u64>,
            tolerance_digests: &'a [String],
            output_artifact_digests: &'a [String],
        }
        digest(
            RESEARCH_REPRODUCIBILITY_MANIFEST_DIGEST_DOMAIN,
            &Identity {
                schema: &self.schema,
                manifest_id: &self.manifest_id,
                project_id: &self.project_id,
                run_id: &self.run_id,
                provider_id: &self.provider_id,
                model_id: &self.model_id,
                model_revision_digest: self.model_revision_digest.as_deref(),
                environment_lock_digest: &self.environment_lock_digest,
                code_digest: &self.code_digest,
                workflow_digest: &self.workflow_digest,
                parameter_digest: &self.parameter_digest,
                random_seed: self.random_seed,
                tolerance_digests: &self.tolerance_digests,
                output_artifact_digests: &self.output_artifact_digests,
            },
        )
    }
}

fn validate_sorted_unique_digests(
    field: &'static str,
    digests: &[String],
) -> Result<(), ResearchContractError> {
    for pair in digests.windows(2) {
        if pair[0] >= pair[1] {
            return Err(ResearchContractError::InvalidField(field));
        }
    }
    for value in digests {
        validate_digest_field(field, value)?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::capability::{
        CapabilityCeiling, CapabilityContribution, CapabilityDescriptor,
        CapabilityExecutionCeiling, CapabilityKind, CapabilitySet, CapabilitySource,
        CodeCatalogGeneration, GovernanceCapabilityCeiling, RunCapabilityBindingV1, Sha256Digest,
        WorkspaceCapabilityCeiling,
    };
    use crate::research::{ResearchArtifactKindV1, ResearchRunStatusV1};

    fn digest(ch: char) -> String {
        format!("sha256:{}", ch.to_string().repeat(64))
    }

    fn binding() -> RunCapabilityBindingV1 {
        let source =
            CapabilitySource::builtin("test", Sha256Digest::new(digest('c')).unwrap()).unwrap();
        let descriptor = CapabilityDescriptor::new(
            &source,
            CapabilityKind::Tool,
            "tool",
            "tool",
            Sha256Digest::new(digest('d')).unwrap(),
            [],
        )
        .unwrap();
        let contribution = CapabilityContribution::new(source, [descriptor]).unwrap();
        let set = CapabilitySet::from_contributions(CodeCatalogGeneration::new(1), [contribution])
            .unwrap();
        let ceiling = CapabilityCeiling::all(
            &set,
            WorkspaceCapabilityCeiling::default(),
            GovernanceCapabilityCeiling::default(),
            CapabilityExecutionCeiling::new(1, 1, None, None, None).unwrap(),
        )
        .unwrap();
        RunCapabilityBindingV1::from_set_and_ceiling(&set, &ceiling).unwrap()
    }

    fn admitted_run() -> ResearchRunV1 {
        let mut run = ResearchRunV1::new(
            "run-1",
            "project-1",
            1,
            digest('1'),
            digest('2'),
            binding(),
            "provider-1",
            "model-1",
            ResearchReproducibilityV1::Deterministic,
            Some(7),
        )
        .unwrap();
        run.transition_to(ResearchRunStatusV1::Admitted).unwrap();
        run
    }

    #[test]
    fn manifest_binds_run_and_provenance_outputs() {
        let run = admitted_run();
        let manifest = ResearchReproducibilityManifestV1::new_for_run(
            "manifest-1",
            &run,
            Some(digest('3')),
            digest('4'),
            digest('5'),
            digest('6'),
            digest('7'),
            vec![digest('9'), digest('8')],
            vec![digest('a'), digest('b')],
        )
        .unwrap();
        assert_eq!(manifest.random_seed, Some(7));
        assert_eq!(manifest.tolerance_digests, vec![digest('8'), digest('9')]);
        let receipt_a = ResearchProvenanceReceiptV1::new(
            "project-1",
            1,
            "run-1",
            "artifact-a",
            ResearchArtifactKindV1::Figure,
            digest('a'),
            vec![digest('2')],
            digest('6'),
            digest('5'),
            digest('4'),
            "provider-1",
            Some(digest('3')),
            Some(7),
            None,
        )
        .unwrap();
        let receipt_b = ResearchProvenanceReceiptV1::new(
            "project-1",
            1,
            "run-1",
            "artifact-b",
            ResearchArtifactKindV1::Report,
            digest('b'),
            vec![digest('2')],
            digest('6'),
            digest('5'),
            digest('4'),
            "provider-1",
            Some(digest('3')),
            Some(7),
            None,
        )
        .unwrap();
        manifest
            .validate_against_provenance(&[receipt_a, receipt_b])
            .unwrap();
        let encoded = manifest.to_vec().unwrap();
        assert_eq!(
            ResearchReproducibilityManifestV1::from_slice(&encoded).unwrap(),
            manifest
        );
    }

    #[test]
    fn seed_and_output_drift_fail_closed() {
        let run = admitted_run();
        let manifest = ResearchReproducibilityManifestV1::new_for_run(
            "manifest-1",
            &run,
            None,
            digest('4'),
            digest('5'),
            digest('6'),
            digest('7'),
            Vec::new(),
            vec![digest('a')],
        )
        .unwrap();
        let mut drifted = run.clone();
        drifted.random_seed = Some(8);
        assert!(matches!(
            manifest.validate_for_run(&drifted),
            Err(ResearchContractError::InvalidField("randomSeed"))
        ));
        let foreign = ResearchProvenanceReceiptV1::new(
            "project-1",
            1,
            "run-1",
            "artifact-x",
            ResearchArtifactKindV1::Figure,
            digest('c'),
            vec![digest('2')],
            digest('6'),
            digest('5'),
            digest('4'),
            "provider-1",
            None,
            Some(7),
            None,
        )
        .unwrap();
        assert!(matches!(
            manifest.validate_against_provenance(&[foreign]),
            Err(ResearchContractError::InvalidField("artifactDigest"))
        ));
    }
}