wenlan-types 0.17.2

Shared wire-format types for Wenlan, the local-first personal agent memory system.
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
// SPDX-License-Identifier: Apache-2.0
use super::*;
use serde::{de::Error as _, Deserialize, Deserializer, Serialize};
use std::collections::BTreeSet;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LintTotals {
    checks: u32,
    passed: u32,
    findings: u32,
    actionable_findings: u32,
    advisory_findings: u32,
    incomplete: u32,
}
impl LintTotals {
    fn from_checks(checks: &[LintCheckResult]) -> Result<Self, LintContractError> {
        let checks_count =
            u32::try_from(checks.len()).map_err(|_| LintContractError::TooManyChecks)?;
        let mut totals = Self {
            checks: checks_count,
            passed: 0,
            findings: 0,
            actionable_findings: 0,
            advisory_findings: 0,
            incomplete: 0,
        };
        for check in checks {
            match check.outcome {
                LintOutcome::Pass => totals.passed += 1,
                LintOutcome::Finding => {
                    totals.findings += 1;
                    match check.gate_effect() {
                        LintGateEffect::Actionable => totals.actionable_findings += 1,
                        LintGateEffect::Advisory => totals.advisory_findings += 1,
                    }
                }
                LintOutcome::NotRunPrerequisite
                | LintOutcome::InconsistentSnapshot
                | LintOutcome::FailedToRun => totals.incomplete += 1,
            }
        }
        Ok(totals)
    }
    pub const fn checks(&self) -> u32 {
        self.checks
    }
    pub const fn findings(&self) -> u32 {
        self.findings
    }
    pub const fn actionable_findings(&self) -> u32 {
        self.actionable_findings
    }
    pub const fn advisory_findings(&self) -> u32 {
        self.advisory_findings
    }
    pub const fn passed(&self) -> u32 {
        self.passed
    }
    pub const fn incomplete(&self) -> u32 {
        self.incomplete
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LintReport {
    report_schema_version: u16,
    check_catalog_version: u16,
    profile: LintProfile,
    scope: LintScope,
    capability_context: LintCapabilityContext,
    snapshots: LintSnapshotReceipts,
    config_fingerprint: LintConfigFingerprint,
    producer_receipt: LintProducerReceipt,
    #[serde(skip_serializing_if = "Option::is_none")]
    agent_work: Option<LintAgentWork>,
    checks: Vec<LintCheckResult>,
    totals: LintTotals,
    complete: bool,
}
#[derive(Deserialize)]
struct LintReportWire {
    report_schema_version: u16,
    check_catalog_version: u16,
    profile: LintProfile,
    scope: LintScope,
    capability_context: LintCapabilityContext,
    snapshots: LintSnapshotReceipts,
    config_fingerprint: LintConfigFingerprint,
    producer_receipt: LintProducerReceipt,
    #[serde(default)]
    agent_work: Option<LintAgentWork>,
    checks: Vec<LintCheckResult>,
    totals: LintTotals,
    complete: bool,
}
impl LintReport {
    pub fn try_new(
        scope: LintScope,
        capability_context: LintCapabilityContext,
        snapshots: LintSnapshotReceipts,
        config_fingerprint: LintConfigFingerprint,
        producer_receipt: LintProducerReceipt,
        checks: Vec<LintCheckResult>,
    ) -> Result<Self, LintContractError> {
        Self::try_new_for_profile(
            LintProfile::General,
            scope,
            capability_context,
            snapshots,
            config_fingerprint,
            producer_receipt,
            checks,
        )
    }

    pub fn try_new_for_profile(
        profile: LintProfile,
        scope: LintScope,
        capability_context: LintCapabilityContext,
        snapshots: LintSnapshotReceipts,
        config_fingerprint: LintConfigFingerprint,
        producer_receipt: LintProducerReceipt,
        checks: Vec<LintCheckResult>,
    ) -> Result<Self, LintContractError> {
        Self::try_new_for_profile_with_agent_work(
            profile,
            scope,
            capability_context,
            snapshots,
            config_fingerprint,
            producer_receipt,
            checks,
            None,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn try_new_for_profile_with_agent_work(
        profile: LintProfile,
        scope: LintScope,
        capability_context: LintCapabilityContext,
        snapshots: LintSnapshotReceipts,
        config_fingerprint: LintConfigFingerprint,
        producer_receipt: LintProducerReceipt,
        mut checks: Vec<LintCheckResult>,
        agent_work: Option<LintAgentWork>,
    ) -> Result<Self, LintContractError> {
        if profile == LintProfile::General && agent_work.is_some() {
            return Err(LintContractError::InvalidAgentWork);
        }
        checks.sort_by(|left, right| left.check_id().cmp(right.check_id()));
        let totals = LintTotals::from_checks(&checks)?;
        let complete = checks.iter().all(|check| check.outcome.is_complete());
        Ok(Self {
            report_schema_version: LINT_REPORT_SCHEMA_VERSION,
            check_catalog_version: LINT_CHECK_CATALOG_VERSION,
            profile,
            scope,
            capability_context,
            snapshots,
            config_fingerprint,
            producer_receipt,
            agent_work,
            checks,
            totals,
            complete,
        })
    }
    pub const fn complete(&self) -> bool {
        self.complete
    }
    pub const fn profile(&self) -> LintProfile {
        self.profile
    }
    pub const fn totals(&self) -> &LintTotals {
        &self.totals
    }
    pub fn checks(&self) -> &[LintCheckResult] {
        &self.checks
    }
    pub fn scope(&self) -> &LintScope {
        &self.scope
    }
    pub const fn capability_context(&self) -> LintCapabilityContext {
        self.capability_context
    }
    pub fn snapshots(&self) -> &LintSnapshotReceipts {
        &self.snapshots
    }
    pub fn config_fingerprint(&self) -> &LintConfigFingerprint {
        &self.config_fingerprint
    }
    pub fn producer_receipt(&self) -> &LintProducerReceipt {
        &self.producer_receipt
    }
    pub fn agent_work(&self) -> Option<&LintAgentWork> {
        self.agent_work.as_ref()
    }
}
impl<'de> Deserialize<'de> for LintReport {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let wire = LintReportWire::deserialize(deserializer)?;
        if wire.report_schema_version != LINT_REPORT_SCHEMA_VERSION {
            return Err(D::Error::custom(LintContractError::UnsupportedReportSchema));
        }
        if wire.check_catalog_version != LINT_CHECK_CATALOG_VERSION {
            return Err(D::Error::custom(LintContractError::UnsupportedCheckCatalog));
        }
        let expected_checks = match wire.profile {
            LintProfile::General => LINT_GENERAL_CHECK_COUNT,
            LintProfile::Deep => LINT_DEEP_CHECK_COUNT,
        };
        let unique_ids = wire
            .checks
            .iter()
            .map(LintCheckResult::check_id)
            .collect::<BTreeSet<_>>();
        if wire.checks.len() != expected_checks || unique_ids.len() != wire.checks.len() {
            return Err(D::Error::custom(LintContractError::InvalidCatalogShape));
        }
        if wire.checks.iter().any(|check| {
            canonical_gate_effect(wire.profile, check.check_id()) != Some(check.gate_effect())
        }) {
            return Err(D::Error::custom(LintContractError::InvalidCatalogShape));
        }
        let report = Self::try_new_for_profile_with_agent_work(
            wire.profile,
            wire.scope,
            wire.capability_context,
            wire.snapshots,
            wire.config_fingerprint,
            wire.producer_receipt,
            wire.checks,
            wire.agent_work,
        )
        .map_err(D::Error::custom)?;
        if report.totals != wire.totals {
            return Err(D::Error::custom(LintContractError::InvalidTotals));
        }
        if report.complete != wire.complete {
            return Err(D::Error::custom(LintContractError::InvalidCompleteness));
        }
        Ok(report)
    }
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LintQuery {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub profile: Option<LintProfile>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub space: Option<String>,
}

impl LintQuery {
    pub const fn new(profile: Option<LintProfile>, space: Option<String>) -> Self {
        Self { profile, space }
    }

    pub fn applied_profile(&self) -> LintProfile {
        self.profile.unwrap_or_default()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LintRequestQuery {
    #[serde(flatten)]
    lint: LintQuery,
    #[serde(default, skip_serializing_if = "is_false")]
    external_egress: bool,
    #[serde(default, skip_serializing_if = "is_false")]
    agent_assist: bool,
}

impl LintRequestQuery {
    pub const fn new(lint: LintQuery, external_egress: bool, agent_assist: bool) -> Self {
        Self {
            lint,
            external_egress,
            agent_assist,
        }
    }

    pub const fn lint(&self) -> &LintQuery {
        &self.lint
    }

    pub const fn external_egress(&self) -> bool {
        self.external_egress
    }

    pub const fn agent_assist(&self) -> bool {
        self.agent_assist
    }
}

const fn is_false(value: &bool) -> bool {
    !*value
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LintErrorResponse {
    error: String,
}

impl LintErrorResponse {
    pub fn new(error: impl Into<String>) -> Self {
        Self {
            error: error.into(),
        }
    }

    pub fn error(&self) -> &str {
        &self.error
    }
}

impl LintReport {
    /// The report as the text `wenlan lint` prints and the MCP `lint` tool
    /// returns: totals, per-group counts, then every finding, advisory and
    /// incomplete check with its recommendation and coverage. The typed
    /// report stays the contract; this is what a person or an agent reads.
    pub fn render_text(&self) -> String {
        let totals = self.totals();
        let mut groups = [(0_u32, 0_u32, 0_u32); 7];
        for check in self.checks() {
            let Some(group) = LintCheckGroup::for_check_id(check.check_id()) else {
                continue;
            };
            let counts = &mut groups[group_index(group)];
            counts.0 += 1;
            match check.outcome() {
                LintOutcome::Pass => {}
                LintOutcome::Finding => counts.1 += 1,
                LintOutcome::NotRunPrerequisite
                | LintOutcome::InconsistentSnapshot
                | LintOutcome::FailedToRun => counts.2 += 1,
            }
        }
        let mut output = format!(
            "Lint: {} checks, {} passed, {} actionable findings, {} advisor{}, {} incomplete\nGroups:\n",
            totals.checks(),
            totals.passed(),
            totals.actionable_findings(),
            totals.advisory_findings(),
            if totals.advisory_findings() == 1 { "y" } else { "ies" },
            totals.incomplete()
        );
        for group in LintCheckGroup::ALL {
            let (checks, findings, incomplete) = groups[group_index(group)];
            if checks == 0 {
                continue;
            }
            output.push_str(&format!(
                "  {}: {checks} check{}, {findings} findings, {incomplete} incomplete\n",
                group.as_str(),
                if checks == 1 { "" } else { "s" }
            ));
        }
        if let Some(work) = self.agent_work() {
            output.push_str(&format!(
                "Agent work: {} bounded records; digest={}\n",
                work.records().len(),
                work.work_digest().as_str()
            ));
        }
        append_findings(&mut output, "Findings", self, LintGateEffect::Actionable);
        append_findings(&mut output, "Advisories", self, LintGateEffect::Advisory);
        // A check that passed but still carries advice is waiting on the owner, not
        // broken — e.g. a graph channel with no model source chosen. It is not a
        // finding and does not change the exit code, but the reader still has to see
        // the one thing they can do about it.
        output.push_str("Waiting on you");
        let waiting: Vec<_> = self
            .checks()
            .iter()
            .filter(|check| check.outcome() == LintOutcome::Pass && check.action_code().is_some())
            .collect();
        append_selected(&mut output, &waiting);
        output.push_str("Incomplete");
        let incomplete: Vec<_> = self
            .checks()
            .iter()
            .filter(|check| !matches!(check.outcome(), LintOutcome::Pass | LintOutcome::Finding))
            .collect();
        append_selected(&mut output, &incomplete);
        output
    }
}

fn append_findings(
    output: &mut String,
    label: &str,
    report: &LintReport,
    gate_effect: LintGateEffect,
) {
    output.push_str(label);
    let selected: Vec<_> = report
        .checks()
        .iter()
        .filter(|check| {
            check.outcome() == LintOutcome::Finding && check.gate_effect() == gate_effect
        })
        .collect();
    append_selected(output, &selected);
}

fn append_selected(output: &mut String, checks: &[&LintCheckResult]) {
    if checks.is_empty() {
        output.push_str(": none\n");
        return;
    }
    output.push_str(&format!(" ({}):\n", checks.len()));
    for check in checks {
        let summary = summary_name(check.summary_code());
        let code_suffix = match (check.recommendation_code(), check.action_code()) {
            (Some(recommendation), _) => {
                format!("; recommendation: {}", recommendation_name(recommendation))
            }
            (None, Some(action)) => format!("; action: {}", action_name(action)),
            (None, None) => String::new(),
        };
        output.push_str(&format!("  {}: {summary}{code_suffix}\n", check.check_id()));
        // The codes above are stable identifiers for scripts. This line is the
        // same thing in plain English for the person reading the terminal.
        output.push_str(&format!(
            "    {}{}\n",
            check.summary_code().meaning(),
            match (check.recommendation_code(), check.action_code()) {
                (Some(recommendation), _) => format!(" {}", recommendation.action()),
                (None, Some(action)) => format!(" {}", action.action()),
                (None, None) => String::new(),
            }
        ));
        let affected = check.metrics().iter().find_map(|metric| {
            if metric.code() == LintMetricCode::AffectedRecords {
                match metric.value() {
                    LintMetricValue::Count { value } => Some(*value),
                    LintMetricValue::Boolean { .. } | LintMetricValue::CatalogCode { .. } => None,
                }
            } else {
                None
            }
        });
        let mut evidence_items = check
            .evidence()
            .iter()
            .take(8)
            .map(evidence_name)
            .collect::<Vec<_>>();
        if check.evidence().len() > evidence_items.len() {
            evidence_items.push(format!(
                "+{}_more",
                check.evidence().len() - evidence_items.len()
            ));
        }
        let evidence = evidence_items.join(",");
        output.push_str(&format!(
            "    affected={}; evaluated={}/{}; evidence={}; truncated={}\n",
            affected.map_or_else(|| "unknown".to_string(), |value| value.to_string()),
            check.coverage().evaluated(),
            check.coverage().denominator(),
            if evidence.is_empty() {
                "none"
            } else {
                &evidence
            },
            check.coverage().truncated(),
        ));
    }
}

const fn group_index(group: LintCheckGroup) -> usize {
    match group {
        LintCheckGroup::Identity => 0,
        LintCheckGroup::KnowledgeGraph => 1,
        LintCheckGroup::Memories => 2,
        LintCheckGroup::Operations => 3,
        LintCheckGroup::Pages => 4,
        LintCheckGroup::Runtime => 5,
        LintCheckGroup::Serving => 6,
    }
}

fn evidence_name(evidence: &LintEvidenceRef) -> String {
    match evidence {
        LintEvidenceRef::OpaqueId { opaque_id } => format!("opaque:{}", opaque_id.ordinal()),
        LintEvidenceRef::OpaqueDigest { opaque_digest } => {
            format!("opaque-digest:{}", opaque_digest.as_str())
        }
        LintEvidenceRef::ReasonCode { reason_code } => {
            format!("reason:{}", reason_name(*reason_code))
        }
        LintEvidenceRef::SafeRootRelativePath {
            safe_root_relative_path,
        } => format!("path:{safe_root_relative_path:?}"),
        LintEvidenceRef::SemanticFinding { finding } => format!(
            "semantic:{}:{:?}:{:?}:{}:{:?}",
            finding.candidate_id().ordinal(),
            finding.proposed_action(),
            finding.reason_code(),
            finding.confidence_basis_points(),
            finding.provider_route(),
        ),
    }
}

const fn reason_name(reason: LintReasonCode) -> &'static str {
    match reason {
        LintReasonCode::MissingArtifact => "missing_artifact",
        LintReasonCode::InvalidCatalogState => "invalid_catalog_state",
        LintReasonCode::ExpectedEmptySubstrate => "expected_empty_substrate",
        LintReasonCode::InvalidSourceConfiguration => "invalid_source_configuration",
        LintReasonCode::TerminalOperationFailure => "terminal_operation_failure",
        LintReasonCode::ExpiredRetry => "expired_retry",
        LintReasonCode::InvalidOperationState => "invalid_operation_state",
        LintReasonCode::DurableNoProgress => "durable_no_progress",
        LintReasonCode::SemanticProviderUnavailable => "semantic_provider_unavailable",
        LintReasonCode::InsufficientSemanticEvidence => "insufficient_semantic_evidence",
        LintReasonCode::SemanticExecutionFailure => "semantic_execution_failure",
        LintReasonCode::SemanticAgentAdjudicationRequired => "semantic_agent_adjudication_required",
        LintReasonCode::SemanticAgentWorkStale => "semantic_agent_work_stale",
        LintReasonCode::SemanticAgentSubmissionInvalid => "semantic_agent_submission_invalid",
        LintReasonCode::SemanticCandidateGenerationFailure => {
            "semantic_candidate_generation_failure"
        }
        LintReasonCode::SemanticPopulationIncomplete => "semantic_population_incomplete",
        LintReasonCode::SemanticDisagreementUnresolved => "semantic_disagreement_unresolved",
        LintReasonCode::SemanticSecondJudgeRequired => "semantic_second_judge_required",
    }
}

const fn recommendation_name(recommendation: LintRecommendationCode) -> &'static str {
    match recommendation {
        LintRecommendationCode::ReviewFinding => "review_finding",
        LintRecommendationCode::RestorePrerequisite => "restore_prerequisite",
        LintRecommendationCode::RerunAfterSnapshotStabilizes => "rerun_after_snapshot_stabilizes",
        LintRecommendationCode::InspectRuntime => "inspect_runtime",
    }
}

const fn action_name(action: LintActionCode) -> &'static str {
    match action {
        LintActionCode::ChooseModelSource => "choose_model_source",
    }
}

const fn summary_name(summary: LintSummaryCode) -> &'static str {
    match summary {
        LintSummaryCode::CheckPassed => "check_passed",
        LintSummaryCode::FindingDetected => "finding_detected",
        LintSummaryCode::PrerequisiteUnavailable => "prerequisite_unavailable",
        LintSummaryCode::SnapshotInconsistent => "snapshot_inconsistent",
        LintSummaryCode::ExecutionFailed => "execution_failed",
        LintSummaryCode::ExpectedEmpty => "expected_empty",
    }
}