starweaver-session 0.10.0

Durable session contracts for Starweaver
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
//! Product-neutral durable environment attachment and mount contracts.
//!
//! These records deliberately contain only public environment identities and reviewed display
//! projections. Provider endpoints, credentials, and private resource source references belong to
//! the product-side resolver and must never be placed in these values.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::{
    DurableHostEventClass, DurableHostEventScope, MutationReceipt, PendingHostEventPublication,
    SessionStoreError, SessionStoreResult,
};

/// Maximum page size accepted by durable attachment queries, matching the host schema.
pub const MAX_ENVIRONMENT_PAGE_SIZE: u32 = 200;
/// Maximum active mounts retained for one run, matching the host result bound.
pub const MAX_ENVIRONMENT_MOUNTS_PER_RUN: u32 = 128;
/// Stable operation identifier for attachment creation.
pub const ENVIRONMENT_ATTACH_OPERATION: &str = "environment.attach";
/// Stable operation identifier for attachment detachment.
pub const ENVIRONMENT_DETACH_OPERATION: &str = "environment.detach";
/// Stable operation identifier for mounting an allowlisted resource.
pub const ENVIRONMENT_MOUNT_OPERATION: &str = "environment.mount";
/// Stable operation identifier for unmounting a resource.
pub const ENVIRONMENT_UNMOUNT_OPERATION: &str = "environment.unmount";

/// Product-neutral attachment scope.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum DurableEnvironmentScope {
    /// Scope follows one transport-owned connection and is never transferable.
    Connection {
        /// Opaque host-generated connection identity.
        connection_id: String,
    },
    /// Scope follows one durable session.
    Session {
        /// Session identity.
        session_id: String,
    },
    /// Scope follows one durable run.
    Run {
        /// Session identity.
        session_id: String,
        /// Run identity.
        run_id: String,
    },
}

impl DurableEnvironmentScope {
    /// Validate scope identities.
    ///
    /// # Errors
    ///
    /// Returns an error when a scoped session or run identity is empty.
    pub fn validate(&self) -> SessionStoreResult<()> {
        match self {
            Self::Connection { connection_id } => {
                require_non_empty("environment scope connection", connection_id)
            }
            Self::Session { session_id } => {
                require_non_empty("environment scope session", session_id)
            }
            Self::Run { session_id, run_id } => {
                require_non_empty("environment scope session", session_id)?;
                require_non_empty("environment scope run", run_id)
            }
        }
    }

    /// Return whether this scope permits use by the specified run.
    #[must_use]
    pub fn permits_run(&self, connection_id: Option<&str>, session_id: &str, run_id: &str) -> bool {
        match self {
            Self::Connection {
                connection_id: owner,
            } => connection_id == Some(owner.as_str()),
            Self::Session { session_id: owner } => owner == session_id,
            Self::Run {
                session_id: owner_session,
                run_id: owner_run,
            } => owner_session == session_id && owner_run == run_id,
        }
    }
}

/// Durable attachment lifecycle state.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DurableEnvironmentStatus {
    /// Provider is being prepared.
    Attaching,
    /// Provider is ready for use.
    Ready,
    /// Provider is available with reduced capability.
    Degraded,
    /// Attachment is terminally detached.
    Detached,
}

/// Safe durable projection of an environment attachment.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DurableEnvironmentAttachment {
    /// Trusted authority binding owning the record and its idempotency namespace.
    pub authority_binding: String,
    /// Stable attachment identity.
    pub attachment_id: String,
    /// Public, configured environment catalog identity.
    pub environment_id: String,
    /// Optional reviewed display label; never a provider endpoint or source reference.
    pub display_name: Option<String>,
    /// Attachment scope.
    pub scope: DurableEnvironmentScope,
    /// Lifecycle state.
    pub status: DurableEnvironmentStatus,
    /// Monotonic record revision, beginning at one.
    pub revision: u64,
    /// Last committed mutation time.
    pub updated_at: DateTime<Utc>,
}

impl starweaver_core::VersionedRecord for DurableEnvironmentAttachment {
    const SCHEMA: &'static str = "starweaver.session.environment_attachment";
}

impl DurableEnvironmentAttachment {
    /// Validate persisted attachment invariants.
    ///
    /// # Errors
    ///
    /// Returns an error when an identity, display label, scope, or revision is invalid.
    pub fn validate(&self) -> SessionStoreResult<()> {
        require_non_empty("environment authority binding", &self.authority_binding)?;
        require_non_empty("environment attachment id", &self.attachment_id)?;
        require_non_empty("public environment id", &self.environment_id)?;
        if self.display_name.as_ref().is_some_and(String::is_empty) {
            return Err(SessionStoreError::Failed(
                "environment display name cannot be empty".to_string(),
            ));
        }
        self.scope.validate()?;
        require_revision(self.revision, "environment attachment")
    }
}

/// Durable mount lifecycle state.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DurableEnvironmentMountStatus {
    /// Resource is actively mounted.
    Mounted,
    /// Resource is terminally unmounted.
    Unmounted,
}

/// Safe durable projection of one mounted resource.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DurableEnvironmentMount {
    /// Trusted authority binding owning the mount.
    pub authority_binding: String,
    /// Stable mount identity.
    pub mount_id: String,
    /// Owning attachment identity.
    pub attachment_id: String,
    /// Owning session identity.
    pub session_id: String,
    /// Owning run identity.
    pub run_id: String,
    /// Reviewed label resolved from a product-private resource allowlist.
    pub resource_label: String,
    /// Mount lifecycle state.
    pub status: DurableEnvironmentMountStatus,
    /// Monotonic record revision, beginning at one.
    pub revision: u64,
    /// Last committed mutation time.
    pub updated_at: DateTime<Utc>,
}

impl starweaver_core::VersionedRecord for DurableEnvironmentMount {
    const SCHEMA: &'static str = "starweaver.session.environment_mount";
}

impl DurableEnvironmentMount {
    /// Validate persisted mount invariants.
    ///
    /// # Errors
    ///
    /// Returns an error when an identity, safe resource label, or revision is invalid.
    pub fn validate(&self) -> SessionStoreResult<()> {
        require_non_empty("environment authority binding", &self.authority_binding)?;
        require_non_empty("environment mount id", &self.mount_id)?;
        require_non_empty("environment attachment id", &self.attachment_id)?;
        require_non_empty("environment mount session", &self.session_id)?;
        require_non_empty("environment mount run", &self.run_id)?;
        require_non_empty("environment resource label", &self.resource_label)?;
        require_revision(self.revision, "environment mount")
    }
}

/// Stable identity and visibility scope for an environment event projected at commit time.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EnvironmentHostEventContext {
    /// Stable identity of the logical environment transition across retries.
    pub transition_identity: String,
    /// Durable visibility scope for the resulting event.
    pub scope: DurableHostEventScope,
}

impl EnvironmentHostEventContext {
    /// Validate stable event identity and scope.
    ///
    /// # Errors
    ///
    /// Returns an error when the transition or a scoped durable identity is empty.
    pub fn validate(&self) -> SessionStoreResult<()> {
        require_non_empty(
            "environment host event transition identity",
            &self.transition_identity,
        )?;
        match &self.scope {
            DurableHostEventScope::Global => Ok(()),
            DurableHostEventScope::Session { session_id } => {
                require_non_empty("environment host event session", session_id.as_str())
            }
            DurableHostEventScope::Run { session_id, run_id } => {
                require_non_empty("environment host event session", session_id.as_str())?;
                require_non_empty("environment host event run", run_id.as_str())
            }
        }
    }

    /// Project one transaction-final attachment into its exact durable host event.
    ///
    /// # Errors
    ///
    /// Returns an error when the context, attachment, or projected publication is invalid.
    pub fn publication(
        &self,
        attachment: &DurableEnvironmentAttachment,
    ) -> SessionStoreResult<PendingHostEventPublication> {
        self.validate()?;
        attachment.validate()?;
        let attachment_scope = match &attachment.scope {
            DurableEnvironmentScope::Connection { .. } => {
                serde_json::json!({"kind": "connection"})
            }
            DurableEnvironmentScope::Session { session_id } => {
                serde_json::json!({"kind": "session", "sessionId": session_id})
            }
            DurableEnvironmentScope::Run { session_id, run_id } => serde_json::json!({
                "kind": "run",
                "sessionId": session_id,
                "runId": run_id,
            }),
        };
        let status = match attachment.status {
            DurableEnvironmentStatus::Attaching => "attaching",
            DurableEnvironmentStatus::Ready => "ready",
            DurableEnvironmentStatus::Degraded => "degraded",
            DurableEnvironmentStatus::Detached => "detached",
        };
        let mut projected_attachment = serde_json::Map::from_iter([
            (
                "attachmentId".to_string(),
                serde_json::Value::String(attachment.attachment_id.clone()),
            ),
            (
                "environmentId".to_string(),
                serde_json::Value::String(attachment.environment_id.clone()),
            ),
            (
                "revision".to_string(),
                serde_json::Value::String(attachment.revision.to_string()),
            ),
            ("scope".to_string(), attachment_scope),
            (
                "status".to_string(),
                serde_json::Value::String(status.to_string()),
            ),
        ]);
        if let Some(display_name) = &attachment.display_name {
            projected_attachment.insert(
                "displayName".to_string(),
                serde_json::Value::String(display_name.clone()),
            );
        }
        PendingHostEventPublication::new(
            &self.transition_identity,
            0,
            self.scope.clone(),
            DurableHostEventClass::EnvironmentChanged,
            serde_json::json!({
                "kind": "environment_changed",
                "attachment": serde_json::Value::Object(projected_attachment),
            }),
            attachment.updated_at,
        )
    }
}

/// Shared authority, idempotency, timestamp, and event evidence for an environment mutation.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EnvironmentMutationContext {
    /// Trusted authority binding owning state and the idempotency namespace.
    pub authority_binding: String,
    /// Idempotency key scoped to `authority_binding` across environment operations.
    pub idempotency_key: String,
    /// Canonical fingerprint of the normalized, authorized command.
    pub command_fingerprint: String,
    /// Authoritative commit timestamp.
    pub occurred_at: DateTime<Utc>,
    /// Optional stable event context projected from transaction-final attachment state.
    pub host_event: Option<EnvironmentHostEventContext>,
}

impl EnvironmentMutationContext {
    /// Validate shared mutation evidence.
    ///
    /// # Errors
    ///
    /// Returns an error when authority, idempotency, fingerprint, or event evidence is invalid.
    pub fn validate(&self) -> SessionStoreResult<()> {
        require_non_empty("environment authority binding", &self.authority_binding)?;
        require_non_empty("environment idempotency key", &self.idempotency_key)?;
        require_non_empty("environment command fingerprint", &self.command_fingerprint)?;
        if let Some(host_event) = &self.host_event {
            host_event.validate()?;
        }
        Ok(())
    }
}

/// Atomic attachment creation command. All provider-private resolution has already occurred.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AttachEnvironment {
    /// Shared mutation evidence.
    pub context: EnvironmentMutationContext,
    /// Host-generated stable attachment identity.
    pub attachment_id: String,
    /// Public configured environment identity.
    pub environment_id: String,
    /// Reviewed public display label.
    pub display_name: Option<String>,
    /// Authorized attachment scope.
    pub scope: DurableEnvironmentScope,
    /// Initial resolved provider status.
    pub status: DurableEnvironmentStatus,
}

impl AttachEnvironment {
    /// Validate command evidence.
    ///
    /// # Errors
    ///
    /// Returns an error when shared mutation evidence or a command identity is invalid.
    pub fn validate(&self) -> SessionStoreResult<()> {
        self.context.validate()?;
        require_non_empty("environment attachment id", &self.attachment_id)?;
        require_non_empty("public environment id", &self.environment_id)?;
        if self.display_name.as_ref().is_some_and(String::is_empty) {
            return Err(SessionStoreError::Failed(
                "environment display name cannot be empty".to_string(),
            ));
        }
        if self.status == DurableEnvironmentStatus::Detached {
            return Err(SessionStoreError::Failed(
                "new environment attachment cannot be detached".to_string(),
            ));
        }
        self.scope.validate()
    }
}

/// Atomic attachment detachment command.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DetachEnvironment {
    /// Shared mutation evidence.
    pub context: EnvironmentMutationContext,
    /// Attachment identity.
    pub attachment_id: String,
}

impl DetachEnvironment {
    /// Validate command evidence.
    ///
    /// # Errors
    ///
    /// Returns an error when shared mutation evidence or a command identity is invalid.
    pub fn validate(&self) -> SessionStoreResult<()> {
        self.context.validate()?;
        require_non_empty("environment attachment id", &self.attachment_id)
    }
}

/// Atomic allowlisted-resource mount command.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MountEnvironmentResource {
    /// Shared mutation evidence.
    pub context: EnvironmentMutationContext,
    /// Host-generated mount identity.
    pub mount_id: String,
    /// Ready attachment identity.
    pub attachment_id: String,
    /// Owning session.
    pub session_id: String,
    /// Owning run.
    pub run_id: String,
    /// Current trusted connection identity when consuming connection-scoped authority.
    pub connection_id: Option<String>,
    /// Reviewed safe label from the product-private resource resolver.
    pub resource_label: String,
}

impl MountEnvironmentResource {
    /// Validate command evidence.
    ///
    /// # Errors
    ///
    /// Returns an error when shared mutation evidence or a command identity is invalid.
    pub fn validate(&self) -> SessionStoreResult<()> {
        self.context.validate()?;
        require_non_empty("environment mount id", &self.mount_id)?;
        require_non_empty("environment attachment id", &self.attachment_id)?;
        require_non_empty("environment mount session", &self.session_id)?;
        require_non_empty("environment mount run", &self.run_id)?;
        if self.connection_id.as_ref().is_some_and(String::is_empty) {
            return Err(SessionStoreError::Failed(
                "environment mount connection identity cannot be empty".to_string(),
            ));
        }
        require_non_empty("environment resource label", &self.resource_label)
    }
}

/// Atomic resource unmount command.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UnmountEnvironmentResource {
    /// Shared mutation evidence.
    pub context: EnvironmentMutationContext,
    /// Mount identity.
    pub mount_id: String,
}

impl UnmountEnvironmentResource {
    /// Validate command evidence.
    ///
    /// # Errors
    ///
    /// Returns an error when shared mutation evidence or a command identity is invalid.
    pub fn validate(&self) -> SessionStoreResult<()> {
        self.context.validate()?;
        require_non_empty("environment mount id", &self.mount_id)
    }
}

/// Durable result of an attachment mutation.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EnvironmentAttachmentMutationResult {
    /// Authoritative post-mutation attachment.
    pub attachment: DurableEnvironmentAttachment,
    /// Durable mutation receipt.
    pub receipt: MutationReceipt,
}

impl starweaver_core::VersionedRecord for EnvironmentAttachmentMutationResult {
    const SCHEMA: &'static str = "starweaver.session.environment_attachment_mutation_result";
}

impl EnvironmentAttachmentMutationResult {
    /// Validate receipt and state linkage.
    ///
    /// # Errors
    ///
    /// Returns an error when state, receipt, operation, or target evidence disagrees.
    pub fn validate(&self) -> SessionStoreResult<()> {
        self.attachment.validate()?;
        self.receipt.validate()?;
        if self.receipt.target_ref != self.attachment.attachment_id {
            return Err(SessionStoreError::Conflict(
                "environment receipt target does not match attachment".to_string(),
            ));
        }
        Ok(())
    }

    /// Return an exact-replay projection.
    #[must_use]
    pub fn replayed_projection(&self) -> Self {
        Self {
            attachment: self.attachment.clone(),
            receipt: self.receipt.replayed_projection(),
        }
    }
}

/// Durable result of a mount mutation.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EnvironmentMountMutationResult {
    /// Authoritative post-mutation mount.
    pub mount: DurableEnvironmentMount,
    /// Durable mutation receipt.
    pub receipt: MutationReceipt,
}

impl starweaver_core::VersionedRecord for EnvironmentMountMutationResult {
    const SCHEMA: &'static str = "starweaver.session.environment_mount_mutation_result";
}

impl EnvironmentMountMutationResult {
    /// Validate receipt and state linkage.
    ///
    /// # Errors
    ///
    /// Returns an error when state, receipt, operation, or target evidence disagrees.
    pub fn validate(&self) -> SessionStoreResult<()> {
        self.mount.validate()?;
        self.receipt.validate()?;
        if self.receipt.target_ref != self.mount.mount_id {
            return Err(SessionStoreError::Conflict(
                "environment receipt target does not match mount".to_string(),
            ));
        }
        Ok(())
    }

    /// Return an exact-replay projection.
    #[must_use]
    pub fn replayed_projection(&self) -> Self {
        Self {
            mount: self.mount.clone(),
            receipt: self.receipt.replayed_projection(),
        }
    }
}

/// Typed payload stored in the shared environment idempotency namespace.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(tag = "result_kind", rename_all = "snake_case")]
pub enum EnvironmentMutationResult {
    /// Attachment attach/detach result.
    Attachment(EnvironmentAttachmentMutationResult),
    /// Resource mount/unmount result.
    Mount(EnvironmentMountMutationResult),
}

impl starweaver_core::VersionedRecord for EnvironmentMutationResult {
    const SCHEMA: &'static str = "starweaver.session.environment_mutation_result";
}

/// Stable keyset position for attachment queries.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EnvironmentAttachmentPageKey {
    /// Last seen update time.
    pub updated_at: DateTime<Utc>,
    /// Last seen stable identity.
    pub attachment_id: String,
}

/// Bounded authority-scoped attachment query.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EnvironmentAttachmentQuery {
    /// Trusted authority binding.
    pub authority_binding: String,
    /// Optional exact scope filter.
    pub scope: Option<DurableEnvironmentScope>,
    /// Trusted viewer connection identity used to hide foreign connection scopes.
    pub connection_id: Option<String>,
    /// Requested page size, from one through [`MAX_ENVIRONMENT_PAGE_SIZE`].
    pub limit: u32,
    /// Exclusive descending keyset cursor.
    pub after: Option<EnvironmentAttachmentPageKey>,
}

impl EnvironmentAttachmentQuery {
    /// Validate query bounds and identities.
    ///
    /// # Errors
    ///
    /// Returns an error when authority, scope, cursor, target, or page bounds are invalid.
    pub fn validate(&self) -> SessionStoreResult<()> {
        require_non_empty("environment authority binding", &self.authority_binding)?;
        require_page_limit(self.limit)?;
        if let Some(scope) = &self.scope {
            scope.validate()?;
            if let DurableEnvironmentScope::Connection { connection_id } = scope
                && self.connection_id.as_deref() != Some(connection_id)
            {
                return Err(SessionStoreError::NotFound(
                    "environment connection scope".to_string(),
                ));
            }
        }
        if self.connection_id.as_ref().is_some_and(String::is_empty) {
            return Err(SessionStoreError::Failed(
                "environment viewer connection identity cannot be empty".to_string(),
            ));
        }
        if let Some(after) = &self.after {
            require_non_empty("environment attachment page key", &after.attachment_id)?;
        }
        Ok(())
    }
}

/// Stable bounded attachment page.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EnvironmentAttachmentPage {
    /// Items ordered by `(updated_at DESC, attachment_id DESC)`.
    pub items: Vec<DurableEnvironmentAttachment>,
    /// Exclusive key for the next page.
    pub next: Option<EnvironmentAttachmentPageKeyProjection>,
}

/// Serializable form of an attachment page key.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EnvironmentAttachmentPageKeyProjection {
    /// Last seen update time.
    pub updated_at: DateTime<Utc>,
    /// Last seen stable identity.
    pub attachment_id: String,
}

impl From<EnvironmentAttachmentPageKeyProjection> for EnvironmentAttachmentPageKey {
    fn from(value: EnvironmentAttachmentPageKeyProjection) -> Self {
        Self {
            updated_at: value.updated_at,
            attachment_id: value.attachment_id,
        }
    }
}

/// Bounded stable active-mount query for one run.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EnvironmentMountQuery {
    /// Trusted authority binding.
    pub authority_binding: String,
    /// Owning session.
    pub session_id: String,
    /// Owning run.
    pub run_id: String,
    /// Requested maximum, from one through [`MAX_ENVIRONMENT_PAGE_SIZE`].
    pub limit: u32,
}

impl EnvironmentMountQuery {
    /// Validate query bounds and identities.
    ///
    /// # Errors
    ///
    /// Returns an error when authority, scope, cursor, target, or page bounds are invalid.
    pub fn validate(&self) -> SessionStoreResult<()> {
        require_non_empty("environment authority binding", &self.authority_binding)?;
        require_non_empty("environment mount session", &self.session_id)?;
        require_non_empty("environment mount run", &self.run_id)?;
        require_page_limit(self.limit)
    }
}

fn require_page_limit(limit: u32) -> SessionStoreResult<()> {
    if !(1..=MAX_ENVIRONMENT_PAGE_SIZE).contains(&limit) {
        return Err(SessionStoreError::Failed(format!(
            "environment page limit must be between 1 and {MAX_ENVIRONMENT_PAGE_SIZE}"
        )));
    }
    Ok(())
}

fn require_revision(revision: u64, label: &str) -> SessionStoreResult<()> {
    if revision == 0 {
        return Err(SessionStoreError::Failed(format!(
            "{label} revision must be greater than zero"
        )));
    }
    Ok(())
}

fn require_non_empty(label: &str, value: &str) -> SessionStoreResult<()> {
    if value.is_empty() {
        return Err(SessionStoreError::Failed(format!(
            "{label} cannot be empty"
        )));
    }
    Ok(())
}