kcode-k1-rust-code-ktool-service 0.1.5

Coordinate authenticated persistent K1 Rust code operations
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
use kcode_k1_access_types::{AccessContext, Authority, GroupId, TxId as AccessTxId, UserId};
use kcode_k1_groups::K1Groups;
use kcode_k1_objects::K1Objects;
use kcode_k1_rust_code_document::{RustCodeDocument, RustCodeDocumentError};
use kcode_k1_rust_coding::{
    K1RustCoding, RustCodingConfig, RustCodingError, RustPublishAuthorization, VerifiedCheck,
};
use kcode_k1_rust_package::{LibraryFamily, SourcePackage};
use kcode_k1_rust_projection::{K1RustProjection, PublishOutcome};
use kcode_k1_rust_transaction::{RustSourceTransaction, TransactionError, encode};
use kcode_k1_rust_worktree::{OpenedSource, SourceSelector, publication_source};
use kcode_k1_transaction_id::TxId as RustTxId;
use semver::Version;
use std::collections::HashMap;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};

const OBJECT_FILENAME: &str = "rust-publication.k1rust";
const OBJECT_MEDIA_TYPE: &str = "application/vnd.kennedy.k1-rust-source-transaction.v1";

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OpenedDocument {
    selector: SourceSelector,
    revision: Option<RustTxId>,
    document: RustCodeDocument,
}

impl OpenedDocument {
    pub fn selector(&self) -> &SourceSelector {
        &self.selector
    }

    pub const fn revision(&self) -> Option<RustTxId> {
        self.revision
    }

    pub fn document(&self) -> &RustCodeDocument {
        &self.document
    }

    fn from_source(source: OpenedSource) -> Result<Self, RustCodeKtoolServiceError> {
        let selector = source.selector().clone();
        let revision = source.revision();
        let document = RustCodeDocument::from_source_package(source.into_source())
            .map_err(RustCodeKtoolServiceError::Unsupported)?;
        Ok(Self {
            selector,
            revision,
            document,
        })
    }
}

#[derive(Debug)]
pub enum RustCodeKtoolServiceError {
    State(String),
    Authorization(String),
    AccessDenied,
    LibraryExists,
    LibraryAbsent,
    NoPublishedVersion,
    WorktreeChanged,
    VersionUsed,
    Unsupported(RustCodeDocumentError),
    CodingInitialization(String),
    Coding(RustCodingError),
    SourceEncoding(TransactionError),
    Object(String),
    CheckMismatch,
    Projection(String),
}

impl Display for RustCodeKtoolServiceError {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(self, formatter)
    }
}

impl Error for RustCodeKtoolServiceError {}

pub struct ServiceCheck {
    user: UserId,
    package: SourcePackage,
    verified: VerifiedCheck,
}

#[derive(Default)]
struct UserCoding {
    value: OnceLock<Result<Arc<Mutex<K1RustCoding>>, String>>,
}

pub struct RustCodeKtoolService {
    cache_root: PathBuf,
    config: RustCodingConfig,
    projection: Arc<K1RustProjection>,
    objects: Arc<K1Objects>,
    groups: Arc<K1Groups>,
    users: Mutex<HashMap<UserId, Arc<UserCoding>>>,
}

impl RustCodeKtoolService {
    pub fn new(
        cache_root: impl AsRef<Path>,
        config: RustCodingConfig,
        projection: Arc<K1RustProjection>,
        objects: Arc<K1Objects>,
        groups: Arc<K1Groups>,
    ) -> Self {
        Self {
            cache_root: cache_root.as_ref().to_path_buf(),
            config,
            projection,
            objects,
            groups,
            users: Mutex::new(HashMap::new()),
        }
    }

    pub fn create(
        &self,
        authenticated: &AccessContext,
        family: LibraryFamily,
    ) -> Result<OpenedDocument, RustCodeKtoolServiceError> {
        self.authorize(authenticated, &family)?;
        if self
            .projection
            .family_exists(&family)
            .map_err(RustCodeKtoolServiceError::Projection)?
        {
            return Err(RustCodeKtoolServiceError::LibraryExists);
        }
        let identity = kcode_k1_rust_package::LibraryId::new(family, Version::new(0, 0, 0))
            .map_err(|error| RustCodeKtoolServiceError::State(error.to_string()))?;
        let document = RustCodeDocument::approved_default(identity)
            .map_err(RustCodeKtoolServiceError::Unsupported)?;
        let worktree = self
            .projection
            .create(&document.clone().into_source_package())
            .map_err(map_write_error)?;
        OpenedDocument::from_source(OpenedSource::unpublished(&worktree))
    }

    pub fn open(
        &self,
        authenticated: &AccessContext,
        family: &LibraryFamily,
        selector: Option<&SourceSelector>,
    ) -> Result<OpenedDocument, RustCodeKtoolServiceError> {
        let source = self
            .projection
            .open_source(family, selector)
            .map_err(RustCodeKtoolServiceError::Projection)?
            .ok_or(RustCodeKtoolServiceError::LibraryAbsent)?;
        if matches!(source.selector(), SourceSelector::Unpublished(_)) {
            self.authorize(authenticated, family)?;
        }
        OpenedDocument::from_source(source)
    }

    pub fn latest_published(
        &self,
        family: &LibraryFamily,
    ) -> Result<OpenedDocument, RustCodeKtoolServiceError> {
        if let Some(source) = self
            .projection
            .latest_published(family)
            .map_err(RustCodeKtoolServiceError::Projection)?
        {
            return OpenedDocument::from_source(source);
        }
        if self
            .projection
            .family_exists(family)
            .map_err(RustCodeKtoolServiceError::Projection)?
        {
            Err(RustCodeKtoolServiceError::NoPublishedVersion)
        } else {
            Err(RustCodeKtoolServiceError::LibraryAbsent)
        }
    }

    pub fn overwrite(
        &self,
        authenticated: &AccessContext,
        opened: &OpenedDocument,
        next: RustCodeDocument,
    ) -> Result<OpenedDocument, RustCodeKtoolServiceError> {
        let family = opened.document.identity().family();
        self.authorize(authenticated, family)?;
        if next.identity() != opened.document.identity() {
            return Err(RustCodeKtoolServiceError::WorktreeChanged);
        }
        let source = next.into_source_package();
        let worktree = match opened.selector() {
            SourceSelector::Published(version) => {
                if opened.revision().is_some() {
                    return Err(RustCodeKtoolServiceError::WorktreeChanged);
                }
                let published = self
                    .projection
                    .open_source(family, Some(&SourceSelector::Published(version.clone())))
                    .map_err(RustCodeKtoolServiceError::Projection)?
                    .ok_or(RustCodeKtoolServiceError::WorktreeChanged)?;
                if published.source() != &opened.document.clone().into_source_package() {
                    return Err(RustCodeKtoolServiceError::WorktreeChanged);
                }
                self.projection.fork(&source).map_err(map_write_error)?
            }
            SourceSelector::Unpublished(id) => {
                let expected_revision = opened
                    .revision()
                    .ok_or(RustCodeKtoolServiceError::WorktreeChanged)?;
                let current = self
                    .projection
                    .load_unpublished(family, *id)
                    .map_err(RustCodeKtoolServiceError::Projection)?
                    .ok_or(RustCodeKtoolServiceError::WorktreeChanged)?;
                if current.revision() != expected_revision
                    || current.source() != &opened.document.clone().into_source_package()
                {
                    return Err(RustCodeKtoolServiceError::WorktreeChanged);
                }
                self.projection
                    .overwrite(*id, expected_revision, &source)
                    .map_err(map_write_error)?
            }
        };
        OpenedDocument::from_source(OpenedSource::unpublished(&worktree))
    }

    pub fn versioned(
        &self,
        document: &RustCodeDocument,
        version: Version,
    ) -> Result<RustCodeDocument, RustCodeKtoolServiceError> {
        let package = publication_source(&document.clone().into_source_package(), version)
            .map_err(|error| RustCodeKtoolServiceError::State(error.to_string()))?;
        RustCodeDocument::from_source_package(package)
            .map_err(RustCodeKtoolServiceError::Unsupported)
    }

    pub fn check_fresh(
        &self,
        authenticated: &AccessContext,
        document: &RustCodeDocument,
    ) -> Result<ServiceCheck, RustCodeKtoolServiceError> {
        let package = document.clone().into_source_package();
        let coding = self.coding(authenticated)?;
        let mut coding = coding
            .lock()
            .map_err(|_| RustCodeKtoolServiceError::State("user coding mutex poisoned".into()))?;
        let verified = coding
            .check_fresh(&package)
            .map_err(RustCodeKtoolServiceError::Coding)?;
        Ok(ServiceCheck {
            user: authenticated.user(),
            package,
            verified,
        })
    }

    pub fn current_check(
        &self,
        authenticated: &AccessContext,
        document: &RustCodeDocument,
    ) -> Result<Option<ServiceCheck>, RustCodeKtoolServiceError> {
        let package = document.clone().into_source_package();
        let coding = self.coding(authenticated)?;
        let mut coding = coding
            .lock()
            .map_err(|_| RustCodeKtoolServiceError::State("user coding mutex poisoned".into()))?;
        let verified = coding
            .current_check(&package)
            .map_err(RustCodeKtoolServiceError::Coding)?;
        Ok(verified.map(|verified| ServiceCheck {
            user: authenticated.user(),
            package,
            verified,
        }))
    }

    pub fn publish_checked(
        &self,
        authenticated: &AccessContext,
        document: &RustCodeDocument,
        checked: ServiceCheck,
    ) -> Result<PublishOutcome, RustCodeKtoolServiceError> {
        let package = document.clone().into_source_package();
        if checked.user != authenticated.user() || checked.package != package {
            return Err(RustCodeKtoolServiceError::CheckMismatch);
        }
        if self
            .projection
            .version_exists(package.id())
            .map_err(RustCodeKtoolServiceError::Projection)?
        {
            return Err(RustCodeKtoolServiceError::VersionUsed);
        }
        let coding = self.coding(authenticated)?;
        let mut coding = coding
            .lock()
            .map_err(|_| RustCodeKtoolServiceError::State("user coding mutex poisoned".into()))?;
        let bytes = encode(&RustSourceTransaction::Publish(package.clone()))
            .map_err(RustCodeKtoolServiceError::SourceEncoding)?;
        self.objects
            .save(OBJECT_FILENAME, OBJECT_MEDIA_TYPE, "", &bytes)
            .map_err(RustCodeKtoolServiceError::Object)?;
        let gate = AuthorityGate {
            authenticated,
            groups: &self.groups,
        };
        coding
            .publish_checked(&package, &checked.verified, &gate)
            .map_err(RustCodeKtoolServiceError::Coding)
    }

    fn authorize(
        &self,
        authenticated: &AccessContext,
        family: &LibraryFamily,
    ) -> Result<(), RustCodeKtoolServiceError> {
        match (AuthorityGate {
            authenticated,
            groups: &self.groups,
        })
        .authorize_publish(family)
        {
            Ok(true) => Ok(()),
            Ok(false) => Err(RustCodeKtoolServiceError::AccessDenied),
            Err(error) => Err(RustCodeKtoolServiceError::Authorization(error)),
        }
    }

    fn coding(
        &self,
        authenticated: &AccessContext,
    ) -> Result<Arc<Mutex<K1RustCoding>>, RustCodeKtoolServiceError> {
        let slot = {
            let mut users = self.users.lock().map_err(|_| {
                RustCodeKtoolServiceError::State("user coding map mutex poisoned".into())
            })?;
            users
                .entry(authenticated.user())
                .or_insert_with(|| Arc::new(UserCoding::default()))
                .clone()
        };
        match slot.value.get_or_init(|| {
            let user = RustTxId::from_bytes(*authenticated.user().as_tx_id().as_bytes());
            K1RustCoding::open(
                &self.cache_root,
                user,
                self.config.clone(),
                self.projection.clone(),
            )
            .map(|coding| Arc::new(Mutex::new(coding)))
            .map_err(|error| error.to_string())
        }) {
            Ok(coding) => Ok(coding.clone()),
            Err(error) => Err(RustCodeKtoolServiceError::CodingInitialization(
                error.clone(),
            )),
        }
    }
}

fn map_write_error(error: String) -> RustCodeKtoolServiceError {
    if error == "unpublished version changed" || error == "source transaction was rejected" {
        RustCodeKtoolServiceError::WorktreeChanged
    } else if error == "library family already exists" {
        RustCodeKtoolServiceError::LibraryExists
    } else {
        RustCodeKtoolServiceError::Projection(error)
    }
}

struct AuthorityGate<'a> {
    authenticated: &'a AccessContext,
    groups: &'a K1Groups,
}

impl RustPublishAuthorization for AuthorityGate<'_> {
    fn authorize_publish(&self, family: &LibraryFamily) -> Result<bool, String> {
        let authority = family.authority();
        let authority = authority.transaction_id();
        if authority.as_bytes() == self.authenticated.user().as_tx_id().as_bytes() {
            return Ok(true);
        }
        let group = GroupId::new(AccessTxId::from_bytes(*authority.as_bytes()));
        if group.sentinel().is_some()
            || self
                .authenticated
                .filter()
                .contains(Authority::Group(group))
        {
            return Ok(false);
        }
        let memberships = self
            .groups
            .memberships(self.authenticated.user(), self.authenticated.model())?;
        Ok(memberships.shared_groups().contains(&group))
    }
}