lenso-service 0.1.24

Public contracts for Lenso Providers and Autonomous Services.
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
use std::collections::{BTreeMap, BTreeSet};

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use utoipa::ToSchema;

use crate::extraction_input_digest;

pub const PERFORMANCE_PROFILE_PROTOCOL: &str = "lenso.performance-profile.v1";

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema, ToSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum PerformanceProfileScope {
    ReducedDeterministic,
    EnvironmentVerification,
}

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema, ToSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum PerformanceMetric {
    DirectCallLatency,
    DirectCallThroughput,
    ResolverClientOverhead,
    PublishToConsumeLatency,
    InboxOutboxLag,
    WorkflowTransitionLatency,
    WorkflowTimerDelay,
    StoryFreshness,
    ConsoleQueryLatency,
    ConvergenceLatency,
    CpuUtilization,
    MemoryBytes,
    DatabaseConnections,
    BrokerBytes,
}

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema, ToSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum PerformanceBudgetDirection {
    AtMost,
    AtLeast,
}

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema, ToSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum PerformanceDecision {
    Passed,
    Blocked,
}

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema, ToSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum PerformanceIssueCode {
    TopologyInvalid,
    MetadataIncomplete,
    MetricMissing,
    BudgetExceeded,
    VarianceExceeded,
    HiddenDataPlaneDependency,
    EnvironmentEvidenceInsufficient,
}

impl PerformanceIssueCode {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::TopologyInvalid => "performance_topology_invalid",
            Self::MetadataIncomplete => "performance_metadata_incomplete",
            Self::MetricMissing => "performance_metric_missing",
            Self::BudgetExceeded => "performance_budget_exceeded",
            Self::VarianceExceeded => "performance_variance_exceeded",
            Self::HiddenDataPlaneDependency => "performance_hidden_data_plane_dependency",
            Self::EnvironmentEvidenceInsufficient => {
                "performance_environment_evidence_insufficient"
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PerformanceIssue {
    pub code: PerformanceIssueCode,
    pub message: String,
    pub remediation: String,
    pub next_actions: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ReferenceService {
    pub service_id: String,
    pub contract_id: String,
    pub store_id: String,
    pub release_digest: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ReferenceSystemTopology {
    pub topology_digest: String,
    pub services: Vec<ReferenceService>,
    pub transport_adapter_version: String,
    pub identity_adapter_version: String,
    pub deployment_adapter_version: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PerformanceBudget {
    pub metric: PerformanceMetric,
    pub unit: String,
    pub direction: PerformanceBudgetDirection,
    pub threshold: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PerformanceMeasurement {
    pub metric: PerformanceMetric,
    pub unit: String,
    pub value: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PerformanceRun {
    pub run_id: String,
    pub release_set_digest: String,
    pub dataset_digest: String,
    pub concurrency: u32,
    pub duration_ms: u64,
    pub warmup_ms: u64,
    pub machine: BTreeMap<String, String>,
    pub infrastructure: BTreeMap<String, String>,
    pub measurements: Vec<PerformanceMeasurement>,
    pub system_plane_data_plane_requests: u64,
    pub runtime_console_data_plane_requests: u64,
    pub telemetry_data_plane_requests: u64,
    pub policy_data_plane_requests: u64,
    pub registry_data_plane_requests: u64,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PerformanceProfileInput {
    pub scope: PerformanceProfileScope,
    pub support_manifest_digest: String,
    pub topology: ReferenceSystemTopology,
    pub budgets: Vec<PerformanceBudget>,
    pub runs: Vec<PerformanceRun>,
    pub variance_tolerance_basis_points: u32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct PerformanceProfile {
    pub protocol: String,
    pub profile_id: String,
    pub profile_digest: String,
    pub scope: PerformanceProfileScope,
    pub support_manifest_digest: String,
    pub topology: ReferenceSystemTopology,
    pub budgets: Vec<PerformanceBudget>,
    pub runs: Vec<PerformanceRun>,
    pub variance_basis_points: BTreeMap<PerformanceMetric, u32>,
    pub decision: PerformanceDecision,
    pub issues: Vec<PerformanceIssue>,
    pub next_actions: Vec<String>,
}

#[must_use]
pub fn evaluate_performance_profile(mut input: PerformanceProfileInput) -> PerformanceProfile {
    input
        .topology
        .services
        .sort_by(|left, right| left.service_id.cmp(&right.service_id));
    input.budgets.sort_by_key(|budget| budget.metric);
    input
        .runs
        .sort_by(|left, right| left.run_id.cmp(&right.run_id));
    for run in &mut input.runs {
        run.measurements
            .sort_by_key(|measurement| measurement.metric);
    }

    let mut issues = Vec::new();
    let services = &input.topology.services;
    let service_ids = services
        .iter()
        .map(|service| service.service_id.as_str())
        .collect::<BTreeSet<_>>();
    let contract_ids = services
        .iter()
        .map(|service| service.contract_id.as_str())
        .collect::<BTreeSet<_>>();
    let store_ids = services
        .iter()
        .map(|service| service.store_id.as_str())
        .collect::<BTreeSet<_>>();
    if services.len() != 3
        || service_ids.len() != 3
        || contract_ids.len() != 3
        || store_ids.len() != 3
        || services.iter().any(|service| {
            service.service_id.trim().is_empty()
                || service.contract_id.trim().is_empty()
                || service.store_id.trim().is_empty()
                || !valid_digest(&service.release_digest)
        })
        || !valid_digest(&input.topology.topology_digest)
    {
        issues.push(issue(
            PerformanceIssueCode::TopologyInvalid,
            "The reference System is not exactly three distinct logical Services, Contracts, and Stores.",
            "Use three independently identified Service boundaries rather than replicas.",
            "Correct the reference topology and repeat the profile.",
        ));
    }

    let required_metrics = required_metrics();
    let budget_metrics = input
        .budgets
        .iter()
        .map(|budget| budget.metric)
        .collect::<BTreeSet<_>>();
    if !valid_digest(&input.support_manifest_digest)
        || input.topology.transport_adapter_version.trim().is_empty()
        || input.topology.identity_adapter_version.trim().is_empty()
        || input.topology.deployment_adapter_version.trim().is_empty()
        || input.budgets.len() != required_metrics.len()
        || budget_metrics != required_metrics
        || input.variance_tolerance_basis_points == 0
    {
        issues.push(issue(
            PerformanceIssueCode::MetadataIncomplete,
            "Performance profile metadata or evidence-backed budgets are incomplete.",
            "Bind the profile to exact releases, topology, adapters, units, and tolerances.",
            "Complete the pinned-environment profile metadata.",
        ));
    }

    let required_run_count = match input.scope {
        PerformanceProfileScope::ReducedDeterministic => 1,
        PerformanceProfileScope::EnvironmentVerification => 3,
    };
    if input.runs.len() < required_run_count {
        issues.push(issue(
            PerformanceIssueCode::EnvironmentEvidenceInsufficient,
            "The selected profile scope does not include enough repeated runs.",
            "Use at least three runs for Environment Verification and one for reduced diagnosis.",
            "Collect the missing pinned-environment runs.",
        ));
    }

    let budgets = input
        .budgets
        .iter()
        .map(|budget| (budget.metric, budget))
        .collect::<BTreeMap<_, _>>();
    for run in &input.runs {
        let metrics = run
            .measurements
            .iter()
            .map(|measurement| (measurement.metric, measurement))
            .collect::<BTreeMap<_, _>>();
        let metadata_valid = !run.run_id.trim().is_empty()
            && valid_digest(&run.release_set_digest)
            && valid_digest(&run.dataset_digest)
            && run.concurrency > 0
            && run.duration_ms > 0
            && run.warmup_ms > 0
            && !run.machine.is_empty()
            && !run.infrastructure.is_empty();
        if !metadata_valid {
            issues.push(issue(
                PerformanceIssueCode::MetadataIncomplete,
                format!("Performance run `{}` has incomplete metadata.", run.run_id),
                "Record concurrency, duration, warm-up, machine, infrastructure, dataset, and release facts.",
                "Repeat the run with the complete versioned profile.",
            ));
        }
        if metrics.len() != required_metrics.len()
            || required_metrics
                .iter()
                .any(|metric| !metrics.contains_key(metric))
        {
            issues.push(issue(
                PerformanceIssueCode::MetricMissing,
                format!("Performance run `{}` does not cover every required path.", run.run_id),
                "Measure request, Event, Workflow, Story, Console, convergence, and resource paths together.",
                "Collect the missing measurements and rerun the profile.",
            ));
        }
        for (metric, measurement) in metrics {
            let Some(budget) = budgets.get(&metric) else {
                issues.push(issue(
                    PerformanceIssueCode::MetricMissing,
                    format!("Performance metric `{:?}` has no reviewed budget.", metric),
                    "Define one unique budget for every required metric.",
                    "Correct the budget set and repeat the profile.",
                ));
                continue;
            };
            if measurement.unit != budget.unit
                || match budget.direction {
                    PerformanceBudgetDirection::AtMost => measurement.value > budget.threshold,
                    PerformanceBudgetDirection::AtLeast => measurement.value < budget.threshold,
                }
            {
                issues.push(issue(
                    PerformanceIssueCode::BudgetExceeded,
                    format!(
                        "Performance run `{}` exceeded the {:?} budget.",
                        run.run_id, metric
                    ),
                    "Treat the pinned threshold as an evidence-backed environment budget.",
                    "Diagnose the regression or update the reviewed profile with new evidence.",
                ));
            }
        }
        if run.system_plane_data_plane_requests > 0
            || run.runtime_console_data_plane_requests > 0
            || run.telemetry_data_plane_requests > 0
            || run.policy_data_plane_requests > 0
            || run.registry_data_plane_requests > 0
        {
            issues.push(issue(
                PerformanceIssueCode::HiddenDataPlaneDependency,
                "Established Data Plane traffic depended on a coordination or observability surface.",
                "Keep System Plane, Console, telemetry, policy, and registry services outside established traffic.",
                "Correct the topology and repeat the profile with those surfaces withheld.",
            ));
        }
    }

    let variance_basis_points = calculate_variance(&input.runs);
    if variance_basis_points
        .values()
        .any(|variance| *variance > input.variance_tolerance_basis_points)
    {
        issues.push(issue(
            PerformanceIssueCode::VarianceExceeded,
            "Repeated runs exceed the declared variance tolerance.",
            "Separate environment drift from a product regression before accepting the profile.",
            "Stabilize or re-pin the environment and repeat the measurements.",
        ));
    }

    let decision = if issues.is_empty() {
        PerformanceDecision::Passed
    } else {
        PerformanceDecision::Blocked
    };
    let next_actions = if issues.is_empty() {
        vec!["Attach this profile to the M6 acceptance evidence set.".to_owned()]
    } else {
        issues
            .iter()
            .flat_map(|issue| issue.next_actions.iter().cloned())
            .collect()
    };
    let mut profile = PerformanceProfile {
        protocol: PERFORMANCE_PROFILE_PROTOCOL.to_owned(),
        profile_id: String::new(),
        profile_digest: String::new(),
        scope: input.scope,
        support_manifest_digest: input.support_manifest_digest,
        topology: input.topology,
        budgets: input.budgets,
        runs: input.runs,
        variance_basis_points,
        decision,
        issues,
        next_actions,
    };
    profile.profile_digest = digest_without_identity(&profile);
    profile.profile_id = format!("performance-profile:{}", &profile.profile_digest[7..23]);
    profile
}

#[must_use]
pub fn performance_profile_schema() -> Value {
    let mut schema = serde_json::to_value(schemars::schema_for!(PerformanceProfile))
        .expect("performance profile schema serializes");
    schema["$id"] = Value::String(
        "https://contracts.lenso.local/ga/lenso.performance-profile.v1.schema.json".to_owned(),
    );
    schema
}

fn required_metrics() -> BTreeSet<PerformanceMetric> {
    [
        PerformanceMetric::DirectCallLatency,
        PerformanceMetric::DirectCallThroughput,
        PerformanceMetric::ResolverClientOverhead,
        PerformanceMetric::PublishToConsumeLatency,
        PerformanceMetric::InboxOutboxLag,
        PerformanceMetric::WorkflowTransitionLatency,
        PerformanceMetric::WorkflowTimerDelay,
        PerformanceMetric::StoryFreshness,
        PerformanceMetric::ConsoleQueryLatency,
        PerformanceMetric::ConvergenceLatency,
        PerformanceMetric::CpuUtilization,
        PerformanceMetric::MemoryBytes,
        PerformanceMetric::DatabaseConnections,
        PerformanceMetric::BrokerBytes,
    ]
    .into_iter()
    .collect()
}

fn calculate_variance(runs: &[PerformanceRun]) -> BTreeMap<PerformanceMetric, u32> {
    let mut values = BTreeMap::<PerformanceMetric, Vec<u64>>::new();
    for run in runs {
        for measurement in &run.measurements {
            values
                .entry(measurement.metric)
                .or_default()
                .push(measurement.value);
        }
    }
    values
        .into_iter()
        .map(|(metric, values)| {
            let min = values.iter().copied().min().unwrap_or(0);
            let max = values.iter().copied().max().unwrap_or(0);
            let variance = if min == 0 {
                if max == 0 { 0 } else { u32::MAX }
            } else {
                u32::try_from(((max - min) as u128 * 10_000) / u128::from(min)).unwrap_or(u32::MAX)
            };
            (metric, variance)
        })
        .collect()
}

fn issue(
    code: PerformanceIssueCode,
    message: impl Into<String>,
    remediation: impl Into<String>,
    next_action: impl Into<String>,
) -> PerformanceIssue {
    PerformanceIssue {
        code,
        message: message.into(),
        remediation: remediation.into(),
        next_actions: vec![next_action.into()],
    }
}

fn valid_digest(value: &str) -> bool {
    value.strip_prefix("sha256:").is_some_and(|digest| {
        digest.len() == 64
            && digest
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    })
}

fn digest_without_identity(profile: &PerformanceProfile) -> String {
    let mut canonical = profile.clone();
    canonical.profile_id.clear();
    canonical.profile_digest.clear();
    extraction_input_digest(
        &serde_json::to_vec(&canonical).expect("performance profile serializes"),
    )
}

#[must_use]
pub fn performance_profile_integrity_is_valid(profile: &PerformanceProfile) -> bool {
    valid_digest(&profile.profile_digest)
        && profile.profile_digest == digest_without_identity(profile)
        && profile.profile_id == format!("performance-profile:{}", &profile.profile_digest[7..23])
}