everruns-host 0.21.0

Shared host orchestration for Everruns execution adapters
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
//! Host-owned workspace, head, and session-environment contracts.
//!
//! A workspace is logical lineage. A head is one reopenable mutable view of
//! that lineage. Physical storage stays provider-owned and is projected into
//! execution through the existing [`SessionFileSystem`] contract.

use std::any::{Any, TypeId};
use std::collections::{BTreeMap, HashMap};
use std::fmt;
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use everruns_core::session_files::SessionFileSystem;
use everruns_provider::typed_id::{SessionId, WorkspaceId};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;

/// Stable, provider-defined SPI identifier.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct WorkspaceProviderId(String);

impl WorkspaceProviderId {
    pub fn new(value: impl Into<String>) -> Result<Self, WorkspaceError> {
        let value = value.into();
        if value.trim().is_empty() || value.len() > 128 {
            return Err(WorkspaceError::InvalidRequest(
                "workspace provider id must contain 1..=128 characters".into(),
            ));
        }
        Ok(Self(value))
    }

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

impl fmt::Display for WorkspaceProviderId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

/// Stable identity of one mutable workspace head.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct WorkspaceHeadId(Uuid);

impl WorkspaceHeadId {
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }

    pub const fn from_uuid(value: Uuid) -> Self {
        Self(value)
    }

    pub const fn uuid(self) -> Uuid {
        self.0
    }
}

impl Default for WorkspaceHeadId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for WorkspaceHeadId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, formatter)
    }
}

/// Provider-owned data sufficient to reopen the exact recorded head.
///
/// Callers persist this value opaquely. Providers must not place credentials
/// in `payload`; local persistence intentionally stores it as plain data.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkspaceBinding {
    pub provider_id: WorkspaceProviderId,
    pub workspace_id: WorkspaceId,
    pub head_id: WorkspaceHeadId,
    pub access: WorkspaceHeadAccess,
    #[serde(default)]
    pub payload: Vec<u8>,
}

impl WorkspaceBinding {
    /// Maximum provider payload accepted by Framework persistence.
    pub const MAX_PAYLOAD_BYTES: usize = 64 * 1024;

    pub fn validate(&self) -> Result<(), WorkspaceError> {
        if self.payload.len() > Self::MAX_PAYLOAD_BYTES {
            return Err(WorkspaceError::InvalidRequest(
                "workspace binding payload is too large".into(),
            ));
        }
        Ok(())
    }
}

/// Whether a head is intended for one session or intentionally shared.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkspaceHeadAccess {
    #[default]
    Isolated,
    Shared,
}

/// Provider-neutral identity and base metadata for a logical workspace.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WorkspaceDescriptor {
    pub id: WorkspaceId,
    pub name: String,
    pub metadata: BTreeMap<String, String>,
}

/// Provider-neutral identity and base metadata for a head.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WorkspaceHeadDescriptor {
    pub id: WorkspaceHeadId,
    pub name: String,
    pub base: Option<String>,
    pub access: WorkspaceHeadAccess,
    pub metadata: BTreeMap<String, String>,
}

/// A provider-produced head resource before the Framework attaches lifecycle.
pub struct WorkspaceHeadResource {
    pub workspace: WorkspaceDescriptor,
    pub head: WorkspaceHeadDescriptor,
    pub binding: WorkspaceBinding,
    pub file_system: Arc<dyn SessionFileSystem>,
}

/// Request to create or fork one head.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WorkspaceHeadRequest {
    pub name: String,
    pub base: Option<String>,
    pub access: WorkspaceHeadAccess,
}

/// Provider-neutral checkpoint metadata.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WorkspaceCheckpoint {
    pub revision: String,
    pub metadata: BTreeMap<String, String>,
}

/// Provider-neutral mutable-head status.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct WorkspaceHeadStatus {
    pub dirty: bool,
    pub conflicted: bool,
    pub archived: bool,
    pub metadata: BTreeMap<String, String>,
}

/// Provider-neutral diff summary for one mutable head.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct WorkspaceDiff {
    pub changed: bool,
    pub conflicted: bool,
    pub metadata: BTreeMap<String, String>,
}

/// Errors exposed by workspace providers and lifecycle operations.
#[derive(Clone, Debug, Error, PartialEq, Eq)]
#[non_exhaustive]
pub enum WorkspaceError {
    #[error("invalid workspace request: {0}")]
    InvalidRequest(String),
    #[error("workspace provider is unavailable: {0}")]
    ProviderUnavailable(String),
    #[error("workspace or head was not found")]
    NotFound,
    #[error("workspace head is archived")]
    Archived,
    #[error("workspace head has a conflicting update")]
    Conflict,
    #[error("workspace binding does not match the requested provider, workspace, or head")]
    BindingMismatch,
    #[error("workspace provider failed: {0}")]
    Provider(String),
}

/// Open service-provider interface for physical workspace implementations.
///
/// Git worktrees are one implementation. Remote filesystems, containers, and
/// object-backed snapshots can implement the same trait without registering a
/// backend enum or exposing physical paths in the universal contract.
#[async_trait]
pub trait WorkspaceProvider: Send + Sync {
    fn id(&self) -> WorkspaceProviderId;

    async fn open_workspace(&self, locator: &str) -> Result<WorkspaceDescriptor, WorkspaceError>;

    /// Reopen a logical workspace using only its recorded opaque binding.
    async fn open_workspace_from_binding(
        &self,
        binding: &WorkspaceBinding,
    ) -> Result<WorkspaceDescriptor, WorkspaceError>;

    async fn create_head(
        &self,
        workspace: &WorkspaceDescriptor,
        request: WorkspaceHeadRequest,
    ) -> Result<WorkspaceHeadResource, WorkspaceError>;

    async fn reopen_head(
        &self,
        binding: &WorkspaceBinding,
    ) -> Result<WorkspaceHeadResource, WorkspaceError>;

    async fn checkpoint(
        &self,
        binding: &WorkspaceBinding,
    ) -> Result<WorkspaceCheckpoint, WorkspaceError>;

    async fn status(
        &self,
        binding: &WorkspaceBinding,
    ) -> Result<WorkspaceHeadStatus, WorkspaceError>;

    async fn diff(&self, binding: &WorkspaceBinding) -> Result<WorkspaceDiff, WorkspaceError>;

    /// Archive a head while retaining its provider-owned contents.
    async fn archive(&self, binding: &WorkspaceBinding) -> Result<(), WorkspaceError>;

    /// Explicitly destroy provider-owned head storage.
    ///
    /// Providers must never call this from `Drop`. Provider-specific durable
    /// lineage such as a Git branch is retained unless the provider documents
    /// a separate, explicit deletion operation.
    async fn destroy(&self, binding: &WorkspaceBinding) -> Result<(), WorkspaceError>;
}

/// One logical workspace opened through a provider.
#[derive(Clone)]
pub struct Workspace {
    provider: Arc<dyn WorkspaceProvider>,
    descriptor: WorkspaceDescriptor,
}

impl Workspace {
    pub fn from_descriptor(
        provider: Arc<dyn WorkspaceProvider>,
        descriptor: WorkspaceDescriptor,
    ) -> Self {
        Self {
            provider,
            descriptor,
        }
    }

    pub async fn open(
        provider: Arc<dyn WorkspaceProvider>,
        locator: impl AsRef<str>,
    ) -> Result<Self, WorkspaceError> {
        let descriptor = provider.open_workspace(locator.as_ref()).await?;
        Ok(Self {
            provider,
            descriptor,
        })
    }

    pub fn id(&self) -> WorkspaceId {
        self.descriptor.id
    }

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

    pub fn metadata(&self) -> &BTreeMap<String, String> {
        &self.descriptor.metadata
    }

    pub fn head(&self, name: impl Into<String>) -> WorkspaceHeadBuilder {
        WorkspaceHeadBuilder {
            workspace: self.clone(),
            name: name.into(),
            base: None,
            access: WorkspaceHeadAccess::Isolated,
        }
    }

    pub async fn reopen(
        &self,
        binding: &WorkspaceBinding,
    ) -> Result<WorkspaceHead, WorkspaceError> {
        if binding.provider_id != self.provider.id() || binding.workspace_id != self.id() {
            return Err(WorkspaceError::BindingMismatch);
        }
        let resource = self.provider.reopen_head(binding).await?;
        self.attach(resource, Some(binding))
    }

    fn attach(
        &self,
        resource: WorkspaceHeadResource,
        expected: Option<&WorkspaceBinding>,
    ) -> Result<WorkspaceHead, WorkspaceError> {
        resource.binding.validate()?;
        if resource.workspace.id != self.id()
            || resource.binding.provider_id != self.provider.id()
            || resource.binding.workspace_id != self.id()
            || resource.binding.head_id != resource.head.id
            || resource.binding.access != resource.head.access
            || expected.is_some_and(|expected| expected != &resource.binding)
        {
            return Err(WorkspaceError::BindingMismatch);
        }
        Ok(WorkspaceHead {
            provider: self.provider.clone(),
            workspace: resource.workspace,
            descriptor: resource.head,
            binding: resource.binding,
            file_system: resource.file_system,
        })
    }
}

impl fmt::Debug for Workspace {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Workspace")
            .field("provider", &self.provider.id())
            .field("descriptor", &self.descriptor)
            .finish()
    }
}

/// Builder for explicit isolated or shared heads.
pub struct WorkspaceHeadBuilder {
    workspace: Workspace,
    name: String,
    base: Option<String>,
    access: WorkspaceHeadAccess,
}

impl WorkspaceHeadBuilder {
    pub fn from_revision(mut self, revision: impl Into<String>) -> Self {
        self.base = Some(revision.into());
        self
    }

    /// Opt into concurrent sessions addressing the same mutable head.
    pub fn shared(mut self) -> Self {
        self.access = WorkspaceHeadAccess::Shared;
        self
    }

    pub async fn create(self) -> Result<WorkspaceHead, WorkspaceError> {
        if self.name.trim().is_empty() || self.name.len() > 256 {
            return Err(WorkspaceError::InvalidRequest(
                "workspace head name must contain 1..=256 characters".into(),
            ));
        }
        let resource = self
            .workspace
            .provider
            .create_head(
                &self.workspace.descriptor,
                WorkspaceHeadRequest {
                    name: self.name,
                    base: self.base,
                    access: self.access,
                },
            )
            .await?;
        self.workspace.attach(resource, None)
    }
}

/// One stable, reopenable mutable view of a workspace.
#[derive(Clone)]
pub struct WorkspaceHead {
    provider: Arc<dyn WorkspaceProvider>,
    workspace: WorkspaceDescriptor,
    descriptor: WorkspaceHeadDescriptor,
    binding: WorkspaceBinding,
    file_system: Arc<dyn SessionFileSystem>,
}

impl WorkspaceHead {
    /// Provider that owns this head. Applications normally use lifecycle
    /// methods on the head; the facade uses this handle to make typed resume
    /// available for the Agent lifetime.
    pub fn provider(&self) -> Arc<dyn WorkspaceProvider> {
        self.provider.clone()
    }

    pub fn workspace_id(&self) -> WorkspaceId {
        self.workspace.id
    }

    pub fn id(&self) -> WorkspaceHeadId {
        self.descriptor.id
    }

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

    pub fn base(&self) -> Option<&str> {
        self.descriptor.base.as_deref()
    }

    pub fn access(&self) -> WorkspaceHeadAccess {
        self.descriptor.access
    }

    pub fn binding(&self) -> &WorkspaceBinding {
        &self.binding
    }

    pub fn file_system(&self) -> Arc<dyn SessionFileSystem> {
        self.file_system.clone()
    }

    pub async fn checkpoint(&self) -> Result<WorkspaceCheckpoint, WorkspaceError> {
        self.provider.checkpoint(&self.binding).await
    }

    pub async fn status(&self) -> Result<WorkspaceHeadStatus, WorkspaceError> {
        self.provider.status(&self.binding).await
    }

    pub async fn diff(&self) -> Result<WorkspaceDiff, WorkspaceError> {
        self.provider.diff(&self.binding).await
    }

    pub async fn archive(&self) -> Result<(), WorkspaceError> {
        self.provider.archive(&self.binding).await
    }

    pub async fn destroy(self) -> Result<(), WorkspaceError> {
        self.provider.destroy(&self.binding).await
    }

    /// Create a new isolated head from this head's current checkpoint.
    pub async fn fork(&self, name: impl Into<String>) -> Result<WorkspaceHead, WorkspaceError> {
        let checkpoint = self.checkpoint().await?;
        Workspace {
            provider: self.provider.clone(),
            descriptor: self.workspace.clone(),
        }
        .head(name)
        .from_revision(checkpoint.revision)
        .create()
        .await
    }
}

impl fmt::Debug for WorkspaceHead {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("WorkspaceHead")
            .field("provider", &self.provider.id())
            .field("workspace", &self.workspace)
            .field("descriptor", &self.descriptor)
            .field("binding", &self.binding)
            .finish_non_exhaustive()
    }
}

/// Session execution resources. Workspace is first; future compute/network
/// resources attach through the open type-keyed extension seam.
#[derive(Clone)]
pub struct Environment {
    head: WorkspaceHead,
    extensions: Arc<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>,
}

impl Environment {
    /// Create an Environment containing one workspace head and no extensions.
    pub fn new(head: WorkspaceHead) -> Self {
        Self {
            head,
            extensions: Arc::new(HashMap::new()),
        }
    }

    pub fn builder() -> EnvironmentBuilder {
        EnvironmentBuilder::default()
    }

    pub fn workspace_head(&self) -> &WorkspaceHead {
        &self.head
    }

    pub fn extension<T: Any + Send + Sync>(&self) -> Option<Arc<T>> {
        self.extensions
            .get(&TypeId::of::<T>())
            .and_then(|value| value.clone().downcast().ok())
    }
}

impl fmt::Debug for Environment {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Environment")
            .field("head", &self.head)
            .field("extension_count", &self.extensions.len())
            .finish()
    }
}

#[derive(Default)]
pub struct EnvironmentBuilder {
    head: Option<WorkspaceHead>,
    extensions: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
}

impl EnvironmentBuilder {
    pub fn workspace(mut self, head: WorkspaceHead) -> Self {
        self.head = Some(head);
        self
    }

    pub fn extension<T: Any + Send + Sync>(mut self, value: Arc<T>) -> Self {
        self.extensions.insert(TypeId::of::<T>(), value);
        self
    }

    /// Attach a typed resource constructed from the selected head.
    ///
    /// Compute providers use this form when their process, container, or
    /// remote mount must address the exact same head as Framework file tools.
    /// Call [`workspace`](Self::workspace) first.
    pub fn workspace_extension<T: Any + Send + Sync>(
        mut self,
        create: impl FnOnce(&WorkspaceHead) -> Arc<T>,
    ) -> Result<Self, WorkspaceError> {
        let head = self.head.as_ref().ok_or_else(|| {
            WorkspaceError::InvalidRequest(
                "workspace must be selected before a workspace extension".into(),
            )
        })?;
        self.extensions.insert(TypeId::of::<T>(), create(head));
        Ok(self)
    }

    pub fn build(self) -> Result<Environment, WorkspaceError> {
        Ok(Environment {
            head: self.head.ok_or_else(|| {
                WorkspaceError::InvalidRequest("environment requires a workspace head".into())
            })?,
            extensions: Arc::new(self.extensions),
        })
    }
}

/// Durable compare-and-set store for a session's opaque environment binding.
#[async_trait]
pub trait EnvironmentBindingStore: Send + Sync {
    async fn load(
        &self,
        session_id: SessionId,
    ) -> Result<Option<WorkspaceBinding>, EnvironmentBindingError>;

    async fn bind(
        &self,
        session_id: SessionId,
        binding: &WorkspaceBinding,
    ) -> Result<(), EnvironmentBindingError>;
}

#[derive(Clone, Debug, Error, PartialEq, Eq)]
#[non_exhaustive]
pub enum EnvironmentBindingError {
    #[error("session is already bound to a different workspace head")]
    Conflict,
    #[error("environment binding store is unavailable")]
    Unavailable,
    #[error("persisted environment binding is corrupt")]
    Corrupt,
}

/// Process-local binding store used by the default embedded Agent lifecycle.
#[derive(Default)]
pub struct InMemoryEnvironmentBindingStore {
    bindings: Mutex<HashMap<SessionId, WorkspaceBinding>>,
}

#[async_trait]
impl EnvironmentBindingStore for InMemoryEnvironmentBindingStore {
    async fn load(
        &self,
        session_id: SessionId,
    ) -> Result<Option<WorkspaceBinding>, EnvironmentBindingError> {
        Ok(self
            .bindings
            .lock()
            .map_err(|_| EnvironmentBindingError::Unavailable)?
            .get(&session_id)
            .cloned())
    }

    async fn bind(
        &self,
        session_id: SessionId,
        binding: &WorkspaceBinding,
    ) -> Result<(), EnvironmentBindingError> {
        if binding.payload.len() > WorkspaceBinding::MAX_PAYLOAD_BYTES {
            return Err(EnvironmentBindingError::Corrupt);
        }
        let mut bindings = self
            .bindings
            .lock()
            .map_err(|_| EnvironmentBindingError::Unavailable)?;
        match bindings.get(&session_id) {
            Some(recorded) if recorded != binding => Err(EnvironmentBindingError::Conflict),
            Some(_) => Ok(()),
            None => {
                let incompatible_claim = bindings.iter().any(|(recorded_session, recorded)| {
                    recorded_session != &session_id
                        && recorded.provider_id == binding.provider_id
                        && recorded.workspace_id == binding.workspace_id
                        && recorded.head_id == binding.head_id
                        && (binding.access == WorkspaceHeadAccess::Isolated
                            || recorded.access == WorkspaceHeadAccess::Isolated)
                });
                if incompatible_claim {
                    return Err(EnvironmentBindingError::Conflict);
                }
                bindings.insert(session_id, binding.clone());
                Ok(())
            }
        }
    }
}