Skip to content

Commit 741a280

Browse files
fix(skills): fail closed on pre-rename resolution and make queued writes disposal-aware
Review follow-ups: the pre-rename realpath has no fallback to the stale path (the error propagates, the owned sidecar is removed, and the rename is skipped), the update rename-rejection test asserts the existing SKILL.md and directory survive, and queued writes reject when the manager is disposed before the operation starts and re-check disposal after acquiring the directory lock and before staging/validation/rename. Descriptor-relative publication is tracked in Zoo-Code-Org#2203. SkillsManager.spec.ts 128 passed; tsc clean.
1 parent ca68406 commit 741a280

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

‎src/services/skills/SkillsManager.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,11 @@ export class SkillsManager {
743743
// back to the resolved path when the directory cannot be resolved (e.g. not created yet).
744744
const key = await fs.realpath(directory).catch(() => path.resolve(directory))
745745
const previous = SkillsManager.directoryQueues.get(key) ?? Promise.resolve()
746-
const run = previous.then(operation)
746+
const run = previous.then(async () => {
747+
// A manager disposed while this operation waited must not run at all.
748+
if (this.isDisposed) throw new Error("SkillsManager is disposed")
749+
return operation()
750+
})
747751
const tracked = run.then(
748752
() => undefined,
749753
() => undefined,
@@ -774,12 +778,15 @@ export class SkillsManager {
774778
// it: when wx fails because the path already exists, that file belongs to someone else.
775779
let ownsTemporaryPath = false
776780
try {
781+
if (this.isDisposed) throw new Error("SkillsManager is disposed")
777782
// Exclusive creation: a pre-created symlink at the generated name cannot be followed.
778783
await fs.writeFile(temporaryPath, content, { encoding: "utf-8", flag: "wx" })
779784
ownsTemporaryPath = true
785+
if (this.isDisposed) throw new Error("SkillsManager is disposed")
780786
// Re-validate the final target immediately before the rename: the target (or an
781787
// ancestor) can change between the caller's resolution and this publish.
782788
await validateTarget?.(filePath)
789+
if (this.isDisposed) throw new Error("SkillsManager is disposed")
783790
await fs.rename(temporaryPath, filePath)
784791
} catch (error) {
785792
// Best effort: a partial temp file must not be left behind, but an EEXIST failure
@@ -1235,7 +1242,9 @@ Add your skill instructions here.
12351242
const targetPath = await fs.realpath(skill.path)
12361243
await this.assertTargetWithinSkillsRoots(targetPath, source)
12371244
await this.writeSkillFileAtomically(targetPath, newContent, async (target) => {
1238-
const freshTarget = await fs.realpath(target).catch(() => target)
1245+
// Fail closed: a resolution failure must propagate so the staged temp write is removed
1246+
// and the rename is skipped, rather than publishing through a stale path.
1247+
const freshTarget = await fs.realpath(target)
12391248
await this.assertTargetWithinSkillsRoots(freshTarget, source)
12401249
})
12411250

‎src/services/skills/__tests__/SkillsManager.spec.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,9 +2632,53 @@ Body here.`
26322632
// the failed rename never committed.
26332633
expect(mockWriteFile).toHaveBeenCalledTimes(1)
26342634
expect(mockRename).toHaveBeenCalledWith(temporaryPath, skillMd)
2635+
// The existing SKILL.md and its directory remain untouched.
2636+
expect(mockReadFile).toHaveBeenCalledWith(skillMd, "utf-8")
2637+
expect(mockWriteFile).not.toHaveBeenCalledWith(skillMd, expect.anything(), expect.anything())
2638+
expect(mockRmdir).not.toHaveBeenCalled()
2639+
expect(mockRm).not.toHaveBeenCalled()
26352640
expect(skillsManager["activeTempFiles"].size).toBe(0)
26362641
})
26372642

2643+
it("fails closed when the pre-rename target resolution fails", async () => {
2644+
const skillDir = p(globalSkillsDir, "resolution-fails")
2645+
const skillMd = p(skillDir, "SKILL.md")
2646+
mockDirectoryExists.mockImplementation(async (dir: string) => dir === globalSkillsDir)
2647+
mockReaddir.mockImplementation(async (dir: string) => (dir === globalSkillsDir ? ["resolution-fails"] : []))
2648+
mockStat.mockImplementation(async (pathArg: string) => {
2649+
if (pathArg === skillDir) {
2650+
return { isDirectory: () => true }
2651+
}
2652+
throw enoent()
2653+
})
2654+
mockFileExists.mockImplementation(async (file: string) => file === skillMd)
2655+
mockReadFile.mockResolvedValue("---\nname: resolution-fails\ndescription: A skill\n---\nInstructions")
2656+
// The first resolution succeeds; the immediate pre-rename resolution fails and must
2657+
// propagate instead of publishing through the stale path.
2658+
let resolutions = 0
2659+
mockRealpath.mockImplementation(async (pathArg: string) => {
2660+
if (pathArg === skillMd) {
2661+
resolutions += 1
2662+
if (resolutions === 1) {
2663+
return skillMd
2664+
}
2665+
throw Object.assign(new Error("EIO: resolution failed"), { code: "EIO" })
2666+
}
2667+
return pathArg
2668+
})
2669+
mockWriteFile.mockResolvedValue(undefined)
2670+
mockUnlink.mockResolvedValue(undefined)
2671+
2672+
await skillsManager.discoverSkills()
2673+
2674+
await expect(skillsManager.updateSkillModes("resolution-fails", "global", ["code"])).rejects.toThrow(
2675+
"EIO: resolution failed",
2676+
)
2677+
const temporaryPath = String(mockWriteFile.mock.calls[0][0])
2678+
expect(mockUnlink).toHaveBeenCalledWith(temporaryPath)
2679+
expect(mockRename).not.toHaveBeenCalled()
2680+
})
2681+
26382682
it("refuses to update a SKILL.md that resolves outside the skills roots", async () => {
26392683
const skillDir = p(globalSkillsDir, "escaping")
26402684
const skillMd = p(skillDir, "SKILL.md")
@@ -4158,6 +4202,18 @@ Instructions`)
41584202
expect(createWatcher).not.toHaveBeenCalled()
41594203
})
41604204

4205+
it("rejects a queued write once the manager is disposed", async () => {
4206+
await skillsManager.dispose()
4207+
mockWriteFile.mockResolvedValue(undefined)
4208+
mockUnlink.mockResolvedValue(undefined)
4209+
4210+
await expect(
4211+
skillsManager["writeSkillFileAtomically"](p(globalSkillsDir, "SKILL.md"), "content"),
4212+
).rejects.toThrow("SkillsManager is disposed")
4213+
expect(mockWriteFile).not.toHaveBeenCalled()
4214+
expect(mockRename).not.toHaveBeenCalled()
4215+
})
4216+
41614217
it("removes tracked temp sidecars on dispose", async () => {
41624218
mockUnlink.mockResolvedValue(undefined)
41634219
skillsManager["activeTempFiles"].add("/mock/skills/foo/.SKILL.md.123.abc.tmp")

0 commit comments

Comments
 (0)