tuff-core 0.5.0

Core lifecycle engine and adapter contract for Tuff.
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
use std::{
    collections::BTreeMap,
    ffi::OsStr,
    path::{Path, PathBuf},
};

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::error::{Result, TuffError};
use crate::manifest::{CapabilityType, ImplementationConfig, McpServerConfig, WorkflowConfig};

/// Current on-disk schema. Older readable versions are migrated in memory
/// by `read_lockfile_at`; writers always emit this version.
pub const LOCKFILE_VERSION: u8 = 2;
/// Oldest schema this build still reads.
pub const OLDEST_READABLE_LOCKFILE_VERSION: u8 = 1;

#[derive(Debug, Serialize, Deserialize)]
pub struct Lockfile {
    /// The schema version the file was read as, or `LOCKFILE_VERSION` for a
    /// lockfile built in memory. Writers ignore it and emit the current one.
    pub version: u8,
    pub capabilities: BTreeMap<String, CapabilityLockEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapabilityLockEntry {
    #[serde(rename = "type")]
    pub capability_type: CapabilityType,
    /// The capability's own version: a declared manifest version, or the
    /// commit that was installed when nothing better exists. Which one is
    /// recorded in `version_scheme`, never guessed from the string.
    pub version: String,
    #[serde(default)]
    pub version_scheme: VersionScheme,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub description: String,
    /// Where this capability came from. One typed value, so every lifecycle
    /// verb dispatches on `match` instead of comparing strings.
    pub source: CapabilitySource,
    pub targets: BTreeMap<String, TargetLockEntry>,
    /// Cached from the manifest at install/update time, the same way
    /// `description` is: after install, only the `files` a manifest declares
    /// get copied to disk, `tuff.toml` itself does not, so this is the only
    /// durable record of how a tool is invoked. Consumed by the generated
    /// capability-index skill (RFC-103 tier 1).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub implementation: Option<ImplementationConfig>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parameters: Option<serde_json::Value>,
    /// Same rationale as `implementation`/`parameters`: a workflow's
    /// `requires` list lives only in its manifest, which isn't copied to the
    /// installed target directory.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub workflow: Option<WorkflowConfig>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub server: Option<McpServerConfig>,
}

/// What kind of string `CapabilityLockEntry::version` holds (RFC-105 D4).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum VersionScheme {
    /// A release chosen by semver tag resolution (RFC-101; not written yet).
    Semver,
    /// The version the manifest declares. Says nothing about releases.
    #[default]
    Declared,
    /// A commit SHA: content-exact, semantically silent.
    Sha,
}

/// The origin of an installed capability. Internally tagged as `kind` on
/// the wire, so a lockfile row reads `[capabilities.source] kind = "git"`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum CapabilitySource {
    Local(LocalSource),
    Git(GitSource),
    Catalog(CatalogSource),
    Pack(PackProvenance),
}

impl CapabilitySource {
    pub fn local(path: impl Into<String>) -> Self {
        Self::Local(LocalSource { path: path.into() })
    }

    /// The `kind` string as written to the lockfile.
    pub fn kind(&self) -> &'static str {
        match self {
            Self::Local(_) => "local",
            Self::Git(_) => "git",
            Self::Catalog(_) => "catalog",
            Self::Pack(_) => "pack",
        }
    }

    pub fn as_git(&self) -> Option<&GitSource> {
        match self {
            Self::Git(git) => Some(git),
            _ => None,
        }
    }

    pub fn as_pack(&self) -> Option<&PackProvenance> {
        match self {
            Self::Pack(pack) => Some(pack),
            _ => None,
        }
    }

    /// The local path a capability was installed from, when it has one.
    pub fn local_path(&self) -> Option<&str> {
        match self {
            Self::Local(local) => Some(local.path.as_str()),
            _ => None,
        }
    }

    /// The version scheme a fresh install from this source records. Git
    /// installs pin a commit until RFC-101 resolves tags; everything else
    /// carries the version its manifest declared.
    pub fn default_version_scheme(&self) -> VersionScheme {
        match self {
            Self::Git(_) => VersionScheme::Sha,
            _ => VersionScheme::Declared,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LocalSource {
    /// Path to the source directory, relative to the lockfile's root when it
    /// lies inside it, absolute otherwise. Empty for an adopted capability
    /// whose only copy is the installed tree.
    #[serde(default)]
    pub path: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GitSource {
    pub url: String,
    /// Subdirectory within the repository holding the capability.
    #[serde(default)]
    pub path: String,
    /// The commit that was installed. Always present.
    #[serde(rename = "ref")]
    pub git_ref: String,
    /// The tag that chose `ref`, when one did (RFC-101).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
    /// The range the user asked for, when they did (RFC-101).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub requested: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CatalogSource {
    /// The catalog entry id: a built-in id, or the server's full registry
    /// name when `registry` is set.
    pub id: String,
    /// That entry's version at install time.
    pub version: String,
    /// The MCP registry this entry came from, when it did not come from the
    /// catalog compiled into the binary.
    ///
    /// Optional so a built-in install writes exactly what it always wrote:
    /// an older Tuff reading a newer lockfile ignores the field rather than
    /// failing to parse the row.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub registry: Option<String>,
}

/// Immutable pack release that delivered a capability entry.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PackProvenance {
    pub name: String,
    pub version: String,
    /// Artifact digest, bare lowercase hex. `sha256:` prefixes exist only at
    /// the OCI boundary.
    pub digest: String,
    /// The OCI registry and repository this pack was pulled from
    /// ("registry/repository", no tag), when known.
    ///
    /// `tuff add pack` only ever sees a local artifact file; it has no way to
    /// know where that file came from unless the caller says so with
    /// `--reference`. Absent, `tuff outdated` cannot check this capability
    /// against anything and reports it as such rather than guessing.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub registry: Option<String>,
    /// The member's path inside the pack's `sources/` tree.
    #[serde(default)]
    pub path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TargetLockEntry {
    #[serde(
        default,
        rename = "managedHooks",
        skip_serializing_if = "Vec::is_empty"
    )]
    pub managed_hooks: Vec<ManagedHook>,
    #[serde(
        default,
        rename = "managedMcpEntry",
        skip_serializing_if = "Option::is_none"
    )]
    pub managed_mcp_entry: Option<ManagedMcpEntry>,
    #[serde(default)]
    pub ownership: TargetOwnership,
    #[serde(default)]
    pub sha256: String,
    #[serde(default)]
    pub installed_path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManagedHook {
    #[serde(rename = "settingsPath")]
    pub settings_path: String,
    pub event: String,
    #[serde(
        default,
        rename = "canonicalEvent",
        skip_serializing_if = "Option::is_none"
    )]
    pub canonical_event: Option<String>,
    pub command: String,
    #[serde(rename = "baselineHash")]
    pub baseline_hash: String,
}

/// Baseline for one Tuff-managed `mcpServers.<id>` entry (RFC-102 stage b).
///
/// MCP config files are shared ground that users hand-edit, so the entry
/// gets the managed-hook treatment: a content hash recorded at registration
/// time, compared on every `check`/`list`, never whole-file ownership. The
/// entry's key is the capability id, so only the file path and hash are
/// stored.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManagedMcpEntry {
    #[serde(rename = "configPath")]
    pub config_path: String,
    #[serde(rename = "baselineHash")]
    pub baseline_hash: String,
}

/// Hash an MCP entry value exactly as `managed_mcp_entry_status` will when
/// it re-reads the file: canonical `serde_json` bytes, so on-disk pretty-
/// printing never matters.
pub fn managed_mcp_entry_baseline(entry: &serde_json::Value) -> Result<String> {
    Ok(hash_bytes(&serde_json::to_vec(entry)?))
}

/// `"clean"`, `"modified"`, or `"missing"` for a managed MCP entry.
pub fn managed_mcp_entry_status(
    repo_root: &Path,
    capability_id: &str,
    entry: &ManagedMcpEntry,
) -> &'static str {
    let path = repo_root.join(&entry.config_path);
    let Ok(raw) = std::fs::read_to_string(path) else {
        return "missing";
    };
    let Ok(config): std::result::Result<serde_json::Value, _> = serde_json::from_str(&raw) else {
        return "modified";
    };
    let Some(current) = config
        .get("mcpServers")
        .and_then(|servers| servers.get(capability_id))
    else {
        return "missing";
    };
    match serde_json::to_vec(current) {
        Ok(bytes) if hash_bytes(&bytes) == entry.baseline_hash => "clean",
        _ => "modified",
    }
}

pub fn managed_hooks_from_fragment(
    repo_root: &Path,
    settings_path: &str,
    fragment: &serde_json::Value,
) -> Result<Vec<ManagedHook>> {
    managed_hooks_from_fragment_with_canonical(repo_root, settings_path, fragment, None)
}

pub fn managed_hooks_from_fragment_with_canonical(
    _repo_root: &Path,
    settings_path: &str,
    fragment: &serde_json::Value,
    canonical_event: Option<&str>,
) -> Result<Vec<ManagedHook>> {
    let mut managed = Vec::new();
    let Some(events) = fragment.get("hooks").and_then(serde_json::Value::as_object) else {
        return Ok(managed);
    };

    for (event, groups) in events {
        let Some(groups) = groups.as_array() else {
            continue;
        };
        for group in groups {
            let hooks = group
                .get("hooks")
                .and_then(serde_json::Value::as_array)
                .map_or_else(|| vec![group], |hooks| hooks.iter().collect());
            for hook in hooks {
                let Some(command) = hook.get("command").and_then(serde_json::Value::as_str) else {
                    continue;
                };
                let baseline = serde_json::to_vec(hook)?;
                managed.push(ManagedHook {
                    settings_path: settings_path.to_string(),
                    event: event.clone(),
                    canonical_event: canonical_event.map(str::to_owned),
                    command: command.to_string(),
                    baseline_hash: hash_bytes(&baseline),
                });
            }
        }
    }
    Ok(managed)
}

pub fn managed_hook_status(repo_root: &Path, hook: &ManagedHook) -> &'static str {
    let path = repo_root.join(&hook.settings_path);
    let Ok(settings) = std::fs::read_to_string(path) else {
        return "missing";
    };
    let Ok(settings): std::result::Result<serde_json::Value, _> = serde_json::from_str(&settings)
    else {
        return "modified";
    };
    let Some(groups) = settings
        .get("hooks")
        .and_then(|hooks| hooks.get(&hook.event))
        .and_then(serde_json::Value::as_array)
    else {
        return "missing";
    };

    for group in groups {
        let entries = group
            .get("hooks")
            .and_then(serde_json::Value::as_array)
            .map_or_else(|| vec![group], |entries| entries.iter().collect());
        for entry in entries {
            if entry.get("command").and_then(serde_json::Value::as_str)
                == Some(hook.command.as_str())
            {
                let Ok(content) = serde_json::to_vec(entry) else {
                    return "modified";
                };
                return if hash_bytes(&content) == hook.baseline_hash {
                    "clean"
                } else {
                    "modified"
                };
            }
        }
    }
    "missing"
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TargetOwnership {
    #[default]
    Generated,
    Imported,
}

/// The project-scope lockfile. Never falls through to the global one: the
/// caller resolved a scope and this is the file for it (RFC-105 D3).
pub fn project_lockfile(repo_root: &Path) -> PathBuf {
    repo_root.join("tuff.lock")
}

/// The lockfile for a resolved scope: `<root>/tuff.lock` for a project,
/// the XDG state file for the global scope (where `scope_root` is the home
/// directory). The scope is always passed, never inferred from the path.
pub fn scoped_lockfile(scope_root: &Path, scope: crate::resolver::Scope) -> PathBuf {
    match scope {
        crate::resolver::Scope::Project => project_lockfile(scope_root),
        crate::resolver::Scope::Global => crate::paths::global_lockfile(scope_root),
    }
}

pub fn require_scoped_lockfile(
    scope_root: &Path,
    scope: crate::resolver::Scope,
) -> Result<Lockfile> {
    read_lockfile_at(&scoped_lockfile(scope_root, scope))
}

pub fn write_scoped_lockfile(
    scope_root: &Path,
    scope: crate::resolver::Scope,
    lockfile: &Lockfile,
) -> Result<()> {
    write_lockfile_at(&scoped_lockfile(scope_root, scope), lockfile)
}

pub fn init_lockfile(repo_root: &Path) -> Result<PathBuf> {
    let lock_path = project_lockfile(repo_root);
    init_lockfile_at(&lock_path)?;
    Ok(lock_path)
}

pub fn init_lockfile_at(lock_path: &Path) -> Result<()> {
    if !lock_path.exists() {
        write_lockfile_at(
            lock_path,
            &Lockfile {
                version: LOCKFILE_VERSION,
                capabilities: BTreeMap::new(),
            },
        )?;
    }
    Ok(())
}

pub fn require_lockfile(repo_root: &Path) -> Result<Lockfile> {
    read_lockfile_at(&project_lockfile(repo_root))
}

/// Read a lockfile that may legitimately not exist.
///
/// `Ok(None)` means "no lockfile here", which is normal for the global
/// scope on a machine that has never used `--global`. Anything else, in
/// particular a corrupt or too-new file, is an error: reporting it as
/// "nothing installed" would be a confident wrong answer.
pub fn read_optional_lockfile(path: &Path) -> Result<Option<Lockfile>> {
    match read_lockfile_at(path) {
        Ok(lockfile) => Ok(Some(lockfile)),
        Err(error) if error.kind() == crate::error::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(error),
    }
}

/// Read a lockfile of any supported schema version into the current model.
///
/// The version is read before anything else is deserialised, so a file from
/// a newer tuff fails with a message about versions rather than a shape
/// error naming some field the reader has never heard of.
pub fn read_lockfile_at(path: &Path) -> Result<Lockfile> {
    if !path.exists() {
        let parent = path.parent().unwrap_or(Path::new("."));
        return Err(TuffError::not_found(format!(
            "{} is missing",
            parent
                .join(path.file_name().unwrap_or(OsStr::new("tuff.lock")))
                .display()
        ))
        .with_hint("run 'tuff init' first"));
    }
    let raw = std::fs::read_to_string(path)?;
    let version = peek_version(&raw, path)?;
    let rows: Vec<Row> = match version {
        1 => read_v1_rows(&raw)?,
        2 => read_v2_rows(&raw)?,
        newer => {
            return Err(TuffError::unsupported(format!(
                "unsupported lockfile version: {newer} ({} was written by a newer tuff; this tuff {} reads versions {OLDEST_READABLE_LOCKFILE_VERSION} to {LOCKFILE_VERSION}, upgrade tuff)",
                path.display(),
                env!("CARGO_PKG_VERSION")
            )));
        }
    };
    let mut capabilities: BTreeMap<String, CapabilityLockEntry> = BTreeMap::new();
    for row in rows {
        let Row {
            name,
            target,
            target_entry,
            entry,
        } = row;
        match capabilities.entry(name) {
            std::collections::btree_map::Entry::Occupied(mut existing) => {
                existing.get_mut().targets.insert(target, target_entry);
            }
            std::collections::btree_map::Entry::Vacant(slot) => {
                let mut entry = entry;
                entry.targets.insert(target, target_entry);
                slot.insert(entry);
            }
        }
    }
    Ok(Lockfile {
        version,
        capabilities,
    })
}

/// One wire row folded to its capability entry plus its target.
struct Row {
    name: String,
    target: String,
    target_entry: TargetLockEntry,
    entry: CapabilityLockEntry,
}

fn peek_version(raw: &str, path: &Path) -> Result<u8> {
    #[derive(Deserialize)]
    struct VersionOnly {
        version: Option<u8>,
    }
    let peek: VersionOnly = toml::from_str(raw).map_err(|error| {
        TuffError::corrupt(format!(
            "{} is not a valid lockfile: {}",
            path.display(),
            error.message()
        ))
    })?;
    match peek.version {
        Some(version) if version >= OLDEST_READABLE_LOCKFILE_VERSION => Ok(version),
        Some(version) => Err(TuffError::unsupported(format!(
            "unsupported lockfile version: {version} ({} predates every schema this tuff reads)",
            path.display()
        ))),
        None => Err(TuffError::corrupt(format!(
            "{} has no version field; it is not a Tuff lockfile or it is corrupt",
            path.display()
        ))),
    }
}

/// Schema version 1, read for migration only (RFC-105 D5). Never written.
fn read_v1_rows(raw: &str) -> Result<Vec<Row>> {
    let wire: WireLockfileV1 = toml::from_str(raw)
        .map_err(|error| TuffError::corrupt(format!("invalid version 1 lockfile: {error}")))?;
    Ok(wire
        .capabilities
        .into_iter()
        .map(|item| {
            let source = match item.pack {
                // A pack member was written as "local" with an empty path
                // plus a pack table; the pack is the real origin. The member
                // path inside the pack was not recorded in v1, and the member
                // id is what `tuff add pack` used, so it is the best backfill.
                Some(pack) => CapabilitySource::Pack(PackProvenance {
                    name: pack.name,
                    version: pack.version,
                    digest: pack.digest,
                    registry: pack.registry,
                    path: item.name.clone(),
                }),
                None => match item.source.as_str() {
                    "git" => CapabilitySource::Git(GitSource {
                        url: item.repository,
                        path: item.source_path,
                        git_ref: item.resolved_ref,
                        tag: None,
                        requested: None,
                    }),
                    // A v1 lockfile predates registry installs, so every
                    // catalog row in one came from the built-in catalog.
                    "catalog" => CapabilitySource::Catalog(CatalogSource {
                        id: item.source_path,
                        version: item.resolved_ref,
                        registry: None,
                    }),
                    // The generated capability index wrote a sentinel path
                    // in v1; it has no source tree and v2 says so plainly.
                    _ if item.source_path == "<generated>" => CapabilitySource::local(""),
                    _ => CapabilitySource::local(item.source_path),
                },
            };
            let version_scheme = source.default_version_scheme();
            Row {
                name: item.name,
                target: item.target,
                target_entry: TargetLockEntry {
                    managed_hooks: item.managed_hooks,
                    managed_mcp_entry: item.managed_mcp_entry,
                    ownership: item.ownership,
                    sha256: item.sha256,
                    installed_path: item.installed_path,
                },
                entry: CapabilityLockEntry {
                    capability_type: item.capability_type,
                    version: item.version,
                    version_scheme,
                    description: item.description,
                    source,
                    targets: BTreeMap::new(),
                    implementation: item.implementation,
                    parameters: item.parameters,
                    workflow: item.workflow,
                    server: item.server,
                },
            }
        })
        .collect())
}

fn read_v2_rows(raw: &str) -> Result<Vec<Row>> {
    let wire: WireLockfile = toml::from_str(raw)
        .map_err(|error| TuffError::corrupt(format!("invalid lockfile: {error}")))?;
    Ok(wire
        .capabilities
        .into_iter()
        .map(|item| Row {
            name: item.name,
            target: item.target,
            target_entry: TargetLockEntry {
                managed_hooks: item.managed_hooks,
                managed_mcp_entry: item.managed_mcp_entry,
                ownership: item.ownership,
                sha256: item.sha256,
                installed_path: item.installed_path,
            },
            entry: CapabilityLockEntry {
                capability_type: item.capability_type,
                version: item.version,
                version_scheme: item.version_scheme,
                description: item.description,
                source: item.source,
                targets: BTreeMap::new(),
                implementation: item.implementation,
                parameters: item.parameters,
                workflow: item.workflow,
                server: item.server,
            },
        })
        .collect())
}

pub fn write_lockfile(repo_root: &Path, lockfile: &Lockfile) -> Result<()> {
    write_lockfile_at(&project_lockfile(repo_root), lockfile)
}

pub fn write_lockfile_at(path: &Path, lockfile: &Lockfile) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let mut capabilities = Vec::new();
    for (name, entry) in &lockfile.capabilities {
        for (target, target_entry) in &entry.targets {
            capabilities.push(WireCapability {
                name: name.clone(),
                capability_type: entry.capability_type,
                version: entry.version.clone(),
                version_scheme: entry.version_scheme,
                description: entry.description.clone(),
                target: target.clone(),
                installed_path: target_entry.installed_path.clone(),
                sha256: target_entry.sha256.clone(),
                ownership: target_entry.ownership,
                source: entry.source.clone(),
                managed_hooks: target_entry.managed_hooks.clone(),
                managed_mcp_entry: target_entry.managed_mcp_entry.clone(),
                implementation: entry.implementation.clone(),
                parameters: entry.parameters.clone(),
                workflow: entry.workflow.clone(),
                server: entry.server.clone(),
            });
        }
    }
    capabilities.sort_by(|a, b| {
        a.name
            .cmp(&b.name)
            .then_with(|| a.capability_type.as_str().cmp(b.capability_type.as_str()))
            .then_with(|| a.target.cmp(&b.target))
            .then_with(|| a.installed_path.cmp(&b.installed_path))
    });
    let wire = WireLockfile {
        version: LOCKFILE_VERSION,
        capabilities,
    };
    let content = format!(
        "# Tuff lockfile. Each entry records one capability installation target.\n{}\n",
        toml::to_string_pretty(&wire)?
    );
    std::fs::write(path, content)?;
    Ok(())
}

/// Schema version 2 (RFC-105 D1). Scalars first, tables after, so the TOML
/// serializer never has to emit a value beneath a table.
#[derive(Debug, Serialize, Deserialize)]
struct WireLockfile {
    version: u8,
    capabilities: Vec<WireCapability>,
}

#[derive(Debug, Serialize, Deserialize)]
struct WireCapability {
    name: String,
    #[serde(rename = "type")]
    capability_type: CapabilityType,
    #[serde(default)]
    version: String,
    #[serde(default)]
    version_scheme: VersionScheme,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    description: String,
    target: String,
    installed_path: String,
    sha256: String,
    #[serde(default)]
    ownership: TargetOwnership,
    source: CapabilitySource,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    managed_hooks: Vec<ManagedHook>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    managed_mcp_entry: Option<ManagedMcpEntry>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    implementation: Option<ImplementationConfig>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    parameters: Option<serde_json::Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    workflow: Option<WorkflowConfig>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    server: Option<McpServerConfig>,
}

/// Schema version 1 as tuff 0.1.x wrote it. Read-only; see `read_v1_rows`.
#[derive(Debug, Deserialize)]
struct WireLockfileV1 {
    #[allow(dead_code)]
    version: u8,
    capabilities: Vec<WireCapabilityV1>,
}

#[derive(Debug, Deserialize)]
struct WireCapabilityV1 {
    name: String,
    #[serde(rename = "type")]
    capability_type: CapabilityType,
    source: String,
    #[serde(default)]
    repository: String,
    #[serde(default)]
    source_path: String,
    #[serde(default)]
    resolved_ref: String,
    sha256: String,
    target: String,
    installed_path: String,
    #[serde(default)]
    version: String,
    #[serde(default)]
    description: String,
    #[serde(default)]
    ownership: TargetOwnership,
    #[serde(default)]
    managed_hooks: Vec<ManagedHook>,
    #[serde(default)]
    managed_mcp_entry: Option<ManagedMcpEntry>,
    #[serde(default)]
    pack: Option<PackProvenanceV1>,
    #[serde(default)]
    implementation: Option<ImplementationConfig>,
    #[serde(default)]
    parameters: Option<serde_json::Value>,
    #[serde(default)]
    workflow: Option<WorkflowConfig>,
    #[serde(default)]
    server: Option<McpServerConfig>,
}

#[derive(Debug, Deserialize)]
struct PackProvenanceV1 {
    name: String,
    version: String,
    digest: String,
    #[serde(default)]
    registry: Option<String>,
}

pub fn hash_bytes(content: &[u8]) -> String {
    let mut hasher = Sha256::new();
    hasher.update(content);
    format!("{:x}", hasher.finalize())
}

pub fn relative_or_absolute_fs(path: &Path, repo_root: &Path) -> String {
    path.strip_prefix(repo_root)
        .map(|relative| relative.to_string_lossy().replace('\\', "/"))
        .unwrap_or_else(|_| path.to_string_lossy().to_string())
}

pub fn absolutize(repo_root: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        repo_root.join(path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn init_lockfile_at_creates_new_file() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("tuff.lock");
        init_lockfile_at(&path).unwrap();
        assert!(path.exists());

        let lf = read_lockfile_at(&path).unwrap();
        assert_eq!(lf.version, LOCKFILE_VERSION);
        assert!(lf.capabilities.is_empty());
    }

    #[test]
    fn read_lockfile_at_rejects_missing() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("tuff.lock");
        assert!(read_lockfile_at(&path).is_err());
    }

    #[test]
    fn read_lockfile_at_rejects_v4_schema() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("tuff.lock");
        fs::write(&path, "version = 4\ncapabilities = []\n").unwrap();

        let error = read_lockfile_at(&path).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("unsupported lockfile version: 4")
        );
    }

    #[test]
    fn write_and_read_roundtrip() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("tuff.lock");
        let mut lf = Lockfile {
            version: LOCKFILE_VERSION,
            capabilities: BTreeMap::new(),
        };
        lf.capabilities.insert(
            "test".into(),
            CapabilityLockEntry {
                capability_type: CapabilityType::Skill,
                version: "1.0".into(),
                version_scheme: VersionScheme::Declared,
                description: "test skill".into(),
                source: CapabilitySource::local(""),
                targets: BTreeMap::from([(
                    "open-agents".into(),
                    TargetLockEntry {
                        managed_hooks: Vec::new(),
                        managed_mcp_entry: None,
                        ownership: TargetOwnership::Generated,
                        sha256: hash_bytes(b"content"),
                        installed_path: ".agents/skills/test".into(),
                    },
                )]),
                implementation: None,
                parameters: None,
                workflow: None,
                server: None,
            },
        );
        write_lockfile_at(&path, &lf).unwrap();
        let read = read_lockfile_at(&path).unwrap();
        assert_eq!(read.capabilities.len(), 1);
    }

    #[test]
    fn missing_target_ownership_defaults_to_generated() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("tuff.lock");
        fs::write(&path, "version = 1\ncapabilities = []\n").unwrap();
        let read = read_lockfile_at(&path).unwrap();
        assert!(read.capabilities.is_empty());
    }

    #[test]
    fn hash_bytes_produces_consistent_output() {
        let h1 = hash_bytes(b"hello");
        let h2 = hash_bytes(b"hello");
        assert_eq!(h1, h2);
        assert_eq!(h1.len(), 64);
        assert_ne!(h1, hash_bytes(b"world"));
    }

    #[test]
    fn a_version_1_lockfile_migrates_every_source_kind() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("tuff.lock");
        fs::write(
            &path,
            r#"version = 1

[[capabilities]]
name = "git-skill"
type = "skill"
source = "git"
repository = "https://example.com/skills.git"
source_path = "skills/git-skill"
resolved_ref = "9b9c499"
sha256 = "aa"
target = "open-agents"
installed_path = ".agents/skills/git-skill"
version = "9b9c499"

[[capabilities]]
name = "memory"
type = "mcp-server"
source = "catalog"
repository = "builtin"
source_path = "memory"
resolved_ref = "1.0.0"
sha256 = "bb"
target = "open-agents"
installed_path = ".agents/mcp-servers/memory"
version = "1.0.0"

[[capabilities]]
name = "pack-skill"
type = "skill"
source = "local"
source_path = ""
resolved_ref = ""
sha256 = "cc"
target = "open-agents"
installed_path = ".agents/skills/pack-skill"
version = "1.5.0"

[capabilities.pack]
name = "com.acme/fixture"
version = "1.0.0"
digest = "dd"
registry = "ghcr.io/acme/fixture"

[[capabilities]]
name = "local-skill"
type = "skill"
source = "local"
source_path = "sources/local-skill"
resolved_ref = ""
sha256 = "ee"
target = "open-agents"
installed_path = ".agents/skills/local-skill"
version = "1.0.0"
"#,
        )
        .unwrap();

        let lf = read_lockfile_at(&path).unwrap();
        assert_eq!(lf.version, 1, "the version read is reported, not rewritten");
        assert_eq!(
            lf.capabilities["git-skill"].source,
            CapabilitySource::Git(GitSource {
                url: "https://example.com/skills.git".into(),
                path: "skills/git-skill".into(),
                git_ref: "9b9c499".into(),
                tag: None,
                requested: None,
            })
        );
        assert_eq!(
            lf.capabilities["git-skill"].version_scheme,
            VersionScheme::Sha
        );
        assert_eq!(
            lf.capabilities["memory"].source,
            CapabilitySource::Catalog(CatalogSource {
                id: "memory".into(),
                version: "1.0.0".into(),
                registry: None,
            })
        );
        assert_eq!(
            lf.capabilities["pack-skill"].source,
            CapabilitySource::Pack(PackProvenance {
                name: "com.acme/fixture".into(),
                version: "1.0.0".into(),
                digest: "dd".into(),
                registry: Some("ghcr.io/acme/fixture".into()),
                path: "pack-skill".into(),
            })
        );
        assert_eq!(
            lf.capabilities["local-skill"].source,
            CapabilitySource::local("sources/local-skill")
        );
        assert_eq!(
            lf.capabilities["local-skill"].version_scheme,
            VersionScheme::Declared
        );

        // Writing produces v2, and v2 round-trips byte for byte.
        write_lockfile_at(&path, &lf).unwrap();
        let written = fs::read_to_string(&path).unwrap();
        assert!(written.contains("version = 2\n"));
        assert!(written.contains("kind = \"pack\""));
        assert!(!written.contains("resolved_ref"));
        let again = read_lockfile_at(&path).unwrap();
        assert_eq!(again.version, 2);
        write_lockfile_at(&path, &again).unwrap();
        assert_eq!(fs::read_to_string(&path).unwrap(), written);
    }

    #[test]
    fn a_lockfile_without_a_version_is_corrupt_not_empty() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("tuff.lock");
        fs::write(&path, "capabilities = []\n").unwrap();
        let error = read_lockfile_at(&path).unwrap_err().to_string();
        assert!(error.contains("no version field"), "{error}");

        fs::write(&path, "version = 2\n[[capabilities]\n").unwrap();
        let error = read_lockfile_at(&path).unwrap_err().to_string();
        assert!(error.contains("not a valid lockfile"), "{error}");
    }

    #[test]
    fn managed_mcp_entry_status_tracks_the_entry_not_the_file() {
        let tmp = TempDir::new().unwrap();
        let config_path = tmp.path().join("mcp.json");
        let entry_value = serde_json::json!({"command": "npx", "args": ["-y", "srv"]});
        let both = |neighbour: &str| {
            serde_json::to_string_pretty(&serde_json::json!({
                "mcpServers": {"github": entry_value, "neighbour": {"command": neighbour}}
            }))
            .unwrap()
        };
        fs::write(&config_path, both("hand")).unwrap();
        let managed = ManagedMcpEntry {
            config_path: "mcp.json".into(),
            baseline_hash: managed_mcp_entry_baseline(&entry_value).unwrap(),
        };

        // Pretty-printing and neighbouring hand-written entries never matter,
        // and editing the neighbour leaves ours clean.
        assert_eq!(
            managed_mcp_entry_status(tmp.path(), "github", &managed),
            "clean"
        );
        fs::write(&config_path, both("edited")).unwrap();
        assert_eq!(
            managed_mcp_entry_status(tmp.path(), "github", &managed),
            "clean"
        );

        // Editing our entry is modified; removing it, or the file, is missing.
        fs::write(
            &config_path,
            r#"{"mcpServers": {"github": {"command": "tampered"}}}"#,
        )
        .unwrap();
        assert_eq!(
            managed_mcp_entry_status(tmp.path(), "github", &managed),
            "modified"
        );
        fs::write(&config_path, r#"{"mcpServers": {}}"#).unwrap();
        assert_eq!(
            managed_mcp_entry_status(tmp.path(), "github", &managed),
            "missing"
        );
        fs::remove_file(&config_path).unwrap();
        assert_eq!(
            managed_mcp_entry_status(tmp.path(), "github", &managed),
            "missing"
        );
    }
}