更新 src/skill-sync.ts

This commit is contained in:
QQ 2026-11-11 11:08:16 +08:00
parent ae91f1a4f4
commit f0e87244bc
1 changed files with 28 additions and 8 deletions

View File

@ -150,6 +150,12 @@ export interface SkillSyncDeps {
client: () => CloudClient
settings: () => { enabled: boolean; skillsSyncEnabled: boolean; skillsDir: string }
log: (message: string) => void
/** Parking area for disabled skills; parked skills are synced in place. */
holdingDir?: () => string
}
async function isDirectory(path: string): Promise<boolean> {
return stat(path).then((value) => value.isDirectory()).catch(() => false)
}
/** The per-skill delivery record the platform's own tooling writes. */
@ -179,6 +185,11 @@ export class SkillSync {
return dir !== '' ? resolve(dir) : join(dshHome(), 'skills')
}
/** The parking area where disabled skills wait, outside the root. */
holdingRoot(): string {
return this.deps.holdingDir?.() ?? join(dshHome(), 'skills-disabled')
}
private statePath(): string {
return join(dshHome(), 'cloud-memory-skills.json')
}
@ -338,7 +349,11 @@ export class SkillSync {
// center lists them too once they are published.
if (state.uploaded[item.name] !== undefined) { stats.skipped += 1; 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.
@ -353,10 +368,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
}
@ -392,7 +407,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)
@ -474,17 +489,22 @@ export class SkillSync {
}
}
// 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