更新 lib/skill-sync.js

This commit is contained in:
QQ 2026-11-11 11:07:37 +08:00
parent f54408e2c7
commit 12044dc45f
1 changed files with 25 additions and 8 deletions

View File

@ -103,6 +103,9 @@ export function parseSkillFrontmatter(markdown) {
}
return out;
}
async function isDirectory(path) {
return stat(path).then((value) => value.isDirectory()).catch(() => false);
}
export class SkillSync {
deps;
running = false;
@ -121,6 +124,10 @@ export class SkillSync {
const dir = this.deps.settings().skillsDir;
return dir !== '' ? resolve(dir) : join(dshHome(), 'skills');
}
/** The parking area where disabled skills wait, outside the root. */
holdingRoot() {
return this.deps.holdingDir?.() ?? join(dshHome(), 'skills-disabled');
}
statePath() {
return join(dshHome(), 'cloud-memory-skills.json');
}
@ -286,7 +293,11 @@ export class SkillSync {
continue;
}
const dir = join(root, item.name);
const exists = await stat(dir).then((s) => s.isDirectory()).catch(() => false);
const parkedDir = join(this.holdingRoot(), item.name);
const inRoot = await isDirectory(dir);
const parked = !inRoot && (await isDirectory(parkedDir));
const home = parked ? parkedDir : dir;
const exists = inRoot || parked;
const removed = state.removed[item.name];
if (removed !== undefined && exists) {
// The user put it back by hand; the removal is over.
@ -303,10 +314,10 @@ export class SkillSync {
delete state.removed[item.name];
stateDirty = true;
}
const delivery = await this.readDelivery(dir);
const delivery = await this.readDelivery(home);
if (exists && delivery !== undefined) {
// Delivered before: local integrity is judged before any download.
if (!(await this.deliveredIntact(dir, delivery))) {
if (!(await this.deliveredIntact(home, delivery))) {
this.conflict(stats, item.name, 'locally_modified');
continue;
}
@ -342,7 +353,7 @@ export class SkillSync {
this.conflict(stats, item.name, 'script_review_required');
continue;
}
await this.install(item, bundle.data, root);
await this.install(item, bundle.data, parked ? this.holdingRoot() : root);
state.delivered[item.name] = { manifest: bundle.data.manifest_sha256 };
stateDirty = true;
(exists ? stats.updated : stats.downloaded).push(item.name);
@ -429,17 +440,23 @@ export class SkillSync {
stats.updated.push(skill.name);
}
}
// A vanished local folder is remembered as a deliberate removal (so
// the next pass does not resurrect it); the cloud copy always stays.
// Presence counts the parking area too: a parked skill is disabled,
// not deleted. Anything missing from both is a deliberate removal
// (remembered, so it is not resurrected); the cloud copy stays.
const presentFolders = new Set(seenFolders);
for (const entry of await readdir(this.holdingRoot(), { withFileTypes: true }).catch(() => [])) {
if (entry.isDirectory())
presentFolders.add(entry.name);
}
for (const [name, record] of Object.entries(state.uploaded)) {
if (!seenFolders.has(record.folder)) {
if (!presentFolders.has(record.folder)) {
state.removed[name] = { manifest: cloudByName.get(name)?.manifest_sha256 ?? record.manifest };
delete state.uploaded[name];
stateDirty = true;
}
}
for (const [name, record] of Object.entries(state.delivered)) {
if (!seenFolders.has(name)) {
if (!presentFolders.has(name)) {
state.removed[name] = { manifest: record.manifest };
delete state.delivered[name];
stateDirty = true;