ito-core 0.1.29

Core functionality and business logic for Ito
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
//! Repository runtime selection and composition.
//!
//! Centralizes selection of repository implementations for the active
//! persistence mode, so adapters do not instantiate concrete repositories
//! directly.

use std::path::{Path, PathBuf};
use std::sync::Arc;

use ito_config::ito_dir::{absolutize_and_normalize, lexical_normalize};
use ito_config::types::{ItoConfig, RepositoryPersistenceMode};
use ito_config::{ConfigContext, load_cascading_project_config};

use crate::backend_change_repository::BackendChangeRepository;
use crate::backend_client::{BackendRuntime, resolve_backend_runtime};
use crate::backend_http::BackendHttpClient;
use crate::backend_module_repository::BackendModuleRepository;
use crate::backend_spec_repository::BackendSpecRepository;
use crate::change_repository::FsChangeRepository;
use crate::errors::{CoreError, CoreResult};
use crate::module_repository::FsModuleRepository;
use crate::remote_task_repository::RemoteTaskRepository;
use crate::spec_repository::FsSpecRepository;
use crate::sqlite_project_store::SqliteBackendProjectStore;
use crate::task_mutations::{FsTaskMutationService, boxed_fs_task_mutation_service};
use crate::task_repository::FsTaskRepository;
use ito_domain::changes::ChangeRepository;
use ito_domain::modules::ModuleRepository;
use ito_domain::specs::SpecRepository;
use ito_domain::tasks::{TaskMutationService, TaskRepository};

/// Client persistence mode used for repository selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PersistenceMode {
    /// Local filesystem-backed repositories.
    Filesystem,
    /// Local SQLite-backed repositories.
    Sqlite,
    /// Remote repositories backed by the backend API.
    Remote,
}

/// Bundle of domain repositories selected for the active persistence mode.
#[derive(Clone)]
pub struct RepositorySet {
    /// Change repository implementation.
    pub changes: Arc<dyn ChangeRepository + Send + Sync>,
    /// Module repository implementation.
    pub modules: Arc<dyn ModuleRepository + Send + Sync>,
    /// Task repository implementation.
    pub tasks: Arc<dyn TaskRepository + Send + Sync>,
    /// Task mutation service implementation.
    pub task_mutations: Arc<dyn TaskMutationService + Send + Sync>,
    /// Promoted spec repository implementation.
    pub specs: Arc<dyn SpecRepository + Send + Sync>,
}

/// Resolved SQLite runtime settings for local persistence.
#[derive(Debug, Clone)]
pub struct SqliteRuntime {
    /// Path to the SQLite database file.
    pub db_path: PathBuf,
    /// Organization namespace for the local project (currently derived as `local`).
    pub org: String,
    /// Repository namespace for the local project (derived from the project root directory name).
    pub repo: String,
}

/// Resolved repository runtime for the current configuration.
pub struct RepositoryRuntime {
    mode: PersistenceMode,
    ito_path: PathBuf,
    backend_runtime: Option<BackendRuntime>,
    sqlite_runtime: Option<SqliteRuntime>,
    repositories: RepositorySet,
}

impl RepositoryRuntime {
    /// Active persistence mode.
    pub fn mode(&self) -> PersistenceMode {
        self.mode
    }

    /// Root `.ito/` path for filesystem-backed helpers.
    pub fn ito_path(&self) -> &Path {
        self.ito_path.as_path()
    }

    /// Resolved backend runtime, if remote mode is active.
    pub fn backend_runtime(&self) -> Option<&BackendRuntime> {
        self.backend_runtime.as_ref()
    }

    /// Resolved SQLite runtime, if SQLite mode is active.
    pub fn sqlite_runtime(&self) -> Option<&SqliteRuntime> {
        self.sqlite_runtime.as_ref()
    }

    /// Selected repository bundle.
    pub fn repositories(&self) -> &RepositorySet {
        &self.repositories
    }
}

/// Factory interface for building remote repository bundles.
pub trait RemoteRepositoryFactory: Send + Sync {
    /// Build a repository bundle using the provided backend runtime.
    fn build(&self, runtime: &BackendRuntime) -> CoreResult<RepositorySet>;
}

/// Remote factory that uses HTTP-backed repositories.
pub struct HttpRemoteRepositoryFactory;

impl RemoteRepositoryFactory for HttpRemoteRepositoryFactory {
    fn build(&self, runtime: &BackendRuntime) -> CoreResult<RepositorySet> {
        let client = BackendHttpClient::new(runtime.clone());
        Ok(RepositorySet {
            changes: Arc::new(BackendChangeRepository::new(client.clone())),
            modules: Arc::new(BackendModuleRepository::new(client.clone())),
            tasks: Arc::new(RemoteTaskRepository::new(client.clone())),
            task_mutations: Arc::new(client.clone()),
            specs: Arc::new(BackendSpecRepository::new(client.clone())),
        })
    }
}

/// Builder for repository runtime selection.
pub struct RepositoryRuntimeBuilder {
    ito_path: PathBuf,
    mode: PersistenceMode,
    backend_runtime: Option<BackendRuntime>,
    sqlite_runtime: Option<SqliteRuntime>,
    remote_factory: Arc<dyn RemoteRepositoryFactory>,
}

impl RepositoryRuntimeBuilder {
    /// Create a builder targeting the provided `.ito/` path.
    pub fn new(ito_path: impl Into<PathBuf>) -> Self {
        Self {
            ito_path: ito_path.into(),
            mode: PersistenceMode::Filesystem,
            backend_runtime: None,
            sqlite_runtime: None,
            remote_factory: Arc::new(HttpRemoteRepositoryFactory),
        }
    }

    /// Set the persistence mode.
    pub fn mode(mut self, mode: PersistenceMode) -> Self {
        self.mode = mode;
        self
    }

    /// Set the backend runtime for remote mode.
    pub fn backend_runtime(mut self, runtime: BackendRuntime) -> Self {
        self.backend_runtime = Some(runtime);
        self
    }

    /// Set the SQLite runtime for SQLite mode.
    pub fn sqlite_runtime(mut self, runtime: SqliteRuntime) -> Self {
        self.sqlite_runtime = Some(runtime);
        self
    }

    /// Override the remote repository factory.
    pub fn remote_factory(mut self, factory: Arc<dyn RemoteRepositoryFactory>) -> Self {
        self.remote_factory = factory;
        self
    }

    /// Build the repository runtime.
    pub fn build(self) -> CoreResult<RepositoryRuntime> {
        match self.mode {
            PersistenceMode::Filesystem => {
                let repositories = filesystem_repository_set(&self.ito_path);
                Ok(RepositoryRuntime {
                    mode: PersistenceMode::Filesystem,
                    ito_path: self.ito_path,
                    backend_runtime: None,
                    sqlite_runtime: None,
                    repositories,
                })
            }
            PersistenceMode::Sqlite => {
                let runtime = self.sqlite_runtime.ok_or_else(|| {
                    CoreError::validation("sqlite mode requires sqlite runtime".to_string())
                })?;
                let repositories = sqlite_repository_set(&runtime)?;
                Ok(RepositoryRuntime {
                    mode: PersistenceMode::Sqlite,
                    ito_path: self.ito_path,
                    backend_runtime: None,
                    sqlite_runtime: Some(runtime),
                    repositories,
                })
            }
            PersistenceMode::Remote => {
                let runtime = self.backend_runtime.ok_or_else(|| {
                    CoreError::validation("remote mode requires backend runtime".to_string())
                })?;
                let repositories = self.remote_factory.build(&runtime)?;
                Ok(RepositoryRuntime {
                    mode: PersistenceMode::Remote,
                    ito_path: self.ito_path,
                    backend_runtime: Some(runtime),
                    sqlite_runtime: None,
                    repositories,
                })
            }
        }
    }
}

/// Resolve repository runtime for the current configuration.
pub fn resolve_repository_runtime(
    ito_path: &Path,
    ctx: &ConfigContext,
) -> CoreResult<RepositoryRuntime> {
    let project_root = ctx
        .project_dir
        .as_deref()
        .unwrap_or_else(|| ito_path.parent().unwrap_or(ito_path));
    let merged = load_cascading_project_config(project_root, ito_path, ctx).merged;
    let backend_enabled = merged
        .pointer("/backend/enabled")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);
    let raw_mode = merged
        .pointer("/repository/mode")
        .and_then(|v| v.as_str())
        .unwrap_or("filesystem");

    // Fail fast on unrecognized repository.mode values before attempting full
    // config deserialization. This prevents silent fallback to filesystem mode
    // when the user has set an invalid mode string.
    if RepositoryPersistenceMode::parse_value(raw_mode).is_none() {
        let valid = RepositoryPersistenceMode::ALL.join(", ");
        return Err(CoreError::validation(format!(
            "Invalid repository.mode '{raw_mode}': must be one of {valid}"
        )));
    }

    let sqlite_enabled = raw_mode == "sqlite";

    let config = match serde_json::from_value::<ItoConfig>(merged) {
        Ok(config) => config,
        Err(err) => {
            if backend_enabled || sqlite_enabled {
                let mode = if backend_enabled {
                    "backend mode is enabled"
                } else {
                    "sqlite persistence mode is enabled"
                };
                return Err(CoreError::validation(format!(
                    "Failed to parse Ito config while {mode}: {err}"
                )));
            }
            return RepositoryRuntimeBuilder::new(ito_path).build();
        }
    };

    if !config.backend.enabled {
        return match config.repository.mode {
            RepositoryPersistenceMode::Filesystem => {
                RepositoryRuntimeBuilder::new(ito_path).build()
            }
            RepositoryPersistenceMode::Sqlite => {
                let runtime = resolve_sqlite_runtime(&config, project_root)?;
                RepositoryRuntimeBuilder::new(ito_path)
                    .mode(PersistenceMode::Sqlite)
                    .sqlite_runtime(runtime)
                    .build()
            }
        };
    }

    let runtime = resolve_backend_runtime(&config.backend)?.ok_or_else(|| {
        CoreError::validation("Backend mode is enabled but runtime was not resolved".to_string())
    })?;

    RepositoryRuntimeBuilder::new(ito_path)
        .mode(PersistenceMode::Remote)
        .backend_runtime(runtime)
        .build()
}

fn resolve_sqlite_runtime(config: &ItoConfig, project_root: &Path) -> CoreResult<SqliteRuntime> {
    let Some(db_path) = config.repository.sqlite.db_path.as_deref() else {
        return Err(CoreError::validation(
            "SQLite persistence mode requires 'repository.sqlite.dbPath' to be set",
        ));
    };
    let db_path = db_path.trim();
    if db_path.is_empty() {
        return Err(CoreError::validation(
            "SQLite persistence mode requires 'repository.sqlite.dbPath' to be set",
        ));
    }

    let db_path = PathBuf::from(db_path);
    let db_path = if db_path.is_absolute() {
        db_path
    } else {
        project_root.join(db_path)
    };
    let db_path =
        absolutize_and_normalize(&db_path).unwrap_or_else(|_| lexical_normalize(&db_path));

    let repo = match project_root.file_name().and_then(|s| s.to_str()) {
        Some(name) if !name.trim().is_empty() => name.to_string(),
        _ => "project".to_string(),
    };

    Ok(SqliteRuntime {
        db_path,
        org: "local".to_string(),
        repo,
    })
}

fn filesystem_repository_set(ito_path: &Path) -> RepositorySet {
    let ito_path = ito_path.to_path_buf();
    RepositorySet {
        changes: Arc::new(OwnedFsChangeRepository::new(ito_path.clone())),
        modules: Arc::new(OwnedFsModuleRepository::new(ito_path.clone())),
        tasks: Arc::new(OwnedFsTaskRepository::new(ito_path.clone())),
        task_mutations: Arc::new(FsTaskMutationService::new(ito_path.clone())),
        specs: Arc::new(OwnedFsSpecRepository::new(ito_path)),
    }
}

fn sqlite_repository_set(runtime: &SqliteRuntime) -> CoreResult<RepositorySet> {
    let store = SqliteBackendProjectStore::open(&runtime.db_path)?;
    store.repository_set(&runtime.org, &runtime.repo)
}

pub(crate) fn boxed_fs_change_repository(ito_path: PathBuf) -> Box<dyn ChangeRepository + Send> {
    Box::new(OwnedFsChangeRepository::new(ito_path))
}

pub(crate) fn boxed_fs_module_repository(ito_path: PathBuf) -> Box<dyn ModuleRepository + Send> {
    Box::new(OwnedFsModuleRepository::new(ito_path))
}

pub(crate) fn boxed_fs_task_repository(ito_path: PathBuf) -> Box<dyn TaskRepository + Send> {
    Box::new(OwnedFsTaskRepository::new(ito_path))
}

pub(crate) fn boxed_fs_task_mutation_port(
    ito_path: PathBuf,
) -> Box<dyn TaskMutationService + Send> {
    boxed_fs_task_mutation_service(ito_path)
}

pub(crate) fn boxed_fs_spec_repository(ito_path: PathBuf) -> Box<dyn SpecRepository + Send> {
    Box::new(OwnedFsSpecRepository::new(ito_path))
}

// ── Owned-path filesystem wrappers ─────────────────────────────────

#[derive(Debug, Clone)]
struct OwnedFsChangeRepository {
    ito_path: PathBuf,
}

impl OwnedFsChangeRepository {
    fn new(ito_path: PathBuf) -> Self {
        Self { ito_path }
    }

    fn inner(&self) -> FsChangeRepository<'_> {
        FsChangeRepository::new(&self.ito_path)
    }
}

impl ChangeRepository for OwnedFsChangeRepository {
    fn resolve_target_with_options(
        &self,
        input: &str,
        options: ito_domain::changes::ResolveTargetOptions,
    ) -> ito_domain::changes::ChangeTargetResolution {
        self.inner().resolve_target_with_options(input, options)
    }

    fn suggest_targets(&self, input: &str, max: usize) -> Vec<String> {
        self.inner().suggest_targets(input, max)
    }

    fn exists(&self, id: &str) -> bool {
        self.inner().exists(id)
    }

    fn exists_with_filter(
        &self,
        id: &str,
        filter: ito_domain::changes::ChangeLifecycleFilter,
    ) -> bool {
        self.inner().exists_with_filter(id, filter)
    }

    fn get_with_filter(
        &self,
        id: &str,
        filter: ito_domain::changes::ChangeLifecycleFilter,
    ) -> ito_domain::errors::DomainResult<ito_domain::changes::Change> {
        self.inner().get_with_filter(id, filter)
    }

    fn list_with_filter(
        &self,
        filter: ito_domain::changes::ChangeLifecycleFilter,
    ) -> ito_domain::errors::DomainResult<Vec<ito_domain::changes::ChangeSummary>> {
        self.inner().list_with_filter(filter)
    }

    fn list_by_module_with_filter(
        &self,
        module_id: &str,
        filter: ito_domain::changes::ChangeLifecycleFilter,
    ) -> ito_domain::errors::DomainResult<Vec<ito_domain::changes::ChangeSummary>> {
        self.inner().list_by_module_with_filter(module_id, filter)
    }

    fn list_incomplete_with_filter(
        &self,
        filter: ito_domain::changes::ChangeLifecycleFilter,
    ) -> ito_domain::errors::DomainResult<Vec<ito_domain::changes::ChangeSummary>> {
        self.inner().list_incomplete_with_filter(filter)
    }

    fn list_complete_with_filter(
        &self,
        filter: ito_domain::changes::ChangeLifecycleFilter,
    ) -> ito_domain::errors::DomainResult<Vec<ito_domain::changes::ChangeSummary>> {
        self.inner().list_complete_with_filter(filter)
    }

    fn get_summary_with_filter(
        &self,
        id: &str,
        filter: ito_domain::changes::ChangeLifecycleFilter,
    ) -> ito_domain::errors::DomainResult<ito_domain::changes::ChangeSummary> {
        self.inner().get_summary_with_filter(id, filter)
    }
}

#[derive(Debug, Clone)]
struct OwnedFsModuleRepository {
    ito_path: PathBuf,
}

impl OwnedFsModuleRepository {
    fn new(ito_path: PathBuf) -> Self {
        Self { ito_path }
    }

    fn inner(&self) -> FsModuleRepository<'_> {
        FsModuleRepository::new(&self.ito_path)
    }
}

impl ModuleRepository for OwnedFsModuleRepository {
    fn exists(&self, id: &str) -> bool {
        self.inner().exists(id)
    }

    fn get(
        &self,
        id_or_name: &str,
    ) -> ito_domain::errors::DomainResult<ito_domain::modules::Module> {
        self.inner().get(id_or_name)
    }

    fn list(&self) -> ito_domain::errors::DomainResult<Vec<ito_domain::modules::ModuleSummary>> {
        self.inner().list()
    }

    fn list_sub_modules(
        &self,
        parent_id: &str,
    ) -> ito_domain::errors::DomainResult<Vec<ito_domain::modules::SubModuleSummary>> {
        self.inner().list_sub_modules(parent_id)
    }

    fn get_sub_module(
        &self,
        composite_id: &str,
    ) -> ito_domain::errors::DomainResult<ito_domain::modules::SubModule> {
        self.inner().get_sub_module(composite_id)
    }
}

#[derive(Debug, Clone)]
struct OwnedFsTaskRepository {
    ito_path: PathBuf,
}

impl OwnedFsTaskRepository {
    fn new(ito_path: PathBuf) -> Self {
        Self { ito_path }
    }

    fn inner(&self) -> FsTaskRepository<'_> {
        FsTaskRepository::new(&self.ito_path)
    }
}

impl TaskRepository for OwnedFsTaskRepository {
    fn load_tasks(
        &self,
        change_id: &str,
    ) -> ito_domain::errors::DomainResult<ito_domain::tasks::TasksParseResult> {
        self.inner().load_tasks(change_id)
    }
}

#[derive(Debug, Clone)]
struct OwnedFsSpecRepository {
    ito_path: PathBuf,
}

impl OwnedFsSpecRepository {
    fn new(ito_path: PathBuf) -> Self {
        Self { ito_path }
    }

    fn inner(&self) -> FsSpecRepository<'_> {
        FsSpecRepository::new(&self.ito_path)
    }
}

impl SpecRepository for OwnedFsSpecRepository {
    fn list(&self) -> ito_domain::errors::DomainResult<Vec<ito_domain::specs::SpecSummary>> {
        self.inner().list()
    }

    fn get(&self, id: &str) -> ito_domain::errors::DomainResult<ito_domain::specs::SpecDocument> {
        self.inner().get(id)
    }
}