diff --git a/AGENTS.md b/AGENTS.md index a08b4a32..797a1546 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -83,7 +83,7 @@ The runner owns three cross-cutting behaviors that individual commands do not re Under `lib/src/`, each folder is one layer, and the rule for where a type lives is: -- `model/` holds only the types two or more feature layers share: `Harness`, `PlannedChange` with `ChangeMode` and `DocumentFormat`, `InstallManifest` with its entries, `Contribution` with the `Drift` and `Subtraction` its methods return, `Scope` with `UserScope` and `ProjectScope`, the one answer to where an install goes and how its files spell a path, `PluginDirectory`, and `DocumentCodec`, the one definition of how documents are decoded and compared. A value a shared type returns lives beside it, even when only one feature reads it. It imports nothing else under `src/`. +- `model/` holds only the types two or more feature layers share: `Harness`, `PlannedChange` with `ChangeMode` and `DocumentFormat`, `InstallManifest` with its entries and the `PluginSnapshot` of each plugin it was written from, `Contribution` with the `Drift` and `Subtraction` its methods return, `Scope` with `UserScope` and `ProjectScope`, the one answer to where an install goes and how its files spell a path, `PluginDirectory`, and `DocumentCodec`, the one definition of how documents are decoded and compared. A value a shared type returns lives beside it, even when only one feature reads it. It imports nothing else under `src/`. - `document/` edits config text through `TomlText` and `MarkdownText`, and writes files safely through the `File` extensions. It imports `model/`. - `detect/`, `resolve/`, `translate/`, `reconcile/` and `manifest/` are the features: finding the installed harnesses, fetching and parsing the pinned plugins, turning the plugins into each harness's files, applying planned changes, and verifying or reversing what the manifest records. Each imports `model/` and `document/`, never another feature. - `/model/` holds every type the feature itself defines, its public results and the values it passes between its own steps alike, one class per file, exported from `/model/model.dart`. A status enum lives in the file of the result it describes, as `ReverseOutcome` does beside `ReversedEntry` and `ChangeOutcome` beside `AppliedChange`. A service such as `Reconciler` takes the `DocumentCodec` through its constructor, while a value type such as `TomlText` uses the default. The classes that do the work sit at the feature's root. @@ -126,7 +126,7 @@ You should not need to bump the version by hand at all: release-please owns it. ## Code Conventions -- Linting: `very_good_analysis` ^11.1.0 with no rule overrides, the same as Very Good CLI. Generated files are excluded in the `analyzer` section rather than by turning rules off. +- Linting: `very_good_analysis` ^11.0.0 with no rule overrides, the same as Very Good CLI. Generated files are excluded in the `analyzer` section rather than by turning rules off. - Every public class carries `/// {@template name}` / `/// {@endtemplate}`, and declares its constructor in the body under `/// {@macro name}` — `this;`, or `this { ... }` when it has one. The declaration is not optional: `public_member_api_docs` reads a primary constructor as an undocumented member, and the class's own doc comment does not satisfy it. - The codebase uses **Dart 3.13 syntax**. Unnamed constructors are declared with the `new` keyword — `new({required this._logger})`, not `UpdateCommand({...})`. Empty class bodies use a semicolon — `class _MockLogger extends Mock implements Logger;`. That is for private classes such as test doubles; a public class still needs a body to hold the documented constructor above. - Commits and PR titles follow [Conventional Commits](https://www.conventionalcommits.org/). diff --git a/packages/vgv_ai_cli/README.md b/packages/vgv_ai_cli/README.md index 279cb146..da8ee959 100644 --- a/packages/vgv_ai_cli/README.md +++ b/packages/vgv_ai_cli/README.md @@ -114,7 +114,10 @@ vgv_ai install --plugin vgv-wingspan `install` reads the pinned plugins from the Claude Code plugin cache when it holds the pinned version, and otherwise clones them into `~/.vgv_ai/plugins`. It records every file and key it writes in `~/.vgv_ai/manifest.json`, which is -how `doctor` and `uninstall` tell what's yours from what's VGV's. +how `doctor` and `uninstall` tell what's yours from what's VGV's. Beside them, it +records the tag of each plugin and a SHA-256 of its files, so a later run can +tell whether a plugin still holds what it was installed from, even when its tag +was moved. - **Your settings:** settings files such as `~/.gemini/settings.json` are merged, not replaced. Without `--force`, a value you set is kept, and @@ -182,7 +185,8 @@ directory, apart from the plugins `install` clones into `~/.vgv_ai/plugins`. `AGENTS.md`, which it reads as written. - **Trust:** Codex and Pi load a repository's files only once you trust the project, and Gemini too when its folder trust is on. `install` reminds you. -- **Reruns:** `.vgv_ai/manifest.json` records what `install --project` wrote. +- **Reruns:** `.vgv_ai/manifest.json` records what `install --project` wrote, + and the tag and content hash of each plugin it was written from. A rerun that covers every harness and plugin it records also removes what it no longer writes, such as the copy of a plugin from its previous tag. A file you changed is kept and becomes yours. @@ -199,8 +203,6 @@ Not installed yet, each tracked as part of [#139](https://github.com/VeryGoodOpenSource/vgv_ai_cli/issues/139)). - Failing on anything it can't translate ([#141](https://github.com/VeryGoodOpenSource/vgv_ai_cli/issues/141)). -- A content hash per plugin in the manifest - ([#142](https://github.com/VeryGoodOpenSource/vgv_ai_cli/issues/142)). ### doctor diff --git a/packages/vgv_ai_cli/lib/src/commands/install_command.dart b/packages/vgv_ai_cli/lib/src/commands/install_command.dart index b4012633..a2695476 100644 --- a/packages/vgv_ai_cli/lib/src/commands/install_command.dart +++ b/packages/vgv_ai_cli/lib/src/commands/install_command.dart @@ -233,6 +233,7 @@ class InstallCommand({ from: manifest, to: retirement.manifest.withPlugins( _pluginVersions(manifest, resolved.assets.plugins, selected), + snapshots: _snapshots(manifest, resolved.directories, selected), ), ); final anyFailed = @@ -540,6 +541,31 @@ class InstallCommand({ }; } + /// The snapshots to record for each harness of [selected], as + /// [_pluginVersions] picks its versions: of the [resolved] copies for the + /// plugins this run wrote, and what [manifest] already records for the + /// ones it skipped. Every other harness keeps what it had. + Map> _snapshots( + InstallManifest manifest, + List resolved, + Set selected, + ) { + final written = { + for (final directory in resolved) + directory.plugin.name: PluginSnapshot.of(directory), + }; + return { + ...manifest.snapshots, + for (final harness in selected) + harness: { + for (final plugin in vgvPlugins) + plugin.name: + ?(written[plugin.name] ?? + manifest.snapshots[harness]?[plugin.name]), + }, + }; + } + /// Resolves and parses the [plugins] into one asset bundle, with every /// body naming the plugin's files where [scope] keeps them. /// diff --git a/packages/vgv_ai_cli/lib/src/manifest/manifest_auditor.dart b/packages/vgv_ai_cli/lib/src/manifest/manifest_auditor.dart index cd8fd896..4649ae37 100644 --- a/packages/vgv_ai_cli/lib/src/manifest/manifest_auditor.dart +++ b/packages/vgv_ai_cli/lib/src/manifest/manifest_auditor.dart @@ -88,7 +88,7 @@ class const ManifestAuditor({ manifest: InstallManifest(kept, { for (final MapEntry(:key, :value) in manifest.plugins.entries) if (remaining.contains(key)) key: value, - }), + }, manifest.snapshots), ); } diff --git a/packages/vgv_ai_cli/lib/src/model/install_manifest.dart b/packages/vgv_ai_cli/lib/src/model/install_manifest.dart index 985f4c81..8a7c0d27 100644 --- a/packages/vgv_ai_cli/lib/src/model/install_manifest.dart +++ b/packages/vgv_ai_cli/lib/src/model/install_manifest.dart @@ -6,6 +6,7 @@ import 'package:path/path.dart' as p; import 'package:vgv_ai_cli/src/model/contribution.dart'; import 'package:vgv_ai_cli/src/model/harness.dart'; import 'package:vgv_ai_cli/src/model/planned_change.dart'; +import 'package:vgv_ai_cli/src/model/plugin_snapshot.dart'; /// {@template manifest_entry} /// What VGV wrote to one file, and so what `doctor` checks and `uninstall` @@ -185,8 +186,8 @@ final class const MarkdownSections({ } /// {@template install_manifest} -/// Everything VGV wrote under a home directory, by file, and the plugin -/// versions each harness was written from. +/// Everything VGV wrote under a home directory or project, by file, and the +/// plugin versions and snapshots each harness was written from. /// /// `install` writes it, and `doctor` and `uninstall` read it, so this is the /// one place ownership is defined. It is persisted rather than recomputed @@ -199,8 +200,29 @@ final class const MarkdownSections({ /// {@endtemplate} class InstallManifest extends Equatable { /// {@macro install_manifest} - new([Iterable entries = const [], this.plugins = const {}]) - : _entries = {for (final entry in entries) entry.path: entry}; + /// + /// A snapshot of a plugin [plugins] does not record for its harness is + /// dropped, so taking a harness or plugin out of [plugins] takes its + /// snapshot with it. + new([ + Iterable entries = const [], + this.plugins = const {}, + Map> snapshots = const {}, + ]) : _entries = {for (final entry in entries) entry.path: entry}, + snapshots = _recorded(snapshots, plugins); + + /// The [snapshots] of the plugins [plugins] records for their harness. + static Map> _recorded( + Map> snapshots, + Map> plugins, + ) => { + for (final MapEntry(key: harness, value: byName) in snapshots.entries) + if (plugins[harness] case final versions?) + harness: { + for (final MapEntry(:key, :value) in byName.entries) + if (versions.containsKey(key)) key: value, + }, + }; /// Decodes [source], with its paths resolved against [home], keeping /// every entry it understands, or returns null when a newer version of @@ -224,10 +246,14 @@ class InstallManifest extends Equatable { 'version': InstallManifest.version, 'entries': final List list, } => - InstallManifest([ - for (final item in list) - ?ManifestEntry.fromJson(_resolved(item, home: home)), - ], _plugins(json['plugins'])), + InstallManifest( + [ + for (final item in list) + ?ManifestEntry.fromJson(_resolved(item, home: home)), + ], + _plugins(json['plugins']), + _snapshots(json['snapshots']), + ), {'version': final int other} when other > version => null, _ => InstallManifest(), }; @@ -248,6 +274,24 @@ class InstallManifest extends Equatable { }; } + /// The snapshots [json] records per harness, without what this version + /// cannot read. + /// + /// A manifest from before snapshots were recorded has none, which leaves + /// the copy each plugin was written from unknown. + static Map> _snapshots(Object? json) { + if (json is! Map) return const {}; + return { + for (final MapEntry(:key, :value) in json.entries) + if (Harness.values.asNameMap()[key] case final harness?) + if (value is Map) + harness: { + for (final MapEntry(:key, :value) in value.entries) + key: ?PluginSnapshot.fromJson(value), + }, + }; + } + /// [item] with its path resolved against [home], or null when the path /// is absolute or leads out of [home]. static Object? _resolved(Object? item, {required String home}) { @@ -272,6 +316,14 @@ class InstallManifest extends Equatable { /// plugin out of one harness while another is installed from it. final Map> plugins; + /// Each harness to the snapshots of the plugins its files were written + /// from, by plugin name, so a later run can tell whether the plugin it + /// resolves holds the same files. + /// + /// Per harness for the same reason as [plugins], and kept apart from it so + /// a version of vgv_ai that predates snapshots still reads [plugins]. + final Map> snapshots; + /// Every entry, in the order they were recorded. Iterable get entries => _entries.values; @@ -291,7 +343,7 @@ class InstallManifest extends Equatable { /// This manifest with [entry] in place of any entry for its path. InstallManifest record(ManifestEntry entry) { - return InstallManifest([..._entries.values, entry], plugins); + return InstallManifest([..._entries.values, entry], plugins, snapshots); } /// This manifest without the entries for [paths]. @@ -300,13 +352,18 @@ class InstallManifest extends Equatable { return InstallManifest( entries.where((e) => !dropped.contains(e.path)), plugins, + snapshots, ); } /// This manifest with [plugins] as the versions each harness was written - /// from. - InstallManifest withPlugins(Map> plugins) { - return InstallManifest(entries, plugins); + /// from, and [snapshots] as the copies it was written from, or the ones + /// this manifest records when there are none. + InstallManifest withPlugins( + Map> plugins, { + Map>? snapshots, + }) { + return InstallManifest(entries, plugins, snapshots ?? this.snapshots); } /// The manifest as the JSON [InstallManifest.decode] reads, with its @@ -317,6 +374,13 @@ class InstallManifest extends Equatable { 'plugins': { for (final MapEntry(:key, :value) in plugins.entries) key.name: value, }, + 'snapshots': { + for (final MapEntry(key: harness, value: byName) in snapshots.entries) + harness.name: { + for (final MapEntry(:key, :value) in byName.entries) + key: value.toJson(), + }, + }, 'entries': [ for (final entry in entries) {...entry.toJson(), 'path': p.relative(entry.path, from: home)}, @@ -326,7 +390,7 @@ class InstallManifest extends Equatable { } @override - List get props => [entries.toList(), plugins]; + List get props => [entries.toList(), plugins, snapshots]; } /// The SHA-256 of [bytes], as hex. diff --git a/packages/vgv_ai_cli/lib/src/model/model.dart b/packages/vgv_ai_cli/lib/src/model/model.dart index 6f275dce..41e80bbe 100644 --- a/packages/vgv_ai_cli/lib/src/model/model.dart +++ b/packages/vgv_ai_cli/lib/src/model/model.dart @@ -9,6 +9,7 @@ export 'harness.dart'; export 'install_manifest.dart'; export 'planned_change.dart'; export 'plugin_directory.dart'; +export 'plugin_snapshot.dart'; export 'run_process.dart'; export 'scope.dart'; export 'subtraction.dart'; diff --git a/packages/vgv_ai_cli/lib/src/model/plugin_directory.dart b/packages/vgv_ai_cli/lib/src/model/plugin_directory.dart index b8f24cd1..4aad4cae 100644 --- a/packages/vgv_ai_cli/lib/src/model/plugin_directory.dart +++ b/packages/vgv_ai_cli/lib/src/model/plugin_directory.dart @@ -2,8 +2,8 @@ import 'package:file/file.dart'; import 'package:vgv_ai_plugins/vgv_ai_plugins.dart'; /// {@template plugin_directory} -/// Where [plugin] was resolved to, and which of its top-level entries a -/// copy of it needs. +/// Where [plugin] was resolved to, which of its top-level entries a copy of +/// it needs, and a hash of what it holds. /// {@endtemplate} class const PluginDirectory({ /// The plugin. @@ -12,6 +12,10 @@ class const PluginDirectory({ /// The directory holding its files. required final Directory directory, + /// The SHA-256 of every file under [directory] but its `.git`, by path, + /// so it changes with any file the plugin adds, removes or edits. + required final String sha256, + /// The top-level entries of [directory] that its skill and agent bodies /// reference, such as `references`. final List referencedEntries = const [], diff --git a/packages/vgv_ai_cli/lib/src/model/plugin_snapshot.dart b/packages/vgv_ai_cli/lib/src/model/plugin_snapshot.dart new file mode 100644 index 00000000..f3aa218d --- /dev/null +++ b/packages/vgv_ai_cli/lib/src/model/plugin_snapshot.dart @@ -0,0 +1,44 @@ +import 'package:equatable/equatable.dart'; +import 'package:vgv_ai_cli/src/model/plugin_directory.dart'; + +/// {@template plugin_snapshot} +/// Which copy of a plugin a harness's files were written from: the tag it +/// was pinned to, and a hash of the files it held. +/// +/// The tag alone does not pin the content: a tag can be moved, and a plugin +/// read from the Claude Code cache holds whatever its default branch was +/// when it was fetched. Two snapshots with the same [sha256] were written +/// from the same files, so a rerun from them writes the same thing. +/// {@endtemplate} +class const PluginSnapshot({ + /// The tag the plugin was pinned to. + required final String tag, + + /// The SHA-256 of the plugin's files, as `PluginDirectory.sha256` hashes + /// them. + required final String sha256, +}) extends Equatable { + /// {@macro plugin_snapshot} + this; + + /// {@macro plugin_snapshot} + /// + /// Takes both from where [directory] was resolved. + new of(PluginDirectory directory) + : this(tag: directory.plugin.gitRef, sha256: directory.sha256); + + /// Decodes what [toJson] writes, or returns null when [json] is not one. + static PluginSnapshot? fromJson(Object? json) => switch (json) { + {'tag': final String tag, 'sha256': final String sha256} => PluginSnapshot( + tag: tag, + sha256: sha256, + ), + _ => null, + }; + + /// The snapshot as [fromJson] reads it. + Map toJson() => {'tag': tag, 'sha256': sha256}; + + @override + List get props => [tag, sha256]; +} diff --git a/packages/vgv_ai_cli/lib/src/resolve/content_hash.dart b/packages/vgv_ai_cli/lib/src/resolve/content_hash.dart new file mode 100644 index 00000000..1024cc77 --- /dev/null +++ b/packages/vgv_ai_cli/lib/src/resolve/content_hash.dart @@ -0,0 +1,46 @@ +import 'dart:convert'; + +import 'package:crypto/crypto.dart'; +import 'package:file/file.dart'; + +/// Hashes what a resolved plugin holds, so a manifest can tell two copies of +/// it apart even when they share a tag. +extension ContentHash on Directory { + /// The SHA-256 of every file under this directory but its `.git`, as hex. + /// + /// Each file is listed by its `/`-separated path with the SHA-256 of its + /// bytes, in path order, and the list is hashed, so the result is the same + /// on every machine and changes when a file is added, removed, renamed or + /// edited. A symlink is hashed by where it points rather than followed, so + /// a link out of the directory, or a cycle, cannot change or hang it. + String contentHash() { + final path = fileSystem.path; + final entities = [ + for (final entity in listSync(followLinks: false)) + if (path.basename(entity.path) != '.git') + if (entity is Directory) + ...entity.listSync(recursive: true, followLinks: false) + else + entity, + ]; + + final hashes = { + for (final entity in entities) + if (_bytes(entity) case final bytes?) + path.split(path.relative(entity.path, from: this.path)).join('/'): + sha256.convert(bytes), + }; + final listing = [ + for (final name in hashes.keys.toList()..sort()) '${hashes[name]} $name', + ]; + return sha256.convert(utf8.encode(listing.join('\n'))).toString(); + } + + /// What [entity] is hashed by: a file's bytes, or where a link points, or + /// null for a directory, which only its files stand for. + static List? _bytes(FileSystemEntity entity) => switch (entity) { + final File file => file.readAsBytesSync(), + final Link link => utf8.encode(link.targetSync()), + _ => null, + }; +} diff --git a/packages/vgv_ai_cli/lib/src/resolve/plugin_resolver.dart b/packages/vgv_ai_cli/lib/src/resolve/plugin_resolver.dart index ae33ef6c..2ffa004a 100644 --- a/packages/vgv_ai_cli/lib/src/resolve/plugin_resolver.dart +++ b/packages/vgv_ai_cli/lib/src/resolve/plugin_resolver.dart @@ -2,6 +2,7 @@ import 'dart:io' show Process; import 'package:file/file.dart'; import 'package:vgv_ai_cli/src/model/model.dart'; +import 'package:vgv_ai_cli/src/resolve/content_hash.dart'; import 'package:vgv_ai_cli/src/resolve/model/model.dart'; import 'package:vgv_ai_plugins/vgv_ai_plugins.dart'; @@ -100,6 +101,7 @@ class const PluginResolver({ PluginDirectory( plugin: plugin, directory: directory, + sha256: directory.contentHash(), referencedEntries: assets.referencedEntries, ), ], diff --git a/packages/vgv_ai_cli/test/src/commands/install_command_test.dart b/packages/vgv_ai_cli/test/src/commands/install_command_test.dart index ef5460ea..49c9052f 100644 --- a/packages/vgv_ai_cli/test/src/commands/install_command_test.dart +++ b/packages/vgv_ai_cli/test/src/commands/install_command_test.dart @@ -10,6 +10,7 @@ import 'package:vgv_ai_cli/src/commands/commands.dart'; import 'package:vgv_ai_cli/src/detect/detect.dart'; import 'package:vgv_ai_cli/src/manifest/manifest.dart'; import 'package:vgv_ai_cli/src/model/model.dart'; +import 'package:vgv_ai_cli/src/resolve/content_hash.dart'; import 'package:vgv_ai_cli/src/resolve/resolve.dart'; import 'package:vgv_ai_cli/src/translate/translate.dart'; import 'package:vgv_ai_plugins/vgv_ai_plugins.dart'; @@ -362,6 +363,39 @@ void main() { }); }); + test('keeps the recorded snapshot of a plugin it skips', () async { + final skipped = vgvPlugins.last; + const old = PluginSnapshot(tag: 'v0.0.1', sha256: 'old'); + await runInstall(['--only', 'claude']); + final store = ManifestStore(home); + final recorded = store.load()!; + store.save( + recorded.withPlugins( + recorded.plugins, + snapshots: { + Harness.claude: { + ...?recorded.snapshots[Harness.claude], + skipped.name: old, + }, + }, + ), + ); + + await runInstall([ + '--only', + 'claude', + '--plugin', + vgvPlugins.first.name, + ]); + + final snapshots = store.load()!.snapshots[Harness.claude]!; + expect(snapshots[skipped.name], old); + expect( + snapshots[vgvPlugins.first.name], + recorded.snapshots[Harness.claude]![vgvPlugins.first.name], + ); + }); + group('in a terminal', () { /// What the picker was given as its [argument], such as `choices`. T asked(String argument) { @@ -1045,6 +1079,31 @@ void main() { }, ); + test('records the tag and content of each plugin it wrote', () async { + planOne(); + + await runInstall(['--only', 'claude']); + + Directory cloned(PluginRef plugin) => fs.directory( + fs.path.joinAll([ + home.path, + '.vgv_ai/plugins', + plugin.name, + plugin.version, + ?plugin.path, + ]), + ); + expect(ManifestStore(home).load()!.snapshots, { + Harness.claude: { + for (final plugin in vgvPlugins) + plugin.name: PluginSnapshot( + tag: plugin.gitRef, + sha256: cloned(plugin).contentHash(), + ), + }, + }); + }); + test('records what was written', () async { planOne(); @@ -1442,6 +1501,40 @@ void main() { .called(1); }); + test('records the tag and content hash of the plugin', () async { + /// What the project's manifest records for the plugin, per harness. + Map snapshots() => + (jsonDecode( + project + .childFile('.vgv_ai/manifest.json') + .readAsStringSync(), + ) as Map)['snapshots'] + as Map; + String hash(String version) => fs + .directory('${home.path}/.vgv_ai/plugins/${ffca.name}/$version') + .contentHash(); + + await install(); + + expect(snapshots(), { + for (final harness in ['gemini', 'codex']) + harness: { + ffca.name: {'tag': 'v1.0.0', 'sha256': hash('1.0.0')}, + }, + }); + + shipsAgent = false; + await install(tag: 'v2.0.0'); + + expect(hash('2.0.0'), isNot(hash('1.0.0'))); + expect(snapshots(), { + for (final harness in ['gemini', 'codex']) + harness: { + ffca.name: {'tag': 'v2.0.0', 'sha256': hash('2.0.0')}, + }, + }); + }); + test('removes the plugin copy of the previous tag', () async { await install(); expect(oldCopy().existsSync(), isTrue); diff --git a/packages/vgv_ai_cli/test/src/manifest/manifest_auditor_test.dart b/packages/vgv_ai_cli/test/src/manifest/manifest_auditor_test.dart index aeed6de6..4d6683b6 100644 --- a/packages/vgv_ai_cli/test/src/manifest/manifest_auditor_test.dart +++ b/packages/vgv_ai_cli/test/src/manifest/manifest_auditor_test.dart @@ -671,36 +671,57 @@ void main() { expect(manifest.file.readAsStringSync(), before); }); - test('keeps the plugin versions of a harness it leaves alone', () { + test('keeps the plugin versions and snapshots of a harness it leaves ' + 'alone', () { const recorded = { Harness.codex: {'vgv-wingspan': '0.0.5'}, }; + const snapshots = { + Harness.codex: { + 'vgv-wingspan': PluginSnapshot(tag: 'v0.0.5', sha256: 'cafe'), + }, + }; install([ owned(seed('.codex/a.md', 'a'), 'a', harness: Harness.codex), ]); final result = auditor.reverse( - manifest.load()!.withPlugins(recorded), + manifest.load()!.withPlugins(recorded, snapshots: snapshots), fs, owners: const {Harness.claude}, ); expect(result.manifest.plugins, recorded); - }); - - test('forgets the plugin versions of a harness it empties', () { - install([owned(file('.codex/a.md'), 'a', harness: Harness.codex)]); - - final result = auditor.reverse( - manifest.load()!.withPlugins(const { - Harness.codex: {'vgv-wingspan': '0.0.5'}, - }), - fs, - ); - - expect(result.manifest.entries, isEmpty); - expect(result.manifest.plugins, isEmpty); - }); + expect(result.manifest.snapshots, snapshots); + }); + + test( + 'forgets the plugin versions and snapshots of a harness it empties', + () { + install([owned(file('.codex/a.md'), 'a', harness: Harness.codex)]); + + final result = auditor.reverse( + manifest.load()!.withPlugins( + const { + Harness.codex: {'vgv-wingspan': '0.0.5'}, + }, + snapshots: const { + Harness.codex: { + 'vgv-wingspan': PluginSnapshot( + tag: 'v0.0.5', + sha256: 'cafe', + ), + }, + }, + ), + fs, + ); + + expect(result.manifest.entries, isEmpty); + expect(result.manifest.plugins, isEmpty); + expect(result.manifest.snapshots, isEmpty); + }, + ); test('does nothing with an empty manifest', () { final result = auditor.reverse(InstallManifest(), fs); diff --git a/packages/vgv_ai_cli/test/src/model/install_manifest_test.dart b/packages/vgv_ai_cli/test/src/model/install_manifest_test.dart index cc87bc8b..f5904c3d 100644 --- a/packages/vgv_ai_cli/test/src/model/install_manifest_test.dart +++ b/packages/vgv_ai_cli/test/src/model/install_manifest_test.dart @@ -40,6 +40,8 @@ void main() { 'path': p.relative(entry.path, from: home), }; + const snapshot = PluginSnapshot(tag: 'v0.0.5', sha256: 'cafe'); + group(InstallManifest, () { test('round trips every kind of entry, and the plugin versions', () { final manifest = InstallManifest( @@ -48,6 +50,9 @@ void main() { Harness.claude: {'vgv-wingspan': '0.0.5'}, Harness.codex: {'vgv-ai-flutter-plugin': '0.0.6'}, }, + const { + Harness.claude: {'vgv-wingspan': snapshot}, + }, ); final decoded = InstallManifest.decode( @@ -85,6 +90,9 @@ void main() { Harness.claude: {'vgv-wingspan': '0.0.5'}, Harness.codex: {'vgv-ai-flutter-plugin': '0.0.6'}, }); + expect(decoded.snapshots, { + Harness.claude: {'vgv-wingspan': snapshot}, + }); }); test('encodes the version, plugins, owners and modes by name', () { @@ -94,6 +102,9 @@ void main() { const { Harness.codex: {'vgv-wingspan': '0.0.5'}, }, + const { + Harness.codex: {'vgv-wingspan': snapshot}, + }, ).encode(home: home), ) as Map; @@ -101,6 +112,11 @@ void main() { expect(json['plugins'], { 'codex': {'vgv-wingspan': '0.0.5'}, }); + expect(json['snapshots'], { + 'codex': { + 'vgv-wingspan': {'tag': 'v0.0.5', 'sha256': 'cafe'}, + }, + }); expect(json['entries'], [ { 'path': '.agents/skills/a/SKILL.md', @@ -163,6 +179,64 @@ void main() { ); }); + test('has no snapshots when they are missing or malformed', () { + InstallManifest decode(Object? snapshots) => InstallManifest.decode( + jsonEncode({ + 'version': InstallManifest.version, + 'plugins': { + 'codex': { + 'vgv-wingspan': '0.0.5', + 'vgv-ai-flutter-plugin': '0.0.5', + }, + }, + 'snapshots': snapshots, + 'entries': [], + }), + home: home, + )!; + + expect(decode(null).snapshots, isEmpty); + expect(decode(['vgv-wingspan']).snapshots, isEmpty); + expect(decode({'codex': 'vgv-wingspan'}).snapshots, isEmpty); + expect( + decode({ + 'nope': {'vgv-wingspan': snapshot.toJson()}, + }).snapshots, + isEmpty, + ); + expect( + decode({ + 'codex': { + 'vgv-wingspan': 'v0.0.5', + 'vgv-ai-flutter-plugin': snapshot.toJson(), + }, + }).snapshots, + { + Harness.codex: {'vgv-ai-flutter-plugin': snapshot}, + }, + ); + }); + + test('drops the snapshot of a plugin it does not record', () { + final manifest = InstallManifest( + const [], + const { + Harness.codex: {'vgv-wingspan': '0.0.5'}, + }, + const { + Harness.codex: { + 'vgv-wingspan': snapshot, + 'vgv-ai-flutter-plugin': snapshot, + }, + Harness.gemini: {'vgv-wingspan': snapshot}, + }, + ); + + expect(manifest.snapshots, { + Harness.codex: {'vgv-wingspan': snapshot}, + }); + }); + test('is null when a newer version wrote it', () { expect( InstallManifest.decode( @@ -270,12 +344,18 @@ void main() { const { Harness.codex: {'vgv-wingspan': '0.0.5'}, }, + const { + Harness.codex: {'vgv-wingspan': snapshot}, + }, ).record(updated); expect(manifest.entries, [updated, merged]); expect(manifest.plugins, { Harness.codex: {'vgv-wingspan': '0.0.5'}, }); + expect(manifest.snapshots, { + Harness.codex: {'vgv-wingspan': snapshot}, + }); }); test('drops the entries for the given paths', () { @@ -284,12 +364,18 @@ void main() { const { Harness.codex: {'vgv-wingspan': '0.0.5'}, }, + const { + Harness.codex: {'vgv-wingspan': snapshot}, + }, ).without([owned.path, sections.path]); expect(manifest.entries, [merged]); expect(manifest.plugins, { Harness.codex: {'vgv-wingspan': '0.0.5'}, }); + expect(manifest.snapshots, { + Harness.codex: {'vgv-wingspan': snapshot}, + }); }); test('replaces the plugin versions, keeping the entries', () { @@ -309,6 +395,50 @@ void main() { }); }); + test('replaces the snapshots with the plugin versions', () { + const newer = PluginSnapshot(tag: 'v0.0.6', sha256: 'beef'); + final manifest = InstallManifest( + const [owned], + const { + Harness.codex: {'vgv-wingspan': '0.0.5'}, + }, + const { + Harness.codex: {'vgv-wingspan': snapshot}, + }, + ); + + expect( + manifest + .withPlugins( + const { + Harness.codex: {'vgv-wingspan': '0.0.6'}, + }, + snapshots: const { + Harness.codex: {'vgv-wingspan': newer}, + }, + ) + .snapshots, + { + Harness.codex: {'vgv-wingspan': newer}, + }, + ); + expect( + manifest.withPlugins(const { + Harness.codex: {'vgv-wingspan': '0.0.5'}, + Harness.gemini: {'vgv-wingspan': '0.0.5'}, + }).snapshots, + { + Harness.codex: {'vgv-wingspan': snapshot}, + }, + reason: 'without new snapshots, it keeps the ones it records', + ); + expect( + manifest.withPlugins(const {}).snapshots, + isEmpty, + reason: 'a snapshot leaves with the plugin it describes', + ); + }); + test('equals a manifest with the same entries, in the same order', () { expect( InstallManifest(const [owned, merged]), @@ -342,6 +472,25 @@ void main() { ), ), ); + expect( + InstallManifest( + const [owned], + const { + Harness.codex: {'vgv-wingspan': '0.0.5'}, + }, + ), + isNot( + InstallManifest( + const [owned], + const { + Harness.codex: {'vgv-wingspan': '0.0.5'}, + }, + const { + Harness.codex: {'vgv-wingspan': snapshot}, + }, + ), + ), + ); }); test('has no entry for a path it has not recorded', () { diff --git a/packages/vgv_ai_cli/test/src/model/plugin_snapshot_test.dart b/packages/vgv_ai_cli/test/src/model/plugin_snapshot_test.dart new file mode 100644 index 00000000..348ae8ab --- /dev/null +++ b/packages/vgv_ai_cli/test/src/model/plugin_snapshot_test.dart @@ -0,0 +1,45 @@ +import 'package:file/memory.dart'; +import 'package:test/test.dart'; +import 'package:vgv_ai_cli/src/model/model.dart'; +import 'package:vgv_ai_plugins/vgv_ai_plugins.dart'; + +void main() { + group(PluginSnapshot, () { + const snapshot = PluginSnapshot(tag: 'v1.0.0', sha256: 'abc'); + + test('takes the tag and hash of a resolved plugin', () { + final directory = PluginDirectory( + plugin: const PluginRef( + name: 'vgv-wingspan', + repo: 'VeryGoodOpenSource/vgv-wingspan', + version: '1.0.0', + gitRef: 'v1.0.0', + ), + directory: MemoryFileSystem.test().directory('/plugin'), + sha256: 'abc', + ); + + expect(PluginSnapshot.of(directory), snapshot); + }); + + test('round trips through JSON', () { + expect(snapshot.toJson(), {'tag': 'v1.0.0', 'sha256': 'abc'}); + expect(PluginSnapshot.fromJson(snapshot.toJson()), snapshot); + }); + + test('is null for JSON that is not one', () { + expect(PluginSnapshot.fromJson(null), isNull); + expect(PluginSnapshot.fromJson('v1.0.0'), isNull); + expect(PluginSnapshot.fromJson({'tag': 'v1.0.0'}), isNull); + expect(PluginSnapshot.fromJson({'tag': 1, 'sha256': 'abc'}), isNull); + }); + + test('equals a snapshot with the same tag and hash', () { + expect(snapshot, const PluginSnapshot(tag: 'v1.0.0', sha256: 'abc')); + expect( + snapshot, + isNot(const PluginSnapshot(tag: 'v1.0.0', sha256: 'def')), + ); + }); + }); +} diff --git a/packages/vgv_ai_cli/test/src/resolve/content_hash_test.dart b/packages/vgv_ai_cli/test/src/resolve/content_hash_test.dart new file mode 100644 index 00000000..6fc66e57 --- /dev/null +++ b/packages/vgv_ai_cli/test/src/resolve/content_hash_test.dart @@ -0,0 +1,90 @@ +import 'package:file/file.dart'; +import 'package:file/memory.dart'; +import 'package:test/test.dart'; +import 'package:vgv_ai_cli/src/resolve/content_hash.dart'; + +void main() { + group('ContentHash', () { + late FileSystem fs; + late Directory plugin; + + setUp(() { + fs = MemoryFileSystem.test(); + plugin = fs.directory('/plugin')..createSync(); + }); + + void write(String path, String contents) => + fs.file(fs.path.join(plugin.path, path)) + ..createSync(recursive: true) + ..writeAsStringSync(contents); + + /// The hash of [plugin] after [change] is made to it. + String hashAfter(void Function() change) { + change(); + return plugin.contentHash(); + } + + setUp(() { + write('.claude-plugin/plugin.json', '{}'); + write('skills/a/SKILL.md', 'a'); + }); + + test('is a hex SHA-256', () { + expect(plugin.contentHash(), matches(RegExp(r'^[0-9a-f]{64}$'))); + }); + + test('is the same for the same files, wherever they are', () { + final copy = fs.directory('/elsewhere/plugin'); + for (final entity in plugin.listSync(recursive: true)) { + if (entity is! File) continue; + fs.file( + fs.path.join( + copy.path, + fs.path.relative(entity.path, from: plugin.path), + ), + ) + ..createSync(recursive: true) + ..writeAsBytesSync(entity.readAsBytesSync()); + } + + expect(copy.contentHash(), plugin.contentHash()); + }); + + test('changes when a file is edited, added, renamed or removed', () { + final hashes = [ + plugin.contentHash(), + hashAfter(() => write('skills/a/SKILL.md', 'b')), + hashAfter(() => write('skills/b/SKILL.md', 'b')), + hashAfter( + () => fs + .file('/plugin/skills/b/SKILL.md') + .renameSync('/plugin/skills/b/OTHER.md'), + ), + hashAfter(() => fs.file('/plugin/skills/b/OTHER.md').deleteSync()), + ]; + + expect(hashes.toSet(), hasLength(hashes.length - 1)); + expect(hashes.last, hashes[1]); + }); + + test('ignores empty directories and the .git directory', () { + final before = plugin.contentHash(); + + fs.directory('/plugin/empty').createSync(); + write('.git/HEAD', 'ref: refs/heads/main'); + + expect(plugin.contentHash(), before); + }); + + test('hashes a symlink by where it points', () { + write('shared/x.md', 'x'); + final link = fs.link('/plugin/skills/a/x.md') + ..createSync('../../shared/x.md'); + final before = plugin.contentHash(); + + link.updateSync('../../shared/y.md'); + + expect(plugin.contentHash(), isNot(before)); + }); + }); +} diff --git a/packages/vgv_ai_cli/test/src/resolve/plugin_resolver_test.dart b/packages/vgv_ai_cli/test/src/resolve/plugin_resolver_test.dart index 4cb05085..327b1c39 100644 --- a/packages/vgv_ai_cli/test/src/resolve/plugin_resolver_test.dart +++ b/packages/vgv_ai_cli/test/src/resolve/plugin_resolver_test.dart @@ -4,6 +4,7 @@ import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:test/test.dart'; import 'package:vgv_ai_cli/src/model/model.dart'; +import 'package:vgv_ai_cli/src/resolve/content_hash.dart'; import 'package:vgv_ai_cli/src/resolve/resolve.dart'; import 'package:vgv_ai_plugins/vgv_ai_plugins.dart'; @@ -141,6 +142,20 @@ void main() { ); }); + test('hashes what each plugin was resolved to', () async { + cache(wingspan); + final resolver = PluginResolver( + runProcess: processes.never, + parser: parser, + plugins: const [wingspan], + ); + + final resolution = await resolver.resolve(home); + + final directory = (resolution as ResolvedAssets).directories.single; + expect(directory.sha256, directory.directory.contentHash()); + }); + test('reports the entries each plugin references', () async { cache(wingspan); final resolver = PluginResolver( diff --git a/packages/vgv_ai_cli/test/src/translate/translator_test.dart b/packages/vgv_ai_cli/test/src/translate/translator_test.dart index 8481cf30..b64cb96e 100644 --- a/packages/vgv_ai_cli/test/src/translate/translator_test.dart +++ b/packages/vgv_ai_cli/test/src/translate/translator_test.dart @@ -597,6 +597,7 @@ void main() { gitRef: 'v1.0.0', ), directory: fs.directory('/cache/ffca'), + sha256: 'abc', referencedEntries: const ['references'], ), ]; diff --git a/packages/vgv_ai_cli/test/src/translate/translators/plugin_files_translator_test.dart b/packages/vgv_ai_cli/test/src/translate/translators/plugin_files_translator_test.dart index 6c39cac6..91cb6a7c 100644 --- a/packages/vgv_ai_cli/test/src/translate/translators/plugin_files_translator_test.dart +++ b/packages/vgv_ai_cli/test/src/translate/translators/plugin_files_translator_test.dart @@ -34,6 +34,7 @@ void main() { PluginDirectory( plugin: ffca, directory: plugin, + sha256: 'abc', referencedEntries: entries, ), ],