@rbbtsn0w/adg 0.3.0 → 0.4.0-beta.2

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.
@@ -8,7 +8,7 @@ import { isExposed } from "../components.js";
8
8
  import { installedPluginDir, lockPath } from "../paths.js";
9
9
  import { readLock } from "../lock.js";
10
10
  import { ANTIGRAVITY_PROJECTION_DIR } from "../adapters/antigravity.js";
11
- import { makeCli } from "./base.js";
11
+ import { makeCli, skippedResult } from "./base.js";
12
12
  /**
13
13
  * Antigravity (`agy`) agent.
14
14
  *
@@ -140,7 +140,12 @@ export const antigravityAgent = {
140
140
  available,
141
141
  activate(ctx) {
142
142
  if (!available())
143
- return { agent: ID, affected: [], skipped: true };
143
+ return skippedResult(ID);
144
+ // Query agy once up front so the pre-install uninstall below is issued only
145
+ // for plugins that are actually present (a brand-new plugin has no residual
146
+ // to clear). `undefined` (couldn't enumerate) falls back to always
147
+ // uninstalling, preserving the clean-replace guarantee.
148
+ const installed = antigravityInstalled();
144
149
  const affected = [];
145
150
  for (const p of ctx.plugins) {
146
151
  // Isolate each plugin: a malformed manifest or a filesystem error must not
@@ -150,6 +155,14 @@ export const antigravityAgent = {
150
155
  if (!store)
151
156
  continue;
152
157
  writeAntigravityProjection(store.dir, store.selection);
158
+ // `agy plugin install` *merges* into an existing `<store>/plugins/<name>`
159
+ // rather than replacing it, so components dropped since the last sync (a
160
+ // narrowed selection, or a skill removed upstream) would linger as
161
+ // residual data. Uninstall an existing copy first to force a clean
162
+ // re-import; skip it for a not-yet-installed plugin (nothing to clear).
163
+ if (installed === undefined || installed.includes(p)) {
164
+ run(["plugin", "uninstall", p]);
165
+ }
153
166
  if (run(["plugin", "install", join(store.dir, ANTIGRAVITY_PROJECTION_DIR)]).ok)
154
167
  affected.push(p);
155
168
  }
@@ -161,7 +174,7 @@ export const antigravityAgent = {
161
174
  },
162
175
  deactivate(ctx) {
163
176
  if (!available())
164
- return { agent: ID, affected: [], skipped: true };
177
+ return skippedResult(ID);
165
178
  const affected = [];
166
179
  for (const p of ctx.plugins) {
167
180
  if (run(["plugin", "uninstall", p]).ok)
@@ -169,8 +182,46 @@ export const antigravityAgent = {
169
182
  }
170
183
  return { agent: ID, affected, skipped: false };
171
184
  },
172
- // `agy plugin install` re-imports the source dir, so re-running it is the refresh.
185
+ // Re-running activate is the refresh: it uninstalls then re-imports the source
186
+ // dir, so a shrunk component set replaces the stale copy instead of merging.
173
187
  refresh(ctx) {
174
188
  return antigravityAgent.activate(ctx);
175
189
  },
190
+ // `agy plugin list` emits JSON (`{ imports: [{ name }] }`). agy has no
191
+ // marketplace concept, so this returns *every* imported plugin — including
192
+ // ones imported outside ADG; the caller treats agy-only names as "not managed
193
+ // here" rather than asserting they are ADG orphans.
194
+ // `available()` (stdio-ignored probe) gates the query, so an absent CLI is a
195
+ // quiet `undefined` ("unknown") rather than echoing a spawn error to stderr.
196
+ listInstalled: () => antigravityInstalled(),
176
197
  };
198
+ /**
199
+ * The plugin names agy currently has imported, or `undefined` when the CLI is
200
+ * absent or its output can't be parsed. Shared by `listInstalled` and by
201
+ * `activate` (to skip the pre-install uninstall for brand-new plugins).
202
+ */
203
+ function antigravityInstalled() {
204
+ if (!available())
205
+ return undefined;
206
+ const res = run(["plugin", "list"]);
207
+ if (!res.ok)
208
+ return undefined;
209
+ return parseAntigravityPluginList(res.out);
210
+ }
211
+ /**
212
+ * Parse `agy plugin list` JSON (`{ imports: [{ name }] }`) into deduped plugin
213
+ * names, or `undefined` when the output isn't the expected JSON. Pure (no CLI)
214
+ * so it is unit-testable against captured output.
215
+ */
216
+ export function parseAntigravityPluginList(out) {
217
+ try {
218
+ const parsed = JSON.parse(out);
219
+ const names = (parsed.imports ?? [])
220
+ .map((i) => i.name)
221
+ .filter((n) => typeof n === "string");
222
+ return [...new Set(names)];
223
+ }
224
+ catch {
225
+ return undefined; // unparseable output — report "unknown", don't guess
226
+ }
227
+ }
@@ -1,4 +1,13 @@
1
1
  import { spawnSync } from "node:child_process";
2
+ /**
3
+ * The canonical "CLI absent, so nothing was touched" lifecycle result. Every
4
+ * agent's activate/deactivate guard returns this shape; centralizing it keeps
5
+ * the agent id the single thing that varies and rules out a hand-typed drift
6
+ * (e.g. a wrong `agent` field or a missing `skipped`).
7
+ */
8
+ export function skippedResult(agent) {
9
+ return { agent, affected: [], skipped: true };
10
+ }
2
11
  /**
3
12
  * Build the shared `available()` / `run()` pair for an agent that drives an
4
13
  * external plugin CLI. Centralizing the `spawnSync` here keeps error handling,
@@ -6,8 +15,14 @@ import { spawnSync } from "node:child_process";
6
15
  * rather than copied across every agent.
7
16
  */
8
17
  export function makeCli(bin, opts) {
18
+ // A CLI's presence can't change within a single `adg` run, yet `available()`
19
+ // is called as a guard on every agent lifecycle op (and once per plugin in
20
+ // some loops). Memoize the first probe so one `plugins update --both` can't
21
+ // re-shell the same `--help` many times. Cached on first call, not eagerly,
22
+ // so importing an agent module never spawns a subprocess.
23
+ let probed;
9
24
  return {
10
- available: () => spawnSync(bin, opts.probeArgs, { stdio: "ignore" }).status === 0,
25
+ available: () => (probed ??= spawnSync(bin, opts.probeArgs, { stdio: "ignore" }).status === 0),
11
26
  run: (args) => {
12
27
  const r = spawnSync(bin, args, { encoding: "utf8" });
13
28
  // A launch failure (e.g. ENOENT for a missing binary, EACCES) leaves
@@ -5,7 +5,7 @@ import { toPosix, writeJson } from "../fsutil.js";
5
5
  import { readManifest } from "../manifest.js";
6
6
  import { installedPluginDir, lockPath } from "../paths.js";
7
7
  import { readLock } from "../lock.js";
8
- import { makeCli } from "./base.js";
8
+ import { makeCli, skippedResult } from "./base.js";
9
9
  /**
10
10
  * Claude Code agent.
11
11
  *
@@ -76,7 +76,7 @@ export const claudeAgent = {
76
76
  available,
77
77
  activate(ctx) {
78
78
  if (!available())
79
- return { agent: "claude", affected: [], skipped: true };
79
+ return skippedResult("claude");
80
80
  writeClaudeCatalog(ctx.pluginsDir);
81
81
  syncMarketplace(ctx.pluginsDir);
82
82
  const affected = [];
@@ -88,7 +88,7 @@ export const claudeAgent = {
88
88
  },
89
89
  deactivate(ctx) {
90
90
  if (!available())
91
- return { agent: "claude", affected: [], skipped: true };
91
+ return skippedResult("claude");
92
92
  const affected = [];
93
93
  for (const p of ctx.plugins) {
94
94
  if (run(["plugin", "uninstall", p, "--scope", ctx.scope]).ok)
@@ -98,7 +98,7 @@ export const claudeAgent = {
98
98
  },
99
99
  refresh(ctx) {
100
100
  if (!available())
101
- return { agent: "claude", affected: [], skipped: true };
101
+ return skippedResult("claude");
102
102
  // Claude caches a copy on install and won't re-pull from a local marketplace,
103
103
  // so uninstall (keeping data) then re-install to force a fresh copy.
104
104
  for (const p of ctx.plugins)
@@ -106,4 +106,57 @@ export const claudeAgent = {
106
106
  const act = claudeAgent.activate(ctx);
107
107
  return { agent: "claude", affected: act.affected, skipped: act.skipped };
108
108
  },
109
+ // Query Claude's live plugin state for `adg plugins status`, scoped to the
110
+ // ADG marketplace and the requested install scope. `available()` gates the
111
+ // query so an absent CLI is a quiet `undefined` ("unknown").
112
+ listInstalled(ctx) {
113
+ if (!available())
114
+ return undefined;
115
+ const res = run(["plugin", "list"]);
116
+ if (!res.ok)
117
+ return undefined;
118
+ return parseClaudePluginList(res.out, MARKETPLACE, ctx.scope);
119
+ },
109
120
  };
121
+ /**
122
+ * Parse `claude plugin list` output into the *enabled* plugin names from a given
123
+ * marketplace and install scope. The listing groups each plugin as a
124
+ * `❯ <name>@<marketplace>` header followed by indented `Scope:` / `Status:`
125
+ * lines; we pair them so a plugin enabled only in another scope, disabled, or
126
+ * from a different marketplace is excluded. Pure (no CLI) so it is unit-testable
127
+ * against captured output.
128
+ */
129
+ export function parseClaudePluginList(out, marketplace, scope) {
130
+ const names = [];
131
+ const seen = new Set();
132
+ let cur;
133
+ const flush = () => {
134
+ if (cur && cur.mp === marketplace && cur.scope === scope && cur.enabled && !seen.has(cur.name)) {
135
+ seen.add(cur.name);
136
+ names.push(cur.name);
137
+ }
138
+ };
139
+ for (const line of out.split("\n")) {
140
+ const head = line.match(/^\s*❯\s*(\S+?)@([\w.-]+)\s*$/);
141
+ if (head) {
142
+ flush();
143
+ cur = { name: head[1], mp: head[2] };
144
+ continue;
145
+ }
146
+ if (!cur)
147
+ continue;
148
+ const sc = line.match(/^\s*Scope:\s*(\S+)/);
149
+ if (sc) {
150
+ // Normalize case so a future `Scope: Project` still matches "project".
151
+ cur.scope = sc[1].toLowerCase();
152
+ continue;
153
+ }
154
+ const st = line.match(/^\s*Status:\s*(.+)$/);
155
+ // "disabled" does not contain "enabled", so a substring test cleanly
156
+ // distinguishes the two states.
157
+ if (st)
158
+ cur.enabled = /enabled/i.test(st[1]);
159
+ }
160
+ flush();
161
+ return names;
162
+ }
@@ -3,7 +3,7 @@ import { homedir } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import { marketplacePath } from "../paths.js";
5
5
  import { readMarketplace } from "../marketplace.js";
6
- import { makeCli } from "./base.js";
6
+ import { makeCli, skippedResult } from "./base.js";
7
7
  /**
8
8
  * Codex agent.
9
9
  *
@@ -28,7 +28,7 @@ export const codexAgent = {
28
28
  activate(ctx) {
29
29
  const mp = marketplaceName(ctx.pluginsDir);
30
30
  if (!available())
31
- return { agent: "codex", affected: [], skipped: true };
31
+ return skippedResult("codex");
32
32
  const affected = [];
33
33
  for (const p of ctx.plugins) {
34
34
  if (run(["plugin", "add", `${p}@${mp}`]).ok)
@@ -39,7 +39,7 @@ export const codexAgent = {
39
39
  deactivate(ctx) {
40
40
  const mp = marketplaceName(ctx.pluginsDir);
41
41
  if (!available())
42
- return { agent: "codex", affected: [], skipped: true };
42
+ return skippedResult("codex");
43
43
  const affected = [];
44
44
  for (const p of ctx.plugins) {
45
45
  if (run(["plugin", "remove", `${p}@${mp}`]).ok)
@@ -47,9 +47,54 @@ export const codexAgent = {
47
47
  }
48
48
  return { agent: "codex", affected, skipped: false };
49
49
  },
50
- // `codex plugin add` is idempotent and re-copies into the cache, so re-adding
51
- // is the refresh.
50
+ // `codex plugin add` stages into `.remote-plugin-install-staging` and swaps the
51
+ // cache entry (keeping a `.tmp/plugins-backup-*`), so it *replaces* rather than
52
+ // merges — re-adding is a clean, atomic refresh with no stale-component residue.
53
+ // (Contrast agy, whose `install` merges and so needs an explicit uninstall.)
52
54
  refresh(ctx) {
53
55
  return codexAgent.activate(ctx);
54
56
  },
57
+ // Query Codex's live plugin state for `adg plugins status`, scoped to our
58
+ // generated marketplace. `available()` gates the query so an absent CLI is a
59
+ // quiet `undefined` ("unknown").
60
+ listInstalled(ctx) {
61
+ if (!available())
62
+ return undefined;
63
+ const mp = marketplaceName(ctx.pluginsDir);
64
+ if (!mp)
65
+ return undefined; // no generated marketplace → can't scope the query
66
+ const res = run(["plugin", "list"]);
67
+ if (!res.ok)
68
+ return undefined;
69
+ return parseCodexPluginList(res.out, mp);
70
+ },
55
71
  };
72
+ /**
73
+ * Parse `codex plugin list` output into the *installed and enabled* plugin names
74
+ * from a given marketplace. The listing is a whitespace-aligned table whose
75
+ * columns are `<name>@<marketplace>`, `STATUS` (e.g. "installed, enabled"),
76
+ * `VERSION`, `PATH`. We split on 2+ spaces and read the STATUS column so that
77
+ * an available-but-not-added or disabled row isn't miscounted as live, and the
78
+ * header / banner / path lines (no `name@mp` first column) are skipped. Pure
79
+ * (no CLI) so it is unit-testable against captured output.
80
+ */
81
+ export function parseCodexPluginList(out, marketplace) {
82
+ const names = [];
83
+ const seen = new Set();
84
+ for (const line of out.split("\n")) {
85
+ const cols = line.split(/\s{2,}/);
86
+ const head = cols[0]?.match(/^(\S+?)@(\S+)$/);
87
+ if (!head || head[2] !== marketplace)
88
+ continue;
89
+ const status = (cols[1] ?? "").toLowerCase();
90
+ // Count only plugins actually added and active; "disabled" never contains
91
+ // "enabled", so the substring tests cleanly separate the states.
92
+ if (!status.includes("installed") || status.includes("disabled"))
93
+ continue;
94
+ if (!seen.has(head[1])) {
95
+ seen.add(head[1]);
96
+ names.push(head[1]);
97
+ }
98
+ }
99
+ return names;
100
+ }