@rbbtsn0w/adg 0.8.0-beta.1 → 0.8.0-beta.3
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/dist/src/agents/antigravity.js +92 -73
- package/dist/src/projection-slot.js +254 -23
- package/package.json +1 -1
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { existsSync, rmSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import {
|
|
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
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
111
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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
|
|
150
|
-
* clobber a plugin slot we don't own.
|
|
151
|
-
* projection (which carries the
|
|
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
|
-
|
|
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
|
-
|
|
186
|
+
adgName = readManifest(target).name;
|
|
160
187
|
}
|
|
161
188
|
catch {
|
|
162
|
-
|
|
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
|
-
|
|
170
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
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
|
|
200
|
-
|
|
201
|
-
|
|
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):
|
|
205
|
-
//
|
|
206
|
-
//
|
|
207
|
-
|
|
208
|
-
|
|
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) {
|
|
@@ -1,26 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* external exposure symlink today; any future agent with the same
|
|
5
|
-
* symlink-or-copy-fallback shape can reuse this).
|
|
6
|
-
*
|
|
7
|
-
* This module makes no filesystem calls. It exists to separate *deciding*
|
|
8
|
-
* what to do at a path from *doing* it, because the two ad-hoc
|
|
9
|
-
* implementations that predate it (`removeProjectionTarget` in
|
|
10
|
-
* adapters/antigravity.ts, `rmIfSymlink`/`linkOrCopy` in
|
|
11
|
-
* agents/antigravity.ts) each re-derived "is this safe to touch?" via
|
|
12
|
-
* one-off lstat/existsSync checks, and every edge case that surfaced (a
|
|
13
|
-
* stale symlink, a broken link, a foreign directory, a copy-fallback left
|
|
14
|
-
* over where symlinks aren't available) got patched where it was found
|
|
15
|
-
* rather than modeled — see the fix(antigravity) commit history and
|
|
16
|
-
* https://github.com/RbBtSn0w/adg/issues/85 finding #1.
|
|
17
|
-
*
|
|
18
|
-
* `reconcileSlot` is the single place that decision lives now: a table over
|
|
19
|
-
* two small closed unions, exhaustively tested in
|
|
20
|
-
* test/projection-slot.test.ts. observeSlot/applySlotAction (the IO layer
|
|
21
|
-
* that calls this) land in a later phase; this phase is additive only —
|
|
22
|
-
* nothing in the codebase calls `reconcileSlot` yet.
|
|
23
|
-
*/
|
|
1
|
+
import { lstatSync, mkdirSync, readFileSync, readlinkSync, rmSync, statSync, symlinkSync, unlinkSync, writeFileSync, cpSync } from "node:fs";
|
|
2
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
3
|
+
import { ensureDir } from "./fsutil.js";
|
|
24
4
|
/** Exported so tests assert against this exact string instead of duplicating it. */
|
|
25
5
|
export const FOREIGN_REASON = "a real, non-ADG-owned directory or file already exists at this path";
|
|
26
6
|
/**
|
|
@@ -63,3 +43,254 @@ export function reconcileSlot(desired, observed) {
|
|
|
63
43
|
}
|
|
64
44
|
return assertNever(desired);
|
|
65
45
|
}
|
|
46
|
+
// ── IO layer ─────────────────────────────────────────────────────────────
|
|
47
|
+
/**
|
|
48
|
+
* Marker file written at the root of every copy-fallback directory this
|
|
49
|
+
* module creates (`<dir>/.adg-owned`), so a later `observeSlot` can tell it
|
|
50
|
+
* apart from a same-shaped foreign directory — a real (non-symlink) dir
|
|
51
|
+
* otherwise carries no signal of who created it. Exported so a caller that
|
|
52
|
+
* needs to construct the marker path itself (e.g. to exclude it from a
|
|
53
|
+
* content listing) doesn't have to duplicate the filename.
|
|
54
|
+
*/
|
|
55
|
+
export const OWNERSHIP_MARKER = ".adg-owned";
|
|
56
|
+
/**
|
|
57
|
+
* A directory merely named `.adg-owned` proves nothing — a foreign directory
|
|
58
|
+
* could coincidentally contain that name, or someone could create it
|
|
59
|
+
* deliberately, and either way `observeSlot` would misclassify real content
|
|
60
|
+
* as ours to `remove-link`/`relink`. Require exact content, not just
|
|
61
|
+
* presence, so the marker can't be spoofed by an empty or arbitrary file.
|
|
62
|
+
*/
|
|
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
|
+
}
|
|
110
|
+
function hasOwnershipMarker(path) {
|
|
111
|
+
const markerPath = join(path, OWNERSHIP_MARKER);
|
|
112
|
+
try {
|
|
113
|
+
// lstat first, not just readFileSync (which follows symlinks): a
|
|
114
|
+
// foreign directory could plant `.adg-owned` as a symlink to some other
|
|
115
|
+
// file that happens to contain the magic string, spoofing ownership.
|
|
116
|
+
// Require the marker to be a real, non-symlink regular file.
|
|
117
|
+
if (!lstatSync(markerPath).isFile())
|
|
118
|
+
return false;
|
|
119
|
+
return readFileSync(markerPath, "utf8") === OWNERSHIP_MARKER_MAGIC;
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
if (!isEnoent(error))
|
|
123
|
+
throw error;
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** Rethrow anything but "the path doesn't exist" — an EACCES/EPERM/ENOTDIR
|
|
128
|
+
* on lstat means something real is there and unreadable, not absent; letting
|
|
129
|
+
* a caller treat that as absent risks a write/removal against a path it
|
|
130
|
+
* couldn't actually stat. */
|
|
131
|
+
function isEnoent(error) {
|
|
132
|
+
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
133
|
+
}
|
|
134
|
+
function isBrokenLink(path, stat) {
|
|
135
|
+
try {
|
|
136
|
+
stat(path); // follows the link
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
if (!isEnoent(error))
|
|
141
|
+
throw error;
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Classify what's actually at `path` on disk: one lstat, plus a follow-up
|
|
147
|
+
* stat of the target to determine `broken` for a symlink, or a read of the
|
|
148
|
+
* ownership marker file to distinguish `owned-dir` from `foreign` for a real
|
|
149
|
+
* directory.
|
|
150
|
+
*/
|
|
151
|
+
export function observeSlot(path, opts = {}) {
|
|
152
|
+
const lstat = opts.lstat ?? lstatSync;
|
|
153
|
+
const stat = opts.stat ?? statSync;
|
|
154
|
+
let st;
|
|
155
|
+
try {
|
|
156
|
+
st = lstat(path);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
if (!isEnoent(error))
|
|
160
|
+
throw error;
|
|
161
|
+
return { kind: "absent" };
|
|
162
|
+
}
|
|
163
|
+
if (st.isSymbolicLink()) {
|
|
164
|
+
return {
|
|
165
|
+
kind: "symlink",
|
|
166
|
+
target: resolve(dirname(path), readlinkSync(path)),
|
|
167
|
+
// stat (follows the link), not existsSync: existsSync reports any
|
|
168
|
+
// failure as false, so an EACCES/EPERM on the target would be read as
|
|
169
|
+
// "broken" and drive a destructive relink. Only ENOENT means broken;
|
|
170
|
+
// anything else rethrows, same as the lstat above.
|
|
171
|
+
broken: isBrokenLink(path, stat),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (st.isDirectory() && hasOwnershipMarker(path)) {
|
|
175
|
+
return { kind: "owned-dir" };
|
|
176
|
+
}
|
|
177
|
+
return { kind: "foreign" };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Remove whatever `reconcileSlot` has already established is ours at `path`
|
|
181
|
+
* (a symlink or an owned-dir — never foreign; `remove-link`/`relink` are
|
|
182
|
+
* only ever returned for those two observations). A symlink is trusted and
|
|
183
|
+
* removed outright; a real directory is re-checked for the ownership marker
|
|
184
|
+
* immediately before the recursive delete, rather than fully trusting the
|
|
185
|
+
* caller's already-computed action — the cheap belt-and-suspenders check
|
|
186
|
+
* that actually bounds the blast radius of a caller bug or a stale action.
|
|
187
|
+
*
|
|
188
|
+
* Symlink removal uses `unlinkSync`, not `rmSync(..., { force: true })`: on
|
|
189
|
+
* Node < 24 the latter follows the link, hits `ENOENT` at the (possibly
|
|
190
|
+
* absent) target, and `force` then swallows that error without unlinking
|
|
191
|
+
* the symlink itself — the same quirk `rmIfSymlink` in agents/antigravity.ts
|
|
192
|
+
* works around today.
|
|
193
|
+
*/
|
|
194
|
+
function removeOwned(path) {
|
|
195
|
+
let st;
|
|
196
|
+
try {
|
|
197
|
+
st = lstatSync(path);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
if (!isEnoent(error))
|
|
201
|
+
throw error;
|
|
202
|
+
return; // already absent
|
|
203
|
+
}
|
|
204
|
+
if (st.isSymbolicLink()) {
|
|
205
|
+
unlinkSync(path);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
// Re-check right before the recursive delete rather than fully trusting
|
|
209
|
+
// the caller's action: reconcileSlot only ever returns remove-link/relink
|
|
210
|
+
// for a symlink or owned-dir observation, but a caller bug, a stale action
|
|
211
|
+
// computed against an earlier observation, or a race could still reach
|
|
212
|
+
// here with something else — including a plain file, not just a foreign
|
|
213
|
+
// directory. Check that explicitly before hasOwnershipMarker: joining the
|
|
214
|
+
// marker name onto a file path and lstat'ing it throws ENOTDIR, which
|
|
215
|
+
// would otherwise surface in place of this function's own clear refusal.
|
|
216
|
+
if (!st.isDirectory()) {
|
|
217
|
+
throw new Error(`refusing to remove ${path}: it is a real file, not a directory or symlink — ` +
|
|
218
|
+
"removeOwned must only ever be called for a symlink or an owned-dir observation");
|
|
219
|
+
}
|
|
220
|
+
if (!hasOwnershipMarker(path)) {
|
|
221
|
+
throw new Error(`refusing to remove ${path}: it is a real directory without the ADG ownership marker (${OWNERSHIP_MARKER}) — ` +
|
|
222
|
+
"removeOwned must only ever be called for a symlink or an owned-dir observation");
|
|
223
|
+
}
|
|
224
|
+
rmSync(path, { recursive: true, force: true });
|
|
225
|
+
}
|
|
226
|
+
function createLink(path, target, symlink, cp) {
|
|
227
|
+
ensureDir(dirname(path));
|
|
228
|
+
try {
|
|
229
|
+
symlink(relative(dirname(path), target), path, "dir");
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
catch {
|
|
233
|
+
// fall through to the copy fallback below
|
|
234
|
+
}
|
|
235
|
+
// create-link/relink should only ever reach here right after path was
|
|
236
|
+
// confirmed absent (relink's caller already ran removeOwned; create-link's
|
|
237
|
+
// caller only fires on an absent observation) — but don't fully trust
|
|
238
|
+
// that: a race against another process, or a caller bug, could still land
|
|
239
|
+
// real content here between the observation and this call. Refuse to copy
|
|
240
|
+
// into/over it rather than silently merging or overwriting.
|
|
241
|
+
if (observeSlot(path).kind !== "absent") {
|
|
242
|
+
throw new Error(`refusing copy-fallback at ${path}: expected it to be absent, but something is already there`);
|
|
243
|
+
}
|
|
244
|
+
// Matches `linkOrCopy` in agents/antigravity.ts, which this replaces in
|
|
245
|
+
// Phase 3. Note `dereference: true` would NOT make this more robust
|
|
246
|
+
// against a symlink *inside* `target`: as of Node 25 it has no effect on
|
|
247
|
+
// symlinks nested in a recursive directory copy (verified — they are
|
|
248
|
+
// recreated as symlinks with or without it), so it would only add a
|
|
249
|
+
// claim the flag doesn't deliver. Hardening the fallback against nested
|
|
250
|
+
// symlinks is a behavior change, not a port, and belongs with the real
|
|
251
|
+
// call sites in Phase 3.
|
|
252
|
+
//
|
|
253
|
+
// Marker written both before AND after the (potentially large, potentially
|
|
254
|
+
// failing) recursive copy — each half guards a different failure:
|
|
255
|
+
// - Before: if cp throws partway through, or the process is killed
|
|
256
|
+
// outright (no catch/finally runs for that case either way), the
|
|
257
|
+
// marker has already landed, so the next observeSlot classifies the
|
|
258
|
+
// partial directory as owned-dir (reclaimable via relink) rather than
|
|
259
|
+
// foreign (unrecoverable by ADG). Relies on cp merging into an
|
|
260
|
+
// already-existing destination directory rather than erroring —
|
|
261
|
+
// verified separately.
|
|
262
|
+
// - After: if `target` itself contains a root-level file also named
|
|
263
|
+
// OWNERSHIP_MARKER, cp overwrites our just-written marker with that
|
|
264
|
+
// file's content when it copies target's own tree — verified directly
|
|
265
|
+
// (cpSync does overwrite a same-named destination file with the
|
|
266
|
+
// source's version). Rewriting after a successful copy guarantees the
|
|
267
|
+
// final on-disk marker is ours regardless of what target contained.
|
|
268
|
+
const markerPath = join(path, OWNERSHIP_MARKER);
|
|
269
|
+
mkdirSync(path);
|
|
270
|
+
writeFileSync(markerPath, OWNERSHIP_MARKER_MAGIC);
|
|
271
|
+
cp(target, path, { recursive: true });
|
|
272
|
+
writeFileSync(markerPath, OWNERSHIP_MARKER_MAGIC);
|
|
273
|
+
}
|
|
274
|
+
/** Perform the action `reconcileSlot` decided on at `path`. The only function
|
|
275
|
+
* in this module that writes to disk. */
|
|
276
|
+
export function applySlotAction(path, action, opts = {}) {
|
|
277
|
+
const symlink = opts.symlink ?? symlinkSync;
|
|
278
|
+
const cp = opts.cp ?? cpSync;
|
|
279
|
+
switch (action.kind) {
|
|
280
|
+
case "noop":
|
|
281
|
+
case "skip-foreign":
|
|
282
|
+
return;
|
|
283
|
+
case "remove-link":
|
|
284
|
+
removeOwned(path);
|
|
285
|
+
return;
|
|
286
|
+
case "create-link":
|
|
287
|
+
createLink(path, action.target, symlink, cp);
|
|
288
|
+
return;
|
|
289
|
+
case "relink":
|
|
290
|
+
removeOwned(path);
|
|
291
|
+
createLink(path, action.target, symlink, cp);
|
|
292
|
+
return;
|
|
293
|
+
default:
|
|
294
|
+
assertNever(action);
|
|
295
|
+
}
|
|
296
|
+
}
|