fix(setup): preserve skill files and report rollback failures
Track attempted writes, preserve original content and modes from one regular-file observation, and continue rollback after individual failures. Return tagged failure details and retained backup paths through setup. Validation: setup filesystem and recovery tests, combined compile/typecheck/lint/format/metadata/architecture checks. Filesystem fault injection covers backup, ambiguous publication, and rollback failures.
This commit is contained in:
@@ -2,11 +2,14 @@ import { createHash } from "node:crypto";
|
||||
import {
|
||||
access,
|
||||
chmod,
|
||||
copyFile,
|
||||
mkdir,
|
||||
readFile,
|
||||
rm,
|
||||
stat,
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import { constants as fsConstants } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
@@ -14,6 +17,8 @@ import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import {
|
||||
canonicalSkillNeedsInstall,
|
||||
installCanonicalSkill,
|
||||
skillDestinations,
|
||||
type SkillInstallFileOperations,
|
||||
} from "../../../src/application/SetupSkill.js";
|
||||
import {
|
||||
runDoctor,
|
||||
@@ -22,10 +27,141 @@ import {
|
||||
import { createDoctorHostFixture } from "../../../src/application/Doctor.fixture.js";
|
||||
import { TOOL_CONTRACTS } from "../../../src/contracts/toolContracts.js";
|
||||
import { PRODUCT_IDENTITY } from "../../../src/identity.js";
|
||||
import writeFileAtomic from "write-file-atomic";
|
||||
import { z } from "zod";
|
||||
import { skillReferenceIssues } from "../../../scripts/lib/docs-facts.mjs";
|
||||
|
||||
const expectPermissionMode = async (
|
||||
path: string,
|
||||
expected: number,
|
||||
): Promise<void> => {
|
||||
if (process.platform === "win32") return;
|
||||
expect((await stat(path)).mode & 0o777).toBe(expected);
|
||||
};
|
||||
|
||||
describe("canonical skill transaction", () => {
|
||||
it("does not roll back files when backup preparation fails", async () => {
|
||||
const home = await createTestTempDirectory("rea-skill-backup-failure-");
|
||||
const root = join(home, ".agents/skills/reverse-engineer-anything");
|
||||
const manifest = join(root, "SKILL.md");
|
||||
const connection = join(root, "references/connection-and-recovery.md");
|
||||
const original = "operator-owned manifest bytes\n";
|
||||
const secondOriginal = "operator-owned reference bytes\n";
|
||||
await mkdir(dirname(connection), { recursive: true });
|
||||
await writeFile(manifest, original, { mode: 0o640 });
|
||||
await writeFile(connection, secondOriginal, { mode: 0o644 });
|
||||
const writes: string[] = [];
|
||||
let backups = 0;
|
||||
const operations: SkillInstallFileOperations = {
|
||||
mkdir: async (path) => {
|
||||
await mkdir(path, { recursive: true });
|
||||
},
|
||||
backup: async (source, target) => {
|
||||
backups += 1;
|
||||
if (backups === 2) throw new Error("injected backup failure");
|
||||
await copyFile(source, target, fsConstants.COPYFILE_EXCL);
|
||||
},
|
||||
write: async (path, content, mode) => {
|
||||
writes.push(path);
|
||||
await writeFile(path, content, { mode });
|
||||
},
|
||||
remove: async (path) => rm(path, { force: true }),
|
||||
};
|
||||
|
||||
const result = await installCanonicalSkill(
|
||||
home,
|
||||
[],
|
||||
{},
|
||||
process.platform,
|
||||
operations,
|
||||
);
|
||||
|
||||
if (result.ok) throw new Error("Expected backup failure");
|
||||
expect(result.error.stage).toBe("backup");
|
||||
expect(result.error.rollbackFailures).toEqual([]);
|
||||
expect(writes).toEqual([]);
|
||||
expect(await readFile(manifest, "utf8")).toBe(original);
|
||||
expect(await readFile(connection, "utf8")).toBe(secondOriginal);
|
||||
expect(await readFile(`${manifest}.rea.backup`, "utf8")).toBe(original);
|
||||
await expect(
|
||||
readFile(`${connection}.rea.backup`, "utf8"),
|
||||
).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
await expectPermissionMode(manifest, 0o640);
|
||||
await expectPermissionMode(connection, 0o644);
|
||||
});
|
||||
|
||||
it("rolls back attempted writes independently and preserves original modes", async () => {
|
||||
const home = await createTestTempDirectory("rea-skill-rollback-failure-");
|
||||
const [destination] = skillDestinations(home, []);
|
||||
if (destination === undefined) throw new Error("Missing shared skill root");
|
||||
const root = destination.path;
|
||||
const manifest = join(root, "SKILL.md");
|
||||
const connection = join(root, "references/connection-and-recovery.md");
|
||||
const native = join(root, "references/native-and-artifacts.md");
|
||||
await mkdir(dirname(native), { recursive: true });
|
||||
await writeFile(manifest, "old manifest\n", { mode: 0o640 });
|
||||
await writeFile(connection, "old connection\n", { mode: 0o644 });
|
||||
await writeFile(native, "old native\n", { mode: 0o600 });
|
||||
const writes = new Map<string, number>();
|
||||
const nativeWrites: string[] = [];
|
||||
const operations: SkillInstallFileOperations = {
|
||||
mkdir: async (path) => {
|
||||
await mkdir(path, { recursive: true });
|
||||
},
|
||||
backup: async (source, target) =>
|
||||
copyFile(source, target, fsConstants.COPYFILE_EXCL),
|
||||
write: async (path, content, mode) => {
|
||||
const attempt = (writes.get(path) ?? 0) + 1;
|
||||
writes.set(path, attempt);
|
||||
if (path === native) nativeWrites.push(content);
|
||||
if (path === native && attempt === 2)
|
||||
throw new Error("injected rollback failure");
|
||||
await writeFileAtomic(path, content, { encoding: "utf8", mode });
|
||||
if (path === native && attempt === 1)
|
||||
throw new Error("injected ambiguous publication failure");
|
||||
},
|
||||
remove: async (path) => rm(path, { force: true }),
|
||||
};
|
||||
|
||||
const result = await installCanonicalSkill(
|
||||
home,
|
||||
[],
|
||||
{},
|
||||
process.platform,
|
||||
operations,
|
||||
);
|
||||
|
||||
if (result.ok) throw new Error("Expected write failure");
|
||||
expect(result.error.stage).toBe("write");
|
||||
expect(result.error.path).toBe(native);
|
||||
expect(result.error.rollbackFailures).toEqual([
|
||||
{ path: native, reason: "injected rollback failure" },
|
||||
]);
|
||||
expect(result.error.recoveryBackupPaths).toEqual([
|
||||
`${manifest}.rea.backup`,
|
||||
`${connection}.rea.backup`,
|
||||
`${native}.rea.backup`,
|
||||
]);
|
||||
expect(result.error.message).toContain(
|
||||
`${native} (injected rollback failure)`,
|
||||
);
|
||||
expect(await readFile(manifest, "utf8")).toBe("old manifest\n");
|
||||
expect(await readFile(connection, "utf8")).toBe("old connection\n");
|
||||
await expectPermissionMode(manifest, 0o640);
|
||||
await expectPermissionMode(connection, 0o644);
|
||||
expect(await readFile(native, "utf8")).not.toBe("old native\n");
|
||||
expect(await readFile(`${native}.rea.backup`, "utf8")).toBe("old native\n");
|
||||
expect(nativeWrites).toHaveLength(2);
|
||||
expect(nativeWrites[1]).toBe("old native\n");
|
||||
expect(result.error.message).toContain(
|
||||
`Existing-file backups remain at ${manifest}.rea.backup`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("canonical skill discovery", () => {
|
||||
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
|
||||
"preserves an unreadable existing skill instead of treating it as absent",
|
||||
async () => {
|
||||
@@ -39,7 +175,9 @@ describe("canonical skill transaction", () => {
|
||||
await writeFile(destination, original, { mode: 0o200 });
|
||||
try {
|
||||
await chmod(destination, 0o200);
|
||||
expect(await installCanonicalSkill(home)).toBe("failed");
|
||||
const result = await installCanonicalSkill(home);
|
||||
if (result.ok) throw new Error("Expected unreadable skill failure");
|
||||
expect(result.error.stage).toBe("prepare");
|
||||
} finally {
|
||||
await chmod(destination, 0o600);
|
||||
}
|
||||
@@ -89,7 +227,7 @@ describe("canonical skill transaction", () => {
|
||||
await installCanonicalSkill(home, ["claude_code"], {
|
||||
CLAUDE_CONFIG_DIR: dirname(skillsDirectory),
|
||||
}),
|
||||
).toBe("installed");
|
||||
).toMatchObject({ ok: true, value: "installed" });
|
||||
expect(
|
||||
await canonicalSkillNeedsInstall(home, ["claude_code"], {
|
||||
CLAUDE_CONFIG_DIR: dirname(skillsDirectory),
|
||||
@@ -125,24 +263,24 @@ describe("canonical skill transaction", () => {
|
||||
});
|
||||
const doctorScope = { clients: ["claude_code", "codex"], skill: true };
|
||||
|
||||
expect(await installCanonicalSkill(home, ["claude_code", "codex"])).toBe(
|
||||
"installed",
|
||||
);
|
||||
expect(
|
||||
await installCanonicalSkill(home, ["claude_code", "codex"]),
|
||||
).toMatchObject({ ok: true, value: "installed" });
|
||||
await rm(sharedSkill);
|
||||
expect(
|
||||
(await runDoctor(undefined, host, doctorScope)).identity?.skill.state,
|
||||
).toBe("stale");
|
||||
expect(await installCanonicalSkill(home, ["claude_code", "codex"])).toBe(
|
||||
"installed",
|
||||
);
|
||||
expect(
|
||||
await installCanonicalSkill(home, ["claude_code", "codex"]),
|
||||
).toMatchObject({ ok: true, value: "installed" });
|
||||
for (const skill of [sharedSkill, claudeSkill]) {
|
||||
await writeFile(skill, "selected copy changed\n");
|
||||
expect(
|
||||
(await runDoctor(undefined, host, doctorScope)).identity?.skill.state,
|
||||
).toBe("stale");
|
||||
expect(await installCanonicalSkill(home, ["claude_code", "codex"])).toBe(
|
||||
"installed",
|
||||
);
|
||||
expect(
|
||||
await installCanonicalSkill(home, ["claude_code", "codex"]),
|
||||
).toMatchObject({ ok: true, value: "installed" });
|
||||
expect(
|
||||
(await runDoctor(undefined, host, doctorScope)).identity?.skill.state,
|
||||
).toBe("aligned");
|
||||
@@ -165,12 +303,15 @@ describe("canonical skill transaction", () => {
|
||||
await mkdir(dirname(destination), { recursive: true });
|
||||
await mkdir(dirname(nativeGuide), { recursive: true });
|
||||
await mkdir(dirname(sibling), { recursive: true });
|
||||
await writeFile(destination, "stale managed skill\n");
|
||||
await writeFile(destination, "stale managed skill\n", { mode: 0o640 });
|
||||
await writeFile(nativeGuide, "stale filesystem-write permission grant\n");
|
||||
await writeFile(sibling, "unrelated skill\n");
|
||||
|
||||
expect(await canonicalSkillNeedsInstall(home)).toBe(true);
|
||||
expect(await installCanonicalSkill(home)).toBe("installed");
|
||||
expect(await installCanonicalSkill(home)).toMatchObject({
|
||||
ok: true,
|
||||
value: "installed",
|
||||
});
|
||||
expect(await readFile(`${destination}.rea.backup`, "utf8")).toBe(
|
||||
"stale managed skill\n",
|
||||
);
|
||||
@@ -187,6 +328,7 @@ describe("canonical skill transaction", () => {
|
||||
expect(installedSkill).toContain(
|
||||
`version: "${PRODUCT_IDENTITY.skillVersion}"`,
|
||||
);
|
||||
await expectPermissionMode(destination, 0o640);
|
||||
expect(await readFile(`${nativeGuide}.rea.backup`, "utf8")).toBe(
|
||||
"stale filesystem-write permission grant\n",
|
||||
);
|
||||
@@ -232,13 +374,19 @@ describe("canonical skill transaction", () => {
|
||||
),
|
||||
).toContain("analyze_javascript_application");
|
||||
expect(await canonicalSkillNeedsInstall(home)).toBe(false);
|
||||
expect(await installCanonicalSkill(home)).toBe("unchanged");
|
||||
expect(await installCanonicalSkill(home)).toMatchObject({
|
||||
ok: true,
|
||||
value: "unchanged",
|
||||
});
|
||||
expect(await skillReferenceIssues(dirname(destination))).toEqual([]);
|
||||
});
|
||||
|
||||
it("binds portable conformance to the generated skill bytes that setup installs", async () => {
|
||||
const home = await createTestTempDirectory("rea-skill-commitment-");
|
||||
expect(await installCanonicalSkill(home)).toBe("installed");
|
||||
expect(await installCanonicalSkill(home)).toMatchObject({
|
||||
ok: true,
|
||||
value: "installed",
|
||||
});
|
||||
const paths = [
|
||||
"SKILL.md",
|
||||
"references/android-applications.md",
|
||||
@@ -298,7 +446,10 @@ it("doctor verifies installed instructions and references even when metadata is
|
||||
expect((await runDoctor(undefined, host)).identity?.skill.state).toBe(
|
||||
"missing",
|
||||
);
|
||||
expect(await installCanonicalSkill(home)).toBe("installed");
|
||||
expect(await installCanonicalSkill(home)).toMatchObject({
|
||||
ok: true,
|
||||
value: "installed",
|
||||
});
|
||||
expect((await runDoctor(undefined, host)).identity?.skill).toMatchObject({
|
||||
state: "aligned",
|
||||
});
|
||||
@@ -312,7 +463,10 @@ it("doctor verifies installed instructions and references even when metadata is
|
||||
expect((await runDoctor(undefined, host)).identity?.skill.state).toBe(
|
||||
"stale",
|
||||
);
|
||||
expect(await installCanonicalSkill(home)).toBe("installed");
|
||||
expect(await installCanonicalSkill(home)).toMatchObject({
|
||||
ok: true,
|
||||
value: "installed",
|
||||
});
|
||||
expect((await runDoctor(undefined, host)).identity?.skill.state).toBe(
|
||||
"aligned",
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user