presolve-compiler 0.1.0-alpha.5

The Presolve compiler toolchain for TypeScript web applications.
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
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
use crate::resume_instance::SerializableInstance;
use crate::resume_plan::ResumePlan;
use crate::semantic_id::SemanticId;
use crate::{
    build_resume_activation_plan, build_resume_anchor_plan, build_resume_boundary_graph,
    build_resume_capture_plan, build_resume_chunk_graph, build_resume_liveness_plan,
    build_resume_restore_plan, build_resume_schema_registry, validate_resume_activation_plan,
    validate_resume_anchor_plan, validate_resume_boundary_graph, validate_resume_capture_plan,
    validate_resume_chunk_graph, validate_resume_liveness_plan, validate_resume_restore_plan,
    validate_resume_schema_registry, ApplicationSemanticModel, ResumeActivationBlockReason,
    ResumeActivationIntegrityCode, ResumeActivationPlan, ResumeAnchorIntegrityCode,
    ResumeAnchorPlan, ResumeBoundaryGraph, ResumeBoundaryIntegrityCode, ResumeCaptureBlockReason,
    ResumeCaptureIntegrityCode, ResumeCapturePlan, ResumeChunkBlockReason, ResumeChunkGraph,
    ResumeChunkIntegrityCode, ResumeLivenessBlockReason, ResumeLivenessIntegrityCode,
    ResumeLivenessPlan, ResumeRestoreBlockReason, ResumeRestoreIntegrityCode, ResumeRestorePlan,
    ResumeSchemaBlockReason, ResumeSchemaIntegrityCode, ResumeSchemaRegistry, SourceProvenance,
};

/// A Phase J diagnostic code reserved before executable resumability products
/// are introduced. J19 alone projects these from immutable Phase J products.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ResumeDiagnosticReservation {
    pub code: &'static str,
    pub meaning: &'static str,
}

/// The contiguous public compiler diagnostic range reserved by J0.
pub const RESUME_DIAGNOSTIC_RESERVATIONS: [ResumeDiagnosticReservation; 16] = [
    ResumeDiagnosticReservation {
        code: "PSC1096",
        meaning: "Unsupported resume value",
    },
    ResumeDiagnosticReservation {
        code: "PSC1097",
        meaning: "Missing resume owner",
    },
    ResumeDiagnosticReservation {
        code: "PSC1098",
        meaning: "Resume boundary cycle",
    },
    ResumeDiagnosticReservation {
        code: "PSC1099",
        meaning: "Invalid resume retention",
    },
    ResumeDiagnosticReservation {
        code: "PSC1100",
        meaning: "Invalid resume recomputation",
    },
    ResumeDiagnosticReservation {
        code: "PSC1101",
        meaning: "Invalid activation policy",
    },
    ResumeDiagnosticReservation {
        code: "PSC1102",
        meaning: "Resume chunk cycle",
    },
    ResumeDiagnosticReservation {
        code: "PSC1103",
        meaning: "Missing resume program",
    },
    ResumeDiagnosticReservation {
        code: "PSC1104",
        meaning: "Invalid resume anchor",
    },
    ResumeDiagnosticReservation {
        code: "PSC1105",
        meaning: "Resume schema collision",
    },
    ResumeDiagnosticReservation {
        code: "PSC1106",
        meaning: "Invalid snapshot stable state",
    },
    ResumeDiagnosticReservation {
        code: "PSC1107",
        meaning: "Resume artifact mismatch",
    },
    ResumeDiagnosticReservation {
        code: "PSC1108",
        meaning: "Lazy event payload unsupported",
    },
    ResumeDiagnosticReservation {
        code: "PSC1109",
        meaning: "Missing resume chunk",
    },
    ResumeDiagnosticReservation {
        code: "PSC1110",
        meaning: "Invalid resume ordering",
    },
    ResumeDiagnosticReservation {
        code: "PSC1111",
        meaning: "Unsupported resume topology",
    },
];

/// J1-J21 must allocate internal integrity codes only within this J0-reserved range.
pub const RESUME_INTEGRITY_RESERVATION_START: u32 = 1289;
pub const RESUME_INTEGRITY_RESERVATION_END: u32 = 1384;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResumeDiagnostic {
    pub code: String,
    pub component: SemanticId,
    pub state: Option<SemanticId>,
}

/// Public J19 projection from immutable Phase J products. These records do not
/// manufacture semantic identities or re-read source/DOM output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResumeProjectedDiagnostic {
    pub code: &'static str,
    pub message: String,
    /// The exact Phase J product identity when an earlier product established
    /// one. Unresolved candidates deliberately retain no fabricated identity.
    pub primary_identity: Option<String>,
    pub primary_provenance: SourceProvenance,
}

/// The immutable Phase J products that authorize J19 public diagnostics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResumeDiagnosticProducts {
    pub liveness: ResumeLivenessPlan,
    pub boundaries: ResumeBoundaryGraph,
    pub activation: ResumeActivationPlan,
    pub chunks: ResumeChunkGraph,
    pub schemas: ResumeSchemaRegistry,
    pub capture: ResumeCapturePlan,
    pub restore: ResumeRestorePlan,
    pub anchors: ResumeAnchorPlan,
}

#[must_use]
pub fn build_resume_diagnostic_products(
    model: &ApplicationSemanticModel,
) -> ResumeDiagnosticProducts {
    ResumeDiagnosticProducts {
        liveness: build_resume_liveness_plan(model),
        boundaries: build_resume_boundary_graph(model),
        activation: build_resume_activation_plan(model),
        chunks: build_resume_chunk_graph(model),
        schemas: build_resume_schema_registry(model),
        capture: build_resume_capture_plan(model),
        restore: build_resume_restore_plan(model),
        anchors: build_resume_anchor_plan(model),
    }
}

#[must_use]
#[allow(clippy::too_many_lines)]
pub fn project_resume_diagnostics(
    model: &ApplicationSemanticModel,
) -> Vec<ResumeProjectedDiagnostic> {
    // Earlier compiler diagnostics mean the required canonical products were
    // never established. Public J19 diagnostics must not invent Phase J
    // identities while reporting the earlier failure.
    if !model.diagnostics.is_empty() {
        return Vec::new();
    }
    project_resume_diagnostics_from_products(model, &build_resume_diagnostic_products(model))
}

/// Projects public J19 records only from already-established Phase J products.
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn project_resume_diagnostics_from_products(
    model: &ApplicationSemanticModel,
    products: &ResumeDiagnosticProducts,
) -> Vec<ResumeProjectedDiagnostic> {
    if !model.diagnostics.is_empty() {
        return Vec::new();
    }
    let mut projected = Vec::new();
    let liveness = &products.liveness;
    for block in &liveness.blocked {
        let code = match block.reason {
            ResumeLivenessBlockReason::RequiredNonSerializableValue => "PSC1096",
            ResumeLivenessBlockReason::RecomputeProofUnavailable => "PSC1100",
            ResumeLivenessBlockReason::MissingCanonicalSource => "PSC1097",
            ResumeLivenessBlockReason::UnknownDependency => "PSC1099",
        };
        projected.push(ResumeProjectedDiagnostic {
            code,
            message: format!(
                "{}: {}",
                resume_diagnostic_meaning(code),
                block.slot.resume_slot_id
            ),
            primary_identity: Some(block.slot.resume_slot_id.to_string()),
            primary_provenance: block.slot.provenance.clone(),
        });
    }
    let schemas = &products.schemas;
    for block in &schemas.blocks {
        let code = match block.reason {
            ResumeSchemaBlockReason::UnsupportedValue => "PSC1096",
            ResumeSchemaBlockReason::UpstreamLivenessBlock => "PSC1099",
            ResumeSchemaBlockReason::MissingCanonicalSlotType
            | ResumeSchemaBlockReason::MalformedSemanticType => "PSC1107",
        };
        projected.push(ResumeProjectedDiagnostic {
            code,
            message: format!("{}: {:?}", resume_diagnostic_meaning(code), block.slot),
            primary_identity: Some(block.slot.resume_slot_id().to_string()),
            primary_provenance: block.provenance.clone(),
        });
    }
    let boundaries = &products.boundaries;
    for block in &boundaries.blocks {
        projected.push(ResumeProjectedDiagnostic {
            code: "PSC1111",
            message: format!(
                "Unsupported resume topology: {:?}",
                block.candidate_boundary
            ),
            primary_identity: block.candidate_boundary.as_ref().map(ToString::to_string),
            primary_provenance: block.provenance.clone(),
        });
    }
    let activation = &products.activation;
    for block in &activation.blocks {
        let code = match block.reason {
            ResumeActivationBlockReason::MissingInteractionReference => "PSC1109",
            ResumeActivationBlockReason::UnknownBoundary
            | ResumeActivationBlockReason::RequiredBoundaryBlocked
            | ResumeActivationBlockReason::NoValidEagerFallback => "PSC1101",
            ResumeActivationBlockReason::UnsupportedLazyEventPayload => "PSC1108",
        };
        projected.push(ResumeProjectedDiagnostic {
            code,
            message: format!("{}: {}", resume_diagnostic_meaning(code), block.boundary),
            primary_identity: Some(block.boundary.to_string()),
            primary_provenance: block.provenance.clone(),
        });
    }
    let chunks = &products.chunks;
    for block in &chunks.blocks {
        let code = match block.reason {
            ResumeChunkBlockReason::MissingActivationBoundary => "PSC1109",
            ResumeChunkBlockReason::MissingProgram => "PSC1103",
            ResumeChunkBlockReason::UnsupportedActivationPolicy => "PSC1101",
        };
        projected.push(ResumeProjectedDiagnostic {
            code,
            message: resume_diagnostic_meaning(code).to_string(),
            primary_identity: block.root_boundary.as_ref().map(ToString::to_string),
            primary_provenance: block.provenance.clone(),
        });
    }
    let capture = &products.capture;
    for block in &capture.blocks {
        let Some(primary_provenance) = block.provenance.clone() else {
            // This is an invalid earlier candidate with no source authority;
            // J19 must not fabricate a public Phase J identity for it.
            continue;
        };
        let code = match block.reason {
            ResumeCaptureBlockReason::MissingBoundarySchema => "PSC1097",
            ResumeCaptureBlockReason::MissingRetainedSlotSchema => "PSC1103",
        };
        projected.push(ResumeProjectedDiagnostic {
            code,
            message: resume_diagnostic_meaning(code).to_string(),
            primary_identity: block
                .slot
                .as_ref()
                .map(|slot| slot.resume_slot_id().to_string())
                .or_else(|| block.boundary.as_ref().map(ToString::to_string)),
            primary_provenance,
        });
    }
    let restore = &products.restore;
    for block in &restore.blocks {
        let code = match block.reason {
            ResumeRestoreBlockReason::MissingSlotSchema
            | ResumeRestoreBlockReason::MissingFormSlotOwner => "PSC1103",
            ResumeRestoreBlockReason::MissingComputedProgram
            | ResumeRestoreBlockReason::UnsupportedRecomputableSlot => "PSC1100",
        };
        projected.push(ResumeProjectedDiagnostic {
            code,
            message: resume_diagnostic_meaning(code).to_string(),
            primary_identity: Some(block.slot.resume_slot_id().to_string()),
            primary_provenance: block.provenance.clone(),
        });
    }
    project_resume_integrity_diagnostics(model, products, &mut projected);
    projected.sort_by(|left, right| {
        (
            left.code,
            &left.primary_identity,
            &left.primary_provenance.path,
            left.primary_provenance.span.start,
            left.primary_provenance.span.end,
            &left.message,
        )
            .cmp(&(
                right.code,
                &right.primary_identity,
                &right.primary_provenance.path,
                right.primary_provenance.span.start,
                right.primary_provenance.span.end,
                &right.message,
            ))
    });
    projected.dedup_by(|left, right| {
        left.code == right.code
            && left.primary_identity == right.primary_identity
            && left.primary_provenance == right.primary_provenance
    });
    projected
}

#[allow(clippy::too_many_lines)]
fn project_resume_integrity_diagnostics(
    model: &ApplicationSemanticModel,
    products: &ResumeDiagnosticProducts,
    projected: &mut Vec<ResumeProjectedDiagnostic>,
) {
    let boundary = |id: Option<&crate::ResumeBoundaryId>| {
        id.and_then(|id| {
            products
                .boundaries
                .boundary(id)
                .map(|record| (id.to_string(), record.provenance.clone()))
        })
    };
    let fallback = || {
        products
            .boundaries
            .boundaries
            .iter()
            .find(|record| record.ownership_parent.is_none())
            .map(|record| (record.id.to_string(), record.provenance.clone()))
    };
    let slot = |slot: Option<&crate::ResumeExistingSlot>| {
        slot.and_then(|slot| {
            products
                .liveness
                .retained
                .iter()
                .map(|record| &record.slot)
                .chain(
                    products
                        .liveness
                        .recomputable
                        .iter()
                        .map(|record| &record.slot),
                )
                .chain(products.liveness.excluded.iter().map(|record| &record.slot))
                .chain(products.liveness.blocked.iter().map(|record| &record.slot))
                .find(|record| &record.existing_slot == slot)
                .map(|record| (record.resume_slot_id.to_string(), record.provenance.clone()))
        })
    };
    let mut push = |code, message: String, primary: Option<(String, SourceProvenance)>| {
        if let Some((primary_identity, primary_provenance)) = primary.or_else(fallback) {
            projected.push(ResumeProjectedDiagnostic {
                code,
                message: format!("{}: {message}", resume_diagnostic_meaning(code)),
                primary_identity: Some(primary_identity),
                primary_provenance,
            });
        }
    };

    for diagnostic in validate_resume_liveness_plan(model, &products.liveness) {
        let code = match diagnostic.code {
            ResumeLivenessIntegrityCode::RequiredUnsupportedValue => "PSC1096",
            ResumeLivenessIntegrityCode::MissingStorageOwner => "PSC1097",
            ResumeLivenessIntegrityCode::UnknownDependency
            | ResumeLivenessIntegrityCode::InvalidRetentionReason
            | ResumeLivenessIntegrityCode::DuplicateClassification
            | ResumeLivenessIntegrityCode::InvalidCandidatePromotion => "PSC1099",
            ResumeLivenessIntegrityCode::RecomputeWithoutProof => "PSC1100",
            ResumeLivenessIntegrityCode::ProvenanceOrderIndexDrift => "PSC1110",
        };
        push(code, diagnostic.message, slot(diagnostic.slot.as_ref()));
    }
    for diagnostic in validate_resume_boundary_graph(model, &products.boundaries) {
        let code = match diagnostic.code {
            ResumeBoundaryIntegrityCode::Cycle => "PSC1098",
            ResumeBoundaryIntegrityCode::DuplicateBoundary => "PSC1105",
            ResumeBoundaryIntegrityCode::PhaseCorrespondence => "PSC1107",
            ResumeBoundaryIntegrityCode::OrderingOrIndexDrift => "PSC1110",
            ResumeBoundaryIntegrityCode::InvalidOwner
            | ResumeBoundaryIntegrityCode::MissingOrMultipleParent
            | ResumeBoundaryIntegrityCode::Unreachable
            | ResumeBoundaryIntegrityCode::Reciprocity
            | ResumeBoundaryIntegrityCode::ProvenanceDrift => "PSC1111",
        };
        push(
            code,
            diagnostic.message,
            boundary(diagnostic.boundary.as_ref()),
        );
    }
    for diagnostic in validate_resume_activation_plan(model, &products.activation) {
        let code = match diagnostic.code {
            ResumeActivationIntegrityCode::UnsupportedLazyPayload => "PSC1108",
            ResumeActivationIntegrityCode::UnknownEventOrBoundary => "PSC1109",
            ResumeActivationIntegrityCode::OrderingOrIndexDrift => "PSC1110",
            ResumeActivationIntegrityCode::MissingOrDuplicatePolicy
            | ResumeActivationIntegrityCode::InvalidPrerequisite
            | ResumeActivationIntegrityCode::InvalidPolicyAuthority => "PSC1101",
        };
        push(
            code,
            diagnostic.message,
            boundary(diagnostic.boundary.as_ref()),
        );
    }
    for diagnostic in validate_resume_chunk_graph(model, &products.chunks) {
        let code = match diagnostic.code {
            ResumeChunkIntegrityCode::DependencyCycle => "PSC1102",
            ResumeChunkIntegrityCode::MissingProgram => "PSC1103",
            ResumeChunkIntegrityCode::RootCorrespondence => "PSC1109",
            ResumeChunkIntegrityCode::DuplicateInclusion
            | ResumeChunkIntegrityCode::UnrelatedProgram
            | ResumeChunkIntegrityCode::OrderingOrOutputDrift => "PSC1110",
        };
        let primary = diagnostic.chunk.as_ref().and_then(|id| {
            products
                .chunks
                .chunk(id)
                .and_then(|chunk| boundary(chunk.root_boundary.as_ref()))
        });
        push(code, diagnostic.message, primary);
    }
    if let Err(diagnostics) = validate_resume_schema_registry(model, &products.schemas) {
        for diagnostic in diagnostics {
            let code = match diagnostic.code {
                ResumeSchemaIntegrityCode::UnsupportedValue => "PSC1096",
                ResumeSchemaIntegrityCode::MissingSlot => "PSC1103",
                ResumeSchemaIntegrityCode::IdentityCollision => "PSC1105",
                ResumeSchemaIntegrityCode::MalformedSemanticType
                | ResumeSchemaIntegrityCode::DuplicateProperty => "PSC1107",
                ResumeSchemaIntegrityCode::OrderingOrIndexDrift => "PSC1110",
            };
            push(
                code,
                diagnostic.message,
                slot(diagnostic.slot.as_ref()).or_else(|| boundary(diagnostic.boundary.as_ref())),
            );
        }
    }
    if let Err(diagnostics) = validate_resume_capture_plan(model, &products.capture) {
        for diagnostic in diagnostics {
            let code = match diagnostic.code {
                ResumeCaptureIntegrityCode::InvalidCaptureState => "PSC1106",
                ResumeCaptureIntegrityCode::OrderingOrOutputDrift => "PSC1110",
                ResumeCaptureIntegrityCode::ProgramCorrespondence
                | ResumeCaptureIntegrityCode::InvalidInstruction => "PSC1103",
            };
            push(
                code,
                diagnostic.message,
                boundary(diagnostic.boundary.as_ref()),
            );
        }
    }
    if let Err(diagnostics) = validate_resume_restore_plan(model, &products.restore) {
        for diagnostic in diagnostics {
            let code = match diagnostic.code {
                ResumeRestoreIntegrityCode::PhaseOrDuplicateWrite
                | ResumeRestoreIntegrityCode::OrderingOrOutputDrift => "PSC1110",
                ResumeRestoreIntegrityCode::ProgramReference
                | ResumeRestoreIntegrityCode::MissingCompletion => "PSC1103",
            };
            push(
                code,
                diagnostic.message,
                boundary(diagnostic.boundary.as_ref()),
            );
        }
    }
    for diagnostic in validate_resume_anchor_plan(model, &products.anchors) {
        let code = match diagnostic.code {
            ResumeAnchorIntegrityCode::OrderingOrOutputDrift => "PSC1110",
            ResumeAnchorIntegrityCode::MissingTarget
            | ResumeAnchorIntegrityCode::UnstableTarget
            | ResumeAnchorIntegrityCode::DuplicateAnchor
            | ResumeAnchorIntegrityCode::WrongKind
            | ResumeAnchorIntegrityCode::StructuralPairMismatch => "PSC1104",
        };
        let primary = diagnostic.anchor_id.as_ref().and_then(|id| {
            products
                .anchors
                .anchors
                .iter()
                .find(|anchor| &anchor.anchor_id == id)
                .and_then(|anchor| boundary(Some(&anchor.boundary_id)))
        });
        push(code, diagnostic.message, primary);
    }
}

fn resume_diagnostic_meaning(code: &str) -> &'static str {
    RESUME_DIAGNOSTIC_RESERVATIONS
        .iter()
        .find(|entry| entry.code == code)
        .map_or("Unknown resumability diagnostic", |entry| entry.meaning)
}

#[must_use]
pub fn validate_resume_instances(
    plan: &ResumePlan,
    instances: &[SerializableInstance],
) -> Vec<ResumeDiagnostic> {
    let mut diagnostics = Vec::new();
    for component in &plan.components {
        let Some(instance) = instances
            .iter()
            .find(|instance| instance.component == component.component)
        else {
            diagnostics.push(ResumeDiagnostic {
                code: "PSRSM1001".to_string(),
                component: component.component.clone(),
                state: None,
            });
            continue;
        };
        for state in &component.state {
            if !instance.state.contains_key(state) {
                diagnostics.push(ResumeDiagnostic {
                    code: "PSRSM1002".to_string(),
                    component: component.component.clone(),
                    state: Some(state.clone()),
                });
            }
        }
    }
    diagnostics
}

#[cfg(test)]
mod tests {
    use super::{
        build_resume_diagnostic_products, project_resume_diagnostics,
        project_resume_diagnostics_from_products, RESUME_DIAGNOSTIC_RESERVATIONS,
        RESUME_INTEGRITY_RESERVATION_END, RESUME_INTEGRITY_RESERVATION_START,
    };

    #[test]
    fn j0_reserves_the_public_and_internal_resumability_ranges_without_products() {
        let codes = RESUME_DIAGNOSTIC_RESERVATIONS
            .iter()
            .map(|reservation| reservation.code)
            .collect::<Vec<_>>();
        assert_eq!(codes.first(), Some(&"PSC1096"));
        assert_eq!(codes.last(), Some(&"PSC1111"));
        assert_eq!(codes.len(), 16);
        assert_eq!(RESUME_INTEGRITY_RESERVATION_START, 1289);
        assert_eq!(RESUME_INTEGRITY_RESERVATION_END, 1384);
        assert_eq!(
            RESUME_INTEGRITY_RESERVATION_END - RESUME_INTEGRITY_RESERVATION_START + 1,
            96
        );
        assert_eq!(crate::RESUME_MANIFEST_SCHEMA_VERSION, 6);
        assert_eq!(crate::SEMANTIC_GRAPH_SCHEMA_VERSION, 6);
        assert_eq!(crate::TEMPLATE_MANIFEST_SCHEMA_VERSION, 5);
    }

    #[test]
    fn j19_projects_unsupported_retained_values_with_exact_source_evidence() {
        let mut model = crate::build_application_semantic_model(&presolve_parser::parse_file(
            "src/ResumeDiagnostics.tsx",
            r#"
@component("x-resume-diagnostics") class ResumeDiagnostics {
  value = state(1);
  render() { return <main>{this.value}</main>; }
}"#,
        ));
        let state = model.components[0].state_fields[0].id.clone();
        model
            .semantic_types
            .assignments
            .get_mut(&state)
            .expect("state type assignment")
            .semantic_type = crate::SemanticType::Unknown;
        let diagnostics = project_resume_diagnostics(&model);
        let diagnostic = diagnostics
            .iter()
            .find(|diagnostic| diagnostic.code == "PSC1096")
            .unwrap_or_else(|| panic!("unsupported retained state value: {diagnostics:#?}"));
        assert!(diagnostic.primary_identity.is_some());
        assert_eq!(
            diagnostic.primary_provenance.path,
            std::path::PathBuf::from("src/ResumeDiagnostics.tsx")
        );
        assert!(diagnostics.windows(2).all(|pair| {
            (pair[0].code, &pair[0].primary_identity, &pair[0].message)
                <= (pair[1].code, &pair[1].primary_identity, &pair[1].message)
        }));
    }

    #[test]
    fn j19_projects_the_integrity_only_catalog_conditions_from_malformed_products() {
        let model = crate::build_application_semantic_model(&presolve_parser::parse_file(
            "src/ResumeIntegrity.tsx",
            r#"@component("x-resume-integrity") class ResumeIntegrity {
  value = state(1);
  render() { return <main>{this.value}</main>; }
}"#,
        ));
        let mut products = build_resume_diagnostic_products(&model);
        let child = products
            .boundaries
            .boundaries
            .iter_mut()
            .find(|boundary| boundary.ownership_parent.is_some())
            .expect("child boundary");
        child.ownership_parent = Some(child.id.clone());
        let eager_chunk = products.chunks.chunks[0].id.clone();
        products.chunks.chunks[0]
            .dependency_chunks
            .push(eager_chunk);
        products
            .schemas
            .schemas
            .push(products.schemas.schemas[0].clone());
        products.capture.envelope_writer.captured_at_is_null = false;
        products
            .anchors
            .anchors
            .push(products.anchors.anchors[0].clone());
        products.schemas.schema_index.clear();

        let codes = project_resume_diagnostics_from_products(&model, &products)
            .into_iter()
            .map(|diagnostic| diagnostic.code)
            .collect::<std::collections::BTreeSet<_>>();
        for code in [
            "PSC1098", "PSC1102", "PSC1104", "PSC1105", "PSC1106", "PSC1110",
        ] {
            assert!(codes.contains(code), "missing {code}: {codes:#?}");
        }
    }
}