@rbbtsn0w/adg 0.11.1-beta.1 → 0.11.1-dev.pr200.c6ce45e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +131 -73
- package/dist/src/agents/antigravity.js +218 -45
- package/dist/src/agents/claude.js +98 -38
- package/dist/src/agents/codex.js +1 -0
- package/dist/src/cli/core-plugin-handlers.js +3 -3
- package/dist/src/cli/handlers.js +171 -39
- package/dist/src/cli/index.js +23 -14
- package/dist/src/cli/review-update.js +80 -0
- package/dist/src/commands/install/default-dsl.js +1 -1
- package/dist/src/commands/install/discovery.js +3 -1
- package/dist/src/commands/install/install-one.js +41 -31
- package/dist/src/commands/install.js +59 -24
- package/dist/src/commands/marketplace.js +171 -115
- package/dist/src/commands/migrate.js +2 -2
- package/dist/src/commands/projection.js +3 -1
- package/dist/src/commands/remove.js +10 -0
- package/dist/src/commands/review-executables.js +106 -0
- package/dist/src/commands/select-agents.js +2 -2
- package/dist/src/commands/select-components.js +2 -2
- package/dist/src/commands/select-plugins.js +15 -2
- package/dist/src/commands/select-scope.js +3 -4
- package/dist/src/commands/source-upgrade.js +552 -0
- package/dist/src/commands/state.js +3 -1
- package/dist/src/commands/update.js +3 -1
- package/dist/src/lock.js +13 -5
- package/dist/src/migration-transaction.js +1011 -0
- package/dist/src/native-retry.js +329 -0
- package/dist/src/process-identity.js +88 -0
- package/dist/src/render/plugins.js +149 -27
- package/dist/src/render/ui.js +4 -4
- package/dist/src/source-history.js +194 -0
- package/dist/src/source-intent.js +7 -0
- package/dist/src/store-mutex.js +179 -0
- package/dist/src/types.js +1 -1
- package/dist/src/update-executables.js +193 -0
- package/dist/src/upgrade-plan.js +378 -0
- package/docs/agents/antigravity.md +5 -0
- package/docs/agents-spec.md +10 -0
- package/docs/branching-and-release.md +369 -55
- package/docs/marketing/README.md +211 -0
- package/docs/marketing/assets.json +228 -0
- package/docs/marketing/capture.json +130 -0
- package/docs/marketing/live-readiness.json +353 -0
- package/docs/marketing/posts.en.md +94 -0
- package/docs/marketing/posts.zh-CN.md +67 -0
- package/docs/marketing/production.md +210 -0
- package/docs/marketing/source/timeline.json +29 -0
- package/docs/marketing/source/workflow-01.svg +16 -0
- package/docs/marketing/source/workflow-02.svg +14 -0
- package/docs/marketing/source/workflow-03.svg +19 -0
- package/docs/marketing/source/workflow-poster.svg +32 -0
- package/docs/marketing/transcript.md +48 -0
- package/docs/plugin-update-semantics.md +53 -10
- package/docs/plugin-upgrade-migrations.md +349 -0
- package/package.json +14 -10
- package/schemas/plugin-lock.schema.json +78 -1
package/dist/src/render/ui.js
CHANGED
|
@@ -17,16 +17,16 @@ export const ui = {
|
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* Lay items out in aligned columns sized to the terminal width (row-major).
|
|
20
|
-
* Items longer than `maxColWidth` are truncated with an ellipsis.
|
|
21
|
-
* a single column on narrow terminals.
|
|
20
|
+
* Items longer than `maxColWidth` are truncated with an ellipsis. Use false to
|
|
21
|
+
* keep full labels. Falls back to a single column on narrow terminals.
|
|
22
22
|
*/
|
|
23
23
|
export function formatColumns(items, opts = {}) {
|
|
24
24
|
const indent = opts.indent ?? 2;
|
|
25
25
|
const gutter = opts.gutter ?? 2;
|
|
26
26
|
const maxColWidth = opts.maxColWidth ?? 24;
|
|
27
27
|
const termWidth = opts.width ?? process.stdout.columns ?? 80;
|
|
28
|
-
const cells = items.map((s) =>
|
|
29
|
-
const colWidth =
|
|
28
|
+
const cells = maxColWidth === false ? items : items.map((s) => s.length > maxColWidth ? s.slice(0, maxColWidth - 1) + "…" : s);
|
|
29
|
+
const colWidth = cells.reduce((width, cell) => Math.max(width, cell.length), 1);
|
|
30
30
|
const cols = Math.max(1, Math.floor((termWidth - indent + gutter) / (colWidth + gutter)));
|
|
31
31
|
const lines = [];
|
|
32
32
|
for (let i = 0; i < cells.length; i += cols) {
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { exposedContents, pluginContents, presentComponents } from "./components.js";
|
|
5
|
+
import { folderHash } from "./hash.js";
|
|
6
|
+
import { resolveSkillEntries } from "./skills.js";
|
|
7
|
+
import { readManifest } from "./manifest.js";
|
|
8
|
+
import { effectivePackageFilter } from "./materialize.js";
|
|
9
|
+
import { PROJECTION_DIRS } from "./package.js";
|
|
10
|
+
import { installedPluginDir, pluginSourceCacheDir } from "./paths.js";
|
|
11
|
+
import { materializeSource } from "./remote/client.js";
|
|
12
|
+
import { parseGitHubSource } from "./sources.js";
|
|
13
|
+
import { resolveSelectionDependencies } from "./types.js";
|
|
14
|
+
import { recordTelemetryEvent } from "./telemetry.js";
|
|
15
|
+
import { discoverPlugins } from "./commands/install/discovery.js";
|
|
16
|
+
import { contentHash } from "./commands/install/install-one.js";
|
|
17
|
+
const SOURCE_CATALOGS = [".agents/.marketplace.json", ".agents/plugins/marketplace.json", ".claude-plugin/marketplace.json"];
|
|
18
|
+
/** A missing legacy full-selection field cannot establish requested intent. */
|
|
19
|
+
export function requestedSelectionKnown(entry) {
|
|
20
|
+
return entry.selectionKnown === true || (entry.selection !== undefined && entry.selectionKnown !== false);
|
|
21
|
+
}
|
|
22
|
+
function effectivePresence(contents) {
|
|
23
|
+
return { components: presentComponents(contents),
|
|
24
|
+
...(contents.skills.length ? { skills: [...contents.skills] } : {}),
|
|
25
|
+
...(contents.mcp.length ? { mcp: [...contents.mcp] } : {}) };
|
|
26
|
+
}
|
|
27
|
+
function verifiedCache(dir, expected) {
|
|
28
|
+
try {
|
|
29
|
+
const manifest = readManifest(dir);
|
|
30
|
+
if (contentHash(dir, manifest) !== expected)
|
|
31
|
+
return undefined;
|
|
32
|
+
return { manifest, contents: pluginContents(dir, manifest) };
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function hasVisibleFile(path) {
|
|
39
|
+
if (!existsSync(path))
|
|
40
|
+
return false;
|
|
41
|
+
if (statSync(path).isFile())
|
|
42
|
+
return true;
|
|
43
|
+
return readdirSync(path, { withFileTypes: true }).some((entry) => !entry.name.startsWith(".")
|
|
44
|
+
&& (entry.isFile() || entry.isDirectory() && hasVisibleFile(join(path, entry.name))));
|
|
45
|
+
}
|
|
46
|
+
function verifiedInstalled(dir, expected) {
|
|
47
|
+
try {
|
|
48
|
+
const manifest = readManifest(dir);
|
|
49
|
+
// The materialized manifest already describes the selected payload. Using
|
|
50
|
+
// requested policy here would conceal bytes when that legacy policy is lost.
|
|
51
|
+
if (folderHash(dir, PROJECTION_DIRS, effectivePackageFilter(manifest)) !== expected)
|
|
52
|
+
return undefined;
|
|
53
|
+
const contents = pluginContents(dir, manifest);
|
|
54
|
+
contents.skills = resolveSkillEntries(dir, manifest)
|
|
55
|
+
.filter((entry) => entry.skillMd && statSync(entry.skillMd).isFile()).map((entry) => entry.name).sort();
|
|
56
|
+
for (const component of ["agents", "commands", "hooks", "apps"]) {
|
|
57
|
+
const path = manifest[component];
|
|
58
|
+
if (!path || !hasVisibleFile(join(dir, path)))
|
|
59
|
+
contents[component] = [];
|
|
60
|
+
}
|
|
61
|
+
return { manifest, contents };
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** Rebuilding projections is not permission to broaden an unknown legacy choice. */
|
|
68
|
+
export function preservedSelection(store, name, entry) {
|
|
69
|
+
if (requestedSelectionKnown(entry))
|
|
70
|
+
return entry.selection;
|
|
71
|
+
const installed = verifiedInstalled(installedPluginDir(store, name, entry.origin), entry.installedHash);
|
|
72
|
+
if (!installed)
|
|
73
|
+
throw new Error(`Cannot verify the installed selection of ${name}; review its selection before rebuilding.`);
|
|
74
|
+
return effectivePresence(installed.contents);
|
|
75
|
+
}
|
|
76
|
+
/** Verified effective bytes can establish existing permissions when the cache is damaged. */
|
|
77
|
+
export function verifiedInstalledPayload(store, name, entry) {
|
|
78
|
+
const dir = installedPluginDir(store, name, entry.origin);
|
|
79
|
+
return verifiedInstalled(dir, entry.installedHash) ? dir : undefined;
|
|
80
|
+
}
|
|
81
|
+
export function installedHistory(store, lock, source, ref) {
|
|
82
|
+
return Object.entries(lock.plugins).flatMap(([name, entry]) => {
|
|
83
|
+
if (entry.origin.type !== "github" || entry.origin.repo.toLowerCase() !== source.toLowerCase() || entry.origin.ref !== ref)
|
|
84
|
+
return [];
|
|
85
|
+
// Source availability and actual installed selection are independent evidence.
|
|
86
|
+
const cached = verifiedCache(pluginSourceCacheDir(store, name), entry.sourceHash);
|
|
87
|
+
const installed = verifiedInstalled(installedPluginDir(store, name, entry.origin), entry.installedHash);
|
|
88
|
+
const selectionKnown = requestedSelectionKnown(entry);
|
|
89
|
+
const evidence = !cached && !installed ? "none" : selectionKnown ? "selection" : installed ? "installed" : "catalog";
|
|
90
|
+
recordTelemetryEvent("adg.update.history.evidence", { "cache.verified": Boolean(cached), "installed.verified": Boolean(installed),
|
|
91
|
+
"selection.known": selectionKnown, "evidence.kind": evidence });
|
|
92
|
+
if (!cached && !installed)
|
|
93
|
+
return [];
|
|
94
|
+
const inventory = cached ?? installed;
|
|
95
|
+
return [{ name, version: entry.version, origin: entry.origin, sourceHash: entry.sourceHash,
|
|
96
|
+
contents: inventory.contents,
|
|
97
|
+
...(selectionKnown && entry.selection !== undefined ? { selection: entry.selection } : {}),
|
|
98
|
+
selectionKnown,
|
|
99
|
+
effectiveSelection: selectionKnown
|
|
100
|
+
? resolveSelectionDependencies(inventory.manifest, entry.selection)
|
|
101
|
+
: installed ? effectivePresence(installed.contents) : { components: [] },
|
|
102
|
+
state: entry.state,
|
|
103
|
+
evidence: selectionKnown ? "selection" : installed ? "installed" : "catalog" }];
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/** Read native catalog metadata without adding a publisher-owned control file. */
|
|
107
|
+
export function marketplaceRenames(checkout) {
|
|
108
|
+
const merged = {};
|
|
109
|
+
for (const file of SOURCE_CATALOGS) {
|
|
110
|
+
const path = join(checkout, file);
|
|
111
|
+
if (!existsSync(path))
|
|
112
|
+
continue;
|
|
113
|
+
const raw = JSON.parse(readFileSync(path, "utf8"));
|
|
114
|
+
if (raw.renames === undefined)
|
|
115
|
+
continue;
|
|
116
|
+
if (!raw.renames || typeof raw.renames !== "object" || Array.isArray(raw.renames))
|
|
117
|
+
throw new Error("Invalid marketplace renames map");
|
|
118
|
+
for (const [name, target] of Object.entries(raw.renames)) {
|
|
119
|
+
if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(name) || target !== null && (typeof target !== "string" || !/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(target))) {
|
|
120
|
+
throw new Error("Invalid marketplace rename identity");
|
|
121
|
+
}
|
|
122
|
+
if (Object.hasOwn(merged, name) && merged[name] !== target)
|
|
123
|
+
throw new Error(`Conflicting marketplace rename: ${name}`);
|
|
124
|
+
merged[name] = target;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return merged;
|
|
128
|
+
}
|
|
129
|
+
/** A published catalog can withdraw a plugin while retaining its source files. */
|
|
130
|
+
export function sourceCatalogNames(checkout) {
|
|
131
|
+
for (const file of SOURCE_CATALOGS) {
|
|
132
|
+
const path = join(checkout, file);
|
|
133
|
+
if (!existsSync(path))
|
|
134
|
+
continue;
|
|
135
|
+
const raw = JSON.parse(readFileSync(path, "utf8"));
|
|
136
|
+
if (typeof raw.name !== "string" || !Array.isArray(raw.plugins))
|
|
137
|
+
throw new Error("Invalid source marketplace catalog");
|
|
138
|
+
const names = raw.plugins.map((entry) => {
|
|
139
|
+
if (!entry || typeof entry !== "object" || !("name" in entry) || typeof entry.name !== "string")
|
|
140
|
+
throw new Error("Invalid source marketplace entry");
|
|
141
|
+
return entry.name;
|
|
142
|
+
});
|
|
143
|
+
if (new Set(names).size !== names.length)
|
|
144
|
+
throw new Error("Duplicate source marketplace identity");
|
|
145
|
+
return new Set(names);
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
/** Recover overwritten legacy inventories from the predecessor's fixed source. */
|
|
150
|
+
export async function recoverLegacyHistory(options) {
|
|
151
|
+
const anchors = options.installed.filter(p => !options.currentNames.has(p.name)
|
|
152
|
+
&& options.installed.some(peer => peer.version !== p.version));
|
|
153
|
+
for (const anchor of anchors) {
|
|
154
|
+
const entry = options.lock.plugins[anchor.name];
|
|
155
|
+
const ref = entry.resolvedRevision || (/^\d+\.\d+\.\d+(?:-[\w.-]+)?$/.test(entry.version) ? `v${entry.version}` : undefined);
|
|
156
|
+
if (!ref)
|
|
157
|
+
continue;
|
|
158
|
+
const checkout = mkdtempSync(join(tmpdir(), "adg-source-history-"));
|
|
159
|
+
try {
|
|
160
|
+
const parsed = parseGitHubSource(options.source);
|
|
161
|
+
await materializeSource({ ...parsed, ref }, checkout, { runner: options.runner,
|
|
162
|
+
...(entry.resolvedRevision ? { resolvedRevision: entry.resolvedRevision } : {}), timeoutMs: 10_000 });
|
|
163
|
+
const { candidates } = discoverPlugins(checkout);
|
|
164
|
+
const predecessor = candidates.get(anchor.name);
|
|
165
|
+
if (!predecessor || predecessor.manifest.version !== anchor.version || contentHash(predecessor.dir, predecessor.manifest) !== anchor.sourceHash)
|
|
166
|
+
continue;
|
|
167
|
+
const recovered = options.installed.flatMap(current => {
|
|
168
|
+
const old = candidates.get(current.name);
|
|
169
|
+
if (!old)
|
|
170
|
+
return [];
|
|
171
|
+
const contents = pluginContents(old.dir, old.manifest);
|
|
172
|
+
const knownPartial = requestedSelectionKnown(current) && current.selection !== undefined;
|
|
173
|
+
const bound = knownPartial ? current.effectiveSelection ?? current.selection
|
|
174
|
+
: current.evidence === "installed" ? current.effectiveSelection : undefined;
|
|
175
|
+
return [{ name: current.name, origin: current.origin, version: old.manifest.version,
|
|
176
|
+
sourceHash: contentHash(old.dir, old.manifest), contents,
|
|
177
|
+
...(knownPartial ? { selection: current.selection } : {}), selectionKnown: knownPartial,
|
|
178
|
+
effectiveSelection: bound ? effectivePresence(exposedContents(contents, bound)) : { components: [] },
|
|
179
|
+
// The old release proves availability. It cannot establish which of
|
|
180
|
+
// those historical capabilities this user ever requested or installed.
|
|
181
|
+
state: current.state, evidence: "catalog", uncertain: true }];
|
|
182
|
+
});
|
|
183
|
+
recordTelemetryEvent("adg.update.history.recovered", { "plugins.count": recovered.length });
|
|
184
|
+
return recovered;
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
recordTelemetryEvent("adg.update.history.unavailable", { "history.available": false });
|
|
188
|
+
}
|
|
189
|
+
finally {
|
|
190
|
+
rmSync(checkout, { recursive: true, force: true });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return [];
|
|
194
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export function sourceIntent(lock, source, ref) {
|
|
2
|
+
return lock.sources?.find((entry) => entry.source.toLowerCase() === source.toLowerCase() && entry.ref === ref);
|
|
3
|
+
}
|
|
4
|
+
export function setSourceIntent(lock, intent) {
|
|
5
|
+
const previous = sourceIntent(lock, intent.source, intent.ref);
|
|
6
|
+
lock.sources = [...(lock.sources ?? []).filter((entry) => entry !== previous), intent];
|
|
7
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { getCurrentProcessIdentity, queryProcessIdentity } from "./process-identity.js";
|
|
7
|
+
import { recordTelemetryEvent } from "./telemetry.js";
|
|
8
|
+
const ownership = new AsyncLocalStorage();
|
|
9
|
+
const defaultMutexDir = join(tmpdir(), `adg-mutation-${process.getuid?.() ?? "user"}.lock`);
|
|
10
|
+
function alive(pid) {
|
|
11
|
+
try {
|
|
12
|
+
process.kill(pid, 0);
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
return error.code !== "ESRCH";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function ownerIdentity(path, pid) {
|
|
20
|
+
let raw;
|
|
21
|
+
try {
|
|
22
|
+
raw = readFileSync(join(path, "owner.json"), "utf8");
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error.code === "ENOENT")
|
|
26
|
+
return undefined; // Legacy contender.
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const owner = JSON.parse(raw);
|
|
31
|
+
if (owner?.version !== 1 || owner.pid !== pid || owner.identity?.version !== 1
|
|
32
|
+
|| typeof owner.identity.kind !== "string" || !owner.identity.kind
|
|
33
|
+
|| typeof owner.identity.birthToken !== "string" || !owner.identity.birthToken)
|
|
34
|
+
throw new Error();
|
|
35
|
+
return owner.identity;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
throw new Error("Invalid ADG mutation owner record; inspect stale mutex records before retrying.");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function transientWindowsAccess(error) {
|
|
42
|
+
const code = error.code;
|
|
43
|
+
return process.platform === "win32" && (code === "EPERM" || code === "EACCES");
|
|
44
|
+
}
|
|
45
|
+
function contenderPresence(path) {
|
|
46
|
+
try {
|
|
47
|
+
lstatSync(path);
|
|
48
|
+
return "present";
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
if (error.code === "ENOENT")
|
|
52
|
+
return "absent";
|
|
53
|
+
// Windows delete-pending records can deny both reads and metadata access.
|
|
54
|
+
// Uncertain presence must block; existsSync would incorrectly report false.
|
|
55
|
+
if (transientWindowsAccess(error))
|
|
56
|
+
return "unknown";
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function contenders(root) {
|
|
61
|
+
return readdirSync(root).flatMap((id) => {
|
|
62
|
+
const match = id.match(/^(\d+)-[a-f0-9-]{36}$/);
|
|
63
|
+
if (!match)
|
|
64
|
+
return [];
|
|
65
|
+
const path = join(root, id);
|
|
66
|
+
// Names are unique for the life of a contender. Reaping this path can never
|
|
67
|
+
// unlink the record of a replacement holder, unlike a shared lock filename.
|
|
68
|
+
if (!alive(Number(match[1]))) {
|
|
69
|
+
rmSync(path, { recursive: true, force: true });
|
|
70
|
+
recordTelemetryEvent("adg.store.mutex.reclaimed", { reason: "process_exit" });
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
const pid = Number(match[1]);
|
|
74
|
+
let identity;
|
|
75
|
+
try {
|
|
76
|
+
identity = ownerIdentity(path, pid);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (transientWindowsAccess(error))
|
|
80
|
+
return [{ id, pid, unreadable: true }];
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
const ticket = JSON.parse(readFileSync(join(path, "ticket.json"), "utf8"));
|
|
85
|
+
if (typeof ticket !== "number" || !Number.isSafeInteger(ticket) || ticket <= 0) {
|
|
86
|
+
throw new Error("Invalid ADG mutation ticket");
|
|
87
|
+
}
|
|
88
|
+
return [{ id, pid, identity, ticket }];
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
if (error.code === "ENOENT" || transientWindowsAccess(error)) {
|
|
92
|
+
const presence = contenderPresence(path);
|
|
93
|
+
if (presence === "absent")
|
|
94
|
+
return [];
|
|
95
|
+
// Only ENOENT with confirmed directory presence proves the choosing
|
|
96
|
+
// phase. An unreadable published ticket may belong to an active holder.
|
|
97
|
+
return [{ id, pid, identity, unreadable: transientWindowsAccess(error) || presence === "unknown" }];
|
|
98
|
+
}
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/** Serialize stores and shared native state using Lamport's bakery algorithm.
|
|
104
|
+
* Every contender publishes an immutable ticket atomically. A dead process is
|
|
105
|
+
* reclaimed by its unique name, including crashes before ticket publication.
|
|
106
|
+
* Nested helpers share ownership; invoke internal helpers instead of child ADG
|
|
107
|
+
* mutators while holding the mutex.
|
|
108
|
+
*/
|
|
109
|
+
export async function withStoreMutex(_pluginsDir, action, timeoutMs = 30_000) {
|
|
110
|
+
if (ownership.getStore())
|
|
111
|
+
return await action();
|
|
112
|
+
const root = process.env.ADG_MUTEX_HOME || defaultMutexDir;
|
|
113
|
+
mkdirSync(root, { recursive: true, mode: 0o700 });
|
|
114
|
+
const stat = lstatSync(root);
|
|
115
|
+
if (!stat.isDirectory() || stat.isSymbolicLink() || (process.getuid && stat.uid !== process.getuid()) || (process.platform !== "win32" && (stat.mode & 0o077))) {
|
|
116
|
+
throw new Error("ADG mutation mutex must be a private directory owned by the current user");
|
|
117
|
+
}
|
|
118
|
+
const id = `${process.pid}-${randomUUID()}`;
|
|
119
|
+
const path = join(root, id);
|
|
120
|
+
const identity = getCurrentProcessIdentity();
|
|
121
|
+
const staging = join(root, `.preparing-${id}`);
|
|
122
|
+
mkdirSync(staging, { mode: 0o700 });
|
|
123
|
+
try {
|
|
124
|
+
writeFileSync(join(staging, "owner.json"), JSON.stringify({ version: 1, pid: process.pid, identity }), { mode: 0o600 });
|
|
125
|
+
// Publish a complete owner atomically; old clients still recognize pid-UUID.
|
|
126
|
+
renameSync(staging, path);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
rmSync(staging, { recursive: true, force: true });
|
|
130
|
+
}
|
|
131
|
+
const started = Date.now();
|
|
132
|
+
// OS birth queries can launch PowerShell. Only actual blockers need them;
|
|
133
|
+
// a stale high ticket merely overestimates our ticket and remains safe.
|
|
134
|
+
const checked = new Map();
|
|
135
|
+
const reclaimReusedBlocker = (other) => {
|
|
136
|
+
if (!other.identity || other.id === id)
|
|
137
|
+
return false;
|
|
138
|
+
const last = checked.get(other.id);
|
|
139
|
+
if (last !== undefined && Date.now() - last < 1000)
|
|
140
|
+
return false;
|
|
141
|
+
const current = queryProcessIdentity(other.pid);
|
|
142
|
+
checked.set(other.id, Date.now());
|
|
143
|
+
if (!current || other.identity.kind !== current.kind || other.identity.version !== current.version
|
|
144
|
+
|| other.identity.birthToken === current.birthToken)
|
|
145
|
+
return false;
|
|
146
|
+
rmSync(join(root, other.id), { recursive: true, force: true });
|
|
147
|
+
recordTelemetryEvent("adg.store.mutex.reclaimed", { reason: "process_identity_changed" });
|
|
148
|
+
return true;
|
|
149
|
+
};
|
|
150
|
+
const wait = async () => {
|
|
151
|
+
if (Date.now() - started >= timeoutMs)
|
|
152
|
+
throw new Error("Another ADG mutation is still running; retry after it completes.");
|
|
153
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
154
|
+
};
|
|
155
|
+
try {
|
|
156
|
+
let snapshot = contenders(root);
|
|
157
|
+
// Do not choose a smaller ticket while a holder's published ticket might be
|
|
158
|
+
// hidden. Genuine choosing contenders stay eligible to choose concurrently.
|
|
159
|
+
while (snapshot.some(entry => entry.unreadable && !reclaimReusedBlocker(entry))) {
|
|
160
|
+
await wait();
|
|
161
|
+
snapshot = contenders(root);
|
|
162
|
+
}
|
|
163
|
+
const ticket = 1 + Math.max(0, ...snapshot.map((entry) => entry.ticket ?? 0));
|
|
164
|
+
writeFileSync(join(path, "ticket.tmp"), JSON.stringify(ticket), { mode: 0o600 });
|
|
165
|
+
renameSync(join(path, "ticket.tmp"), join(path, "ticket.json"));
|
|
166
|
+
for (;;) {
|
|
167
|
+
const earlier = contenders(root).some((other) => other.id !== id && (other.ticket === undefined
|
|
168
|
+
|| other.ticket < ticket || (other.ticket === ticket && other.id < id)) && !reclaimReusedBlocker(other));
|
|
169
|
+
if (!earlier)
|
|
170
|
+
break;
|
|
171
|
+
await wait();
|
|
172
|
+
}
|
|
173
|
+
recordTelemetryEvent("adg.store.mutex.acquired", { "wait.ms": Date.now() - started });
|
|
174
|
+
return await ownership.run(true, action);
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
rmSync(path, { recursive: true, force: true });
|
|
178
|
+
}
|
|
179
|
+
}
|
package/dist/src/types.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* ADG type definitions mirroring the JSON Schemas under ../schemas.
|
|
3
3
|
*/
|
|
4
4
|
export const ADG_SCHEMA_VERSION = "adg.plugin/v1";
|
|
5
|
-
export const LOCK_VERSION =
|
|
5
|
+
export const LOCK_VERSION = 6;
|
|
6
6
|
/** The component categories a plugin can expose. */
|
|
7
7
|
export const COMPONENT_TYPES = ["skills", "agents", "commands", "mcp", "hooks", "apps"];
|
|
8
8
|
/** Canonicalize user intent without adding derived component dependencies. */
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { cpSync, existsSync, lstatSync, mkdtempSync, readFileSync, realpathSync, rmSync, statSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
5
|
+
import { ADAPTER_TARGETS } from "./adapters/index.js";
|
|
6
|
+
import { resolveClaudeHooksFile } from "./adapters/anthropic.js";
|
|
7
|
+
import { resolveCodexHooksFile } from "./adapters/codex.js";
|
|
8
|
+
import { writeAntigravityHooks } from "./adapters/antigravity-hooks.js";
|
|
9
|
+
import { writeAntigravityMcpConfig } from "./adapters/antigravity.js";
|
|
10
|
+
import { isExposed } from "./components.js";
|
|
11
|
+
import { extractMcpServers, mcpConfigPath } from "./mcp.js";
|
|
12
|
+
import { recordTelemetryEvent } from "./telemetry.js";
|
|
13
|
+
function record(value) {
|
|
14
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
15
|
+
}
|
|
16
|
+
function canonical(value) {
|
|
17
|
+
if (Array.isArray(value))
|
|
18
|
+
return `[${value.map(canonical).join(",")}]`;
|
|
19
|
+
if (record(value))
|
|
20
|
+
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(",")}}`;
|
|
21
|
+
return JSON.stringify(value);
|
|
22
|
+
}
|
|
23
|
+
function digest(value) {
|
|
24
|
+
return createHash("sha256").update(canonical(value)).digest("hex");
|
|
25
|
+
}
|
|
26
|
+
function configFile(dir, file) {
|
|
27
|
+
const path = resolve(dir, file);
|
|
28
|
+
const rel = relative(realpathSync(dir), existsSync(path) ? realpathSync(path) : path);
|
|
29
|
+
if (isAbsolute(rel) || rel === ".." || rel.startsWith(`..${sep}`))
|
|
30
|
+
throw new Error("Executable config must remain inside its plugin");
|
|
31
|
+
try {
|
|
32
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
throw new Error("Executable config is missing or invalid JSON");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function validateHandler(value) {
|
|
39
|
+
if (!record(value))
|
|
40
|
+
throw new Error("Invalid executable hook handler");
|
|
41
|
+
const type = value.type ?? "command";
|
|
42
|
+
const fields = {
|
|
43
|
+
command: ["command"], http: ["url"], prompt: ["prompt"], agent: ["prompt"], mcp_tool: ["server", "tool"],
|
|
44
|
+
};
|
|
45
|
+
const required = typeof type === "string" && Object.hasOwn(fields, type) ? fields[type] : undefined;
|
|
46
|
+
if (!required || !required.every(key => typeof value[key] === "string" && value[key].trim())) {
|
|
47
|
+
throw new Error("Invalid executable hook handler");
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Inventory effective runtime configs, without executing their commands or mutating the source. */
|
|
51
|
+
export function executableInventory(dir, manifest, selection, targets = [...ADAPTER_TARGETS]) {
|
|
52
|
+
const entries = new Map();
|
|
53
|
+
const occurrences = new Map();
|
|
54
|
+
const add = (target, component, member, value, label) => {
|
|
55
|
+
const identity = digest({ target, component, member, value });
|
|
56
|
+
const occurrence = occurrences.get(identity) ?? 0;
|
|
57
|
+
occurrences.set(identity, occurrence + 1);
|
|
58
|
+
const id = `sha256-${digest({ identity, occurrence })}`;
|
|
59
|
+
entries.set(id, { id, component, member, label: `${target} ${label}` });
|
|
60
|
+
};
|
|
61
|
+
const mcp = (root, file, target) => {
|
|
62
|
+
const document = configFile(root, file);
|
|
63
|
+
const servers = extractMcpServers(document);
|
|
64
|
+
if (!servers)
|
|
65
|
+
throw new Error("Invalid executable MCP config");
|
|
66
|
+
for (const name of Object.keys(servers).sort()) {
|
|
67
|
+
if (selection?.mcp && !selection.mcp.includes(name))
|
|
68
|
+
continue;
|
|
69
|
+
const server = servers[name];
|
|
70
|
+
if (!record(server) || !["command", "url", "serverUrl"].some((key) => typeof server[key] === "string" && server[key].trim())) {
|
|
71
|
+
throw new Error("Invalid executable MCP server");
|
|
72
|
+
}
|
|
73
|
+
const safeName = /^[a-z][a-z0-9]*(?:[-_.][a-z0-9]+)*$/i.test(name) && name.length <= 40
|
|
74
|
+
&& !/(?:token|secret|password|credential)|[a-f0-9]{20}/i.test(name) ? name : "configured service";
|
|
75
|
+
add(target, "mcp", name, server, `MCP service ${safeName}`);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const hooks = (root, file, target, native = false, validateOnly = false) => {
|
|
79
|
+
const document = configFile(root, file);
|
|
80
|
+
if (!record(document))
|
|
81
|
+
throw new Error("Invalid executable hooks config");
|
|
82
|
+
const shared = Object.fromEntries(Object.entries(document).filter(([key]) => key !== "hooks"));
|
|
83
|
+
const consume = (handler, event, context) => {
|
|
84
|
+
validateHandler(handler);
|
|
85
|
+
const eventLabel = /^(?:SessionStart|PreInvocation|PreToolUse|PostToolUse|Stop|UserPromptSubmit|SessionEnd|Notification|PermissionRequest|SubagentStart|SubagentStop|PreCompact|Setup|TaskCompleted|TeammateIdle|ConfigChange|WorktreeCreate|WorktreeRemove)$/.test(event)
|
|
86
|
+
? event : "configured event";
|
|
87
|
+
if (!validateOnly)
|
|
88
|
+
add(target, "hooks", file.replace(/^\.\//, ""), { event, context, handler }, `${eventLabel} hook`);
|
|
89
|
+
};
|
|
90
|
+
if (native) {
|
|
91
|
+
for (const definition of Object.values(document)) {
|
|
92
|
+
if (!record(definition))
|
|
93
|
+
throw new Error("Invalid native executable hooks config");
|
|
94
|
+
for (const [event, groups] of Object.entries(definition)) {
|
|
95
|
+
if (event === "enabled")
|
|
96
|
+
continue;
|
|
97
|
+
if (!Array.isArray(groups))
|
|
98
|
+
throw new Error("Invalid native executable hooks event");
|
|
99
|
+
for (const item of groups) {
|
|
100
|
+
if (record(item) && Array.isArray(item.hooks))
|
|
101
|
+
for (const handler of item.hooks)
|
|
102
|
+
consume(handler, event, {
|
|
103
|
+
settings: Object.fromEntries(Object.entries(item).filter(([key]) => key !== "hooks")), enabled: definition.enabled,
|
|
104
|
+
});
|
|
105
|
+
else
|
|
106
|
+
consume(item, event, { enabled: definition.enabled });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
if (!record(document.hooks))
|
|
113
|
+
throw new Error("Invalid executable hooks event map");
|
|
114
|
+
for (const [event, groups] of Object.entries(document.hooks)) {
|
|
115
|
+
if (!Array.isArray(groups))
|
|
116
|
+
throw new Error("Invalid executable hooks event");
|
|
117
|
+
for (const group of groups) {
|
|
118
|
+
if (!record(group) || !Array.isArray(group.hooks))
|
|
119
|
+
throw new Error("Invalid executable hooks group");
|
|
120
|
+
for (const handler of group.hooks)
|
|
121
|
+
consume(handler, event, {
|
|
122
|
+
shared, group: Object.fromEntries(Object.entries(group).filter(([key]) => key !== "hooks")),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
for (const target of new Set(targets)) {
|
|
129
|
+
if (!ADAPTER_TARGETS.includes(target))
|
|
130
|
+
throw new Error("Unsupported executable runtime target");
|
|
131
|
+
if (target === "antigravity") {
|
|
132
|
+
if (!(isExposed(selection, "hooks") && manifest.hooks) && !(isExposed(selection, "mcp") && mcpConfigPath(manifest)))
|
|
133
|
+
continue;
|
|
134
|
+
// Reuse the actual adapter's native overrides and alias projection. Stable
|
|
135
|
+
// runtimeRoot keeps identical payloads independent of staging locations.
|
|
136
|
+
const temporary = mkdtempSync(join(tmpdir(), "adg-executable-inventory-"));
|
|
137
|
+
try {
|
|
138
|
+
const scratch = join(temporary, "plugin");
|
|
139
|
+
cpSync(dir, scratch, { recursive: true, filter(source) {
|
|
140
|
+
if (lstatSync(source).isSymbolicLink())
|
|
141
|
+
throw new Error("Cannot verify symlinked executable payload");
|
|
142
|
+
return relative(dir, source).split(sep)[0] !== ".git";
|
|
143
|
+
} });
|
|
144
|
+
if (isExposed(selection, "hooks") && manifest.hooks) {
|
|
145
|
+
const declared = resolve(scratch, manifest.hooks);
|
|
146
|
+
const directory = existsSync(declared) && statSync(declared).isDirectory();
|
|
147
|
+
const native = directory ? join(declared, "hooks-antigravity.json") : undefined;
|
|
148
|
+
if (!native || !existsSync(native)) {
|
|
149
|
+
const source = directory ? join(declared, "hooks.json") : declared;
|
|
150
|
+
if (!source.endsWith("hooks-antigravity.json")) {
|
|
151
|
+
hooks(scratch, relative(scratch, source), target, source === join(scratch, "hooks.json"), true);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
writeAntigravityHooks(scratch, manifest, selection, "/ADG_PLUGIN_ROOT");
|
|
155
|
+
if (existsSync(join(scratch, "hooks.json")))
|
|
156
|
+
hooks(scratch, "hooks.json", target, true);
|
|
157
|
+
}
|
|
158
|
+
if (isExposed(selection, "mcp") && mcpConfigPath(manifest)) {
|
|
159
|
+
writeAntigravityMcpConfig(scratch, manifest, selection);
|
|
160
|
+
mcp(scratch, "mcp_config.json", target);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
throw new Error("Cannot verify effective Antigravity executable config");
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
rmSync(temporary, { recursive: true, force: true });
|
|
168
|
+
}
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (isExposed(selection, "mcp") && mcpConfigPath(manifest))
|
|
172
|
+
mcp(dir, mcpConfigPath(manifest), target);
|
|
173
|
+
if (isExposed(selection, "hooks")) {
|
|
174
|
+
const files = new Set();
|
|
175
|
+
if (target === "claude" && existsSync(join(dir, "hooks/hooks.json")))
|
|
176
|
+
files.add("hooks/hooks.json");
|
|
177
|
+
if (manifest.hooks) {
|
|
178
|
+
const file = target === "claude" ? resolveClaudeHooksFile(dir, manifest.hooks) : resolveCodexHooksFile(dir, manifest.hooks);
|
|
179
|
+
if (file)
|
|
180
|
+
files.add(file.replace(/^\.\//, ""));
|
|
181
|
+
else if (target === "codex" || !existsSync(join(dir, manifest.hooks.replace(/\/+$/, ""), "hooks.json"))) {
|
|
182
|
+
if (!(target === "claude" && manifest.hooks.replace(/^\.\//, "") === "hooks/hooks.json" && existsSync(join(dir, "hooks/hooks.json")))) {
|
|
183
|
+
throw new Error("Cannot resolve executable hooks config");
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
for (const file of files)
|
|
188
|
+
hooks(dir, file, target);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
recordTelemetryEvent("adg.update.executables.inventory", { "executables.count": entries.size, "runtimes.count": new Set(targets).size });
|
|
192
|
+
return [...entries.values()].sort((a, b) => a.id.localeCompare(b.id));
|
|
193
|
+
}
|