Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- `<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 `<feature>/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.
Expand Down Expand Up @@ -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/).
Expand Down
10 changes: 6 additions & 4 deletions packages/vgv_ai_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand Down
26 changes: 26 additions & 0 deletions packages/vgv_ai_cli/lib/src/commands/install_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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<Harness, Map<String, PluginSnapshot>> _snapshots(
InstallManifest manifest,
List<PluginDirectory> resolved,
Set<Harness> 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.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

Expand Down
90 changes: 77 additions & 13 deletions packages/vgv_ai_cli/lib/src/model/install_manifest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand All @@ -199,8 +200,29 @@ final class const MarkdownSections({
/// {@endtemplate}
class InstallManifest extends Equatable {
/// {@macro install_manifest}
new([Iterable<ManifestEntry> 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<ManifestEntry> entries = const [],
this.plugins = const {},
Map<Harness, Map<String, PluginSnapshot>> 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<Harness, Map<String, PluginSnapshot>> _recorded(
Map<Harness, Map<String, PluginSnapshot>> snapshots,
Map<Harness, Map<String, String>> 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
Expand All @@ -224,10 +246,14 @@ class InstallManifest extends Equatable {
'version': InstallManifest.version,
'entries': final List<dynamic> 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(),
};
Expand All @@ -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<Harness, Map<String, PluginSnapshot>> _snapshots(Object? json) {
if (json is! Map<String, dynamic>) return const {};
return {
for (final MapEntry(:key, :value) in json.entries)
if (Harness.values.asNameMap()[key] case final harness?)
if (value is Map<String, dynamic>)
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}) {
Expand All @@ -272,6 +316,14 @@ class InstallManifest extends Equatable {
/// plugin out of one harness while another is installed from it.
final Map<Harness, Map<String, String>> 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<Harness, Map<String, PluginSnapshot>> snapshots;

/// Every entry, in the order they were recorded.
Iterable<ManifestEntry> get entries => _entries.values;

Expand All @@ -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].
Expand All @@ -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<Harness, Map<String, String>> 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<Harness, Map<String, String>> plugins, {
Map<Harness, Map<String, PluginSnapshot>>? snapshots,
}) {
return InstallManifest(entries, plugins, snapshots ?? this.snapshots);
}

/// The manifest as the JSON [InstallManifest.decode] reads, with its
Expand All @@ -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)},
Expand All @@ -326,7 +390,7 @@ class InstallManifest extends Equatable {
}

@override
List<Object?> get props => [entries.toList(), plugins];
List<Object?> get props => [entries.toList(), plugins, snapshots];
}

/// The SHA-256 of [bytes], as hex.
Expand Down
1 change: 1 addition & 0 deletions packages/vgv_ai_cli/lib/src/model/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
8 changes: 6 additions & 2 deletions packages/vgv_ai_cli/lib/src/model/plugin_directory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<String> referencedEntries = const [],
Expand Down
44 changes: 44 additions & 0 deletions packages/vgv_ai_cli/lib/src/model/plugin_snapshot.dart
Original file line number Diff line number Diff line change
@@ -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<String, Object?> toJson() => {'tag': tag, 'sha256': sha256};

@override
List<Object?> get props => [tag, sha256];
}
46 changes: 46 additions & 0 deletions packages/vgv_ai_cli/lib/src/resolve/content_hash.dart
Original file line number Diff line number Diff line change
@@ -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 = <String, Digest>{
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<int>? _bytes(FileSystemEntity entity) => switch (entity) {
final File file => file.readAsBytesSync(),
final Link link => utf8.encode(link.targetSync()),
_ => null,
};
}
2 changes: 2 additions & 0 deletions packages/vgv_ai_cli/lib/src/resolve/plugin_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -100,6 +101,7 @@ class const PluginResolver({
PluginDirectory(
plugin: plugin,
directory: directory,
sha256: directory.contentHash(),
referencedEntries: assets.referencedEntries,
),
],
Expand Down
Loading
Loading