@staix/agent-hosts 0.2.0 → 0.4.0
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 +37 -5
- package/fixtures/MANIFEST.sha256 +16 -0
- package/fixtures/README.md +10 -0
- package/fixtures/receipts/package/owner-absent.json +15 -0
- package/fixtures/receipts/package/owner-present.json +16 -0
- package/fixtures/v1/copilot.json +168 -0
- package/fixtures/v1/suite-managed.json +295 -0
- package/package.json +1 -1
- package/src/entry.ts +44 -8
- package/src/fs-safe.ts +6 -1
- package/src/hosts.ts +54 -0
- package/src/index.ts +3 -0
- package/src/paths.ts +67 -2
- package/src/receipt.ts +7 -1
- package/src/refusal.ts +7 -0
- package/src/skills.ts +404 -0
package/src/skills.ts
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { lstatSync, readlinkSync, renameSync, symlinkSync, unlinkSync, type Stats } from "node:fs";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { checkPhysicalDirectory, ensureDirectory, publishImage, readImage, withConfigLock } from "./fs-safe.ts";
|
|
5
|
+
import { SKILL_HOSTS, type SkillHostId } from "./hosts.ts";
|
|
6
|
+
import type { ConfigImage } from "./json.ts";
|
|
7
|
+
import { asEntryRefusal, EntryRefusal, type RefusalCode } from "./refusal.ts";
|
|
8
|
+
|
|
9
|
+
const markers = [".ahub-skill.json", ".vmem-skill.json", ".amem-skill.json", ".arev-skill.json"] as const;
|
|
10
|
+
const receiptLimit = 64 * 1024;
|
|
11
|
+
const absentImage: ConfigImage = { bytes: null, mode: 0o600 };
|
|
12
|
+
const reserved = new Set([".system"]);
|
|
13
|
+
|
|
14
|
+
interface LinkIdentity {
|
|
15
|
+
targetPath: string;
|
|
16
|
+
link: string;
|
|
17
|
+
device: string;
|
|
18
|
+
inode: string;
|
|
19
|
+
birth: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface SkillReceipt {
|
|
23
|
+
version: 1;
|
|
24
|
+
host: SkillHostId;
|
|
25
|
+
name: string;
|
|
26
|
+
installed: LinkIdentity;
|
|
27
|
+
pending?: { temporary: LinkIdentity; previous: LinkIdentity | null };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SkillEntryTarget {
|
|
31
|
+
host: SkillHostId;
|
|
32
|
+
root: string;
|
|
33
|
+
name: string;
|
|
34
|
+
receiptDir: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface PlanSkillEntryOptions extends SkillEntryTarget {
|
|
38
|
+
sourcePath: string;
|
|
39
|
+
retarget?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type SkillTargetClassification =
|
|
43
|
+
| { kind: "absent" | "system-reserved" | "unmanaged-directory" | "unmanaged-file" }
|
|
44
|
+
| { kind: "owned-symlink" | "foreign-symlink"; link: string }
|
|
45
|
+
| { kind: "tool-marker"; marker: typeof markers[number] };
|
|
46
|
+
|
|
47
|
+
type SkillChange = "add" | "replace" | "retarget" | "unchanged";
|
|
48
|
+
type SkillPreview = {
|
|
49
|
+
change: SkillChange;
|
|
50
|
+
targetPath: string;
|
|
51
|
+
options: PlanSkillEntryOptions;
|
|
52
|
+
intent: string;
|
|
53
|
+
};
|
|
54
|
+
export type SkillEntryPlan = SkillPreview
|
|
55
|
+
| { change: "refused"; code: RefusalCode; reason: string; file?: string };
|
|
56
|
+
|
|
57
|
+
export interface SkillEntryResult {
|
|
58
|
+
change: SkillChange | "removed";
|
|
59
|
+
targetPath: string;
|
|
60
|
+
receipt: string;
|
|
61
|
+
code?: "no-receipt" | "entry-already-absent";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function fail(code: RefusalCode, file: string, reason: string): never {
|
|
65
|
+
throw new EntryRefusal(code, `${file}: ${reason}`, { file });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function stat(path: string): Stats | undefined {
|
|
69
|
+
try {
|
|
70
|
+
return lstatSync(path);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function plainAbsolute(path: unknown): path is string {
|
|
78
|
+
return typeof path === "string" && path.length <= 4096 && !path.includes(String.fromCharCode(0))
|
|
79
|
+
&& isAbsolute(path) && resolve(path) === path;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function sameLink(a: LinkIdentity | undefined | null, b: LinkIdentity | undefined | null): boolean {
|
|
83
|
+
return !!a && !!b && a.targetPath === b.targetPath && a.link === b.link
|
|
84
|
+
&& a.device === b.device && a.inode === b.inode && a.birth === b.birth;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function identify(targetPath: string, named: Stats): LinkIdentity {
|
|
88
|
+
const link = readlinkSync(targetPath);
|
|
89
|
+
const current = stat(targetPath);
|
|
90
|
+
if (!current?.isSymbolicLink() || named.ino !== current.ino || named.dev !== current.dev
|
|
91
|
+
|| named.ctimeMs !== current.ctimeMs) {
|
|
92
|
+
fail("file-changed", targetPath, "link changed during inspection; inspect it and retry");
|
|
93
|
+
}
|
|
94
|
+
return { targetPath, link, device: String(named.dev), inode: String(named.ino), birth: String(named.birthtimeMs) };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function inspect(targetPath: string, owned?: LinkIdentity): {
|
|
98
|
+
classification: SkillTargetClassification; identity?: LinkIdentity;
|
|
99
|
+
} {
|
|
100
|
+
if (!plainAbsolute(targetPath)) fail("unsafe-path", targetPath, "use an absolute physical skill path");
|
|
101
|
+
if (reserved.has(basename(targetPath))) return { classification: { kind: "system-reserved" } };
|
|
102
|
+
checkPhysicalDirectory(dirname(targetPath));
|
|
103
|
+
const named = stat(targetPath);
|
|
104
|
+
if (!named) return { classification: { kind: "absent" } };
|
|
105
|
+
if (named.isSymbolicLink()) {
|
|
106
|
+
const identity = identify(targetPath, named);
|
|
107
|
+
return {
|
|
108
|
+
identity,
|
|
109
|
+
classification: { kind: sameLink(identity, owned) ? "owned-symlink" : "foreign-symlink", link: identity.link },
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
if (!named.isDirectory()) return { classification: { kind: "unmanaged-file" } };
|
|
113
|
+
checkPhysicalDirectory(targetPath);
|
|
114
|
+
for (const marker of markers) {
|
|
115
|
+
if (stat(join(targetPath, marker))) return { classification: { kind: "tool-marker", marker } };
|
|
116
|
+
}
|
|
117
|
+
return { classification: { kind: "unmanaged-directory" } };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Classify ownership only when a validated receipt is supplied by host, name and directory. */
|
|
121
|
+
export function classifySkillTarget(options: {
|
|
122
|
+
targetPath: string; host?: SkillHostId; receiptDir?: string;
|
|
123
|
+
}): SkillTargetClassification {
|
|
124
|
+
const { targetPath, host, receiptDir } = options;
|
|
125
|
+
const owned = host !== undefined && receiptDir !== undefined
|
|
126
|
+
? readSkillReceipt({ host, name: basename(targetPath), root: dirname(targetPath), receiptDir }).receipt?.installed
|
|
127
|
+
: undefined;
|
|
128
|
+
return inspect(targetPath, owned).classification;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function targetFor(options: SkillEntryTarget): { targetPath: string; receiptPath: string } {
|
|
132
|
+
const { host, root, name, receiptDir } = options;
|
|
133
|
+
if (!Object.hasOwn(SKILL_HOSTS, host) || !/^(?:[A-Za-z0-9][A-Za-z0-9._-]{0,127}|\.system)$/.test(name)) {
|
|
134
|
+
fail("invalid-target", root, "supply a verified skill host and a single skill name");
|
|
135
|
+
}
|
|
136
|
+
if (!plainAbsolute(root) || !plainAbsolute(receiptDir)) {
|
|
137
|
+
fail("unsafe-path", root, "use absolute physical skill and receipt directories");
|
|
138
|
+
}
|
|
139
|
+
const key = createHash("sha256").update(JSON.stringify([host, name])).digest("hex");
|
|
140
|
+
return { targetPath: join(root, name), receiptPath: join(receiptDir, `skill-${key}.json`) };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function validLink(value: unknown): value is LinkIdentity {
|
|
144
|
+
if (typeof value !== "object" || value === null) return false;
|
|
145
|
+
const link = value as Partial<LinkIdentity>;
|
|
146
|
+
return plainAbsolute(link.targetPath) && typeof link.link === "string" && link.link.length > 0
|
|
147
|
+
&& link.link.length <= 4096 && !isAbsolute(link.link) && !link.link.includes(String.fromCharCode(0))
|
|
148
|
+
&& /^\d+$/.test(link.device ?? "") && /^\d+$/.test(link.inode ?? "")
|
|
149
|
+
&& /^\d+(\.\d+)?$/.test(link.birth ?? "");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function readSkillReceipt(options: SkillEntryTarget): {
|
|
153
|
+
path: string; image: ConfigImage; receipt?: SkillReceipt;
|
|
154
|
+
} {
|
|
155
|
+
const { receiptPath: path } = targetFor(options);
|
|
156
|
+
const image = readImage(path, receiptLimit);
|
|
157
|
+
if (image.bytes === null) return { path, image };
|
|
158
|
+
let value: SkillReceipt;
|
|
159
|
+
try {
|
|
160
|
+
value = JSON.parse(Buffer.from(image.bytes, "base64").toString("utf8"));
|
|
161
|
+
} catch {
|
|
162
|
+
fail("invalid-receipt", path, "receipt is malformed; inspect it before retrying");
|
|
163
|
+
}
|
|
164
|
+
if (!value || value.version !== 1 || value.host !== options.host || value.name !== options.name
|
|
165
|
+
|| !validLink(value.installed) || basename(value.installed.targetPath) !== value.name
|
|
166
|
+
|| (value.pending !== undefined && (!value.pending || !validLink(value.pending.temporary)
|
|
167
|
+
|| dirname(value.pending.temporary.targetPath) !== dirname(value.installed.targetPath)
|
|
168
|
+
|| !/^\.agent-hosts-skill-[a-f0-9-]{36}\.tmp$/.test(basename(value.pending.temporary.targetPath))
|
|
169
|
+
|| !sameLink({ ...value.pending.temporary, targetPath: value.installed.targetPath }, value.installed)
|
|
170
|
+
|| (value.pending.previous !== null && (!validLink(value.pending.previous)
|
|
171
|
+
|| basename(value.pending.previous.targetPath) !== value.name))))) {
|
|
172
|
+
fail("invalid-receipt", path, "receipt does not match this skill; inspect it before retrying");
|
|
173
|
+
}
|
|
174
|
+
return { path, image, receipt: value };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function receiptImage(receipt: SkillReceipt): ConfigImage {
|
|
178
|
+
return { bytes: Buffer.from(JSON.stringify(receipt) + "\n").toString("base64"), mode: 0o600 };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function checkAvailable(targetPath: string, owned?: LinkIdentity): boolean {
|
|
182
|
+
const { classification } = inspect(targetPath, owned);
|
|
183
|
+
switch (classification.kind) {
|
|
184
|
+
case "absent": return false;
|
|
185
|
+
case "owned-symlink": return true;
|
|
186
|
+
case "foreign-symlink":
|
|
187
|
+
return fail(owned ? "managed-entry-changed" : "foreign-skill-link", targetPath,
|
|
188
|
+
"link is outside this receipt's ownership; move it aside or restore the recorded link, then retry");
|
|
189
|
+
case "tool-marker":
|
|
190
|
+
return fail("skill-tool-marker", targetPath, `owned by ${classification.marker}; manage it with its owning tool`);
|
|
191
|
+
case "system-reserved":
|
|
192
|
+
return fail("system-reserved-skill", targetPath, "name is reserved for host skills; choose another name");
|
|
193
|
+
default:
|
|
194
|
+
return fail("unmanaged-skill-target", targetPath, "entry already exists; move it aside or choose another name");
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function sourceLink(options: PlanSkillEntryOptions, targetPath: string): string {
|
|
199
|
+
if (!plainAbsolute(options.sourcePath) || !checkPhysicalDirectory(options.sourcePath)) {
|
|
200
|
+
fail("invalid-entry", String(options.sourcePath), "supply an existing physical source directory");
|
|
201
|
+
}
|
|
202
|
+
const outside = (path: string): boolean => path === ".." || path.startsWith(".." + sep);
|
|
203
|
+
if (!outside(relative(targetPath, options.sourcePath)) || !outside(relative(options.sourcePath, targetPath))) {
|
|
204
|
+
fail("invalid-entry", targetPath, "source and destination overlap and may create a recursive skill link; choose a source in a separate directory");
|
|
205
|
+
}
|
|
206
|
+
return relative(dirname(targetPath), options.sourcePath);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function plan(options: PlanSkillEntryOptions): SkillPreview {
|
|
210
|
+
const { targetPath } = targetFor(options);
|
|
211
|
+
const { receipt, image } = readSkillReceipt(options);
|
|
212
|
+
const link = sourceLink(options, targetPath);
|
|
213
|
+
const prior = receipt?.pending ? receipt.pending.previous ?? undefined : receipt?.installed;
|
|
214
|
+
const moved = !!prior && prior.targetPath !== targetPath;
|
|
215
|
+
if (receipt?.pending && (receipt.installed.targetPath !== targetPath || receipt.installed.link !== link)) {
|
|
216
|
+
fail("pending-operation", targetPath,
|
|
217
|
+
`preview and apply the interrupted publication for ${receipt.installed.targetPath} from ${resolve(dirname(receipt.installed.targetPath), receipt.installed.link)} first`);
|
|
218
|
+
}
|
|
219
|
+
if (moved && !options.retarget) {
|
|
220
|
+
fail("profile-root-changed", targetPath, `recorded skill root is ${dirname(prior.targetPath)}; pass retarget: true to move it`);
|
|
221
|
+
}
|
|
222
|
+
if (receipt?.pending) {
|
|
223
|
+
const change = moved ? "retarget" : prior ? "replace" : "add";
|
|
224
|
+
return { change, targetPath, options: { ...options }, intent: pendingIntent(image) };
|
|
225
|
+
}
|
|
226
|
+
const present = checkAvailable(targetPath, moved ? undefined : prior);
|
|
227
|
+
const oldPresent = moved && atRecordedRoot(prior, () => checkAvailable(prior.targetPath, prior));
|
|
228
|
+
const change = moved ? "retarget" : !present ? "add" : prior?.link === link ? "unchanged" : "replace";
|
|
229
|
+
const intent = createHash("sha256").update(JSON.stringify([targetPath, link, image.bytes, image.mode, present, oldPresent])).digest("hex");
|
|
230
|
+
return { change, targetPath, options: { ...options }, intent };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function planSkillEntry(options: PlanSkillEntryOptions): SkillEntryPlan {
|
|
234
|
+
try {
|
|
235
|
+
return plan(options);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
const refusal = asEntryRefusal(error, "Plan skill entry");
|
|
238
|
+
return { change: "refused", code: refusal.code, reason: refusal.reason, file: refusal.file };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function removeOwned(link: LinkIdentity): boolean {
|
|
243
|
+
if (!checkAvailable(link.targetPath, link)) return false;
|
|
244
|
+
unlinkSync(link.targetPath);
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function atRecordedRoot<T>(link: LinkIdentity, action: () => T): T {
|
|
249
|
+
try {
|
|
250
|
+
return action();
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (!(error instanceof EntryRefusal) || error.code !== "unsafe-path") throw error;
|
|
253
|
+
fail("unsafe-path", link.targetPath,
|
|
254
|
+
`recorded skill root is redirected or unsafe; restore ${dirname(link.targetPath)} as a physical directory for undo, or move the receipt aside to stop tracking this link without following it`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function pendingIntent(image: ConfigImage): string {
|
|
259
|
+
return createHash("sha256").update(JSON.stringify(["pending", image.bytes, image.mode])).digest("hex");
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function rollback(path: string, image: ConfigImage, temporary: LinkIdentity, restoration: ConfigImage): void {
|
|
263
|
+
try {
|
|
264
|
+
const current = inspect(temporary.targetPath).identity;
|
|
265
|
+
if (sameLink(current, temporary)) unlinkSync(temporary.targetPath);
|
|
266
|
+
} finally {
|
|
267
|
+
// A redirected directory may make the staged link unreachable. Never follow it.
|
|
268
|
+
publishImage(path, receiptLimit, image, restoration);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Finish a publication that reached rename, or roll back one that did not. */
|
|
273
|
+
function recover(options: SkillEntryTarget): boolean {
|
|
274
|
+
const { path, image, receipt } = readSkillReceipt(options);
|
|
275
|
+
if (!receipt?.pending) return false;
|
|
276
|
+
const { installed, pending: { previous, temporary } } = receipt;
|
|
277
|
+
const current = atRecordedRoot(installed, () => inspect(installed.targetPath).identity);
|
|
278
|
+
if (sameLink(current, installed)) {
|
|
279
|
+
if (previous && previous.targetPath !== installed.targetPath) {
|
|
280
|
+
atRecordedRoot(previous, () => removeOwned(previous));
|
|
281
|
+
}
|
|
282
|
+
const { pending: _pending, ...committed } = receipt;
|
|
283
|
+
publishImage(path, receiptLimit, image, receiptImage(committed));
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
const restoration = previous ? receiptImage({ version: 1, host: receipt.host, name: receipt.name, installed: previous }) : absentImage;
|
|
287
|
+
rollback(path, image, temporary, restoration);
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export function applySkillEntry(preview: SkillEntryPlan): SkillEntryResult {
|
|
292
|
+
if (preview.change === "refused") throw new EntryRefusal(preview.code, preview.reason, { file: preview.file });
|
|
293
|
+
const options = preview.options;
|
|
294
|
+
const { targetPath, receiptPath } = targetFor(options);
|
|
295
|
+
try {
|
|
296
|
+
return withConfigLock(receiptPath, () => {
|
|
297
|
+
if (targetPath !== preview.targetPath) {
|
|
298
|
+
fail("changed-since-preview", targetPath, "skill target changed; preview it again before applying");
|
|
299
|
+
}
|
|
300
|
+
const staged = readSkillReceipt(options);
|
|
301
|
+
const freshPendingPreview = staged.receipt?.pending !== undefined && preview.intent === pendingIntent(staged.image);
|
|
302
|
+
const resumed = staged.receipt?.pending ? plan(options) : undefined;
|
|
303
|
+
if (recover(options) && resumed) {
|
|
304
|
+
return { change: resumed.change, targetPath, receipt: receiptPath };
|
|
305
|
+
}
|
|
306
|
+
const current = plan(options);
|
|
307
|
+
if (!freshPendingPreview
|
|
308
|
+
&& (current.intent !== preview.intent || current.change !== preview.change)) {
|
|
309
|
+
fail("changed-since-preview", targetPath, "skill state changed; preview it again before applying");
|
|
310
|
+
}
|
|
311
|
+
const result = { change: current.change, targetPath, receipt: receiptPath };
|
|
312
|
+
if (current.change === "unchanged") return result;
|
|
313
|
+
|
|
314
|
+
const { receipt, image } = readSkillReceipt(options);
|
|
315
|
+
const parentPath = dirname(targetPath);
|
|
316
|
+
ensureDirectory(parentPath);
|
|
317
|
+
const parent = checkPhysicalDirectory(parentPath)!;
|
|
318
|
+
const temporaryPath = join(parentPath, `.agent-hosts-skill-${randomUUID()}.tmp`);
|
|
319
|
+
let created = false;
|
|
320
|
+
let named: Stats | undefined;
|
|
321
|
+
let journal: { image: ConfigImage; link: LinkIdentity } | undefined;
|
|
322
|
+
let renamed = false;
|
|
323
|
+
try {
|
|
324
|
+
symlinkSync(sourceLink(options, targetPath), temporaryPath);
|
|
325
|
+
created = true;
|
|
326
|
+
named = lstatSync(temporaryPath);
|
|
327
|
+
const temporary = identify(temporaryPath, named);
|
|
328
|
+
const pending: SkillReceipt = {
|
|
329
|
+
version: 1, host: options.host, name: options.name, installed: { ...temporary, targetPath },
|
|
330
|
+
pending: { temporary, previous: receipt?.installed ?? null },
|
|
331
|
+
};
|
|
332
|
+
const stagedImage = receiptImage(pending);
|
|
333
|
+
publishImage(receiptPath, receiptLimit, image, stagedImage);
|
|
334
|
+
journal = { image: stagedImage, link: temporary };
|
|
335
|
+
const refreshed = checkPhysicalDirectory(parentPath);
|
|
336
|
+
if (!refreshed || parent.dev !== refreshed.dev || parent.ino !== refreshed.ino
|
|
337
|
+
|| !sameLink(inspect(temporaryPath).identity, temporary)) {
|
|
338
|
+
fail("file-changed", targetPath, "destination directory or staged link changed; inspect it before retrying");
|
|
339
|
+
}
|
|
340
|
+
const previous = receipt?.installed;
|
|
341
|
+
const stillPresent = checkAvailable(targetPath, previous?.targetPath === targetPath ? previous : undefined);
|
|
342
|
+
const expectedPresent = current.change === "replace";
|
|
343
|
+
if (stillPresent !== expectedPresent) {
|
|
344
|
+
fail("changed-since-preview", targetPath, "destination changed after staging; inspect it before retrying");
|
|
345
|
+
}
|
|
346
|
+
// ponytail: rename(2) can replace a foreign file or link created after this check.
|
|
347
|
+
// The receipt lock coordinates cooperating writers only.
|
|
348
|
+
renameSync(temporaryPath, targetPath);
|
|
349
|
+
renamed = true;
|
|
350
|
+
recover(options);
|
|
351
|
+
return result;
|
|
352
|
+
} catch (error) {
|
|
353
|
+
if (journal && !renamed) {
|
|
354
|
+
try {
|
|
355
|
+
rollback(receiptPath, journal.image, journal.link, image);
|
|
356
|
+
} catch (rollbackError) {
|
|
357
|
+
throw new AggregateError([error, rollbackError], "Skill staging failed and cleanup needs inspection");
|
|
358
|
+
}
|
|
359
|
+
} else if (created && !journal) {
|
|
360
|
+
try {
|
|
361
|
+
const currentParent = checkPhysicalDirectory(parentPath);
|
|
362
|
+
const current = stat(temporaryPath);
|
|
363
|
+
if (currentParent?.dev === parent.dev && currentParent.ino === parent.ino
|
|
364
|
+
&& current?.isSymbolicLink() && (!named || (current.dev === named.dev && current.ino === named.ino))) {
|
|
365
|
+
unlinkSync(temporaryPath);
|
|
366
|
+
}
|
|
367
|
+
} catch {
|
|
368
|
+
// Do not replace the publication error with a cleanup error.
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
throw error;
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
} catch (error) {
|
|
375
|
+
throw asEntryRefusal(error, "Apply skill entry", targetPath);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function undoSkillEntry(options: SkillEntryTarget): SkillEntryResult {
|
|
380
|
+
const { targetPath, receiptPath } = targetFor(options);
|
|
381
|
+
try {
|
|
382
|
+
if (!readSkillReceipt(options).receipt) return { change: "unchanged", code: "no-receipt", targetPath, receipt: receiptPath };
|
|
383
|
+
return withConfigLock(receiptPath, () => {
|
|
384
|
+
const staged = readSkillReceipt(options).receipt;
|
|
385
|
+
if (staged && staged.installed.targetPath !== targetPath) {
|
|
386
|
+
fail("profile-root-changed", targetPath, `use the recorded root ${dirname(staged.installed.targetPath)} to undo this skill`);
|
|
387
|
+
}
|
|
388
|
+
recover(options);
|
|
389
|
+
const { receipt, image } = readSkillReceipt(options);
|
|
390
|
+
if (!receipt) return { change: "unchanged", code: "no-receipt", targetPath, receipt: receiptPath };
|
|
391
|
+
const installed = receipt.installed;
|
|
392
|
+
if (installed.targetPath !== targetPath) {
|
|
393
|
+
fail("profile-root-changed", targetPath, `use the recorded root ${dirname(installed.targetPath)} to undo this skill`);
|
|
394
|
+
}
|
|
395
|
+
const removed = atRecordedRoot(installed, () => removeOwned(installed));
|
|
396
|
+
publishImage(receiptPath, receiptLimit, image, absentImage);
|
|
397
|
+
return removed
|
|
398
|
+
? { change: "removed", targetPath, receipt: receiptPath }
|
|
399
|
+
: { change: "unchanged", code: "entry-already-absent", targetPath, receipt: receiptPath };
|
|
400
|
+
});
|
|
401
|
+
} catch (error) {
|
|
402
|
+
throw asEntryRefusal(error, "Undo skill entry", targetPath);
|
|
403
|
+
}
|
|
404
|
+
}
|