1use std::collections::BTreeSet;
28
29use serde::{Deserialize, Serialize};
30
31use crate::ValidationError;
32
33pub const GATE_RECORD_SCHEMA: &str = "leanctx.cloud.process.gate/v1";
35pub const PROCESS_VERSION: &str = "1.0";
37pub const RULE_LOCKED_PATHS_CHANGED: &str = "locked_paths_changed";
39pub const MAX_LOCKED_PATHS: usize = 500;
41pub const MISSING_CI_PIPELINE: &str = "ci_pipeline";
43pub const CLOUD_CLOUD_PROJECT: u64 = 37;
45pub const CLOUD_CLOUD_PG_JOB: &str = "test-pg";
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
49pub enum TaskRisk {
50 S,
51 M,
52 H,
53}
54
55impl TaskRisk {
56 pub fn has_spec(self) -> bool {
58 matches!(self, Self::M | Self::H)
59 }
60}
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
64#[serde(rename_all = "snake_case")]
65pub enum LockedChange {
66 Modified,
68 Added,
70 Deleted,
72 Untracked,
74 TypeChanged,
76 OutsideRepo,
79}
80
81impl LockedChange {
82 pub fn as_str(self) -> &'static str {
83 match self {
84 Self::Modified => "modified",
85 Self::Added => "added",
86 Self::Deleted => "deleted",
87 Self::Untracked => "untracked",
88 Self::TypeChanged => "type_changed",
89 Self::OutsideRepo => "outside_repo",
90 }
91 }
92}
93
94#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
96pub struct ChangedPathV1 {
97 pub path: String,
98 pub change: LockedChange,
99}
100
101#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
103pub struct LockedPathsV1 {
104 pub spec_commit: String,
105 pub checked: Vec<String>,
108 pub ok: bool,
109 pub changed: Vec<ChangedPathV1>,
110}
111
112impl LockedPathsV1 {
113 pub fn validate(&self) -> Result<(), ValidationError> {
116 validate_sha(&self.spec_commit, "locked_paths.spec_commit")?;
117 if self.checked.len() > MAX_LOCKED_PATHS || self.changed.len() > MAX_LOCKED_PATHS {
118 return Err(ValidationError::new("too many locked paths in one record"));
119 }
120 let mut seen = BTreeSet::new();
121 for path in &self.checked {
122 validate_repo_path(path)?;
123 if !seen.insert(path.as_str()) {
124 return Err(ValidationError::new("locked_paths.checked repeats a path"));
125 }
126 }
127 if self.ok != self.changed.is_empty() {
128 return Err(ValidationError::new(
129 "locked_paths.ok is true exactly when changed is empty",
130 ));
131 }
132 let mut paths = BTreeSet::new();
133 for item in &self.changed {
134 validate_repo_path(&item.path)?;
135 if !paths.insert(item.path.as_str()) {
136 return Err(ValidationError::new(
137 "locked_paths.changed names each path once (locked_paths_unique)",
138 ));
139 }
140 if !self.checked.iter().any(|c| under_locked(&item.path, c)) {
141 return Err(ValidationError::new(
142 "every changed path lies under a checked path (locked_paths_changed_unchecked)",
143 ));
144 }
145 }
146 Ok(())
147 }
148}
149
150#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
152pub struct FeatureResultV1 {
153 pub id: String,
154 pub passes: bool,
155}
156
157#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
160pub struct TestStatusV1 {
161 pub test: String,
162 pub status: String,
163}
164
165#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
171pub struct RerunReportV1 {
172 pub format: String,
173 pub sha256: String,
174 pub passed: u64,
175 pub failed: u64,
176 pub ignored: u64,
177 pub names_checked: Vec<TestStatusV1>,
178}
179
180#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
182pub struct RerunV1 {
183 pub command: String,
184 #[serde(default, skip_serializing_if = "Option::is_none")]
185 pub e2e_command: Option<String>,
186 pub host: String,
187 pub commit: String,
189 pub spec_commit: String,
190 pub exit_code: i32,
191 pub log_sha256: String,
192 pub started_at: String,
193 pub finished_at: String,
194 pub features: Vec<FeatureResultV1>,
195 #[serde(default, skip_serializing_if = "Option::is_none")]
197 pub report: Option<RerunReportV1>,
198}
199
200#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
202#[serde(rename_all = "snake_case")]
203pub enum GateResult {
204 Pass,
205 Fail,
206 Blocked,
207}
208
209#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
220#[serde(deny_unknown_fields)]
221pub struct G6RecordV1 {
222 pub schema: String,
223 pub process: String,
224 pub gate: String,
225 pub risk: TaskRisk,
226 pub artifact_commit: String,
228 pub result: GateResult,
229 #[serde(default, skip_serializing_if = "Vec::is_empty")]
231 pub missing: Vec<String>,
232 pub locked_paths: LockedPathsV1,
233 #[serde(default, skip_serializing_if = "Option::is_none")]
236 pub rerun: Option<RerunV1>,
237 #[serde(default, skip_serializing_if = "Option::is_none")]
238 pub rule: Option<String>,
239 #[serde(default, skip_serializing_if = "Option::is_none")]
240 pub reason: Option<String>,
241}
242
243impl G6RecordV1 {
244 pub fn ready_for_cloud(&self) -> bool {
249 self.validate().is_ok()
250 && self.result == GateResult::Blocked
251 && self.missing == [MISSING_CI_PIPELINE]
252 && self.locked_paths.ok
253 && self.rerun.as_ref().is_some_and(|r| r.exit_code == 0)
254 }
255
256 pub fn judges(&self, commit: &str) -> bool {
258 self.artifact_commit == commit
259 }
260
261 pub fn validate(&self) -> Result<(), ValidationError> {
262 if self.schema != GATE_RECORD_SCHEMA {
263 return Err(ValidationError::new(format!(
264 "schema must be {GATE_RECORD_SCHEMA}"
265 )));
266 }
267 if self.process != PROCESS_VERSION || self.gate != "G6" {
268 return Err(ValidationError::new("a G6 record of process 1.0"));
269 }
270 validate_sha(&self.artifact_commit, "artifact_commit")?;
271 validate_core(
272 self.risk,
273 self.result,
274 &self.missing,
275 &self.locked_paths,
276 self.rerun.as_ref(),
277 self.rule.as_deref(),
278 self.reason.as_deref(),
279 &self.artifact_commit,
280 )?;
281 if self.result == GateResult::Pass {
282 return Err(ValidationError::new(
283 "a pass needs ci (§5.1), which the Instance does not read: its record is blocked on ci_pipeline",
284 ));
285 }
286 Ok(())
287 }
288}
289
290#[allow(clippy::too_many_arguments)]
292fn validate_core(
293 risk: TaskRisk,
294 result: GateResult,
295 missing: &[String],
296 locked_paths: &LockedPathsV1,
297 rerun: Option<&RerunV1>,
298 rule: Option<&str>,
299 reason: Option<&str>,
300 artifact: &str,
301) -> Result<(), ValidationError> {
302 locked_paths.validate()?;
303 if risk.has_spec() == locked_paths.checked.is_empty() {
304 return Err(ValidationError::new(
305 "an M or H task checks its feature list's locked paths; an S task checks none",
306 ));
307 }
308 let rule_ok = match (rule, locked_paths.ok) {
309 (None, true) => true,
310 (Some(rule), false) => rule == RULE_LOCKED_PATHS_CHANGED,
311 _ => false,
312 };
313 if !rule_ok || (!locked_paths.ok && result != GateResult::Fail) {
314 return Err(ValidationError::new(
315 "a locked-path change is a fail with rule locked_paths_changed, and that rule appears on nothing else",
316 ));
317 }
318 match result {
319 GateResult::Blocked if missing.is_empty() => {
320 return Err(ValidationError::new(
321 "a blocked record lists what is missing",
322 ));
323 }
324 GateResult::Pass if !missing.is_empty() => {
325 return Err(ValidationError::new("a pass misses nothing"));
326 }
327 _ => {}
328 }
329 if result != GateResult::Pass {
330 let reason_ok = reason.is_some_and(|r| (1..=2000).contains(&r.chars().count()));
331 if !reason_ok {
332 return Err(ValidationError::new(
333 "a fail or blocked record gives a reason (1-2000 chars)",
334 ));
335 }
336 }
337 match rerun {
338 None if locked_paths.ok => {
339 return Err(ValidationError::new(
340 "a G6 record with clean locked paths carries the gate's rerun",
341 ));
342 }
343 None => {}
344 Some(rerun) => {
345 validate_sha(&rerun.commit, "rerun.commit")?;
346 validate_sha(&rerun.spec_commit, "rerun.spec_commit")?;
347 if rerun.commit != artifact {
348 return Err(ValidationError::new(
349 "rerun.commit is the judged commit (rerun_commit)",
350 ));
351 }
352 if rerun.spec_commit != locked_paths.spec_commit {
353 return Err(ValidationError::new(
354 "locked_paths.spec_commit is the rerun's (locked_paths_spec_commit)",
355 ));
356 }
357 if !risk.has_spec() && !rerun.features.is_empty() {
358 return Err(ValidationError::new(
359 "an S record carries no feature results",
360 ));
361 }
362 if rerun.command.trim().is_empty() || rerun.command.chars().count() > 2000 {
363 return Err(ValidationError::new("rerun.command is 1-2000 chars"));
364 }
365 if result != GateResult::Fail && rerun.exit_code != 0 {
366 return Err(ValidationError::new(
367 "a red rerun is a fail, never a pass or blocked",
368 ));
369 }
370 if result != GateResult::Fail && risk.has_spec() && rerun.report.is_none() {
374 return Err(ValidationError::new(
375 "an M/H rerun that is not a fail carries the harness report (rerun_report_missing)",
376 ));
377 }
378 if result != GateResult::Fail && rerun.report.as_ref().is_some_and(|r| r.failed != 0) {
381 return Err(ValidationError::new(
382 "the harness reported failed tests, so the rerun is not green",
383 ));
384 }
385 }
386 }
387 Ok(())
388}
389
390#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
392pub struct ArtifactRefV1 {
393 pub kind: String,
394 #[serde(rename = "ref")]
395 pub reference: String,
396}
397
398#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
400pub struct CiJobV1 {
401 pub name: String,
402 pub status: String,
403 #[serde(default, skip_serializing_if = "Option::is_none")]
404 pub url: Option<String>,
405}
406
407#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
411pub struct CiV1 {
412 pub provider: String,
413 #[serde(default, skip_serializing_if = "Option::is_none")]
414 pub project: Option<u64>,
415 #[serde(default, skip_serializing_if = "Option::is_none")]
416 pub repo: Option<String>,
417 #[serde(default, skip_serializing_if = "Option::is_none")]
418 pub pipeline_id: Option<String>,
419 #[serde(default, skip_serializing_if = "Option::is_none")]
420 pub commit: Option<String>,
421 #[serde(default, skip_serializing_if = "Option::is_none")]
422 pub status: Option<String>,
423 #[serde(default, skip_serializing_if = "Vec::is_empty")]
424 pub required_jobs: Vec<String>,
425 #[serde(default, skip_serializing_if = "Vec::is_empty")]
426 pub jobs: Vec<CiJobV1>,
427 #[serde(default, skip_serializing_if = "Option::is_none")]
428 pub reason: Option<String>,
429}
430
431impl CiV1 {
432 pub fn green_for(&self, artifact: &str) -> Result<(), String> {
438 if self.provider == "none" {
439 return match self.reason.as_deref().map(str::trim) {
440 Some(reason) if !reason.is_empty() => Ok(()),
441 _ => Err("ci provider none without a reason".into()),
442 };
443 }
444 if self.commit.as_deref() != Some(artifact) {
445 return Err("ci is for another commit (ci_commit)".into());
446 }
447 if self.status.as_deref() != Some("success") || self.pipeline_id.is_none() {
448 return Err(format!(
449 "the pipeline is {} (a pass needs success)",
450 self.status.as_deref().unwrap_or("unknown")
451 ));
452 }
453 if self.required_jobs.is_empty() {
454 return Err("ci names no required jobs (ci_declared_jobs)".into());
455 }
456 if self.project == Some(CLOUD_CLOUD_PROJECT)
457 && !self.required_jobs.iter().any(|j| j == CLOUD_CLOUD_PG_JOB)
458 {
459 return Err("a LeanCTX Cloud record always requires test-pg".into());
460 }
461 for required in &self.required_jobs {
462 match self.jobs.iter().find(|j| &j.name == required) {
463 Some(job) if job.status == "success" => {}
464 Some(job) => {
465 return Err(format!(
466 "required job {required} is {} (ci_required_jobs)",
467 job.status
468 ));
469 }
470 None => {
471 return Err(format!(
472 "required job {required} is missing (ci_required_jobs)"
473 ));
474 }
475 }
476 }
477 Ok(())
478 }
479}
480
481#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
487pub struct CloudG6RecordV1 {
488 pub schema: String,
489 pub process: String,
490 pub gate: String,
491 pub risk: TaskRisk,
492 pub artifact: ArtifactRefV1,
493 pub result: GateResult,
494 #[serde(default)]
495 pub missing: Vec<String>,
496 pub locked_paths: LockedPathsV1,
497 #[serde(default, skip_serializing_if = "Option::is_none")]
498 pub rerun: Option<RerunV1>,
499 #[serde(default, skip_serializing_if = "Option::is_none")]
500 pub ci: Option<CiV1>,
501 #[serde(default, skip_serializing_if = "Option::is_none")]
502 pub rule: Option<String>,
503 #[serde(default, skip_serializing_if = "Option::is_none")]
504 pub reason: Option<String>,
505}
506
507#[derive(Debug, Clone, PartialEq, Eq)]
509pub enum NotGreen {
510 NoCi,
512 Red(String),
514}
515
516impl CloudG6RecordV1 {
517 pub fn artifact_commit(&self) -> &str {
519 &self.artifact.reference
520 }
521
522 pub fn judges(&self, commit: &str) -> bool {
524 self.artifact.kind == "commit" && self.artifact.reference == commit
525 }
526
527 pub fn green(&self) -> Result<(), NotGreen> {
530 let red = |e: String| NotGreen::Red(e);
531 if self.schema != GATE_RECORD_SCHEMA || self.process != PROCESS_VERSION || self.gate != "G6"
532 {
533 return Err(red("not a G6 record of process 1.0".into()));
534 }
535 if self.artifact.kind != "commit" {
536 return Err(red("the record judged no commit".into()));
537 }
538 validate_sha(&self.artifact.reference, "artifact.ref").map_err(|e| red(e.to_string()))?;
539 validate_core(
540 self.risk,
541 self.result,
542 &self.missing,
543 &self.locked_paths,
544 self.rerun.as_ref(),
545 self.rule.as_deref(),
546 self.reason.as_deref(),
547 &self.artifact.reference,
548 )
549 .map_err(|e| red(e.to_string()))?;
550 if self.result != GateResult::Pass {
551 return Err(red(format!(
552 "the record is {}",
553 match self.result {
554 GateResult::Fail => "fail",
555 _ => "blocked",
556 }
557 )));
558 }
559 let ci = self.ci.as_ref().ok_or(NotGreen::NoCi)?;
560 ci.green_for(&self.artifact.reference).map_err(red)
561 }
562}
563
564pub const TASK_PROCESS_SCHEMA: &str = "leanctx.cloud.process.task/v1";
566
567#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
572pub struct TaskProcessV1 {
573 pub schema: String,
574 pub process: String,
576 pub task: TaskProcessRefV1,
577 pub kind: String,
579 pub goal_id: String,
580 pub priority: String,
582 pub risk: TaskRisk,
583 pub stage: String,
584 pub state: String,
586 #[serde(default, skip_serializing_if = "Option::is_none")]
587 pub role: Option<String>,
588 pub required_gates: Vec<String>,
589 #[serde(default, skip_serializing_if = "Option::is_none")]
590 pub passed_gates: Option<Vec<TaskProcessPassedGateV1>>,
591 #[serde(default, skip_serializing_if = "Option::is_none")]
593 pub spec_commit: Option<String>,
594 #[serde(default, skip_serializing_if = "Option::is_none")]
596 pub candidate_commit: Option<String>,
597 #[serde(default, skip_serializing_if = "Option::is_none")]
599 pub feature_list: Option<String>,
600 #[serde(default, skip_serializing_if = "Option::is_none")]
601 pub docs_delta: Option<String>,
602 #[serde(default, skip_serializing_if = "Option::is_none")]
604 pub brief: Option<serde_json::Value>,
605 #[serde(default, skip_serializing_if = "Option::is_none")]
606 pub brief_sha256: Option<String>,
607 #[serde(default, skip_serializing_if = "Option::is_none")]
608 pub stops: Option<Vec<String>>,
609 #[serde(default, skip_serializing_if = "Option::is_none")]
610 pub stage_entries: Option<Vec<TaskProcessStageEntryV1>>,
611 pub risk_changes: Vec<TaskProcessRiskChangeV1>,
612}
613
614impl TaskProcessV1 {
615 pub fn validate(&self) -> Result<(), ValidationError> {
616 if self.schema != TASK_PROCESS_SCHEMA {
617 return Err(ValidationError::new(format!(
618 "the task's process fields name schema {:?}, not {TASK_PROCESS_SCHEMA}",
619 self.schema
620 )));
621 }
622 if !self.process.starts_with("1.") {
623 return Err(ValidationError::new(format!(
624 "the task's process fields are process {:?}, not 1.x",
625 self.process
626 )));
627 }
628 Ok(())
629 }
630}
631
632#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
634pub struct TaskProcessRefV1 {
635 pub vault: String,
636 pub number: u64,
637}
638
639#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
641pub struct TaskProcessPassedGateV1 {
642 pub gate: String,
643 pub record: String,
644}
645
646#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
648pub struct TaskProcessStageEntryV1 {
649 pub round: u32,
650 pub stage: String,
651 pub cause: String,
652 #[serde(default, skip_serializing_if = "Option::is_none")]
653 pub record: Option<String>,
654 pub at: String,
655}
656
657#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
659pub struct TaskProcessRiskChangeV1 {
660 pub from: Option<TaskRisk>,
661 pub to: TaskRisk,
662 pub by: serde_json::Value,
663 pub at: String,
664 #[serde(default, skip_serializing_if = "Option::is_none")]
665 pub reason: Option<serde_json::Value>,
666}
667
668#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
678pub struct TaskProcessGateV1 {
679 #[serde(default, skip_serializing_if = "Vec::is_empty")]
681 pub locked_paths: Vec<String>,
682 #[serde(default, skip_serializing_if = "Option::is_none")]
684 pub test_command: Option<String>,
685 #[serde(default, skip_serializing_if = "Option::is_none")]
686 pub e2e_command: Option<String>,
687 #[serde(default, skip_serializing_if = "Vec::is_empty")]
689 pub features: Vec<String>,
690 #[serde(default, skip_serializing_if = "Option::is_none")]
692 pub repo_test_command: Option<String>,
693 #[serde(default, skip_serializing_if = "Option::is_none")]
695 pub g6: Option<CloudG6RecordV1>,
696}
697
698pub fn under_locked(path: &str, pattern: &str) -> bool {
703 fn glob(p: &[u8], s: &[u8]) -> bool {
704 match p {
705 [] => s.is_empty(),
706 [b'*', b'*', b'/', rest @ ..] => {
709 (0..=s.len()).any(|i| (i == 0 || s[i - 1] == b'/') && glob(rest, &s[i..]))
710 }
711 [b'*', b'*', rest @ ..] => (0..=s.len()).any(|i| glob(rest, &s[i..])),
713 [b'*', rest @ ..] => (0..=s.len())
714 .take_while(|&i| i == 0 || s[i - 1] != b'/')
715 .any(|i| glob(rest, &s[i..])),
716 [c, rest @ ..] => s.first() == Some(c) && glob(rest, &s[1..]),
717 }
718 }
719 if pattern.contains('*') {
720 glob(pattern.as_bytes(), path.as_bytes())
721 } else {
722 path == pattern || path.starts_with(&format!("{}/", pattern.trim_end_matches('/')))
723 }
724}
725
726pub fn validate_repo_path(path: &str) -> Result<(), ValidationError> {
729 let len = path.chars().count();
730 let escapes = path.starts_with('/') || path.split('/').any(|segment| segment == "..");
731 if !(1..=500).contains(&len) || escapes || path.contains('\0') || path.contains('\\') {
732 return Err(ValidationError::new(format!(
733 "locked path {path:?} is not a path inside the repository"
734 )));
735 }
736 Ok(())
737}
738
739fn validate_sha(value: &str, field: &str) -> Result<(), ValidationError> {
740 if value.len() == 40
741 && value
742 .bytes()
743 .all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
744 {
745 Ok(())
746 } else {
747 Err(ValidationError::new(format!(
748 "{field} is a full commit id (40 lowercase hex)"
749 )))
750 }
751}
752
753#[cfg(test)]
754mod tests {
755 use super::*;
756
757 const SPEC: &str = "1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d";
758 const HEAD: &str = "9f01cf32a3d4e5f60718293a4b5c6d7e8f901234";
759
760 #[test]
763 fn locked_path_matcher_matches_cloud() {
764 for (pattern, path, under) in [
765 ("a/**/b", "a/b", true),
766 ("a/**/b", "a/c/b", true),
767 ("a/**/b", "a/c/d/b", true),
768 ("a/**/b", "a/xb", false),
769 ("a/**/b", "a/c/xb", false),
770 (
771 "crates/pay/tests/**/refund.rs",
772 "crates/pay/tests/xrefund.rs",
773 false,
774 ),
775 (
776 "crates/pay/tests/**/refund.rs",
777 "crates/pay/tests/refund.rs",
778 true,
779 ),
780 ("a/**", "a/b", true),
781 ("a/**", "a/b/c/d.rs", true),
782 ("a/**", "ab/c", false),
783 ("a/*.rs", "a/b.rs", true),
784 ("a/*.rs", "a/b/c.rs", false),
785 ("a/b.rs", "a/b.rs", true),
786 ("a/b", "a/b/c.rs", true),
787 ("a/b", "a/bc.rs", false),
788 ("**/x.rs", "x.rs", true),
790 ("**/x.rs", "a/x.rs", true),
791 ("**/x.rs", "ax.rs", false),
792 ("a/**", "a", false),
793 ("a/**/b/**/c", "a/b/c", true),
794 ("a/**/b/**/c", "a/x/b/y/z/c", true),
795 ("a/**/b/**/c", "a/xb/c", false),
796 ("a/**/b/**/c", "a/b/xc", false),
797 ("x/**/y.rs", "x/zy.rs", false),
798 ] {
799 assert_eq!(
800 under_locked(path, pattern),
801 under,
802 "{path:?} under {pattern:?}"
803 );
804 }
805 }
806
807 fn rerun(commit: &str) -> RerunV1 {
808 RerunV1 {
809 command: "cargo test -p pay refund".into(),
810 e2e_command: None,
811 host: "pounce".into(),
812 commit: commit.into(),
813 spec_commit: SPEC.into(),
814 exit_code: 0,
815 log_sha256: "fe".repeat(32),
816 started_at: "2026-11-05T09:50:00Z".into(),
817 finished_at: "2026-11-05T09:58:00Z".into(),
818 features: vec![FeatureResultV1 {
819 id: "F1".into(),
820 passes: true,
821 }],
822 report: Some(RerunReportV1 {
823 format: "libtest-text".into(),
824 sha256: "fe".repeat(32),
825 passed: 1,
826 failed: 0,
827 ignored: 0,
828 names_checked: vec![TestStatusV1 {
829 test: "refunds".into(),
830 status: "passed".into(),
831 }],
832 }),
833 }
834 }
835
836 fn ready_m() -> G6RecordV1 {
839 G6RecordV1 {
840 schema: GATE_RECORD_SCHEMA.into(),
841 process: PROCESS_VERSION.into(),
842 gate: "G6".into(),
843 risk: TaskRisk::M,
844 artifact_commit: HEAD.into(),
845 result: GateResult::Blocked,
846 missing: vec![MISSING_CI_PIPELINE.into()],
847 locked_paths: LockedPathsV1 {
848 spec_commit: SPEC.into(),
849 checked: vec![
850 "crates/pay/tests/refund.rs".into(),
851 "crates/pay/tests/refund/**".into(),
852 ],
853 ok: true,
854 changed: vec![],
855 },
856 rerun: Some(rerun(HEAD)),
857 rule: None,
858 reason: Some("the Instance does not read CI".into()),
859 }
860 }
861
862 #[test]
863 fn ready_m_record_and_its_rules() {
864 let record = ready_m();
865 record.validate().unwrap();
866 assert!(record.ready_for_cloud() && record.judges(HEAD) && !record.judges(SPEC));
867
868 let mut pass = ready_m();
870 pass.result = GateResult::Pass;
871 pass.missing.clear();
872 pass.reason = None;
873 assert!(pass.validate().is_err() && !pass.ready_for_cloud());
874
875 let mut nothing_missing = ready_m();
877 nothing_missing.missing.clear();
878 assert!(nothing_missing.validate().is_err());
879
880 let mut other = ready_m();
882 other.rerun = Some(rerun(SPEC));
883 assert!(other.validate().is_err() && !other.ready_for_cloud());
884
885 let mut empty = ready_m();
887 empty.locked_paths.checked.clear();
888 assert!(empty.validate().is_err());
889
890 let mut red = ready_m();
892 red.rerun.as_mut().unwrap().exit_code = 101;
893 assert!(red.validate().is_err() && !red.ready_for_cloud());
894 red.result = GateResult::Fail;
895 red.missing.clear();
896 red.reason = Some("cargo test exited 101".into());
897 red.validate().unwrap();
898 assert!(!red.ready_for_cloud());
899
900 let mut unreported = ready_m();
903 unreported.rerun.as_mut().unwrap().report = None;
904 assert!(unreported.validate().is_err() && !unreported.ready_for_cloud());
905
906 let mut some_failed = ready_m();
908 some_failed
909 .rerun
910 .as_mut()
911 .unwrap()
912 .report
913 .as_mut()
914 .unwrap()
915 .failed = 1;
916 assert!(some_failed.validate().is_err());
917
918 let mut ruled = ready_m();
920 ruled.rule = Some(RULE_LOCKED_PATHS_CHANGED.into());
921 assert!(ruled.validate().is_err());
922 }
923
924 #[test]
925 fn locked_path_failure_record() {
926 let mut record = ready_m();
927 record.result = GateResult::Fail;
928 record.missing.clear();
929 record.rerun = None;
930 record.rule = Some(RULE_LOCKED_PATHS_CHANGED.into());
931 record.reason = Some("crates/pay/tests/refund.rs modified".into());
932 record.locked_paths.ok = false;
933 record.locked_paths.changed = vec![ChangedPathV1 {
934 path: "crates/pay/tests/refund.rs".into(),
935 change: LockedChange::Modified,
936 }];
937 record.validate().unwrap();
938 assert!(!record.ready_for_cloud());
939
940 let mut twice = record.clone();
941 twice.locked_paths.changed.push(ChangedPathV1 {
942 path: "crates/pay/tests/refund.rs".into(),
943 change: LockedChange::Deleted,
944 });
945 assert!(twice.validate().is_err(), "locked_paths_unique");
946
947 let mut unchecked = record.clone();
948 unchecked.locked_paths.changed[0].path = "src/other.rs".into();
949 assert!(
950 unchecked.validate().is_err(),
951 "locked_paths_changed_unchecked"
952 );
953
954 let mut not_ok = record.clone();
955 not_ok.locked_paths.ok = true;
956 assert!(not_ok.validate().is_err());
957
958 let mut blocked = record;
959 blocked.result = GateResult::Blocked;
960 blocked.missing = vec![MISSING_CI_PIPELINE.into()];
961 assert!(blocked.validate().is_err(), "a locked change is a fail");
962 }
963
964 #[test]
965 fn s_record_checks_nothing() {
966 let mut record = ready_m();
967 record.risk = TaskRisk::S;
968 record.locked_paths.checked.clear();
969 record.rerun.as_mut().unwrap().features.clear();
970 record.validate().unwrap();
971 assert!(record.ready_for_cloud());
972
973 let mut with_features = record.clone();
974 with_features.rerun = Some(rerun(HEAD));
975 assert!(with_features.validate().is_err());
976
977 let mut with_check = record;
978 with_check.locked_paths.checked = vec!["tests/a.rs".into()];
979 assert!(with_check.validate().is_err());
980 }
981
982 fn cloud_pass() -> serde_json::Value {
985 serde_json::json!({
986 "schema": "leanctx.cloud.process.gate/v1", "process": "1.0",
987 "id": "gat_01JC0000000000000000000016", "gate": "G6",
988 "task": {"vault": "#acme/payments", "number": 42}, "risk": "M",
989 "stage_from": "test", "stage_to": "review",
990 "artifact": {"kind": "commit", "ref": HEAD},
991 "author": {"kind": "agent", "agent_id": "claude-cccccccccccccccccccc",
992 "worker_id": "wkr_cccccccccccccccccccc",
993 "instance_id": "6f2b9c4e-1a3d-4e5f-8a7b-0c1d2e3f4a5b", "role": "developer"},
994 "checker": {"kind": "cloud", "check": "g6.rerun"}, "result": "pass",
996 "evidence": [{"kind": "rerun", "ref": "https://cloud.example/ev/rerun/16",
997 "sha256": "fe".repeat(32)}],
998 "missing": [],
999 "rerun": {"command": "cargo test -p pay refund", "host": "pounce", "commit": HEAD,
1000 "spec_commit": SPEC, "exit_code": 0, "log_sha256": "fe".repeat(32),
1001 "started_at": "2026-11-05T09:50:00Z", "finished_at": "2026-11-05T09:58:00Z",
1002 "features": [{"id": "F1", "passes": true}, {"id": "F2", "passes": true}],
1003 "report": {"format": "libtest-text", "sha256": "fe".repeat(32), "passed": 2,
1004 "failed": 0, "ignored": 0, "names_checked": [{"test": "refunds", "status": "passed"}]}},
1005 "at": "2026-11-05T10:00:00Z",
1006 "ci": {"provider": "gitlab", "project": 37, "repo": "leanctx/leanctx-cloud",
1007 "runner": "cloud-ci", "pipeline_id": "4711",
1008 "url": "https://gitlab.pounce.ch/leanctx/leanctx-cloud/-/pipelines/4711",
1009 "commit": HEAD, "status": "success",
1010 "required_jobs": ["fmt", "clippy", "test-pg", "c0v2-fixtures", "cargo-deny", "web"],
1011 "jobs": [{"name": "fmt", "status": "success"}, {"name": "clippy", "status": "success"},
1012 {"name": "test-pg", "status": "success", "url": "https://x.example/j/1"},
1013 {"name": "c0v2-fixtures", "status": "success"},
1014 {"name": "cargo-deny", "status": "success"}, {"name": "web", "status": "success"},
1015 {"name": "e2e", "status": "manual"}]},
1016 "locked_paths": {"spec_commit": SPEC,
1017 "checked": ["crates/pay/tests/refund.rs", "crates/pay/tests/refund/**",
1018 "specs/42/features.json"],
1019 "ok": true, "changed": []}
1020 })
1021 }
1022
1023 fn cloud(value: serde_json::Value) -> CloudG6RecordV1 {
1024 serde_json::from_value(value).expect("a contract-complete record parses")
1025 }
1026
1027 #[test]
1028 fn cloud_record_is_green_only_with_green_ci_for_the_judged_commit() {
1029 let record = cloud(cloud_pass());
1030 assert_eq!(record.green(), Ok(()));
1031 assert!(record.judges(HEAD) && !record.judges(SPEC));
1032
1033 let mut v = cloud_pass();
1035 v.as_object_mut().unwrap().remove("ci");
1036 assert_eq!(cloud(v).green(), Err(NotGreen::NoCi));
1037
1038 let red = |edit: &dyn Fn(&mut serde_json::Value)| {
1039 let mut v = cloud_pass();
1040 edit(&mut v);
1041 matches!(cloud(v).green(), Err(NotGreen::Red(_)))
1042 };
1043 assert!(red(&|v| v["ci"]["commit"] = SPEC.into()));
1045 assert!(red(&|v| v["ci"]["status"] = "failed".into()));
1047 assert!(red(&|v| v["ci"]["status"] = "running".into()));
1048 assert!(red(&|v| v["ci"]["jobs"][2]["status"] = "failed".into()));
1050 assert!(red(&|v| v["ci"]["jobs"][0]["status"] = "skipped".into()));
1051 assert!(red(&|v| {
1052 v["ci"]["jobs"].as_array_mut().unwrap().remove(2);
1053 }));
1054 assert!(red(&|v| {
1056 v["ci"]["required_jobs"] = serde_json::json!(["fmt"]);
1057 v["ci"]["jobs"] = serde_json::json!([{"name": "fmt", "status": "success"}]);
1058 }));
1059 assert!(red(&|v| {
1061 v["ci"] = serde_json::json!({"provider": "gitlab", "project": 37,
1062 "repo": "leanctx/leanctx-cloud", "commit": HEAD, "status": "missing",
1063 "reason": "relay returned []"});
1064 }));
1065 assert!(red(&|v| {
1066 v["result"] = "blocked".into();
1067 v["missing"] = serde_json::json!(["ci_pipeline"]);
1068 v["reason"] = "no pipeline".into();
1069 }));
1070 assert!(red(&|v| v["rerun"]["exit_code"] = 101.into()));
1072 assert!(red(&|v| v["rerun"]["commit"] = SPEC.into()));
1073 let mut none = cloud_pass();
1075 none["ci"] = serde_json::json!({"provider": "none", "repo": "leanctx/x",
1076 "reason": "the repo declares no CI"});
1077 assert_eq!(cloud(none).green(), Ok(()));
1078 }
1079
1080 #[test]
1081 fn locked_paths_stay_in_the_repo() {
1082 for bad in ["/etc/passwd", "../x", "a/../../b", "", "a\\b"] {
1083 assert!(validate_repo_path(bad).is_err(), "{bad:?}");
1084 }
1085 for good in ["a", "a/b.rs", "a/**", "..a/b", "a/b../c"] {
1086 validate_repo_path(good).unwrap();
1087 }
1088 let json = serde_json::to_value(LockedChange::TypeChanged).unwrap();
1089 assert_eq!(json, "type_changed");
1090 assert_eq!(LockedChange::OutsideRepo.as_str(), "outside_repo");
1091 }
1092}