ito-core 0.1.33

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
//! Side-effect-free inspection of legacy coordination-worktree state.

use std::fs;
use std::io;
use std::path::{Path, PathBuf};

use ito_config::ito_dir::lexical_normalize;
use ito_config::types::{BackendApiConfig, CoordinationBranchConfig, CoordinationStorage};
use serde::Serialize;

use crate::errors::{CoreError, CoreResult};
use crate::git_remote::resolve_org_repo_from_config_or_remote;
use crate::repo_paths::coordination_worktree_path;

const GITIGNORE_MARKER: &str = "# Ito coordination worktree symlinks";

/// Ito state directories formerly projected through the coordination worktree.
///
/// This neutral definition stays available when coordination runtime code is
/// excluded from a build, so detection and recovery do not depend on it.
pub const MANAGED_STATE_DIRS: &[&str] = &["changes", "specs", "modules", "workflows", "audit"];

const MANAGED_GITIGNORE_ENTRIES: &[&str] = &[
    ".ito/changes",
    ".ito/specs",
    ".ito/modules",
    ".ito/workflows",
    ".ito/audit",
];

/// Canonical legacy `.gitignore` entries corresponding to [`MANAGED_STATE_DIRS`].
#[must_use]
pub const fn managed_gitignore_entries() -> &'static [&'static str] {
    MANAGED_GITIGNORE_ENTRIES
}

/// Resolve the legacy coordination `.ito` root from explicit or local Git evidence.
///
/// This performs no network access and remains separate from coordination
/// provisioning, synchronization, and symlink-runtime code.
#[must_use]
pub fn expected_coordination_ito_root(
    project_root: &Path,
    ito_root: &Path,
    coordination: &CoordinationBranchConfig,
    backend: &BackendApiConfig,
) -> Option<PathBuf> {
    if coordination
        .worktree_path
        .as_deref()
        .is_some_and(|path| !path.trim().is_empty())
    {
        return Some(coordination_worktree_path(coordination, ito_root, "", "").join(".ito"));
    }

    resolve_org_repo_from_config_or_remote(project_root, backend).map(|(org, repo)| {
        coordination_worktree_path(coordination, ito_root, &org, &repo).join(".ito")
    })
}

/// Overall classification of coordination storage evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum LegacyCoordinationClass {
    /// No managed Ito state paths exist yet.
    Absent,
    /// Managed Ito state is stored in real repository directories.
    Embedded,
    /// Legacy coordination storage is configured or visibly wired.
    Legacy,
    /// Evidence conflicts and requires human reconciliation.
    Ambiguous,
}

/// Resolved coordination configuration recorded by the detector.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CoordinationConfigEvidence {
    /// Whether coordination synchronization is enabled.
    pub enabled: bool,
    /// Configured storage identifier.
    pub storage: String,
    /// Configured coordination branch.
    pub branch: String,
    /// Configured worktree path override, if any.
    pub worktree_path: Option<String>,
}

/// Filesystem kind observed at one managed Ito path.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ManagedPathKind {
    /// The managed path is absent.
    Missing,
    /// The managed path is a real directory.
    Directory {
        /// Whether the directory contains no entries.
        empty: bool,
    },
    /// The managed path is a symbolic link or directory junction.
    Link {
        /// Target stored in the link itself.
        target: PathBuf,
        /// Link target lexically resolved against the Ito root when relative.
        resolved_target: PathBuf,
        /// Whether the resolved target matches the expected coordination target.
        matches_expected: Option<bool>,
        /// Whether the target currently exists.
        target_exists: bool,
    },
    /// The path exists but is neither a directory nor a coordination link.
    Other,
}

/// Evidence for one managed Ito state path.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ManagedPathEvidence {
    /// Managed directory name beneath the Ito root.
    pub name: String,
    /// Full path inspected by the detector.
    pub path: PathBuf,
    /// Filesystem kind observed without following links.
    pub kind: ManagedPathKind,
}

/// Evidence found in the repository `.gitignore`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CoordinationGitignoreEvidence {
    /// Whether the managed coordination marker or its complete entry set exists.
    pub marker_present: bool,
    /// Canonical coordination entries found after trimming surrounding whitespace.
    pub matching_entries: Vec<String>,
}

/// Complete side-effect-free legacy coordination inspection report.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct LegacyCoordinationReport {
    /// Overall storage classification.
    pub classification: LegacyCoordinationClass,
    /// Resolved configuration evidence.
    pub config: CoordinationConfigEvidence,
    /// Per-path filesystem evidence.
    pub managed_paths: Vec<ManagedPathEvidence>,
    /// Managed `.gitignore` evidence.
    pub gitignore: CoordinationGitignoreEvidence,
}

/// Inspect coordination configuration and repository evidence without mutation.
///
/// `project_root` identifies the repository containing `.gitignore`, while
/// `ito_root` identifies its resolved Ito directory. `config` must be the
/// already-resolved coordination configuration. When
/// `expected_coordination_ito_root` is present, link targets are compared with
/// the expected shared `.ito` root; when it is absent, target matching remains
/// unknown and wrong-target detection is intentionally unavailable.
///
/// # Errors
///
/// Returns an error when filesystem metadata or repository files cannot be read.
pub fn inspect_legacy_coordination(
    project_root: &Path,
    ito_root: &Path,
    config: &CoordinationBranchConfig,
    expected_coordination_ito_root: Option<&Path>,
) -> CoreResult<LegacyCoordinationReport> {
    let managed_paths = MANAGED_STATE_DIRS
        .iter()
        .map(|name| inspect_managed_path(ito_root, name, expected_coordination_ito_root))
        .collect::<CoreResult<Vec<_>>>()?;
    let gitignore = inspect_gitignore(project_root)?;
    let classification = classify(config, &managed_paths, &gitignore);

    Ok(LegacyCoordinationReport {
        classification,
        config: CoordinationConfigEvidence {
            enabled: config.enabled.0,
            storage: config.storage.as_str().to_string(),
            branch: config.name.clone(),
            worktree_path: config.worktree_path.clone(),
        },
        managed_paths,
        gitignore,
    })
}

fn inspect_managed_path(
    ito_root: &Path,
    name: &str,
    expected_coordination_ito_root: Option<&Path>,
) -> CoreResult<ManagedPathEvidence> {
    let path = ito_root.join(name);
    let metadata = match fs::symlink_metadata(&path) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == io::ErrorKind::NotFound => {
            return Ok(ManagedPathEvidence {
                name: name.to_string(),
                path,
                kind: ManagedPathKind::Missing,
            });
        }
        Err(error) => {
            return Err(CoreError::io(
                format!(
                    "cannot inspect legacy coordination path '{}': check filesystem permissions",
                    path.display()
                ),
                error,
            ));
        }
    };

    let link_target = match read_dir_link(&path) {
        Ok(target) => Some(target),
        Err(error) if metadata.file_type().is_symlink() => {
            return Err(CoreError::io(
                format!(
                    "cannot read legacy coordination link '{}': check filesystem permissions",
                    path.display()
                ),
                error,
            ));
        }
        Err(_) => None,
    };

    let kind = if let Some(target) = link_target {
        let resolved_target = if target.is_absolute() {
            lexical_normalize(&target)
        } else {
            lexical_normalize(&ito_root.join(&target))
        };
        let matches_expected = expected_coordination_ito_root
            .map(|expected_root| resolved_target == lexical_normalize(&expected_root.join(name)));
        let target_exists = path_exists(&resolved_target)?;

        ManagedPathKind::Link {
            target,
            resolved_target,
            matches_expected,
            target_exists,
        }
    } else if metadata.is_dir() {
        let mut entries = fs::read_dir(&path).map_err(|error| {
            CoreError::io(
                format!(
                    "cannot read legacy coordination directory '{}': check filesystem permissions",
                    path.display()
                ),
                error,
            )
        })?;
        let first_entry = entries.next().transpose().map_err(|error| {
            CoreError::io(
                format!(
                    "cannot inspect entries in legacy coordination directory '{}': check filesystem permissions",
                    path.display()
                ),
                error,
            )
        })?;
        ManagedPathKind::Directory {
            empty: first_entry.is_none(),
        }
    } else {
        ManagedPathKind::Other
    };

    Ok(ManagedPathEvidence {
        name: name.to_string(),
        path,
        kind,
    })
}

fn path_exists(path: &Path) -> CoreResult<bool> {
    match fs::metadata(path) {
        Ok(_) => Ok(true),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(CoreError::io(
            format!(
                "cannot inspect legacy coordination link target '{}': check filesystem permissions",
                path.display()
            ),
            error,
        )),
    }
}

fn inspect_gitignore(project_root: &Path) -> CoreResult<CoordinationGitignoreEvidence> {
    let path = project_root.join(".gitignore");
    let contents = match fs::read_to_string(&path) {
        Ok(contents) => contents,
        Err(error) if error.kind() == io::ErrorKind::NotFound => String::new(),
        Err(error) => {
            return Err(CoreError::io(
                format!(
                    "cannot inspect legacy coordination marker in '{}': check filesystem permissions",
                    path.display()
                ),
                error,
            ));
        }
    };

    let lines = contents.lines().map(str::trim).collect::<Vec<_>>();
    let matching_entries = managed_gitignore_entries()
        .iter()
        .filter(|entry| lines.iter().any(|line| line == *entry))
        .map(|entry| (*entry).to_string())
        .collect::<Vec<_>>();
    let marker_present = lines.contains(&GITIGNORE_MARKER)
        || matching_entries.len() == managed_gitignore_entries().len();

    Ok(CoordinationGitignoreEvidence {
        marker_present,
        matching_entries,
    })
}

fn classify(
    config: &CoordinationBranchConfig,
    managed_paths: &[ManagedPathEvidence],
    gitignore: &CoordinationGitignoreEvidence,
) -> LegacyCoordinationClass {
    let configured_legacy = config.enabled.0 && config.storage == CoordinationStorage::Worktree;
    let configured_main = !config.enabled.0 || config.storage == CoordinationStorage::Embedded;
    let has_link = managed_paths
        .iter()
        .any(|evidence| matches!(evidence.kind, ManagedPathKind::Link { .. }));
    let has_wrong_link = managed_paths.iter().any(|evidence| {
        matches!(
            evidence.kind,
            ManagedPathKind::Link {
                matches_expected: Some(false),
                ..
            }
        )
    });
    let link_roots = managed_paths
        .iter()
        .filter_map(|evidence| match &evidence.kind {
            ManagedPathKind::Link {
                resolved_target, ..
            } => resolved_target.parent().map(lexical_normalize),
            _ => None,
        })
        .collect::<std::collections::BTreeSet<_>>();
    let has_inconsistent_link_roots = link_roots.len() > 1;
    let has_authority_link = managed_paths.iter().any(|evidence| {
        matches!(evidence.name.as_str(), "changes" | "specs")
            && matches!(evidence.kind, ManagedPathKind::Link { .. })
    });
    let has_non_empty_authority_directory = managed_paths.iter().any(|evidence| {
        matches!(evidence.name.as_str(), "changes" | "specs")
            && matches!(evidence.kind, ManagedPathKind::Directory { empty: false })
    });
    let has_non_empty_runtime_directory = managed_paths.iter().any(|evidence| {
        matches!(evidence.name.as_str(), "modules" | "workflows" | "audit")
            && matches!(evidence.kind, ManagedPathKind::Directory { empty: false })
    });
    let has_real_directory = managed_paths
        .iter()
        .any(|evidence| matches!(evidence.kind, ManagedPathKind::Directory { .. }));
    let has_other = managed_paths
        .iter()
        .any(|evidence| matches!(evidence.kind, ManagedPathKind::Other));
    let all_missing = managed_paths
        .iter()
        .all(|evidence| matches!(evidence.kind, ManagedPathKind::Missing));
    let has_gitignore_entries = !gitignore.matching_entries.is_empty();
    let partial_gitignore_marker = has_gitignore_entries && !gitignore.marker_present;
    let standalone_gitignore_marker =
        gitignore.marker_present && gitignore.matching_entries.is_empty();
    let worktree_config_with_materialized_paths =
        configured_legacy && !has_link && has_real_directory;

    let conflicting_evidence = has_wrong_link
        || has_inconsistent_link_roots
        || has_other
        || partial_gitignore_marker
        || standalone_gitignore_marker
        || worktree_config_with_materialized_paths
        || (has_authority_link && has_non_empty_authority_directory)
        || (has_link && has_non_empty_runtime_directory)
        || (configured_main && (has_link || has_gitignore_entries))
        || (gitignore.marker_present && !has_link && has_real_directory);

    if conflicting_evidence {
        LegacyCoordinationClass::Ambiguous
    } else if configured_legacy || has_link || has_gitignore_entries {
        LegacyCoordinationClass::Legacy
    } else if all_missing {
        LegacyCoordinationClass::Absent
    } else if has_real_directory {
        LegacyCoordinationClass::Embedded
    } else {
        LegacyCoordinationClass::Ambiguous
    }
}

fn read_dir_link(path: &Path) -> io::Result<PathBuf> {
    #[cfg(windows)]
    {
        junction::get_target(path)
    }

    #[cfg(not(windows))]
    {
        fs::read_link(path)
    }
}

#[cfg(test)]
#[path = "legacy_coordination_tests.rs"]
mod legacy_coordination_tests;