sim-lib-agent 0.1.2

Agent runtime surfaces for SIM.
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
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
//! Change Capsule review model for agent-operated Atelier edits.

use std::collections::BTreeSet;

use sim_kernel::{Expr, Result, Symbol};
use sim_lib_stream_core::{DevCassette, DevEvent, LatencyClass};

/// Auditable package of patches, validation evidence, pins, and replay data.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ChangeCapsule {
    /// Stable capsule id.
    pub id: Symbol,
    /// Repositories and files the capsule is allowed to touch.
    pub scope: CapsuleScope,
    /// Patch summaries included in the review.
    pub patches: Vec<CapsulePatch>,
    /// Generated artifacts produced by tooling.
    pub generated_artifacts: Vec<GeneratedArtifact>,
    /// Validation jobs and their placed execution sites.
    pub validations: Vec<CapsuleJob>,
    /// Documentation jobs and their placed execution sites.
    pub docs_runs: Vec<CapsuleJob>,
    /// Public repo commits produced by the capsule.
    pub commits: Vec<CapsuleCommit>,
    /// Push records for public commits.
    pub pushes: Vec<CapsulePush>,
    /// Planned `repos.toml` pin updates.
    pub pin_plan: Vec<PinPlanEntry>,
    /// Generated front-page changes.
    pub site_changes: Vec<GeneratedArtifact>,
    /// Human-review risk notes.
    pub risks: Vec<String>,
    /// Rollback notes for each affected repo.
    pub rollback_notes: Vec<String>,
    /// Placement plan for editing, validation, docs, pins, and capsule assembly.
    pub placement_plan: Vec<PlacedJob>,
    /// Recorded development cassette for replay.
    pub cassette: DevCassette,
    /// F6-style fairness and attribution facet.
    pub fairness: CapsuleFacet,
}

/// Scope declared by a Change Capsule.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapsuleScope {
    /// Repository names in scope.
    pub repos: Vec<String>,
    /// File or directory targets in scope.
    pub targets: Vec<String>,
}

/// One patch summarized for review.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapsulePatch {
    /// Repository containing the patch.
    pub repo: String,
    /// Path changed by the patch.
    pub path: String,
    /// Review summary.
    pub summary: String,
}

impl CapsulePatch {
    /// Builds a patch summary.
    pub fn new(
        repo: impl Into<String>,
        path: impl Into<String>,
        summary: impl Into<String>,
    ) -> Self {
        Self {
            repo: repo.into(),
            path: path.into(),
            summary: summary.into(),
        }
    }
}

/// Generated artifact or public front-page change.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GeneratedArtifact {
    /// Repository that owns the artifact.
    pub repo: String,
    /// Artifact path.
    pub path: String,
    /// Tool that generated or checked the artifact.
    pub generator: String,
    /// Whether the artifact is part of a generated public docs lane.
    pub generated_public_doc: bool,
    /// Whether the capsule attempted a hand edit.
    pub hand_edited: bool,
}

impl GeneratedArtifact {
    /// Builds a generated artifact record.
    pub fn generated(
        repo: impl Into<String>,
        path: impl Into<String>,
        generator: impl Into<String>,
    ) -> Self {
        Self {
            repo: repo.into(),
            path: path.into(),
            generator: generator.into(),
            generated_public_doc: true,
            hand_edited: false,
        }
    }
}

/// Validation or docs job included in a capsule.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapsuleJob {
    /// Job kind.
    pub kind: CapsuleJobKind,
    /// Stable job label.
    pub label: String,
    /// Command line or typed command template.
    pub command: String,
    /// Job placement.
    pub site: JobSite,
    /// Review outcome.
    pub outcome: CapsuleJobOutcome,
    /// Evidence log path.
    pub log_path: String,
}

impl CapsuleJob {
    /// Builds a passed validation job on a process site.
    pub fn validation(label: impl Into<String>, command: impl Into<String>) -> Self {
        let label = label.into();
        Self {
            kind: CapsuleJobKind::Validation,
            label: label.clone(),
            command: command.into(),
            site: JobSite::Process,
            outcome: CapsuleJobOutcome::Passed,
            log_path: format!(".sim/atelier/logs/{label}.log"),
        }
    }

    /// Builds a passed docs job on a process site.
    pub fn docs(label: impl Into<String>, command: impl Into<String>) -> Self {
        let label = label.into();
        Self {
            kind: CapsuleJobKind::Docs,
            label: label.clone(),
            command: command.into(),
            site: JobSite::Process,
            outcome: CapsuleJobOutcome::Passed,
            log_path: format!(".sim/atelier/logs/{label}.log"),
        }
    }

    /// Returns a copy marked as failed.
    pub fn failed(mut self) -> Self {
        self.outcome = CapsuleJobOutcome::Failed;
        self
    }
}

/// Capsule job kind.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CapsuleJobKind {
    /// Validation command.
    Validation,
    /// Documentation command.
    Docs,
}

impl CapsuleJobKind {
    /// Stable kind label.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Validation => "validation",
            Self::Docs => "docs",
        }
    }
}

/// Result of a capsule job.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CapsuleJobOutcome {
    /// Job passed.
    Passed,
    /// Job failed.
    Failed,
}

/// Site class used for placed validation and docs jobs.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum JobSite {
    /// Local coroutine site for edit and assembly work.
    LocalCoroutine,
    /// Process site reached through `realize`.
    Process,
    /// Fabric site reached through `realize`.
    Fabric,
}

impl JobSite {
    /// Stable site label.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::LocalCoroutine => "local-coroutine",
            Self::Process => "process",
            Self::Fabric => "fabric",
        }
    }

    /// SUP.20 realization operation used for this placement.
    pub fn realize_operation(self) -> &'static str {
        match self {
            Self::LocalCoroutine => "local-coroutine",
            Self::Process | Self::Fabric => "realize",
        }
    }

    fn can_run_validation(self) -> bool {
        matches!(self, Self::Process | Self::Fabric)
    }
}

/// One public commit referenced by a capsule.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapsuleCommit {
    /// Repository name.
    pub repo: String,
    /// Commit hash.
    pub hash: String,
}

/// One push record referenced by a capsule.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapsulePush {
    /// Repository name.
    pub repo: String,
    /// Remote name or URL label.
    pub remote: String,
    /// Commit hash that was pushed.
    pub hash: String,
}

/// Planned `repos.toml` pin update.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PinPlanEntry {
    /// Repository name.
    pub repo: String,
    /// Current pinned commit.
    pub current_commit: String,
    /// New commit to pin.
    pub new_commit: String,
    /// Whether the new commit exists on the upstream remote.
    pub pushed_commit_exists: bool,
}

/// One placement-plan row.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PlacedJob {
    /// Job label such as `edit`, `validation`, `docs`, or `capsule-assembly`.
    pub label: String,
    /// Site used for the job.
    pub site: JobSite,
}

impl PlacedJob {
    /// Builds a placement row.
    pub fn new(label: impl Into<String>, site: JobSite) -> Self {
        Self {
            label: label.into(),
            site,
        }
    }
}

/// F6-style fairness facet attached to a capsule.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapsuleFacet {
    /// Facet label.
    pub label: String,
    /// Evidence summary.
    pub evidence: String,
    /// Confidence token.
    pub confidence: String,
}

/// Deterministic review output for a Change Capsule.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CapsuleReview {
    /// Whether the capsule is accepted.
    pub accepted: bool,
    /// Original cassette content hash.
    pub content_hash: String,
    /// Replay-computed content hash.
    pub replay_content_hash: String,
    /// Repositories previewed before pin edits.
    pub preview_repos: Vec<String>,
    /// Review failures.
    pub failure_reasons: Vec<String>,
}

/// Reviews a capsule against replay, placement, conformance, docs, and pin rules.
pub fn review_change_capsule(capsule: &ChangeCapsule) -> Result<CapsuleReview> {
    let replay_content_hash = capsule.cassette.replay_content_hash()?;
    let mut failure_reasons = Vec::new();
    if capsule.cassette.content_hash() != replay_content_hash {
        failure_reasons.push("cassette replay content hash mismatch".to_owned());
    }
    check_jobs(&capsule.validations, &mut failure_reasons);
    check_jobs(&capsule.docs_runs, &mut failure_reasons);
    check_placements(&capsule.placement_plan, &mut failure_reasons);
    check_generated_docs(&capsule.generated_artifacts, &mut failure_reasons);
    check_generated_docs(&capsule.site_changes, &mut failure_reasons);
    check_pins(&capsule.pin_plan, &mut failure_reasons);

    Ok(CapsuleReview {
        accepted: failure_reasons.is_empty(),
        content_hash: capsule.cassette.content_hash().to_owned(),
        replay_content_hash,
        preview_repos: preview_repos(capsule),
        failure_reasons,
    })
}

/// Builds a deterministic capsule fixture used by agents and view tests.
pub fn fake_change_capsule() -> Result<ChangeCapsule> {
    let node = Symbol::qualified("atelier/agent", "change-capsule");
    let cassette = DevCassette::from_events(
        Symbol::qualified("atelier/dev", "change-capsule-fixture"),
        vec![
            DevEvent::edit(node.clone(), summary("Patch capsule model"))?,
            DevEvent::validate(node.clone(), summary("cargo test change_capsule"))?,
            DevEvent::new(
                "docs",
                node.clone(),
                LatencyClass::OfflineRender,
                summary("simdoc check current"),
            )?,
            DevEvent::new(
                "pin",
                node.clone(),
                LatencyClass::Interactive,
                summary("pushed commit exists before pin"),
            )?,
            DevEvent::new(
                "reflect",
                node,
                LatencyClass::OfflineRender,
                summary("F6 facet records risk and rollback"),
            )?,
        ],
    )?;

    Ok(ChangeCapsule {
        id: Symbol::qualified("atelier/capsule", "fixture"),
        scope: CapsuleScope {
            repos: vec!["sim-agent-net".to_owned(), "sim-tooling".to_owned()],
            targets: vec![
                "crates/sim-lib-agent/src/atelier/capsule.rs".to_owned(),
                "src/atelier/capsule.rs".to_owned(),
            ],
        },
        patches: vec![
            CapsulePatch::new(
                "sim-agent-net",
                "crates/sim-lib-agent/src/atelier/capsule.rs",
                "agent capsule model",
            ),
            CapsulePatch::new("sim-tooling", "src/atelier/capsule.rs", "capsule cache"),
        ],
        generated_artifacts: vec![GeneratedArtifact::generated(
            "sim-tooling",
            "docs/generated/contract.md",
            "xtask simdoc",
        )],
        validations: vec![CapsuleJob::validation(
            "agent-capsule-tests",
            "cargo test -p sim-lib-agent change_capsule",
        )],
        docs_runs: vec![CapsuleJob::docs(
            "simdoc-agent-net",
            "cargo run -p xtask -- simdoc --check",
        )],
        commits: vec![CapsuleCommit {
            repo: "sim-agent-net".to_owned(),
            hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
        }],
        pushes: vec![CapsulePush {
            repo: "sim-agent-net".to_owned(),
            remote: "origin".to_owned(),
            hash: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
        }],
        pin_plan: vec![PinPlanEntry {
            repo: "sim-agent-net".to_owned(),
            current_commit: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb".to_owned(),
            new_commit: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_owned(),
            pushed_commit_exists: true,
        }],
        site_changes: vec![GeneratedArtifact::generated(
            "repo-docs",
            "docs/site/repos.md",
            "simctl site",
        )],
        risks: vec!["review capsule scope before pinning".to_owned()],
        rollback_notes: vec!["reset pin to previous commit and rerun site".to_owned()],
        placement_plan: vec![
            PlacedJob::new("edit", JobSite::LocalCoroutine),
            PlacedJob::new("capsule-assembly", JobSite::LocalCoroutine),
            PlacedJob::new("validation", JobSite::Process),
            PlacedJob::new("docs", JobSite::Process),
            PlacedJob::new("pin-plan", JobSite::LocalCoroutine),
        ],
        cassette,
        fairness: CapsuleFacet {
            label: "F6 trade-off".to_owned(),
            evidence: "validation, docs, pin, and rollback evidence recorded".to_owned(),
            confidence: "0.92".to_owned(),
        },
    })
}

fn check_jobs(jobs: &[CapsuleJob], failure_reasons: &mut Vec<String>) {
    for job in jobs {
        if !job.site.can_run_validation() {
            failure_reasons.push(format!(
                "{} job {} must run on process or fabric site",
                job.kind.as_str(),
                job.label
            ));
        }
        if job.site.realize_operation() != "realize" {
            failure_reasons.push(format!(
                "{} job {} is not realized",
                job.kind.as_str(),
                job.label
            ));
        }
        if job.outcome == CapsuleJobOutcome::Failed {
            failure_reasons.push(format!("{} job {} failed", job.kind.as_str(), job.label));
        }
    }
}

fn check_placements(placements: &[PlacedJob], failure_reasons: &mut Vec<String>) {
    for label in ["edit", "capsule-assembly"] {
        let local = placements
            .iter()
            .any(|job| job.label == label && job.site == JobSite::LocalCoroutine);
        if !local {
            failure_reasons.push(format!("{label} must stay on the local coroutine site"));
        }
    }
    for label in ["validation", "docs"] {
        let realized = placements
            .iter()
            .any(|job| job.label == label && job.site.can_run_validation());
        if !realized {
            failure_reasons.push(format!("{label} must be placed on process or fabric site"));
        }
    }
}

fn check_generated_docs(artifacts: &[GeneratedArtifact], failure_reasons: &mut Vec<String>) {
    for artifact in artifacts {
        if artifact.generated_public_doc && artifact.hand_edited {
            failure_reasons.push(format!(
                "generated public doc {}:{} must be regenerated, not hand-edited",
                artifact.repo, artifact.path
            ));
        }
    }
}

fn check_pins(pins: &[PinPlanEntry], failure_reasons: &mut Vec<String>) {
    for pin in pins {
        if !pin.pushed_commit_exists {
            failure_reasons.push(format!(
                "pin plan for {} requires an existing pushed upstream commit",
                pin.repo
            ));
        }
    }
}

fn preview_repos(capsule: &ChangeCapsule) -> Vec<String> {
    let mut repos = BTreeSet::new();
    repos.extend(capsule.scope.repos.iter().cloned());
    repos.extend(capsule.pin_plan.iter().map(|pin| pin.repo.clone()));
    repos.into_iter().collect()
}

fn summary(text: &str) -> Expr {
    Expr::Map(vec![(
        Expr::Symbol(Symbol::new("summary")),
        Expr::String(text.to_owned()),
    )])
}