@rbbtsn0w/adg 0.8.0-beta.2 → 0.8.0-beta.4

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.
@@ -1,13 +1,14 @@
1
- import { cpSync, existsSync, lstatSync, rmSync, symlinkSync, unlinkSync } from "node:fs";
1
+ import { existsSync, rmSync } from "node:fs";
2
2
  import { homedir } from "node:os";
3
- import { dirname, join, relative, resolve } from "node:path";
3
+ import { join, resolve } from "node:path";
4
4
  import { ensureDir, writeJson } from "../fsutil.js";
5
- import { readManifest } from "../manifest.js";
5
+ import { findManifestFile, readManifest } from "../manifest.js";
6
6
  import { isExposed } from "../components.js";
7
7
  import { installedPluginDir, lockPath } from "../paths.js";
8
8
  import { readLock } from "../lock.js";
9
9
  import { toAntigravityManifest, writeAntigravityMcpConfig } from "../adapters/antigravity.js";
10
10
  import { writeAntigravityHooks } from "../adapters/antigravity-hooks.js";
11
+ import { applySlotAction, markOwned, observeSlot, reconcileSlot } from "../projection-slot.js";
11
12
  import { skippedResult } from "./base.js";
12
13
  import { resolveSelectionDependencies } from "../types.js";
13
14
  /**
@@ -41,6 +42,12 @@ import { resolveSelectionDependencies } from "../types.js";
41
42
  * `["./skills/one","./extra/two"]` — only the `skills/` root is exposed; `extra/`
42
43
  * is not). These would require a separate self-contained projection dir, which the
43
44
  * in-place model deliberately avoids; the full canonical dirs are read in place.
45
+ *
46
+ * Every alias/exposure path here is a "projection slot": deciding whether it's
47
+ * safe to (re)create, relink, or remove one, and actually doing so, is delegated
48
+ * to `../projection-slot.ts` (`reconcileSlot`/`observeSlot`/`applySlotAction`)
49
+ * rather than re-derived ad hoc — see that module's doc and
50
+ * https://github.com/RbBtSn0w/adg/issues/85 finding #1 for why.
44
51
  */
45
52
  const ID = "antigravity";
46
53
  /** The manifest filename Antigravity's auto-scan looks for at a plugin folder root. */
@@ -77,38 +84,21 @@ function antigravityScanDir(ctx, env = process.env) {
77
84
  return ctx.scope === "user" ? antigravityGlobalPluginsDir(env) : ctx.pluginsDir;
78
85
  }
79
86
  /**
80
- * Symlink `linkPath` at `absTarget` (target stored relative so the link survives
81
- * a move), copying instead where symlinks are unavailable (e.g. Windows without
82
- * privilege). Idempotent.
83
- */
84
- function linkOrCopy(linkPath, absTarget) {
85
- rmSync(linkPath, { recursive: true, force: true });
86
- ensureDir(dirname(linkPath));
87
- try {
88
- symlinkSync(relative(dirname(linkPath), absTarget), linkPath, "dir");
89
- }
90
- catch {
91
- cpSync(absTarget, linkPath, { recursive: true });
92
- }
93
- }
94
- /**
95
- * Remove `path` when it is a symlink — including a *broken* one. `lstatSync` (in a
96
- * `try`) is used rather than `existsSync`, which follows the link and reports false
97
- * for a dangling alias, leaving it on disk. Removal uses `unlinkSync`, not
98
- * `rmSync(..., { force: true })`: on Node < 24 the latter follows the link, hits
99
- * `ENOENT`, and the `force` flag then swallows it without unlinking the symlink.
100
- * A real file/dir is left untouched.
87
+ * Reconcile `path` to `desired` via the shared projection-slot pipeline
88
+ * (`src/projection-slot.ts`): observe what's actually there, decide the
89
+ * action, apply it. `warn`, when given, is called (with no arguments — the
90
+ * caller already has `path` in scope for its message) when the desired state
91
+ * can't be reached because real, non-ADG-owned content already occupies
92
+ * `path` — never destructive.
101
93
  */
102
- function rmIfSymlink(path) {
103
- let st;
104
- try {
105
- st = lstatSync(path);
106
- }
107
- catch {
108
- return; // absent
94
+ function reconcile(path, desired, warn) {
95
+ const action = reconcileSlot(desired, observeSlot(path));
96
+ if (action.kind === "skip-foreign") {
97
+ warn?.();
98
+ return false;
109
99
  }
110
- if (st.isSymbolicLink())
111
- unlinkSync(path);
100
+ applySlotAction(path, action);
101
+ return true;
112
102
  }
113
103
  /** First on-disk top segment of a declared component path (e.g. "./agents/" -> "agents"). */
114
104
  function componentSegment(value) {
@@ -132,59 +122,87 @@ export function ensureAntigravityRoot(dir, selection) {
132
122
  writeAntigravityHooks(dir, manifest, selection);
133
123
  for (const field of CONVENTION_FIELDS) {
134
124
  const link = join(dir, field);
135
- rmIfSymlink(link); // clear a stale alias (incl. a broken one); a real source dir is left untouched
136
- if (!isExposed(selection, field))
137
- continue;
138
- const seg = componentSegment(manifest[field]);
139
- if (!seg || seg === field)
140
- continue; // absent, or already convention-named in place
141
- const src = join(dir, seg);
142
- if (existsSync(src))
143
- linkOrCopy(link, src);
125
+ const seg = isExposed(selection, field) ? componentSegment(manifest[field]) : undefined;
126
+ const src = seg && seg !== field ? join(dir, seg) : undefined; // absent, or already convention-named in place
127
+ const desired = src && existsSync(src) ? { kind: "linked", target: resolve(src) } : { kind: "absent" };
128
+ // Only warn when we actually wanted an alias here: `desired: absent` with real
129
+ // content in place is the common, expected case (the component's own dir,
130
+ // already convention-named) — not a collision, so stays silent like the code
131
+ // this replaces.
132
+ const warn = desired.kind === "linked"
133
+ ? () => console.error(`antigravity: skipping "${field}" alias for "${dir}" — ${link} already exists and is not ADG-managed. ` +
134
+ `Remove it to let ADG manage this component.`)
135
+ : undefined;
136
+ reconcile(link, desired, warn);
144
137
  }
145
138
  }
146
139
  /**
147
140
  * Expose `realDir` to Antigravity at `<scanDir>/<name>`. No-op when the real dir
148
141
  * already *is* that path (project, flat). Returns false without touching disk when
149
- * the target is a real directory ADG didn't create (no agy manifest) — we never
150
- * clobber a plugin slot we don't own. Replacing our own alias or a prior
151
- * projection (which carries the manifest) is fine.
142
+ * the target is real content `src/projection-slot.ts`'s `observeSlot` can't prove
143
+ * ADG owns (no `.adg-owned` marker) — we never clobber a plugin slot we don't own.
144
+ * Replacing our own alias or a prior copy-fallback projection (which carries the
145
+ * marker) is fine.
146
+ *
147
+ * Migration note: a copy-fallback exposure created before this module existed
148
+ * carries no marker — ownership was inferred from a root `plugin.json` instead,
149
+ * which is also just Antigravity's own plugin format and proves nothing on its
150
+ * own — so `adoptLegacyExposure` below requires ADG-specific evidence too
151
+ * before it plants the marker; see that function's doc for why a bare
152
+ * `plugin.json` isn't enough by itself.
152
153
  */
153
154
  function exposeAt(scanDir, name, realDir) {
154
155
  const target = join(scanDir, name);
155
156
  if (resolve(target) === resolve(realDir))
156
157
  return true; // project, flat: already in the scan dir
157
- let existing;
158
+ adoptLegacyExposure(target, name);
159
+ return reconcile(target, { kind: "linked", target: resolve(realDir) }, () => console.error(`antigravity: skipping "${name}" — ${target} already exists and is not ADG-managed. ` +
160
+ `Remove it to let ADG manage this plugin.`));
161
+ }
162
+ /**
163
+ * Adopt a copy-fallback exposure created before the projection-slot module
164
+ * existed. A root `plugin.json` alone is *not* sufficient evidence: it's
165
+ * Antigravity's own plugin format, so a directory a user manages directly
166
+ * through Antigravity (unrelated to ADG) could coincidentally sit at the same
167
+ * scan-dir slot under the same plugin `name` and would also have one —
168
+ * adopting on that signal alone would let `reconcile` relink/delete a real,
169
+ * user-owned directory. A genuine legacy copy-fallback is instead a full
170
+ * `cpSync` of ADG's own store folder (`realDir`), so it also carries ADG's own
171
+ * source manifest (`findManifestFile` — `.agents/.plugin.json` or the legacy
172
+ * `.adg-plugin/plugin.json`) declaring this exact plugin `name` — evidence
173
+ * nothing in Antigravity's own format produces. Only that combination plants
174
+ * the marker; anything else is left `foreign`, same as any other unrecognized
175
+ * directory.
176
+ */
177
+ function adoptLegacyExposure(target, name) {
178
+ if (observeSlot(target).kind !== "foreign")
179
+ return;
180
+ if (!existsSync(join(target, ANTIGRAVITY_MANIFEST)))
181
+ return;
182
+ if (!findManifestFile(target))
183
+ return; // no ADG source manifest inside — not provably ours
184
+ let adgName;
158
185
  try {
159
- existing = lstatSync(target);
186
+ adgName = readManifest(target).name;
160
187
  }
161
188
  catch {
162
- existing = undefined; // absent — safe to create
163
- }
164
- if (existing?.isDirectory() && !existing.isSymbolicLink() && !existsSync(join(target, ANTIGRAVITY_MANIFEST))) {
165
- console.error(`antigravity: skipping "${name}" — ${target} already exists and is not ADG-managed. ` +
166
- `Remove it to let ADG manage this plugin.`);
167
- return false;
189
+ return; // can't confirm identity — leave it foreign rather than guess
168
190
  }
169
- linkOrCopy(target, realDir);
170
- return true;
191
+ if (adgName !== name)
192
+ return;
193
+ markOwned(target);
171
194
  }
172
195
  /**
173
196
  * Tear down a plugin's projection. When the real store dir is known, drop its agy
174
197
  * manifest and any convention alias links (in-place cleanup). Remove the external
175
198
  * exposure entry only when it is our own alias (a symlink) or a copy-fallback dir
176
- * that carries the manifest — never recursively delete an unattributable dir, and
177
- * never the in-place store folder itself.
199
+ * the ownership marker proves is ours — never recursively delete an unattributable
200
+ * dir, and never the in-place store folder itself. The marker (rather than a
201
+ * `realDir` comparison) is also what lets this reclaim an orphaned copy-fallback
202
+ * exposure even when `realDir` is unknown, below.
178
203
  */
179
204
  function removeProjection(scanDir, name, realDir) {
180
205
  const target = join(scanDir, name);
181
- let st;
182
- try {
183
- st = lstatSync(target);
184
- }
185
- catch {
186
- st = undefined;
187
- }
188
206
  if (realDir) {
189
207
  if (existsSync(realDir)) {
190
208
  // The real store dir is known: drop the in-place manifest + convention aliases.
@@ -192,20 +210,21 @@ function removeProjection(scanDir, name, realDir) {
192
210
  writeAntigravityMcpConfig(realDir, readManifest(realDir), { components: [] });
193
211
  writeAntigravityHooks(realDir, readManifest(realDir), { components: [] });
194
212
  for (const field of CONVENTION_FIELDS)
195
- rmIfSymlink(join(realDir, field));
213
+ reconcile(join(realDir, field), { kind: "absent" });
196
214
  }
197
215
  if (resolve(target) === resolve(realDir))
198
216
  return; // in-place: target IS the store folder — never delete it
199
- // Aliased exposure we created (symlink, or a copy-fallback dir): safe to drop wholesale.
200
- if (st)
201
- rmSync(target, { recursive: true, force: true });
217
+ // Aliased exposure we created (a symlink, or a copy-fallback dir the ownership
218
+ // marker proves is ours): safe to drop wholesale. `skip-foreign` leaves an
219
+ // unattributable real directory untouched.
220
+ reconcile(target, { kind: "absent" });
202
221
  return;
203
222
  }
204
- // Real dir unknown (e.g. the store entry is already gone): we cannot prove a real
205
- // directory at `target` is our exposure vs. a store/foreign folder, so only ever
206
- // remove a symlink alias — never recursively delete an unattributable directory.
207
- if (st?.isSymbolicLink())
208
- rmSync(target, { force: true });
223
+ // Real dir unknown (e.g. the store entry is already gone): the ownership marker
224
+ // (not a `realDir` comparison) is what proves a real directory at `target` is
225
+ // our own copy-fallback exposure rather than a foreign folder, so this reclaims
226
+ // an orphaned copy-fallback exposure the pre-projection-slot code could not.
227
+ reconcile(target, { kind: "absent" });
209
228
  }
210
229
  /** Resolve a plugin's on-disk dir and selection from the lock, or undefined when not installed. */
211
230
  function pluginRealDir(pluginsDir, name) {
@@ -61,6 +61,52 @@ export const OWNERSHIP_MARKER = ".adg-owned";
61
61
  * presence, so the marker can't be spoofed by an empty or arbitrary file.
62
62
  */
63
63
  const OWNERSHIP_MARKER_MAGIC = "adg-projection-slot/v1\n";
64
+ /**
65
+ * Mark `path` as ADG-owned without going through `applySlotAction` — for
66
+ * adopting a directory that predates this module and was recognized as ADG's
67
+ * by some other, now-retired signal (e.g. Antigravity's pre-Phase-3 heuristic:
68
+ * a root `plugin.json` inside a copy-fallback exposure). Once marked, a later
69
+ * `observeSlot` classifies it `owned-dir` exactly like a copy this module
70
+ * created itself, and normal reconciliation can refresh or remove it.
71
+ *
72
+ * This function does not check whether `path` itself is really ADG's — the
73
+ * caller must already have independently established that, the same way the
74
+ * old heuristic did; calling it against content that isn't ADG's plants a
75
+ * false ownership claim a later `remove-link`/`relink` will act on. It does
76
+ * guard the one thing it writes, the marker file itself, against two ways of
77
+ * losing existing content there: if `.adg-owned` already exists and isn't a
78
+ * regular file (e.g. a symlink someone planted there), `writeFileSync` would
79
+ * follow it and overwrite whatever it points to — the same anti-spoofing
80
+ * check `hasOwnershipMarker` applies on read — so this refuses. And if it IS
81
+ * a regular file, its content decides: already the exact marker content is a
82
+ * no-op (idempotent — adopting an already-owned directory twice is normal),
83
+ * anything else is refused rather than silently clobbered (a real directory
84
+ * being adopted could coincidentally already contain an unrelated file with
85
+ * this exact name).
86
+ */
87
+ export function markOwned(path) {
88
+ const markerPath = join(path, OWNERSHIP_MARKER);
89
+ let st;
90
+ try {
91
+ st = lstatSync(markerPath);
92
+ }
93
+ catch (error) {
94
+ if (!isEnoent(error))
95
+ throw error;
96
+ st = undefined; // absent — safe to create
97
+ }
98
+ if (st) {
99
+ if (!st.isFile()) {
100
+ throw new Error(`refusing to mark ${path} as owned: ${markerPath} already exists and is not a regular file ` +
101
+ "— writing through it (e.g. a symlink) could overwrite unrelated content");
102
+ }
103
+ if (readFileSync(markerPath, "utf8") === OWNERSHIP_MARKER_MAGIC)
104
+ return; // already marked — idempotent
105
+ throw new Error(`refusing to mark ${path} as owned: ${markerPath} already exists with unexpected content — ` +
106
+ "refusing to overwrite what may be unrelated data");
107
+ }
108
+ writeFileSync(markerPath, OWNERSHIP_MARKER_MAGIC);
109
+ }
64
110
  function hasOwnershipMarker(path) {
65
111
  const markerPath = join(path, OWNERSHIP_MARKER);
66
112
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rbbtsn0w/adg",
3
- "version": "0.8.0-beta.2",
3
+ "version": "0.8.0-beta.4",
4
4
  "description": "Agent Directory Group (ADG) toolkit — two domains: plugins and skills.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -71,8 +71,17 @@
71
71
  "semantic-release": "^25.0.5",
72
72
  "typescript": "^5.6.0"
73
73
  },
74
- "comment:overrides": "Security floor: npm 11 before 11.18.0 bundles vulnerable undici 6.26.0; keep npm on patched 11.x until @semantic-release/npm supports a newer major.",
74
+ "comment:overrides": "Security floors for transitive development tooling: keep npm on a patched 11.x release, and pin vulnerable transitive undici/js-yaml branches to their patched versions.",
75
75
  "overrides": {
76
- "npm": "^11.18.0"
76
+ "npm": "^11.19.1",
77
+ "@actions/http-client": {
78
+ "undici": "^6.28.0"
79
+ },
80
+ "@semantic-release/github": {
81
+ "undici": "^7.29.0"
82
+ },
83
+ "cosmiconfig": {
84
+ "js-yaml": "^4.3.1"
85
+ }
77
86
  }
78
87
  }