From 3336fa8d26f26bff1df5ca8dc6b2d946277afcdc Mon Sep 17 00:00:00 2001 From: dsh-bot Date: Wed, 11 Nov 2026 23:07:22 +0800 Subject: [PATCH] feat(p3): skill marketplace, sync conflict resolution, update/rollback --- client/index.js | 271 ++++++++++++++++++++++++++---- docs/management-surface.md | 52 ++++++ lib/index.js | 25 ++- lib/market.js | 217 ++++++++++++++++++++++++ lib/mnemon/index.js | 2 +- lib/routes.js | 209 ++++++++++++++++++++++- lib/skill-admin.js | 97 ++++++++++- lib/skill-sync.js | 190 ++++++++++++++++++++- lib/skill-update.js | 289 ++++++++++++++++++++++++++++++++ package.json | 2 +- src/index.ts | 27 ++- src/market.ts | 306 ++++++++++++++++++++++++++++++++++ src/mnemon/index.ts | 2 +- src/routes.ts | 206 ++++++++++++++++++++++- src/skill-admin.ts | 145 ++++++++++++++-- src/skill-sync.ts | 199 +++++++++++++++++++++- src/skill-update.ts | 329 +++++++++++++++++++++++++++++++++++++ verify-brain-panel.mjs | 4 +- verify-card-render.mjs | 2 +- verify-market.mjs | 153 +++++++++++++++++ verify-skill-update.mjs | 79 +++++++++ verify-sync-resolve.mjs | 98 +++++++++++ 22 files changed, 2830 insertions(+), 74 deletions(-) create mode 100644 lib/market.js create mode 100644 lib/skill-update.js create mode 100644 src/market.ts create mode 100644 src/skill-update.ts create mode 100644 verify-market.mjs create mode 100644 verify-skill-update.mjs create mode 100644 verify-sync-resolve.mjs diff --git a/client/index.js b/client/index.js index db90c0a..174e483 100644 --- a/client/index.js +++ b/client/index.js @@ -27,6 +27,8 @@ window.__ModuleLoader__.load({ test: '/cloud-memory/test', flush: '/cloud-memory/flush', skills: '/cloud-memory/skills', + market: '/cloud-memory/market', + sync: '/cloud-memory/sync', } // The MCP bridge is another plugin's public surface, not ours: it is probed @@ -654,6 +656,232 @@ window.__ModuleLoader__.load({ return undefined } + // ---- P3: skill market ------------------------------------------------- + function MarketSection(props) { + var capabilities = props.capabilities || {} + var tuple = React.useState({ + providers: null, provider: '', query: '', entries: [], nextCursor: null, cursor: 0, + status: 'idle', error: null, detail: null, plan: null, selected: {}, busy: '', notice: null, + }) + var state = tuple[0] + var setState = tuple[1] + function patch(next) { setState(function (prev) { return Object.assign({}, prev, next) }) } + var available = capabilities.features !== undefined && capabilities.features.skillsMarketplace === true + + React.useEffect(function () { + if (available) loadProviders() + }, []) + + function loadProviders() { + request(API.market + '/providers', { method: 'GET' }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var list = Array.isArray(res.body.providers) ? res.body.providers : [] + patch({ providers: list, provider: state.provider !== '' ? state.provider : (list.length > 0 ? list[0].id : '') }) + } else { + patch({ error: bodyError(res, '读取市场来源失败') }) + } + }, function (error) { patch({ error: messageOf(error) }) }) + } + function search(append) { + if (state.provider === '') return + var cursor = append === true ? state.cursor : 0 + patch({ busy: 'search', error: null, status: 'loading' }) + var url = API.market + '/search?provider=' + encodeURIComponent(state.provider) + '&q=' + encodeURIComponent(state.query) + '&cursor=' + String(cursor) + request(url, { method: 'GET' }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var entries = append === true ? state.entries.concat(res.body.entries || []) : (res.body.entries || []) + patch({ entries: entries, nextCursor: res.body.nextCursor === undefined ? null : res.body.nextCursor, cursor: res.body.nextCursor === undefined ? cursor : res.body.nextCursor, status: 'ready', busy: '', detail: null, plan: null }) + } else { + patch({ status: 'error', busy: '', error: bodyError(res, '市场搜索失败') }) + } + }, function (error) { patch({ status: 'error', busy: '', error: messageOf(error) }) }) + } + function openDetail(entry) { + patch({ busy: 'detail', detail: null, plan: null, notice: null }) + postJson(API.market + '/detail', { provider: state.provider, id: entry.id }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ detail: res.body, busy: '' }) + else patch({ busy: '', error: bodyError(res, '读取条目详情失败') }) + }, function (error) { patch({ busy: '', error: messageOf(error) }) }) + } + function makePlan() { + if (state.detail === null) return + patch({ busy: 'plan', plan: null, notice: null }) + postJson(API.market + '/plan', { provider: state.provider, id: state.detail.id }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var selected = {} + ;(res.body.candidates || []).forEach(function (candidate) { + if (candidate.conflict !== true && candidate.problem === undefined) selected[candidate.folder] = true + }) + patch({ plan: res.body, selected: selected, busy: '' }) + } else patch({ busy: '', error: bodyError(res, '无法生成安装计划') }) + }, function (error) { patch({ busy: '', error: messageOf(error) }) }) + } + function toggleFolder(folder) { + var next = Object.assign({}, state.selected) + next[folder] = next[folder] !== true + patch({ selected: next }) + } + function commitPlan() { + if (state.plan === null) return + var folders = Object.keys(state.selected).filter(function (folder) { return state.selected[folder] === true }) + if (folders.length === 0) { patch({ notice: { tone: 'warn', text: '请至少选中一个技能。' } }); return } + patch({ busy: 'commit' }) + postJson(API.market + '/commit', { planId: state.plan.planId, folders: folders }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var skipped = Array.isArray(res.body.skipped) ? res.body.skipped.length : 0 + patch({ busy: '', plan: null, detail: null, notice: { tone: 'ok', text: '已安装:' + ((res.body.installed || []).join('、') || '无') + (skipped > 0 ? ';跳过 ' + skipped + ' 项' : '') } }) + if (typeof props.onInstalled === 'function') props.onInstalled() + } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '安装失败') } }) + }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) + } + + if (!available) { + return h(Section, { title: '技能市场', hint: '本版没有配置经验证的市场来源。' }, + h('p', { style: styles.hint }, '能力缺口:capabilities.features.skillsMarketplace = false,因此不显示任何搜索框或示例条目。')) + } + var providerLabel = state.provider + if (state.providers !== null) { + state.providers.forEach(function (provider) { if (provider.id === state.provider) providerLabel = provider.label }) + } + var resultRows = state.entries.map(function (entry) { + return h('div', { key: entry.id, style: styles.row }, + h('div', { style: styles.rowHead }, + h('span', { style: styles.rowTitle }, entry.name), + h('div', { style: styles.pills }, + entry.version === undefined ? null : h(Pill, { label: '版本', value: String(entry.version), tone: 'muted' }), + h(Pill, { label: '技能', value: (entry.skills || []).length, tone: 'muted' }), + entry.author === undefined ? null : h(Pill, { label: '作者', value: String(entry.author), tone: 'muted' }), + ), + ), + h('p', { style: styles.hint }, entry.description === '' ? '(无描述)' : entry.description), + h('div', { style: styles.actions }, h(Btn, { label: '查看详情', variant: 'small', disabled: state.busy !== '', onClick: function () { openDetail(entry) } })), + ) + }) + var detailBox = null + if (state.detail !== null) { + var details = Array.isArray(state.detail.skillDetails) ? state.detail.skillDetails : [] + detailBox = h('div', { style: styles.nested }, + h('p', { style: styles.hint }, '来源:' + String(state.detail.sourceRepo) + ' · ' + (state.detail.description || '')), + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '4px' } }, + details.map(function (skill) { + return h('div', { key: skill.folder, style: styles.rowHead }, + h('span', { style: styles.rowTitle }, skill.name === undefined ? skill.folder : skill.name), + h('span', { style: styles.hint }, skill.ok === false ? '(无法读取 SKILL.md)' : String(skill.description || '')), + ) + }), + ), + h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: state.busy === 'plan' ? '拉取中…' : '拉取安装计划', disabled: state.busy !== '', onClick: makePlan })), + ) + } + var planBox = null + if (state.plan !== null) { + var candidates = Array.isArray(state.plan.candidates) ? state.plan.candidates : [] + planBox = h('div', { style: styles.nested }, + h('p', { style: styles.hint }, '已固定提交:' + (state.plan.commit === undefined ? '克隆时记录实际提交' : String(state.plan.commit).slice(0, 12)) + ';只安装勾选项,冲突项默认不勾,不执行仓库脚本。'), + candidates.length === 0 ? h('p', { style: styles.hint }, '该条目没有可安装的技能。') : null, + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '6px' } }, + candidates.map(function (candidate) { + return h('label', { key: candidate.folder, style: styles.checkboxRow }, + h('input', { type: 'checkbox', checked: state.selected[candidate.folder] === true, disabled: candidate.conflict === true || candidate.problem !== undefined, onChange: function () { toggleFolder(candidate.folder) } }), + h('span', null, candidate.folder + ' · ' + String(candidate.files) + ' 个文件 · ' + formatBytes(candidate.bytes) + (candidate.conflict === true ? ' · 已存在,跳过' : '') + (candidate.problem === undefined ? '' : ' · ' + candidate.problem)), + ) + }), + ), + h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: state.busy === 'commit' ? '安装中…' : '安装选中', disabled: state.busy !== '', onClick: commitPlan })), + ) + } + return h(Section, { title: '技能市场', hint: '来自经验证的公开市场清单;安装走暂存计划、只装勾选项并固定提交。' }, + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, + h('div', { style: styles.pills }, + h(Pill, { label: '来源', value: String(providerLabel || '未配置'), tone: 'muted' }), + state.providers === null ? null : h(Pill, { label: '已接入', value: state.providers.length, tone: 'ok' }), + ), + h('div', { style: { display: 'flex', gap: '8px', alignItems: 'flex-end', flexWrap: 'wrap' } }, + h('div', { style: { flex: '1 1 220px', minWidth: 0 } }, h(Field, { label: '搜索市场', value: state.query, placeholder: '例如 xlsx / pdf / design', onChange: function (value) { patch({ query: value }) } })), + h(Btn, { variant: 'primary', label: state.busy === 'search' ? '搜索中…' : '搜索', disabled: state.busy !== '', onClick: function () { search(false) } }), + ), + state.error === null ? null : h(Banner, { tone: 'bad' }, state.error), + state.notice === null ? null : h(Banner, { tone: state.notice.tone }, state.notice.text), + state.status === 'ready' && state.entries.length === 0 ? h('p', { style: styles.hint }, '没有匹配的市场条目。') : null, + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, resultRows), + state.nextCursor === null ? null : h('div', { style: styles.actions }, h(Btn, { label: '加载更多', variant: 'small', disabled: state.busy !== '', onClick: function () { search(true) } })), + detailBox, + planBox, + ), + ) + } + + // ---- P3: interactive sync conflict resolution ------------------------- + function SyncResolveSection(props) { + var tuple = React.useState({ status: 'idle', data: null, busy: '', error: null, notice: null }) + var state = tuple[0] + var setState = tuple[1] + function patch(next) { setState(function (prev) { return Object.assign({}, prev, next) }) } + React.useEffect(function () { load() }, []) + function load() { + patch({ status: 'loading', error: null }) + request(API.sync + '/status', { method: 'GET' }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ status: 'ready', data: res.body }) + else patch({ status: 'error', error: bodyError(res, '读取同步状态失败') }) + }, function (error) { patch({ status: 'error', error: messageOf(error) }) }) + } + function resolve(name, action) { + patch({ busy: name + ':' + action, notice: null }) + postJson(API.sync + '/resolve', { name: name, action: action }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var stats = state.data === null ? null : state.data.stats + patch({ busy: '', data: { stats: stats, conflicts: res.body.conflicts || [] }, notice: { tone: 'ok', text: name + ':' + (res.body.note || '已处理') } }) + if (typeof props.onResolved === 'function') props.onResolved() + } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '处理冲突失败') } }) + }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) + } + function runNow() { + patch({ busy: 'run' }) + postJson(API.sync + '/run', {}).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ busy: '', data: { stats: res.body.stats, conflicts: res.body.conflicts || [] }, notice: { tone: 'ok', text: '已执行一次同步。' } }) + else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '同步失败') } }) + }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) + } + var data = state.data + var stats = data === null || data === undefined ? null : data.stats + var conflicts = data === null || data === undefined || !Array.isArray(data.conflicts) ? [] : data.conflicts + var statPills = stats === null ? h('p', { style: styles.hint }, '同步引擎未启用或尚未运行。') : h('div', { style: styles.pills }, + h(Pill, { label: '运行中', value: stats.running === true ? '是' : '否', tone: stats.running === true ? 'warn' : 'muted' }), + h(Pill, { label: '上次运行', value: formatIso(stats.lastRunAt), tone: 'muted' }), + h(Pill, { label: '上传', value: (stats.uploaded || []).length, tone: 'muted' }), + h(Pill, { label: '下载', value: (stats.downloaded || []).length, tone: 'muted' }), + h(Pill, { label: '冲突', value: conflicts.length, tone: conflicts.length > 0 ? 'warn' : 'muted' }), + ) + var conflictRows = conflicts.map(function (conflict) { + var local = conflict.local || {} + var cloud = conflict.cloud + return h('div', { key: conflict.name, style: styles.row }, + h('div', { style: styles.rowHead }, + h('span', { style: styles.rowTitle }, conflict.name), + h('div', { style: styles.pills }, + h(Pill, { label: '原因', value: String(conflict.reason), tone: 'warn' }), + h(Pill, { label: '本地', value: local.exists === true ? (local.files + ' 文件') : '缺失', tone: local.exists === true ? 'muted' : 'bad' }), + h(Pill, { label: '云端', value: cloud === null ? '无' : ('rev ' + String(cloud.revision === undefined ? '?' : cloud.revision)), tone: cloud === null ? 'muted' : 'ok' }), + ), + ), + h('p', { style: styles.hint }, conflict.note), + h('div', { style: styles.actions }, + h(Btn, { label: '保留本地', variant: 'small', disabled: state.busy !== '' || conflict.canKeepLocal !== true, onClick: function () { resolve(conflict.name, 'keep_local') } }), + h(Btn, { label: '采用云端', variant: 'small', disabled: state.busy !== '' || conflict.canTakeCloud !== true, onClick: function () { resolve(conflict.name, 'take_cloud') } }), + h(Btn, { label: '手动合并', variant: 'small', disabled: state.busy !== '', onClick: function () { resolve(conflict.name, 'manual') } }), + ), + ) + }) + return h(Section, { title: '云端同步与冲突', hint: '同步冲突不会被静默覆盖;逐项选择保留本地(上传为新版本)、采用云端(先备份本地)或手动合并。', aside: h(Btn, { label: state.busy === 'run' ? '同步中…' : '立即同步', variant: 'small', disabled: state.busy !== '', onClick: runNow }) }, + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, + state.error === null ? null : h(Banner, { tone: 'bad' }, state.error), + state.notice === null ? null : h(Banner, { tone: state.notice.tone }, state.notice.text), + statPills, + conflicts.length === 0 ? h('p', { style: styles.hint }, '当前没有待处理冲突。') : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, conflictRows), + ), + ) + } + function SettingsCard() { var snapshotState = React.useState(null) var snapshot = snapshotState[0] @@ -2175,42 +2403,19 @@ window.__ModuleLoader__.load({ planBox, ), - h( - Section, - { - title: '技能市场', - hint: '本版没有接入经验证的市场来源,因此不提供搜索与安装入口,也不显示任何示例条目。', + h(MarketSection, { + capabilities: caps, + onInstalled: function () { + loadTree() + loadSkills() }, - h( - 'p', - { style: styles.hint }, - features.skillsMarketplace === true - ? 'capabilities 报告市场可用。' - : '能力缺口:capabilities.features.skillsMarketplace = false。下一步接入一个来源(MarketProvider:search / getDetail / resolveArtifact / checkUpdates)后再开放。', - ), - ), + }), - h( - Section, - { - title: '云端同步状态', - hint: '同步仍由现有引擎负责:本地改动与云端改动冲突时会暂停该项,不会被覆盖。', + h(SyncResolveSection, { + onResolved: function () { + loadSkills() }, - snapshot.skills === undefined || snapshot.skills === null - ? h('p', { style: styles.hint }, '同步引擎未启用。') - : h( - 'div', - { style: styles.pills }, - h(Pill, { label: '运行中', value: snapshot.skills.running === true ? '是' : '否', tone: snapshot.skills.running === true ? 'warn' : 'muted' }), - h(Pill, { label: '上次运行', value: formatIso(snapshot.skills.lastRunAt), tone: 'muted' }), - h(Pill, { label: '下载', value: (snapshot.skills.downloaded || []).length, tone: 'muted' }), - h(Pill, { label: '上传', value: (snapshot.skills.uploaded || []).length, tone: 'muted' }), - h(Pill, { label: '冲突', value: (snapshot.skills.conflicts || []).length, tone: (snapshot.skills.conflicts || []).length > 0 ? 'warn' : 'muted' }), - ), - snapshot.skills === undefined || snapshot.skills === null || snapshot.skills.lastError === undefined - ? null - : h('p', { style: styles.hint }, '最近一次同步错误:' + String(snapshot.skills.lastError)), - ), + }), ) } diff --git a/docs/management-surface.md b/docs/management-surface.md index 1bfc273..7b396e7 100644 --- a/docs/management-surface.md +++ b/docs/management-surface.md @@ -131,3 +131,55 @@ POST /mcp-connector/api { "method": "status" | "healthCheck" | "toolsList" | " ### 5.5 验证 `verify-skill-tree.mjs`:40 项通过——来源与计数、分类增删改、成员唯一性、DSH 真实 park/unpark、「未分类」开关只作用于未分类技能、外来源 `cardOnly` 且目录字节不变、持久化重开、路由闸门与错误码。 + +## 6. P3:技能市场 / 同步冲突 / 更新回退(0.6.0) + +### 6.1 市场来源 + +首个经验证来源是 Anthropic 公开的 Claude 技能市场清单 +`https://raw.githubusercontent.com/anthropics/skills/main/.claude-plugin/marketplace.json`。 +客户端只传 `provider` 与条目 `id`,**从不传仓库地址**,因此宿主不会去拉取任意 URL。 + +| 方法 | 路径 | 说明 | +|---|---|---| +| GET | `/cloud-memory/market/providers` | 已配置来源(id/标签/仓库) | +| GET | `/cloud-memory/market/search` | `?provider=&q=&cursor=` → 条目分页 | +| POST | `/cloud-memory/market/detail` | `{provider,id}` → 逐技能 SKILL.md 元数据 | +| POST | `/cloud-memory/market/plan` | `{provider,id}` → 解析提交 + 克隆暂存计划(只列该条目技能) | +| POST | `/cloud-memory/market/commit` | `{planId,folders}` → 只装勾选项 | +| POST | `/cloud-memory/market/cancel` | `{planId}` → 丢弃暂存 | + +`resolveArtifact` 通过 GitHub API 解析固定提交;克隆后以 `git rev-parse HEAD` 记录**实际**提交,安装失败不会留下半装目录。 + +### 6.2 来源记录与更新/回退 + +安装(Git 或市场)会在技能目录写入 `.skill-source.json`:`provider / repoUrl / ref / commit / sourcePath / entryId / installedAt`,不含任何凭据。 + +| 方法 | 路径 | 说明 | +|---|---|---| +| POST | `/cloud-memory/skills/update/check` | `{folder}` → 与来源最新提交比较,返回 added/removed/changed | +| POST | `/cloud-memory/skills/update/apply` | `{folder,expectedCommit}` → 更新前整目录备份,再切换并重写记录 | +| POST | `/cloud-memory/skills/rollback` | `{folder}` → 恢复最近一次更新前备份 | + +安全点:只信任来源记录里的仓库地址;新克隆在 `$DSH_HOME/.cloud-memory-staging`,复制时剔除 `.git` 与符号链接;`expectedCommit` 过期返回 `stale_plan`,绝不覆盖;同步进行中拒绝写。 + +### 6.3 同步冲突交互 + +`SkillSync` 仍然只按原策略暂停冲突项,但会暴露两侧信息并允许人工处置: + +| 方法 | 路径 | 说明 | +|---|---|---| +| GET | `/cloud-memory/sync/status` | 最近统计 + 每个冲突的本地/云端两侧 | +| POST | `/cloud-memory/sync/run` | 立即执行一次同步 | +| POST | `/cloud-memory/sync/resolve` | `{name,action}`:`keep_local` / `take_cloud` / `manual` | + +- `keep_local`:用本地文件按当前云端 revision 上传为新版本;revision 过期返回 `stale_revision`。 +- `take_cloud`:先把本地目录备份到 `$DSH_HOME/.cloud-memory-backups/sync//<时间>`,再写入云端 bundle。 +- `manual`:只返回两侧信息,不改任何文件。 + +### 6.4 验证 + +- `verify-market.mjs`:19 项通过(清单解析、search/detail、未知来源 400、plan 固定提交、只装勾选项、来源记录)。 +- `verify-skill-update.mjs`:15 项通过(真实 git 仓库的 check/apply/rollback、无来源拒绝、过期提交拒绝、备份存在)。 +- `verify-sync-resolve.mjs`:12 项通过(一次真实 pass 产生 4 个冲突、take_cloud 备份并覆盖、keep_local 上传、manual 只读、stale 拒绝)。 + diff --git a/lib/index.js b/lib/index.js index 59ec8a9..fbbd53d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -17,13 +17,15 @@ import { SettingsStore, dshHome } from "./store.js"; import { SkillSync } from "./skill-sync.js"; import { SkillAdmin, skillsHoldingDir } from "./skill-admin.js"; import { SkillGroupManager, detectSources } from "./skill-groups.js"; +import { ClaudePluginMarketplaceProvider, MarketService } from "./market.js"; +import { SkillUpdater } from "./skill-update.js"; import { createMemoryTools, createSkillTools } from "./tools.js"; /** Package name; also the client bundle id. */ export const name = 'dsh-cloud-memory'; /** Services required before `apply` runs. */ export const inject = ['webServer']; /** Version reported by the status card. */ -const VERSION = '0.5.0'; +const VERSION = '0.6.0'; /** How often the skill directory is reconciled with the cloud center. */ const SKILL_SYNC_INTERVAL_MS = 15 * 60_000; /** How often queued reports are retried. */ @@ -71,6 +73,25 @@ export function apply(ctx, config) { admin: skillAdmin, log: (message) => logger.info(message), }); + // One verified market source to start with: the Claude Code plugin marketplace + // manifest that Anthropic publishes. Entry ids never carry a URL, so the card + // cannot make the host fetch an arbitrary repository. + const market = new MarketService([ + new ClaudePluginMarketplaceProvider({ + id: 'anthropic-skills', + label: 'Anthropic Agent Skills', + description: 'Anthropic 公开的 Claude 技能市场(.claude-plugin/marketplace.json)', + repo: 'anthropics/skills', + ref: 'main', + }), + ]); + const updater = new SkillUpdater({ + admin: skillAdmin, + skillsRoot: () => skillSync.skillsRoot(), + holdingDir: () => skillsHoldingDir(), + syncRunning: () => skillSync.stats().running, + log: (message) => logger.info(message), + }); // Announce at most once per activation so a missing token is obvious in logs. const effective = settings(); if (!effective.enabled) { @@ -114,7 +135,7 @@ export function apply(ctx, config) { logger.warn('webServer service unavailable; the configuration page stays disabled'); } else { - const routes = createRoutes({ version: VERSION, store, reporter, client: () => client, skills: () => skillSync.stats(), admin: skillAdmin, groups: skillGroups }); + const routes = createRoutes({ version: VERSION, store, reporter, client: () => client, skills: () => skillSync.stats(), admin: skillAdmin, groups: skillGroups, market, sync: skillSync, updates: updater }); for (const route of routes) { ctx.effect(() => webServer.register(route), `dsh-cloud-memory: route ${route.path}`); } diff --git a/lib/market.js b/lib/market.js new file mode 100644 index 0000000..c2f1125 --- /dev/null +++ b/lib/market.js @@ -0,0 +1,217 @@ +/** + * Skill market providers (P3). + * + * A provider answers `search` / `getDetail` / `resolveArtifact` for ONE + * configured, verified source. The browser half never passes a repository URL: + * it only names a provider id and an entry id, so no request can make the host + * fetch an arbitrary address or clone an arbitrary repository. + * + * First provider: the Claude Code plugin marketplace manifest that lives at + * `.claude-plugin/marketplace.json` in a public GitHub repository. That format + * lists plugins, each with a description and the relative folders of the skills + * it ships. Installing an entry reuses the same staged plan/commit pipeline as + * a manual git install, pinned to the commit resolved here. + */ +import { parseSkillFrontmatter } from "./skill-sync.js"; +/** Typed failure so routes can map it to a status code. */ +export class MarketError extends Error { + code; + constructor(code, message) { + super(message); + this.code = code; + this.name = 'MarketError'; + } +} +/** Normalize a manifest skill path to a folder name (its basename). */ +function folderOf(skillPath) { + const trimmed = skillPath.replace(/^\.\//, '').replace(/\/+$/, ''); + const parts = trimmed.split('/'); + return parts[parts.length - 1] ?? trimmed; +} +/** Provider over a Claude Code plugin marketplace manifest on GitHub. */ +export class ClaudePluginMarketplaceProvider { + options; + cache = new Map(); + fetchImpl; + constructor(options) { + this.options = options; + this.fetchImpl = options.fetchImpl ?? fetch; + } + info() { + return { + id: this.options.id, + label: this.options.label, + description: this.options.description, + source: this.options.repo, + homepage: this.options.homepage ?? `https://github.com/${this.options.repo}`, + }; + } + ref() { + return this.options.ref ?? 'main'; + } + timeoutMs() { + return this.options.timeoutMs ?? 8000; + } + rawUrl(path) { + const clean = path.replace(/^\.\//, ''); + return `https://raw.githubusercontent.com/${this.options.repo}/${encodeURIComponent(this.ref())}/${clean}`; + } + async fetchText(url) { + let response; + try { + response = await this.fetchImpl(url, { + headers: { accept: 'application/json, text/plain, */*', 'user-agent': 'dsh-cloud-memory-market' }, + signal: AbortSignal.timeout(this.timeoutMs()), + }); + } + catch (error) { + throw new MarketError('unreachable', `市场来源不可达:${error instanceof Error ? error.message : String(error)}`); + } + if (!response.ok) { + throw new MarketError('http_' + String(response.status), `市场来源返回 HTTP ${response.status}`); + } + return response.text(); + } + async fetchJson(url, ttlMs = 60_000) { + const hit = this.cache.get(url); + if (hit !== undefined && Date.now() - hit.at < ttlMs) + return hit.value; + const text = await this.fetchText(url); + let parsed; + try { + parsed = JSON.parse(text); + } + catch { + throw new MarketError('invalid_manifest', '市场清单不是合法 JSON。'); + } + this.cache.set(url, { at: Date.now(), value: parsed }); + return parsed; + } + async manifest() { + const parsed = await this.fetchJson(this.rawUrl('.claude-plugin/marketplace.json')); + if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) { + throw new MarketError('invalid_manifest', '市场清单结构不正确。'); + } + return parsed; + } + entriesOf(manifest) { + const list = Array.isArray(manifest.plugins) ? manifest.plugins : []; + const version = manifest.metadata?.version; + const author = manifest.owner?.name; + const entries = []; + for (const plugin of list) { + const name = typeof plugin.name === 'string' ? plugin.name : ''; + if (name === '') + continue; + const paths = Array.isArray(plugin.skills) ? plugin.skills.filter((p) => typeof p === 'string') : []; + const entry = { + id: name, + providerId: this.options.id, + name, + description: typeof plugin.description === 'string' ? plugin.description : '', + skills: paths.map(folderOf), + skillPaths: paths, + sourceRepo: this.options.repo, + sourceUrl: `https://github.com/${this.options.repo}`, + }; + if (typeof version === 'string') + entry.version = version; + if (typeof author === 'string') + entry.author = author; + entries.push(entry); + } + return entries; + } + async search(query, cursor) { + const entries = this.entriesOf(await this.manifest()); + const needle = query.trim().toLowerCase(); + const filtered = needle === '' + ? entries + : entries.filter((entry) => `${entry.name}\n${entry.description}\n${entry.skills.join(' ')}`.toLowerCase().includes(needle)); + const pageSize = 10; + const start = Number.isFinite(cursor) && cursor > 0 ? Math.floor(cursor) : 0; + const page = filtered.slice(start, start + pageSize); + const next = start + pageSize < filtered.length ? start + pageSize : undefined; + return next === undefined ? { entries: page } : { entries: page, nextCursor: next }; + } + findEntry(manifest, entryId) { + const entry = this.entriesOf(manifest).find((item) => item.id === entryId); + if (entry === undefined) + throw new MarketError('not_found', `市场里没有条目:${entryId}`); + return entry; + } + async getDetail(entryId) { + const entry = this.findEntry(await this.manifest(), entryId); + const skillDetails = []; + // Bound the fan-out: a manifest can list dozens of skills. + for (const path of entry.skillPaths.slice(0, 24)) { + const folder = folderOf(path); + try { + const markdown = await this.fetchText(this.rawUrl(`${path.replace(/^\.\//, '')}/SKILL.md`)); + const frontmatter = parseSkillFrontmatter(markdown); + const detail = { folder, path, ok: true }; + if (typeof frontmatter.name === 'string') + detail.name = frontmatter.name; + if (typeof frontmatter.description === 'string') + detail.description = frontmatter.description.slice(0, 300); + skillDetails.push(detail); + } + catch { + skillDetails.push({ folder, path, ok: false }); + } + } + return { ...entry, skillDetails, fetchedAt: Date.now() }; + } + async resolveArtifact(entryId, _version) { + const entry = this.findEntry(await this.manifest(), entryId); + let commit; + try { + const payload = await this.fetchJson(`https://api.github.com/repos/${this.options.repo}/commits/${encodeURIComponent(this.ref())}`, 30_000); + if (typeof payload.sha === 'string' && /^[0-9a-f]{7,40}$/i.test(payload.sha)) + commit = payload.sha; + } + catch { + // Rate-limited or offline: fall back to the branch ref; git checkout pins + // the actual commit at clone time and the install records it. + } + const artifact = { + repoUrl: `https://github.com/${this.options.repo}.git`, + ref: commit ?? this.ref(), + folders: entry.skills, + }; + if (commit !== undefined) + artifact.commit = commit; + if (entry.version !== undefined) + artifact.version = entry.version; + return artifact; + } +} +/** A small registry of providers; the browser can only ask for one by id. */ +export class MarketService { + byId = new Map(); + constructor(providers) { + for (const provider of providers) + this.byId.set(provider.info().id, provider); + } + list() { + return [...this.byId.values()].map((provider) => provider.info()); + } + has(providerId) { + return this.byId.has(providerId); + } + get(providerId) { + const provider = this.byId.get(providerId); + if (provider === undefined) + throw new MarketError('unknown_provider', `未知的市场来源:${providerId}`); + return provider; + } + search(providerId, query, cursor) { + return this.get(providerId).search(query, cursor); + } + detail(providerId, entryId) { + return this.get(providerId).getDetail(entryId); + } + resolve(providerId, entryId, version) { + return this.get(providerId).resolveArtifact(entryId, version); + } +} diff --git a/lib/mnemon/index.js b/lib/mnemon/index.js index 2217764..80a50bc 100644 --- a/lib/mnemon/index.js +++ b/lib/mnemon/index.js @@ -17,7 +17,7 @@ import { descriptor } from "./descriptor.js"; import { LongMemoryProvider } from "./driver.js"; export { LongMemoryProvider, descriptor }; /** Keep in sync with the package version (asserted by the verify script). */ -const PROVIDER_VERSION = '0.5.0'; +const PROVIDER_VERSION = '0.6.0'; export const definition = defineMemorySpaceProviderDefinition({ manifest: { apiVersion: MEMORY_SPACE_PROVIDER_API_VERSION, diff --git a/lib/routes.js b/lib/routes.js index 032f781..c6b2d6c 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -1,6 +1,7 @@ import { describeError, isSecureLoginTarget } from "./api.js"; import { renderConfigPage } from "./page.js"; import { SkillAdminError } from "./skill-admin.js"; +import { MarketError } from "./market.js"; import { SkillGroupError } from "./skill-groups.js"; import { renderSkillsPage } from "./skill-page.js"; import { randomUUID } from 'node:crypto'; @@ -14,11 +15,14 @@ export const ROUTES = { test: '/cloud-memory/test', flush: '/cloud-memory/flush', skills: '/cloud-memory/skills', + market: '/cloud-memory/market', + sync: '/cloud-memory/sync', }; /** Largest request body we accept (a settings patch is tiny). */ const MAX_BODY_BYTES = 64 * 1024; /** What this build actually implements, stated per feature. */ function capabilities(deps) { + const marketProviders = deps.market?.list().length ?? 0; return { version: deps.version, features: { @@ -28,15 +32,22 @@ function capabilities(deps) { skillInstallPlans: deps.admin !== undefined, skillUninstallPlan: deps.admin !== undefined, skillsSync: deps.skills !== undefined, - // No verified marketplace provider is wired in this build; the card says - // so instead of showing a fake search box. - skillsMarketplace: false, + skillsMarketplace: deps.market !== undefined, + marketProviders, + skillUpdate: deps.updates !== undefined, + syncResolve: deps.sync !== undefined, // The MCP bridge is provided by the host `dsh-mcp-connector` plugin over // the same origin; the browser half probes it at runtime. mcpBridge: 'host-plugin', }, notes: [ - '技能市场尚未接入经验证的来源,本版不提供搜索与安装闭环。', + ...(deps.market === undefined + ? ['技能市场未启用:没有配置市场来源。'] + : [`技能市场已接入 ${marketProviders} 个来源;安装走暂存计划并固定提交。`]), + ...(deps.updates === undefined + ? ['技能更新/回退未启用。'] + : ['更新/回退仅对带来源记录(.skill-source.json)的技能生效,更新前自动备份。']), + ...(deps.sync === undefined ? [] : ['同步冲突可在技能页签逐项“保留本地 / 采用云端 / 手动合并”。']), 'MCP 管理由宿主 dsh-mcp-connector 插件提供,本卡通过其公开接口读写,不另建凭据库。', ], }; @@ -441,7 +452,11 @@ function adminStatus(code) { case 'sync_running': case 'plan_expired': case 'confirmation_required': + case 'no_source': + case 'up_to_date': + case 'stale_plan': case 'unsupported': return 409; + case 'no_backup': return 404; case 'no_skills_found': return 422; case 'git_failed': return 502; default: return 500; @@ -548,6 +563,13 @@ async function serveSkills(deps, req, res) { return { ...(await admin.remove(folder)) }; }, }; + // Update/rollback operate on skills with a `.skill-source.json` record. They + // are optional: without the updater wired in the routes answer 404. + const updateActions = { + '/update/check': async (body) => ({ ...(await requireUpdates(deps).checkUpdate(body.folder)) }), + '/update/apply': async (body) => ({ ...(await requireUpdates(deps).applyUpdate(body.folder, body.expectedCommit)) }), + '/rollback': async (body) => ({ ...(await requireUpdates(deps).rollback(body.folder)) }), + }; // Category and source switches live in the same prefix so the card has one // base URL; they are grouped here because they never touch a skill folder // directly (except the DSH park/unpark the group manager delegates). @@ -560,7 +582,7 @@ async function serveSkills(deps, req, res) { '/source/toggle': async (body) => ({ ...(await requireGroups(deps).toggleSource(body.sourceId, body.enabled === true)) }), '/source/reveal': async (body) => ({ ...(await requireGroups(deps).reveal(body.sourceId)) }), }; - const action = actions[suffix] ?? groupActions[suffix]; + const action = actions[suffix] ?? groupActions[suffix] ?? updateActions[suffix]; if (action === undefined) { fail(res, 404, 'unknown-skill-route'); return; @@ -601,6 +623,173 @@ function groupStatus(code) { default: return 500; } } +/** The updater is optional; its routes answer 404 when it is absent. */ +function requireUpdates(deps) { + if (deps.updates === undefined) + throw new SkillAdminError('unsupported', '技能更新/回退未启用。'); + return deps.updates; +} +/** HTTP status for one market failure code. */ +function marketStatus(code) { + switch (code) { + case 'unknown_provider': + case 'invalid_manifest': return 400; + case 'not_found': return 404; + case 'unreachable': + case 'unavailable': return 502; + default: + return code.startsWith('http_') ? 502 : 500; + } +} +/** + * Market surface, one prefix route: + * `GET /cloud-memory/market/providers` configured sources + * `GET /cloud-memory/market/search` ?provider=&q=&cursor= + * `POST /cloud-memory/market/detail` { provider, id } + * `POST /cloud-memory/market/plan` { provider, id } — pin commit + stage a plan + * `POST /cloud-memory/market/commit` { planId, folders } + * `POST /cloud-memory/market/cancel` { planId } + */ +async function serveMarket(deps, req, res) { + const market = deps.market; + if (market === undefined) { + fail(res, 404, 'market-unavailable'); + return; + } + const url = new URL(req.url ?? '/', 'http://localhost'); + const suffix = url.pathname.slice(ROUTES.market.length); + try { + if (suffix === '' || suffix === '/' || suffix === '/providers') { + if (req.method !== 'GET') { + fail(res, 405, 'method-not-allowed'); + return; + } + json(res, 200, { ok: true, providers: market.list() }); + return; + } + if (suffix === '/search') { + if (req.method !== 'GET') { + fail(res, 405, 'method-not-allowed'); + return; + } + const providerId = url.searchParams.get('provider') ?? market.list()[0]?.id ?? ''; + const query = url.searchParams.get('q') ?? ''; + const cursorRaw = Number(url.searchParams.get('cursor') ?? '0'); + const cursor = Number.isFinite(cursorRaw) && cursorRaw > 0 ? cursorRaw : 0; + const result = await market.search(providerId, query, cursor); + json(res, 200, { ok: true, provider: market.get(providerId).info(), ...result }); + return; + } + const admin = deps.admin; + const requireAdmin = () => { + if (admin === undefined) + throw new MarketError('unavailable', '技能管理未启用,无法安装。'); + return admin; + }; + const actions = { + '/detail': async (body) => ({ ...(await market.detail(String(body.provider ?? ''), String(body.id ?? ''))) }), + '/plan': async (body) => { + const providerId = String(body.provider ?? ''); + const id = String(body.id ?? ''); + const artifact = await market.resolve(providerId, id, typeof body.version === 'string' ? body.version : undefined); + const plan = await requireAdmin().prepareInstall(artifact.repoUrl, { + ref: artifact.ref, + source: { + provider: providerId, + entryId: id, + ...(artifact.version === undefined ? {} : { version: artifact.version }), + }, + }); + const wanted = new Set(artifact.folders); + return { + planId: plan.planId, + ...(plan.commit === undefined ? {} : { commit: plan.commit }), + artifact, + candidates: plan.candidates.filter((candidate) => wanted.has(candidate.folder)), + totalInRepo: plan.candidates.length, + }; + }, + '/commit': async (body) => ({ ...(await requireAdmin().commitInstall({ planId: body.planId, folders: body.folders })) }), + '/cancel': async (body) => ({ ...(await requireAdmin().cancelInstall({ planId: body.planId })) }), + }; + const action = actions[suffix]; + if (action === undefined) { + fail(res, 404, 'unknown-market-route'); + return; + } + const body = await jsonBody(req, res); + if (body === undefined) + return; + json(res, 200, { ok: true, ...(await action(body)) }); + } + catch (error) { + if (error instanceof MarketError) { + json(res, marketStatus(error.code), { ok: false, code: error.code, error: error.message }); + return; + } + if (error instanceof SkillAdminError) { + json(res, adminStatus(error.code), { ok: false, code: error.code, error: error.message }); + return; + } + json(res, 500, { ok: false, code: 'internal', error: error instanceof Error ? error.message : String(error) }); + } +} +/** + * Sync surface: + * `GET /cloud-memory/sync/status` latest stats + both sides of each conflict + * `POST /cloud-memory/sync/run` trigger one pass now + * `POST /cloud-memory/sync/resolve` { name, action: keep_local | take_cloud | manual } + */ +async function serveSync(deps, req, res) { + const sync = deps.sync; + if (sync === undefined) { + fail(res, 404, 'sync-unavailable'); + return; + } + const url = new URL(req.url ?? '/', 'http://localhost'); + const suffix = url.pathname.slice(ROUTES.sync.length); + try { + if (suffix === '' || suffix === '/' || suffix === '/status') { + if (req.method !== 'GET') { + fail(res, 405, 'method-not-allowed'); + return; + } + json(res, 200, { ok: true, stats: sync.stats(), conflicts: await sync.conflictDetails() }); + return; + } + if (suffix === '/run') { + if (req.method !== 'POST') { + fail(res, 405, 'method-not-allowed'); + return; + } + const stats = await sync.syncOnce(); + json(res, 200, { ok: true, stats, conflicts: await sync.conflictDetails() }); + return; + } + if (suffix === '/resolve') { + if (req.method !== 'POST') { + fail(res, 405, 'method-not-allowed'); + return; + } + const body = await jsonBody(req, res); + if (body === undefined) + return; + const result = await sync.resolveConflict(body.name, body.action); + json(res, 200, { ok: true, ...result, conflicts: await sync.conflictDetails() }); + return; + } + fail(res, 404, 'unknown-sync-route'); + } + catch (error) { + const message = error instanceof Error ? error.message : String(error); + const known = ['invalid_name', 'invalid_action', 'no_local_copy', 'no_cloud_copy', 'stale_revision', 'sync_running', 'manifest_mismatch']; + const code = known.includes(message) ? message : 'internal'; + const status = code === 'internal' ? 500 + : code === 'invalid_name' || code === 'invalid_action' ? 400 + : 409; + json(res, status, { ok: false, code, error: message }); + } +} /** Build the exact-route table registered with the host web server. */ export function createRoutes(deps) { return [ @@ -644,5 +833,15 @@ export function createRoutes(deps) { path: ROUTES.skills, handler: (req, res) => serveSkills(deps, req, res), }, + { + kind: 'prefix', + path: ROUTES.market, + handler: (req, res) => serveMarket(deps, req, res), + }, + { + kind: 'prefix', + path: ROUTES.sync, + handler: (req, res) => serveSync(deps, req, res), + }, ]; } diff --git a/lib/skill-admin.js b/lib/skill-admin.js index daeca73..9395539 100644 --- a/lib/skill-admin.js +++ b/lib/skill-admin.js @@ -29,7 +29,7 @@ import { lstatSync } from 'node:fs'; import { execFile } from 'node:child_process'; import { createHash, randomUUID } from 'node:crypto'; import { cp, lstat, mkdir, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises'; -import { basename, dirname, join, resolve, sep } from 'node:path'; +import { basename, dirname, join, relative, resolve, sep } from 'node:path'; import { parseSkillFrontmatter } from "./skill-sync.js"; import { dshHome } from "./store.js"; /** The parking area for disabled skills. */ @@ -53,6 +53,12 @@ export class SkillAdminError extends Error { this.name = 'SkillAdminError'; } } +/** Run git and return stdout (used for resolving the checked-out commit). */ +function execFileText(file, args, timeoutMs = 120_000) { + return new Promise((resolvePromise, rejectPromise) => { + execFile(file, args, { timeout: timeoutMs, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' }, maxBuffer: 4 * 1024 * 1024 }, (error, stdout) => (error ? rejectPromise(error) : resolvePromise(String(stdout)))); + }); +} function fail(code, message) { throw new SkillAdminError(code, message); } @@ -77,6 +83,34 @@ function isSymlinkSync(path) { return true; } } +/** Read a skill's `.skill-source.json`, or undefined when absent/corrupt. */ +export async function readSkillSourceRecord(dir) { + const raw = await readFile(join(dir, '.skill-source.json'), 'utf8').catch(() => undefined); + if (raw === undefined) + return undefined; + try { + const parsed = JSON.parse(raw); + if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) + return undefined; + const record = parsed; + if (typeof record.repoUrl !== 'string' || typeof record.commit !== 'string') + return undefined; + return { + version: 1, + provider: typeof record.provider === 'string' ? record.provider : 'git', + repoUrl: record.repoUrl, + ref: typeof record.ref === 'string' ? record.ref : record.commit, + commit: record.commit, + sourcePath: typeof record.sourcePath === 'string' ? record.sourcePath : '', + installedAt: typeof record.installedAt === 'string' ? record.installedAt : new Date(0).toISOString(), + ...(typeof record.entryId === 'string' ? { entryId: record.entryId } : {}), + ...(typeof record.marketVersion === 'string' ? { marketVersion: record.marketVersion } : {}), + }; + } + catch { + return undefined; + } +} /** Files under one skill folder, skipping `.git` and never following links. */ async function walkFiles(dir, cap = MAX_SKILL_FILES + 1) { const out = []; @@ -178,6 +212,7 @@ export class SkillAdmin { const bytes = files.reduce((sum, file) => sum + file.bytes, 0); const installedAt = await stat(join(dir, 'SKILL.md')).then((s) => new Date(s.mtimeMs).toISOString()).catch(() => undefined); const name = frontmatter.name; + const source = await readSkillSourceRecord(dir); return { folder, name: typeof name === 'string' && name !== '' ? name : folder, @@ -192,6 +227,18 @@ export class SkillAdmin { location: enabled ? 'root' : 'holding', ownerManager: 'dsh-cloud-memory', writable: true, + ...(source === undefined + ? {} + : { + source: { + provider: source.provider, + repoUrl: source.repoUrl, + ref: source.ref, + commit: source.commit, + ...(source.entryId === undefined ? {} : { entryId: source.entryId }), + }, + }), + updatable: source !== undefined, }; } /** Inventory of the skills root and the parking area. */ @@ -359,7 +406,7 @@ export class SkillAdmin { * Stage a git repository and describe its skills. Nothing enters the skills * root here; the caller picks folders and commits. */ - async prepareInstall(urlInput) { + async prepareInstall(urlInput, options = {}) { this.guardSync(); if (typeof urlInput !== 'string') fail('invalid_url', '请提供仓库地址。'); @@ -372,9 +419,17 @@ export class SkillAdmin { const planId = `plan_${Date.now().toString(36)}_${randomUUID().slice(0, 8)}`; const stagingDir = join(stagingRoot, planId); try { - await new Promise((resolvePromise, rejectPromise) => { - execFile('git', ['clone', '--depth', '1', '--no-tags', '--', url, stagingDir], { timeout: 120_000, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' }, maxBuffer: 4 * 1024 * 1024 }, (error) => (error ? rejectPromise(error) : resolvePromise())); - }); + await execFileText('git', ['clone', '--depth', '1', '--no-tags', '--', url, stagingDir]); + if (typeof options.ref === 'string' && options.ref !== '') { + try { + await execFileText('git', ['-C', stagingDir, 'fetch', '--depth', '1', 'origin', options.ref]); + await execFileText('git', ['-C', stagingDir, 'checkout', '--detach', 'FETCH_HEAD']); + } + catch { + // The default-branch clone is still usable; the recorded commit below + // is whatever was actually checked out, so an install stays auditable. + } + } } catch (error) { await rm(stagingDir, { recursive: true, force: true }).catch(() => { }); @@ -383,6 +438,13 @@ export class SkillAdmin { ? '本机没有 git 命令,无法从仓库安装技能。' : '仓库克隆失败(地址不可达、需要登录,或不是 git 仓库)。'); } + let commit = ''; + try { + commit = (await execFileText('git', ['-C', stagingDir, 'rev-parse', 'HEAD'], 20_000)).trim(); + } + catch { + commit = ''; + } const repoName = basename(url.replace(/\/$/, '').replace(/\.git$/, '')) || 'skill'; const found = []; const collect = async (dir, depth) => { @@ -435,6 +497,7 @@ export class SkillAdmin { conflict: existing !== undefined, ...(existing === undefined ? {} : { conflictLocation: existing.area }), ...(problem === undefined ? {} : { problem }), + rel: relative(stagingDir, candidate.dir), }); } const plan = { @@ -445,6 +508,9 @@ export class SkillAdmin { expiresAt: Date.now() + PLAN_TTL_MS, stagingDir, candidates, + ...(commit === '' ? {} : { commit }), + ...(typeof options.ref === 'string' && options.ref !== '' ? { ref: options.ref } : {}), + ...(options.source === undefined ? {} : { source: options.source }), }; const file = await this.sweepPlans(await this.readPlans()); file.plans.push(plan); @@ -454,6 +520,7 @@ export class SkillAdmin { planId, url, expiresAt: plan.expiresAt, + ...(commit === '' ? {} : { commit }), candidates: candidates.map((candidate) => ({ ...candidate, id: candidate.folder })), }; } @@ -488,7 +555,9 @@ export class SkillAdmin { continue; } const stagingRoot = resolve(plan.stagingDir); - const source = candidate.folder === basename(plan.stagingDir) ? stagingRoot : resolve(stagingRoot, candidate.folder); + const source = candidate.rel !== undefined && candidate.rel !== '' + ? resolve(stagingRoot, candidate.rel) + : candidate.folder === basename(plan.stagingDir) ? stagingRoot : resolve(stagingRoot, candidate.folder); const target = resolve(skillsRoot, candidate.folder); // Both sides must stay inside their own root: a crafted plan file cannot // make this write outside the skills directory. @@ -502,6 +571,22 @@ export class SkillAdmin { // repository: both are dropped rather than copied. filter: (sourcePath) => !sourcePath.split(/[\\/]/).includes('.git') && !isSymlinkSync(sourcePath), }); + // Record provenance so the skill can later be diffed, updated and rolled + // back without the cloud center. The file holds no credentials. + if (plan.commit !== undefined && plan.commit !== '') { + const record = { + version: 1, + provider: plan.source?.provider ?? 'git', + repoUrl: plan.url, + ref: plan.ref ?? plan.commit, + commit: plan.commit, + sourcePath: candidate.rel ?? candidate.folder, + installedAt: new Date().toISOString(), + ...(plan.source?.entryId === undefined ? {} : { entryId: plan.source.entryId }), + ...(plan.source?.version === undefined ? {} : { marketVersion: plan.source.version }), + }; + await writeFile(join(target, '.skill-source.json'), `${JSON.stringify(record, null, 2)}\n`, 'utf8').catch(() => { }); + } installed.push(candidate.folder); this.deps.log(`skill installed from git: ${candidate.folder}`); } diff --git a/lib/skill-sync.js b/lib/skill-sync.js index 3bd82d9..9b86619 100644 --- a/lib/skill-sync.js +++ b/lib/skill-sync.js @@ -30,7 +30,7 @@ * silently trimmed. */ import { createHash, randomUUID } from 'node:crypto'; -import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises'; +import { mkdir, cp, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises'; import { dirname, join, resolve } from 'node:path'; import { describeError } from "./api.js"; import { dshHome } from "./store.js"; @@ -516,4 +516,192 @@ export class SkillSync { hasScripts: files.some((file) => file.rel.startsWith('scripts/')), }; } + /** Encode local files exactly the way the upload path does. */ + toUploadFiles(files) { + return files.map((file) => { + const ext = file.rel.slice(file.rel.lastIndexOf('.')).toLowerCase(); + return TEXT_EXT.has(ext) + ? { path: file.rel, content: file.buffer.toString('utf8') } + : { path: file.rel, base64: file.buffer.toString('base64') }; + }); + } + /** Drop one name from the in-memory conflict list after it is resolved. */ + dropConflict(name) { + this.current = { ...this.current, conflicts: this.current.conflicts.filter((conflict) => conflict.name !== name) }; + } + /** + * Both sides of every conflict from the last pass, so a human can decide. + * Read-only: it never changes a file or uploads anything. + */ + async conflictDetails() { + const conflicts = this.current.conflicts; + if (conflicts.length === 0) + return []; + let cloudItems = []; + try { + const listing = await this.deps.client().listSkills(); + if (listing.ok) { + cloudItems = (listing.data.items ?? []).filter((item) => typeof item.name === 'string'); + } + } + catch { + // Listing failed: still show the local side with a null cloud side. + } + const byName = new Map(cloudItems.map((item) => [item.name, item])); + const out = []; + for (const conflict of conflicts) { + const rootDir = join(this.skillsRoot(), conflict.name); + const parkedDir = join(this.holdingRoot(), conflict.name); + let location = 'none'; + let dir = ''; + if (await isDirectory(rootDir)) { + location = 'root'; + dir = rootDir; + } + else if (await isDirectory(parkedDir)) { + location = 'holding'; + dir = parkedDir; + } + let files = 0; + let bytes = 0; + let modified = false; + if (dir !== '') { + try { + const local = await this.readSkillFiles(dir); + files = local.length; + bytes = local.reduce((sum, file) => sum + file.buffer.length, 0); + const delivery = await this.readDelivery(dir); + modified = delivery === undefined ? true : !(await this.deliveredIntact(dir, delivery)); + } + catch { + // Unreadable folder: report zeroed counts, never throw the list away. + } + } + const cloudItem = byName.get(conflict.name); + out.push({ + name: conflict.name, + reason: conflict.reason, + local: { exists: location !== 'none', location, files, bytes, modified }, + cloud: cloudItem === undefined + ? null + : { + ...(cloudItem.revision === undefined ? {} : { revision: cloudItem.revision }), + ...(cloudItem.manifest_sha256 === undefined ? {} : { manifest: cloudItem.manifest_sha256 }), + ...(cloudItem.files === undefined ? {} : { files: cloudItem.files }), + ...(cloudItem.bytes === undefined ? {} : { bytes: cloudItem.bytes }), + ...(cloudItem.enabled === undefined ? {} : { enabled: cloudItem.enabled }), + }, + canKeepLocal: location !== 'none', + canTakeCloud: cloudItem !== undefined, + note: '“保留本地”把本地版本上传成新的云端版本;“采用云端”先备份本地再覆盖;“手动合并”只返回两侧信息,不改文件。', + }); + } + return out; + } + /** Resolve one conflict: keep local / take cloud / manual (no write). */ + async resolveConflict(nameInput, actionInput) { + const name = typeof nameInput === 'string' ? nameInput : ''; + if (name === '' || !CLOUD_SKILL_NAME.test(name)) + throw new Error('invalid_name'); + const action = actionInput === 'keep_local' || actionInput === 'take_cloud' || actionInput === 'manual' ? actionInput : ''; + if (action === '') + throw new Error('invalid_action'); + if (this.running) + throw new Error('sync_running'); + const root = this.skillsRoot(); + const rootDir = join(root, name); + const parkedDir = join(this.holdingRoot(), name); + const inRoot = await isDirectory(rootDir); + const parked = !inRoot && (await isDirectory(parkedDir)); + const dir = inRoot ? rootDir : parked ? parkedDir : ''; + const client = this.deps.client(); + const listing = await client.listSkills(); + if (!listing.ok) + throw new Error(`skill listing failed: ${describeError(listing.error)}`); + const cloud = (listing.data.items ?? []).find((item) => item.name === name); + const state = await this.loadState(); + if (action === 'manual') { + return { + name, + action, + applied: false, + note: '已返回两侧信息,未改动任何文件;手工编辑后在下次同步前保存即可。', + ...(cloud?.revision === undefined ? {} : { revision: cloud.revision }), + }; + } + if (action === 'keep_local') { + if (dir === '') + throw new Error('no_local_copy'); + const skill = await this.readLocalSkill(dir, name); + if (skill === undefined) + throw new Error('not_a_skill'); + if (typeof skill === 'string') + throw new Error(skill); + const files = this.toUploadFiles(skill.files); + const operationId = `dsh-skill-${randomUUID()}`; + if (cloud === undefined) { + const created = await client.createSkill({ + name: skill.name, + description: skill.description, + execution_mode: skill.hasScripts ? 'local-bundle' : 'prompt-only', + files, + expected_revision: 0, + operation_id: operationId, + }); + if (!created.ok) + throw new Error(`upload_failed: ${describeError(created.error)}`); + const revision = created.data.skill?.revision ?? created.data.revision ?? 1; + state.uploaded[name] = { folder: name, revision, manifest: skill.manifest, syncedAt: new Date().toISOString() }; + delete state.removed[name]; + await this.saveState(state); + this.dropConflict(name); + return { name, action, applied: true, revision, note: '已把本地版本新建到云端。' }; + } + const updated = await client.updateSkill(name, { + description: skill.description, + files, + expected_revision: cloud.revision ?? 0, + operation_id: operationId, + }); + if (!updated.ok) { + if (updated.error.code === 'revision_conflict') + throw new Error('stale_revision'); + throw new Error(`upload_failed: ${describeError(updated.error)}`); + } + const revision = updated.data.skill?.revision ?? updated.data.revision ?? (cloud.revision ?? 0) + 1; + state.uploaded[name] = { folder: name, revision, manifest: skill.manifest, syncedAt: new Date().toISOString() }; + delete state.removed[name]; + await this.saveState(state); + this.dropConflict(name); + return { name, action, applied: true, revision, note: '已用本地版本覆盖云端(revision 已更新)。' }; + } + // take_cloud + if (cloud === undefined) + throw new Error('no_cloud_copy'); + const bundle = await client.skillBundle(name); + if (!bundle.ok) + throw new Error(`bundle_failed: ${describeError(bundle.error)}`); + if (linkedSkillManifest(bundle.data.files) !== bundle.data.manifest_sha256) + throw new Error('manifest_mismatch'); + let backup = ''; + if (dir !== '') { + const stamp = new Date().toISOString().replace(/[:.]/g, '-'); + backup = join(dshHome(), '.cloud-memory-backups', 'sync', name, stamp); + await mkdir(join(backup, '..'), { recursive: true }); + await cp(dir, backup, { recursive: true, filter: (p) => !p.split(/[\\/]/).includes('.git') }); + } + await this.install(cloud, bundle.data, parked ? this.holdingRoot() : root); + state.delivered[name] = { manifest: bundle.data.manifest_sha256 }; + delete state.removed[name]; + await this.saveState(state); + this.dropConflict(name); + return { + name, + action, + applied: true, + revision: bundle.data.revision, + ...(backup === '' ? {} : { backup }), + note: '已采用云端版本覆盖本地(本地已备份)。', + }; + } } diff --git a/lib/skill-update.js b/lib/skill-update.js new file mode 100644 index 0000000..ed60a09 --- /dev/null +++ b/lib/skill-update.js @@ -0,0 +1,289 @@ +/** + * Skill update & rollback (P3). + * + * Works only for skills that carry a `.skill-source.json` provenance record + * (written by the market / git install pipeline). Nothing here talks to the + * cloud center: it re-reads the pinned git source, shows a diff, keeps a local + * backup before touching the folder, and restores that backup on rollback. + * + * Safety rules baked in: + * - the source record is the only thing trusted for a repository URL; + * - a fresh clone happens in `$DSH_HOME/.cloud-memory-staging` and the copy is + * filtered (no `.git`, no symlinks); + * - a backup is always taken before an update so rollback is possible even + * when the update is interrupted; + * - a repository in-flight sync blocks any write. + */ +import { execFile } from 'node:child_process'; +import { lstatSync } from 'node:fs'; +import { createHash } from 'node:crypto'; +import { cp, lstat, mkdir, readFile, readdir, rename, rm, writeFile } from 'node:fs/promises'; +import { join, resolve } from 'node:path'; +import { tmpdir } from 'node:os'; +import { readSkillSourceRecord, SkillAdminError } from "./skill-admin.js"; +import { dshHome } from "./store.js"; +/** Where pre-update backups are kept (one folder per skill, one dir per run). */ +export function skillBackupsDir() { + return join(dshHome(), '.cloud-memory-backups'); +} +/** Run git and return stdout. */ +function git(args, timeoutMs = 120_000) { + return new Promise((resolvePromise, rejectPromise) => { + execFile('git', args, { timeout: timeoutMs, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' }, maxBuffer: 8 * 1024 * 1024 }, (error, stdout) => (error ? rejectPromise(error) : resolvePromise(String(stdout)))); + }); +} +async function isSymlink(path) { + return lstat(path).then((s) => s.isSymbolicLink()).catch(() => true); +} +/** Hash every regular file under one directory (skips `.git` and symlinks). */ +async function hashTree(dir) { + const out = new Map(); + const walk = async (sub) => { + for (const entry of await readdir(join(dir, sub), { withFileTypes: true }).catch(() => [])) { + if (entry.name === '.git') + continue; + const rel = sub === '' ? entry.name : `${sub}/${entry.name}`; + const abs = join(dir, rel); + if (entry.isSymbolicLink()) + continue; + if (entry.isDirectory()) { + await walk(rel); + continue; + } + if (!entry.isFile()) + continue; + const buffer = await readFile(abs).catch(() => undefined); + if (buffer === undefined) + continue; + out.set(rel, { path: rel, sha256: createHash('sha256').update(buffer).digest('hex'), bytes: buffer.byteLength }); + } + }; + await walk(''); + return out; +} +/** A short human bucket for what changed. */ +function diffTrees(before, after) { + const added = []; + const removed = []; + const changed = []; + for (const [path, file] of after) { + const old = before.get(path); + if (old === undefined) + added.push(path); + else if (old.sha256 !== file.sha256) + changed.push(path); + } + for (const path of before.keys()) + if (!after.has(path)) + removed.push(path); + added.sort(); + removed.sort(); + changed.sort(); + return { added, removed, changed }; +} +/** Copy a skill tree, dropping `.git` and symlinks (same rule as install). */ +async function copySkillTree(source, target) { + await cp(source, target, { + recursive: true, + filter: (path) => !path.split(/[\\/]/).includes('.git') && (() => { + try { + return !lstatSync(path).isSymbolicLink(); + } + catch { + return false; + } + })(), + }); +} +export class SkillUpdater { + deps; + constructor(deps) { + this.deps = deps; + } + backupsRoot() { + return this.deps.backupsDir?.() ?? skillBackupsDir(); + } + guard() { + if (this.deps.syncRunning()) + throw new SkillAdminError('sync_running', '技能同步正在进行,请稍候再试。'); + } + async find(folder) { + const found = await this.deps.admin.locate(folder); + if (found === undefined) + throw new SkillAdminError('not_found', `没有找到技能:${folder}`); + const source = await readSkillSourceRecord(found.dir); + if (source === undefined) { + throw new SkillAdminError('no_source', `该技能没有来源记录(不是从 Git 或市场安装的),无法更新或回退。`); + } + return { dir: found.dir, source }; + } + /** Clone the pinned source at its latest commit into a scratch directory. */ + async cloneLatest(source) { + const root = join(dshHome(), '.cloud-memory-staging'); + await mkdir(root, { recursive: true }); + const dir = join(root, `update_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`); + try { + await git(['clone', '--depth', '1', '--no-tags', '--', source.repoUrl, dir]); + // A commit SHA is a pin, not a moving ref: leave the default-branch head + // in place so an update check compares against the latest upstream. A + // branch/tag name is fetched and checked out instead. + const isSha = /^[0-9a-f]{7,40}$/i.test(source.ref); + if (!isSha && source.ref !== '') { + try { + await git(['-C', dir, 'fetch', '--depth', '1', 'origin', source.ref]); + await git(['-C', dir, 'checkout', '--detach', 'FETCH_HEAD']); + } + catch { + // Fall back to the default branch head. + } + } + } + catch (error) { + await rm(dir, { recursive: true, force: true }).catch(() => { }); + const code = error.code; + throw new SkillAdminError('git_failed', code === 'ENOENT' + ? '本机没有 git 命令,无法检查更新。' + : '无法读取来源仓库(地址不可达、需要登录,或仓库已删除)。'); + } + const commit = (await git(['-C', dir, 'rev-parse', 'HEAD'], 20_000)).trim(); + return { dir, commit, cleanup: () => rm(dir, { recursive: true, force: true }).catch(() => { }) }; + } + /** Compare the installed folder against the source's latest commit. */ + async checkUpdate(folderInput) { + const folder = typeof folderInput === 'string' ? folderInput : ''; + if (folder === '') + throw new SkillAdminError('invalid_name', '缺少技能目录名。'); + const fetchedAt = Date.now(); + const found = await this.deps.admin.locate(folder); + if (found === undefined) + throw new SkillAdminError('not_found', `没有找到技能:${folder}`); + const source = await readSkillSourceRecord(found.dir); + if (source === undefined) { + return { folder, updatable: false, reason: 'no_source', available: false, fetchedAt }; + } + const clone = await this.cloneLatest(source); + try { + if (clone.commit === source.commit) { + return { + folder, updatable: true, available: false, provider: source.provider, + repoUrl: source.repoUrl, currentCommit: source.commit, remoteCommit: clone.commit, fetchedAt, + }; + } + const remoteDir = source.sourcePath === '' ? clone.dir : resolve(clone.dir, source.sourcePath); + const before = await hashTree(found.dir); + const after = await hashTree(remoteDir); + // The provenance file is ours, not the source's: never show it as removed. + before.delete('.skill-source.json'); + const diff = diffTrees(before, after); + return { + folder, updatable: true, available: true, provider: source.provider, + repoUrl: source.repoUrl, currentCommit: source.commit, remoteCommit: clone.commit, + ...diff, fetchedAt, + }; + } + finally { + await clone.cleanup(); + } + } + /** Back up the current folder, then replace it with the source's latest. */ + async applyUpdate(folderInput, expectedCommit) { + this.guard(); + const folder = typeof folderInput === 'string' ? folderInput : ''; + if (folder === '') + throw new SkillAdminError('invalid_name', '缺少技能目录名。'); + const { dir, source } = await this.find(folder); + const check = await this.checkUpdate(folder); + if (!check.available) + throw new SkillAdminError('up_to_date', '已经是最新版本,无需更新。'); + if (typeof expectedCommit === 'string' && expectedCommit !== '' && check.remoteCommit !== expectedCommit) { + throw new SkillAdminError('stale_plan', '来源在上次检查后已更新,请重新检查差异再更新。'); + } + return this.replaceWith(dir, source, { commit: check.remoteCommit ?? source.commit, ref: source.ref }); + } + /** Restore the most recent pre-update backup. */ + async rollback(folderInput) { + this.guard(); + const folder = typeof folderInput === 'string' ? folderInput : ''; + if (folder === '') + throw new SkillAdminError('invalid_name', '缺少技能目录名。'); + const { dir, source } = await this.find(folder); + const root = join(this.backupsRoot(), folder); + const entries = (await readdir(root, { withFileTypes: true }).catch(() => [])) + .filter((entry) => entry.isDirectory()) + .map((entry) => entry.name) + .sort(); + const latest = entries[entries.length - 1]; + if (latest === undefined) + throw new SkillAdminError('no_backup', '没有可回退的备份。'); + const backupDir = join(root, latest); + const restored = await readSkillSourceRecord(backupDir); + // Restore in place: move the current aside, copy the backup over, then drop + // the aside copy only after the backup has landed. + const aside = `${dir}.rollbacking-${Date.now().toString(36)}`; + await mkdir(join(dir, '..'), { recursive: true }); + await rename(dir, aside); + try { + await copySkillTree(backupDir, dir); + } + catch (error) { + await rm(dir, { recursive: true, force: true }).catch(() => { }); + await rename(aside, dir).catch(() => { }); + throw new SkillAdminError('rollback_failed', `回退失败,原目录已尽量还原:${error instanceof Error ? error.message : String(error)}`); + } + await rm(aside, { recursive: true, force: true }).catch(() => { }); + this.deps.log(`skill rolled back: ${folder} <- ${latest}`); + const beforeFiles = await hashTree(backupDir); + beforeFiles.delete('.skill-source.json'); + const nowFiles = await hashTree(dir); + nowFiles.delete('.skill-source.json'); + const diff = diffTrees(nowFiles, beforeFiles); + return { + folder, + from: source.commit, + to: restored?.commit ?? source.commit, + backup: backupDir, + ...diff, + }; + } + /** Shared write path: backup, copy new tree, rewrite provenance. */ + async replaceWith(dir, source, next) { + const clone = await this.cloneLatest({ ...source, ref: next.ref }); + let newDir = source.sourcePath === '' ? clone.dir : resolve(clone.dir, source.sourcePath); + try { + if (clone.commit !== next.commit) { + // A different head landed between check and apply: refuse, never guess. + throw new SkillAdminError('stale_plan', '来源在更新过程中已变化,请重新检查。'); + } + const stamp = new Date().toISOString().replace(/[:.]/g, '-'); + const backupDir = join(this.backupsRoot(), this.lastSegment(dir), stamp); + await mkdir(join(backupDir, '..'), { recursive: true }); + await copySkillTree(dir, backupDir); + const before = await hashTree(dir); + const after = await hashTree(newDir); + const diff = diffTrees(before, after); + const aside = `${dir}.updating-${Date.now().toString(36)}`; + await rename(dir, aside); + try { + await copySkillTree(newDir, dir); + await writeFile(join(dir, '.skill-source.json'), `${JSON.stringify({ + ...source, commit: next.commit, ref: next.ref, installedAt: new Date().toISOString(), + }, null, 2)}\n`, 'utf8'); + } + catch (error) { + await rm(dir, { recursive: true, force: true }).catch(() => { }); + await rename(aside, dir).catch(() => { }); + throw new SkillAdminError('update_failed', `更新失败,原目录已还原:${error instanceof Error ? error.message : String(error)}`); + } + await rm(aside, { recursive: true, force: true }).catch(() => { }); + this.deps.log(`skill updated: ${this.lastSegment(dir)} -> ${next.commit}`); + return { folder: this.lastSegment(dir), from: source.commit, to: next.commit, backup: backupDir, ...diff }; + } + finally { + await clone.cleanup(); + } + } + lastSegment(path) { + const parts = path.split(/[\\/]/).filter((part) => part !== ''); + return parts[parts.length - 1] ?? path; + } +} diff --git a/package.json b/package.json index fd56cfc..32e71c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsh-cloud-memory", - "version": "0.5.0", + "version": "0.6.0", "description": "Cloud memory for DSH: report one activity turn per conversation round and expose memory_search / memory_read / memory_add tools against a LongMemory cloud endpoint.", "type": "module", "license": "MIT", diff --git a/src/index.ts b/src/index.ts index 054e49f..9442b71 100644 --- a/src/index.ts +++ b/src/index.ts @@ -18,6 +18,8 @@ import { SettingsStore, dshHome } from './store.ts' import { SkillSync } from './skill-sync.ts' import { SkillAdmin, skillsHoldingDir } from './skill-admin.ts' import { SkillGroupManager, detectSources } from './skill-groups.ts' +import { ClaudePluginMarketplaceProvider, MarketService } from './market.ts' +import { SkillUpdater } from './skill-update.ts' import { createMemoryTools, createSkillTools } from './tools.ts' /** Package name; also the client bundle id. */ @@ -27,7 +29,7 @@ export const name = 'dsh-cloud-memory' export const inject = ['webServer'] /** Version reported by the status card. */ -const VERSION = '0.5.0' +const VERSION = '0.6.0' /** How often the skill directory is reconciled with the cloud center. */ const SKILL_SYNC_INTERVAL_MS = 15 * 60_000 @@ -83,6 +85,27 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void { admin: skillAdmin, log: (message) => logger.info(message), }) + + // One verified market source to start with: the Claude Code plugin marketplace + // manifest that Anthropic publishes. Entry ids never carry a URL, so the card + // cannot make the host fetch an arbitrary repository. + const market = new MarketService([ + new ClaudePluginMarketplaceProvider({ + id: 'anthropic-skills', + label: 'Anthropic Agent Skills', + description: 'Anthropic 公开的 Claude 技能市场(.claude-plugin/marketplace.json)', + repo: 'anthropics/skills', + ref: 'main', + }), + ]) + + const updater = new SkillUpdater({ + admin: skillAdmin, + skillsRoot: () => skillSync.skillsRoot(), + holdingDir: () => skillsHoldingDir(), + syncRunning: () => skillSync.stats().running, + log: (message) => logger.info(message), + }) // Announce at most once per activation so a missing token is obvious in logs. const effective = settings() if (!effective.enabled) { @@ -131,7 +154,7 @@ export function apply(ctx: CloudMemoryContext, config: unknown): void { if (webServer === undefined) { logger.warn('webServer service unavailable; the configuration page stays disabled') } else { - const routes = createRoutes({ version: VERSION, store, reporter, client: () => client, skills: () => skillSync.stats(), admin: skillAdmin, groups: skillGroups }) + const routes = createRoutes({ version: VERSION, store, reporter, client: () => client, skills: () => skillSync.stats(), admin: skillAdmin, groups: skillGroups, market, sync: skillSync, updates: updater }) for (const route of routes) { ctx.effect(() => webServer.register(route), `dsh-cloud-memory: route ${route.path}`) } diff --git a/src/market.ts b/src/market.ts new file mode 100644 index 0000000..f72de14 --- /dev/null +++ b/src/market.ts @@ -0,0 +1,306 @@ +/** + * Skill market providers (P3). + * + * A provider answers `search` / `getDetail` / `resolveArtifact` for ONE + * configured, verified source. The browser half never passes a repository URL: + * it only names a provider id and an entry id, so no request can make the host + * fetch an arbitrary address or clone an arbitrary repository. + * + * First provider: the Claude Code plugin marketplace manifest that lives at + * `.claude-plugin/marketplace.json` in a public GitHub repository. That format + * lists plugins, each with a description and the relative folders of the skills + * it ships. Installing an entry reuses the same staged plan/commit pipeline as + * a manual git install, pinned to the commit resolved here. + */ +import { parseSkillFrontmatter } from './skill-sync.ts' + +/** One provider as the card lists it (no credentials, no local paths). */ +export interface MarketProviderInfo { + id: string + label: string + description: string + /** `owner/repo` the manifest is read from. */ + source: string + homepage: string +} + +/** One normalized marketplace entry. */ +export interface MarketEntry { + id: string + providerId: string + name: string + description: string + version?: string + author?: string + /** Skill folder names (basename of each skill path). */ + skills: string[] + /** Relative paths as written in the manifest, e.g. `./skills/xlsx`. */ + skillPaths: string[] + sourceRepo: string + sourceUrl: string +} + +/** Detail adds per-skill metadata read from each SKILL.md (best effort). */ +export interface MarketDetail extends MarketEntry { + skillDetails: { folder: string; path: string; name?: string; description?: string; ok: boolean }[] + fetchedAt: number +} + +/** What an install plan needs to clone a pinned artifact. */ +export interface MarketArtifact { + repoUrl: string + /** A ref git can fetch (branch, tag or commit SHA). */ + ref: string + /** Resolved commit SHA when the host could look it up. */ + commit?: string + version?: string + folders: string[] +} + +export interface MarketProvider { + info(): MarketProviderInfo + search(query: string, cursor: number): Promise<{ entries: MarketEntry[]; nextCursor?: number }> + getDetail(entryId: string): Promise + resolveArtifact(entryId: string, version?: string): Promise +} + +/** Typed failure so routes can map it to a status code. */ +export class MarketError extends Error { + constructor(public readonly code: string, message: string) { + super(message) + this.name = 'MarketError' + } +} + +/** Row shape of `.claude-plugin/marketplace.json`. */ +interface MarketplaceManifest { + name?: string + owner?: { name?: string } + metadata?: { version?: string; description?: string } + plugins?: { + name?: string + description?: string + source?: string + skills?: string[] + }[] +} + +/** Normalize a manifest skill path to a folder name (its basename). */ +function folderOf(skillPath: string): string { + const trimmed = skillPath.replace(/^\.\//, '').replace(/\/+$/, '') + const parts = trimmed.split('/') + return parts[parts.length - 1] ?? trimmed +} + +/** Small TTL cache so one search + one detail does not refetch the manifest. */ +interface CacheEntry { + at: number + value: unknown +} + +/** Provider over a Claude Code plugin marketplace manifest on GitHub. */ +export class ClaudePluginMarketplaceProvider implements MarketProvider { + private readonly cache = new Map() + private readonly fetchImpl: typeof fetch + + constructor( + private readonly options: { + id: string + label: string + description: string + repo: string + ref?: string + homepage?: string + timeoutMs?: number + fetchImpl?: typeof fetch + }, + ) { + this.fetchImpl = options.fetchImpl ?? fetch + } + + info(): MarketProviderInfo { + return { + id: this.options.id, + label: this.options.label, + description: this.options.description, + source: this.options.repo, + homepage: this.options.homepage ?? `https://github.com/${this.options.repo}`, + } + } + + private ref(): string { + return this.options.ref ?? 'main' + } + + private timeoutMs(): number { + return this.options.timeoutMs ?? 8000 + } + + private rawUrl(path: string): string { + const clean = path.replace(/^\.\//, '') + return `https://raw.githubusercontent.com/${this.options.repo}/${encodeURIComponent(this.ref())}/${clean}` + } + + private async fetchText(url: string): Promise { + let response: Response + try { + response = await this.fetchImpl(url, { + headers: { accept: 'application/json, text/plain, */*', 'user-agent': 'dsh-cloud-memory-market' }, + signal: AbortSignal.timeout(this.timeoutMs()), + }) + } catch (error) { + throw new MarketError('unreachable', `市场来源不可达:${error instanceof Error ? error.message : String(error)}`) + } + if (!response.ok) { + throw new MarketError('http_' + String(response.status), `市场来源返回 HTTP ${response.status}`) + } + return response.text() + } + + private async fetchJson(url: string, ttlMs = 60_000): Promise { + const hit = this.cache.get(url) + if (hit !== undefined && Date.now() - hit.at < ttlMs) return hit.value as T + const text = await this.fetchText(url) + let parsed: unknown + try { + parsed = JSON.parse(text) + } catch { + throw new MarketError('invalid_manifest', '市场清单不是合法 JSON。') + } + this.cache.set(url, { at: Date.now(), value: parsed }) + return parsed as T + } + + private async manifest(): Promise { + const parsed = await this.fetchJson(this.rawUrl('.claude-plugin/marketplace.json')) + if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) { + throw new MarketError('invalid_manifest', '市场清单结构不正确。') + } + return parsed as MarketplaceManifest + } + + private entriesOf(manifest: MarketplaceManifest): MarketEntry[] { + const list = Array.isArray(manifest.plugins) ? manifest.plugins : [] + const version = manifest.metadata?.version + const author = manifest.owner?.name + const entries: MarketEntry[] = [] + for (const plugin of list) { + const name = typeof plugin.name === 'string' ? plugin.name : '' + if (name === '') continue + const paths = Array.isArray(plugin.skills) ? plugin.skills.filter((p): p is string => typeof p === 'string') : [] + const entry: MarketEntry = { + id: name, + providerId: this.options.id, + name, + description: typeof plugin.description === 'string' ? plugin.description : '', + skills: paths.map(folderOf), + skillPaths: paths, + sourceRepo: this.options.repo, + sourceUrl: `https://github.com/${this.options.repo}`, + } + if (typeof version === 'string') entry.version = version + if (typeof author === 'string') entry.author = author + entries.push(entry) + } + return entries + } + + async search(query: string, cursor: number): Promise<{ entries: MarketEntry[]; nextCursor?: number }> { + const entries = this.entriesOf(await this.manifest()) + const needle = query.trim().toLowerCase() + const filtered = needle === '' + ? entries + : entries.filter((entry) => + `${entry.name}\n${entry.description}\n${entry.skills.join(' ')}`.toLowerCase().includes(needle), + ) + const pageSize = 10 + const start = Number.isFinite(cursor) && cursor > 0 ? Math.floor(cursor) : 0 + const page = filtered.slice(start, start + pageSize) + const next = start + pageSize < filtered.length ? start + pageSize : undefined + return next === undefined ? { entries: page } : { entries: page, nextCursor: next } + } + + private findEntry(manifest: MarketplaceManifest, entryId: string): MarketEntry { + const entry = this.entriesOf(manifest).find((item) => item.id === entryId) + if (entry === undefined) throw new MarketError('not_found', `市场里没有条目:${entryId}`) + return entry + } + + async getDetail(entryId: string): Promise { + const entry = this.findEntry(await this.manifest(), entryId) + const skillDetails: MarketDetail['skillDetails'] = [] + // Bound the fan-out: a manifest can list dozens of skills. + for (const path of entry.skillPaths.slice(0, 24)) { + const folder = folderOf(path) + try { + const markdown = await this.fetchText(this.rawUrl(`${path.replace(/^\.\//, '')}/SKILL.md`)) + const frontmatter = parseSkillFrontmatter(markdown) + const detail: MarketDetail['skillDetails'][number] = { folder, path, ok: true } + if (typeof frontmatter.name === 'string') detail.name = frontmatter.name + if (typeof frontmatter.description === 'string') detail.description = frontmatter.description.slice(0, 300) + skillDetails.push(detail) + } catch { + skillDetails.push({ folder, path, ok: false }) + } + } + return { ...entry, skillDetails, fetchedAt: Date.now() } + } + + async resolveArtifact(entryId: string, _version?: string): Promise { + const entry = this.findEntry(await this.manifest(), entryId) + let commit: string | undefined + try { + const payload = await this.fetchJson<{ sha?: unknown }>( + `https://api.github.com/repos/${this.options.repo}/commits/${encodeURIComponent(this.ref())}`, + 30_000, + ) + if (typeof payload.sha === 'string' && /^[0-9a-f]{7,40}$/i.test(payload.sha)) commit = payload.sha + } catch { + // Rate-limited or offline: fall back to the branch ref; git checkout pins + // the actual commit at clone time and the install records it. + } + const artifact: MarketArtifact = { + repoUrl: `https://github.com/${this.options.repo}.git`, + ref: commit ?? this.ref(), + folders: entry.skills, + } + if (commit !== undefined) artifact.commit = commit + if (entry.version !== undefined) artifact.version = entry.version + return artifact + } +} + +/** A small registry of providers; the browser can only ask for one by id. */ +export class MarketService { + private readonly byId = new Map() + + constructor(providers: MarketProvider[]) { + for (const provider of providers) this.byId.set(provider.info().id, provider) + } + + list(): MarketProviderInfo[] { + return [...this.byId.values()].map((provider) => provider.info()) + } + + has(providerId: string): boolean { + return this.byId.has(providerId) + } + + get(providerId: string): MarketProvider { + const provider = this.byId.get(providerId) + if (provider === undefined) throw new MarketError('unknown_provider', `未知的市场来源:${providerId}`) + return provider + } + + search(providerId: string, query: string, cursor: number): ReturnType { + return this.get(providerId).search(query, cursor) + } + + detail(providerId: string, entryId: string): ReturnType { + return this.get(providerId).getDetail(entryId) + } + + resolve(providerId: string, entryId: string, version?: string): ReturnType { + return this.get(providerId).resolveArtifact(entryId, version) + } +} diff --git a/src/mnemon/index.ts b/src/mnemon/index.ts index 6b0734d..a2a44df 100644 --- a/src/mnemon/index.ts +++ b/src/mnemon/index.ts @@ -23,7 +23,7 @@ import { LongMemoryProvider } from './driver.ts' export { LongMemoryProvider, descriptor } /** Keep in sync with the package version (asserted by the verify script). */ -const PROVIDER_VERSION = '0.5.0' +const PROVIDER_VERSION = '0.6.0' export const definition = defineMemorySpaceProviderDefinition({ manifest: { diff --git a/src/routes.ts b/src/routes.ts index 73833c0..75515fb 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -26,9 +26,12 @@ import type { SettingsView, SettingsStore, TokenView } from './store.ts' import type { Reporter } from './reporter.ts' import { renderConfigPage } from './page.ts' import type { CloudMemoryWebRoute } from './context-types.ts' -import type { SkillSyncStats } from './skill-sync.ts' +import type { SkillSync, SkillSyncStats } from './skill-sync.ts' import { SkillAdminError } from './skill-admin.ts' import type { SkillAdmin } from './skill-admin.ts' +import { MarketError } from './market.ts' +import type { MarketService } from './market.ts' +import type { SkillUpdater } from './skill-update.ts' import { SkillGroupError } from './skill-groups.ts' import type { SkillGroupManager } from './skill-groups.ts' import { renderSkillsPage } from './skill-page.ts' @@ -44,6 +47,8 @@ export const ROUTES = { test: '/cloud-memory/test', flush: '/cloud-memory/flush', skills: '/cloud-memory/skills', + market: '/cloud-memory/market', + sync: '/cloud-memory/sync', } as const /** Largest request body we accept (a settings patch is tiny). */ @@ -61,6 +66,12 @@ export interface RouteDeps { admin?: SkillAdmin /** Multi-source skill tree (sources, categories, per-group switches). */ groups?: SkillGroupManager + /** Skill market providers (search / detail / resolve artifact). */ + market?: MarketService + /** The sync engine itself, for conflict details and interactive resolution. */ + sync?: SkillSync + /** Update / rollback over a recorded git or market source. */ + updates?: SkillUpdater } /** One feature flag the card reads before it renders a control. */ @@ -74,6 +85,12 @@ export interface CapabilityReport { skillUninstallPlan: boolean skillsSync: boolean skillsMarketplace: boolean + /** Market search/detail/install is wired to a real provider. */ + marketProviders?: number + /** Update/rollback is available for skills with a source record. */ + skillUpdate?: boolean + /** Interactive sync conflict resolution is available. */ + syncResolve?: boolean mcpBridge: 'host-plugin' | 'unavailable' } notes: string[] @@ -81,6 +98,7 @@ export interface CapabilityReport { /** What this build actually implements, stated per feature. */ function capabilities(deps: RouteDeps): CapabilityReport { + const marketProviders = deps.market?.list().length ?? 0 return { version: deps.version, features: { @@ -90,15 +108,22 @@ function capabilities(deps: RouteDeps): CapabilityReport { skillInstallPlans: deps.admin !== undefined, skillUninstallPlan: deps.admin !== undefined, skillsSync: deps.skills !== undefined, - // No verified marketplace provider is wired in this build; the card says - // so instead of showing a fake search box. - skillsMarketplace: false, + skillsMarketplace: deps.market !== undefined, + marketProviders, + skillUpdate: deps.updates !== undefined, + syncResolve: deps.sync !== undefined, // The MCP bridge is provided by the host `dsh-mcp-connector` plugin over // the same origin; the browser half probes it at runtime. mcpBridge: 'host-plugin', }, notes: [ - '技能市场尚未接入经验证的来源,本版不提供搜索与安装闭环。', + ...(deps.market === undefined + ? ['技能市场未启用:没有配置市场来源。'] + : [`技能市场已接入 ${marketProviders} 个来源;安装走暂存计划并固定提交。`]), + ...(deps.updates === undefined + ? ['技能更新/回退未启用。'] + : ['更新/回退仅对带来源记录(.skill-source.json)的技能生效,更新前自动备份。']), + ...(deps.sync === undefined ? [] : ['同步冲突可在技能页签逐项“保留本地 / 采用云端 / 手动合并”。']), 'MCP 管理由宿主 dsh-mcp-connector 插件提供,本卡通过其公开接口读写,不另建凭据库。', ], } @@ -517,7 +542,11 @@ function adminStatus(code: string): number { case 'sync_running': case 'plan_expired': case 'confirmation_required': + case 'no_source': + case 'up_to_date': + case 'stale_plan': case 'unsupported': return 409 + case 'no_backup': return 404 case 'no_skills_found': return 422 case 'git_failed': return 502 default: return 500 @@ -625,6 +654,13 @@ async function serveSkills(deps: RouteDeps, req: IncomingMessage, res: ServerRes return { ...(await admin.remove(folder)) } }, } + // Update/rollback operate on skills with a `.skill-source.json` record. They + // are optional: without the updater wired in the routes answer 404. + const updateActions: Record = { + '/update/check': async (body) => ({ ...(await requireUpdates(deps).checkUpdate(body.folder)) }), + '/update/apply': async (body) => ({ ...(await requireUpdates(deps).applyUpdate(body.folder, body.expectedCommit)) }), + '/rollback': async (body) => ({ ...(await requireUpdates(deps).rollback(body.folder)) }), + } // Category and source switches live in the same prefix so the card has one // base URL; they are grouped here because they never touch a skill folder // directly (except the DSH park/unpark the group manager delegates). @@ -637,7 +673,7 @@ async function serveSkills(deps: RouteDeps, req: IncomingMessage, res: ServerRes '/source/toggle': async (body) => ({ ...(await requireGroups(deps).toggleSource(body.sourceId, body.enabled === true)) }), '/source/reveal': async (body) => ({ ...(await requireGroups(deps).reveal(body.sourceId)) }), } - const action = actions[suffix] ?? groupActions[suffix] + const action = actions[suffix] ?? groupActions[suffix] ?? updateActions[suffix] if (action === undefined) { fail(res, 404, 'unknown-skill-route') return @@ -678,6 +714,154 @@ function groupStatus(code: string): number { } } +/** The updater is optional; its routes answer 404 when it is absent. */ +function requireUpdates(deps: RouteDeps): SkillUpdater { + if (deps.updates === undefined) throw new SkillAdminError('unsupported', '技能更新/回退未启用。') + return deps.updates +} + +/** HTTP status for one market failure code. */ +function marketStatus(code: string): number { + switch (code) { + case 'unknown_provider': + case 'invalid_manifest': return 400 + case 'not_found': return 404 + case 'unreachable': + case 'unavailable': return 502 + default: + return code.startsWith('http_') ? 502 : 500 + } +} + +/** + * Market surface, one prefix route: + * `GET /cloud-memory/market/providers` configured sources + * `GET /cloud-memory/market/search` ?provider=&q=&cursor= + * `POST /cloud-memory/market/detail` { provider, id } + * `POST /cloud-memory/market/plan` { provider, id } — pin commit + stage a plan + * `POST /cloud-memory/market/commit` { planId, folders } + * `POST /cloud-memory/market/cancel` { planId } + */ +async function serveMarket(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { + const market = deps.market + if (market === undefined) { + fail(res, 404, 'market-unavailable') + return + } + const url = new URL(req.url ?? '/', 'http://localhost') + const suffix = url.pathname.slice(ROUTES.market.length) + try { + if (suffix === '' || suffix === '/' || suffix === '/providers') { + if (req.method !== 'GET') { fail(res, 405, 'method-not-allowed'); return } + json(res, 200, { ok: true, providers: market.list() }) + return + } + if (suffix === '/search') { + if (req.method !== 'GET') { fail(res, 405, 'method-not-allowed'); return } + const providerId = url.searchParams.get('provider') ?? market.list()[0]?.id ?? '' + const query = url.searchParams.get('q') ?? '' + const cursorRaw = Number(url.searchParams.get('cursor') ?? '0') + const cursor = Number.isFinite(cursorRaw) && cursorRaw > 0 ? cursorRaw : 0 + const result = await market.search(providerId, query, cursor) + json(res, 200, { ok: true, provider: market.get(providerId).info(), ...result }) + return + } + type Handler = (body: Record) => Promise> + const admin = deps.admin + const requireAdmin = (): SkillAdmin => { + if (admin === undefined) throw new MarketError('unavailable', '技能管理未启用,无法安装。') + return admin + } + const actions: Record = { + '/detail': async (body) => ({ ...(await market.detail(String(body.provider ?? ''), String(body.id ?? ''))) }), + '/plan': async (body) => { + const providerId = String(body.provider ?? '') + const id = String(body.id ?? '') + const artifact = await market.resolve(providerId, id, typeof body.version === 'string' ? body.version : undefined) + const plan = await requireAdmin().prepareInstall(artifact.repoUrl, { + ref: artifact.ref, + source: { + provider: providerId, + entryId: id, + ...(artifact.version === undefined ? {} : { version: artifact.version }), + }, + }) + const wanted = new Set(artifact.folders) + return { + planId: plan.planId, + ...(plan.commit === undefined ? {} : { commit: plan.commit }), + artifact, + candidates: plan.candidates.filter((candidate) => wanted.has(candidate.folder)), + totalInRepo: plan.candidates.length, + } + }, + '/commit': async (body) => ({ ...(await requireAdmin().commitInstall({ planId: body.planId, folders: body.folders })) }), + '/cancel': async (body) => ({ ...(await requireAdmin().cancelInstall({ planId: body.planId })) }), + } + const action = actions[suffix] + if (action === undefined) { fail(res, 404, 'unknown-market-route'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + json(res, 200, { ok: true, ...(await action(body)) }) + } catch (error) { + if (error instanceof MarketError) { + json(res, marketStatus(error.code), { ok: false, code: error.code, error: error.message }) + return + } + if (error instanceof SkillAdminError) { + json(res, adminStatus(error.code), { ok: false, code: error.code, error: error.message }) + return + } + json(res, 500, { ok: false, code: 'internal', error: error instanceof Error ? error.message : String(error) }) + } +} + +/** + * Sync surface: + * `GET /cloud-memory/sync/status` latest stats + both sides of each conflict + * `POST /cloud-memory/sync/run` trigger one pass now + * `POST /cloud-memory/sync/resolve` { name, action: keep_local | take_cloud | manual } + */ +async function serveSync(deps: RouteDeps, req: IncomingMessage, res: ServerResponse): Promise { + const sync = deps.sync + if (sync === undefined) { + fail(res, 404, 'sync-unavailable') + return + } + const url = new URL(req.url ?? '/', 'http://localhost') + const suffix = url.pathname.slice(ROUTES.sync.length) + try { + if (suffix === '' || suffix === '/' || suffix === '/status') { + if (req.method !== 'GET') { fail(res, 405, 'method-not-allowed'); return } + json(res, 200, { ok: true, stats: sync.stats(), conflicts: await sync.conflictDetails() }) + return + } + if (suffix === '/run') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const stats = await sync.syncOnce() + json(res, 200, { ok: true, stats, conflicts: await sync.conflictDetails() }) + return + } + if (suffix === '/resolve') { + if (req.method !== 'POST') { fail(res, 405, 'method-not-allowed'); return } + const body = await jsonBody(req, res) + if (body === undefined) return + const result = await sync.resolveConflict(body.name, body.action) + json(res, 200, { ok: true, ...result, conflicts: await sync.conflictDetails() }) + return + } + fail(res, 404, 'unknown-sync-route') + } catch (error) { + const message = error instanceof Error ? error.message : String(error) + const known = ['invalid_name', 'invalid_action', 'no_local_copy', 'no_cloud_copy', 'stale_revision', 'sync_running', 'manifest_mismatch'] + const code = known.includes(message) ? message : 'internal' + const status = code === 'internal' ? 500 + : code === 'invalid_name' || code === 'invalid_action' ? 400 + : 409 + json(res, status, { ok: false, code, error: message }) + } +} + /** Build the exact-route table registered with the host web server. */ export function createRoutes(deps: RouteDeps): CloudMemoryWebRoute[] { return [ @@ -721,5 +905,15 @@ export function createRoutes(deps: RouteDeps): CloudMemoryWebRoute[] { path: ROUTES.skills, handler: (req, res) => serveSkills(deps, req, res), }, + { + kind: 'prefix', + path: ROUTES.market, + handler: (req, res) => serveMarket(deps, req, res), + }, + { + kind: 'prefix', + path: ROUTES.sync, + handler: (req, res) => serveSync(deps, req, res), + }, ] } diff --git a/src/skill-admin.ts b/src/skill-admin.ts index 9541bb3..02cc36e 100644 --- a/src/skill-admin.ts +++ b/src/skill-admin.ts @@ -29,7 +29,7 @@ import { lstatSync } from 'node:fs' import { execFile } from 'node:child_process' import { createHash, randomUUID } from 'node:crypto' import { cp, lstat, mkdir, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises' -import { basename, dirname, join, resolve, sep } from 'node:path' +import { basename, dirname, join, relative, resolve, sep } from 'node:path' import { parseSkillFrontmatter } from './skill-sync.ts' import { dshHome } from './store.ts' @@ -58,6 +58,18 @@ export class SkillAdminError extends Error { } } +/** Run git and return stdout (used for resolving the checked-out commit). */ +function execFileText(file: string, args: string[], timeoutMs = 120_000): Promise { + return new Promise((resolvePromise, rejectPromise) => { + execFile( + file, + args, + { timeout: timeoutMs, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' }, maxBuffer: 4 * 1024 * 1024 }, + (error, stdout) => (error ? rejectPromise(error) : resolvePromise(String(stdout))), + ) + }) +} + function fail(code: string, message: string): never { throw new SkillAdminError(code, message) } @@ -86,6 +98,31 @@ function isSymlinkSync(path: string): boolean { } } +/** Read a skill's `.skill-source.json`, or undefined when absent/corrupt. */ +export async function readSkillSourceRecord(dir: string): Promise { + const raw = await readFile(join(dir, '.skill-source.json'), 'utf8').catch(() => undefined) + if (raw === undefined) return undefined + try { + const parsed: unknown = JSON.parse(raw) + if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) return undefined + const record = parsed as Partial + if (typeof record.repoUrl !== 'string' || typeof record.commit !== 'string') return undefined + return { + version: 1, + provider: typeof record.provider === 'string' ? record.provider : 'git', + repoUrl: record.repoUrl, + ref: typeof record.ref === 'string' ? record.ref : record.commit, + commit: record.commit, + sourcePath: typeof record.sourcePath === 'string' ? record.sourcePath : '', + installedAt: typeof record.installedAt === 'string' ? record.installedAt : new Date(0).toISOString(), + ...(typeof record.entryId === 'string' ? { entryId: record.entryId } : {}), + ...(typeof record.marketVersion === 'string' ? { marketVersion: record.marketVersion } : {}), + } + } catch { + return undefined + } +} + /** Files under one skill folder, skipping `.git` and never following links. */ async function walkFiles(dir: string, cap = MAX_SKILL_FILES + 1): Promise<{ path: string; bytes: number; script: boolean }[]> { const out: { path: string; bytes: number; script: boolean }[] = [] @@ -122,6 +159,29 @@ function checkFolder(folder: unknown): string { return folder } +/** Where a skill came from, recorded next to it so it can be updated/rolled back. */ +export interface SkillInstallSource { + /** `git` for a manual repository install, or a market provider id. */ + provider: string + /** Market entry id, when the install came from a marketplace. */ + entryId?: string + version?: string +} + +/** On-disk record written into an installed skill as `.skill-source.json`. */ +export interface SkillSourceRecord { + version: 1 + provider: string + repoUrl: string + ref: string + commit: string + /** Path inside the repository the folder was copied from. */ + sourcePath: string + entryId?: string + marketVersion?: string + installedAt: string +} + /** One planned install candidate (metadata only). */ interface InstallPlanCandidate { folder: string @@ -133,6 +193,8 @@ interface InstallPlanCandidate { conflict: boolean conflictLocation?: 'root' | 'holding' problem?: string + /** Path of the candidate relative to the staging clone root. */ + rel?: string } interface InstallPlan { @@ -143,6 +205,10 @@ interface InstallPlan { expiresAt: number stagingDir: string candidates: InstallPlanCandidate[] + /** Commit the clone was pinned to (recorded for update/rollback). */ + commit?: string + ref?: string + source?: SkillInstallSource } interface PlansFile { @@ -165,6 +231,16 @@ export interface SkillEntry { location: 'root' | 'holding' ownerManager: string writable: boolean + /** Present when the folder carries a `.skill-source.json` record. */ + source?: { + provider: string + repoUrl: string + ref: string + commit: string + entryId?: string + } + /** True when a source record exists, so update/rollback can be offered. */ + updatable?: boolean } export interface SkillAdminDeps { @@ -246,6 +322,7 @@ export class SkillAdmin { const bytes = files.reduce((sum, file) => sum + file.bytes, 0) const installedAt = await stat(join(dir, 'SKILL.md')).then((s) => new Date(s.mtimeMs).toISOString()).catch(() => undefined) const name = frontmatter.name + const source = await readSkillSourceRecord(dir) return { folder, name: typeof name === 'string' && name !== '' ? name : folder, @@ -260,6 +337,18 @@ export class SkillAdmin { location: enabled ? 'root' : 'holding', ownerManager: 'dsh-cloud-memory', writable: true, + ...(source === undefined + ? {} + : { + source: { + provider: source.provider, + repoUrl: source.repoUrl, + ref: source.ref, + commit: source.commit, + ...(source.entryId === undefined ? {} : { entryId: source.entryId }), + }, + }), + updatable: source !== undefined, } } @@ -439,10 +528,11 @@ export class SkillAdmin { * Stage a git repository and describe its skills. Nothing enters the skills * root here; the caller picks folders and commits. */ - async prepareInstall(urlInput: unknown): Promise<{ + async prepareInstall(urlInput: unknown, options: { ref?: string; source?: SkillInstallSource } = {}): Promise<{ planId: string url: string expiresAt: number + commit?: string candidates: (InstallPlanCandidate & { id: string })[] }> { this.guardSync() @@ -456,14 +546,16 @@ export class SkillAdmin { const planId = `plan_${Date.now().toString(36)}_${randomUUID().slice(0, 8)}` const stagingDir = join(stagingRoot, planId) try { - await new Promise((resolvePromise, rejectPromise) => { - execFile( - 'git', - ['clone', '--depth', '1', '--no-tags', '--', url, stagingDir], - { timeout: 120_000, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' }, maxBuffer: 4 * 1024 * 1024 }, - (error) => (error ? rejectPromise(error) : resolvePromise()), - ) - }) + await execFileText('git', ['clone', '--depth', '1', '--no-tags', '--', url, stagingDir]) + if (typeof options.ref === 'string' && options.ref !== '') { + try { + await execFileText('git', ['-C', stagingDir, 'fetch', '--depth', '1', 'origin', options.ref]) + await execFileText('git', ['-C', stagingDir, 'checkout', '--detach', 'FETCH_HEAD']) + } catch { + // The default-branch clone is still usable; the recorded commit below + // is whatever was actually checked out, so an install stays auditable. + } + } } catch (error) { await rm(stagingDir, { recursive: true, force: true }).catch(() => {}) const code = (error as NodeJS.ErrnoException).code @@ -471,6 +563,12 @@ export class SkillAdmin { ? '本机没有 git 命令,无法从仓库安装技能。' : '仓库克隆失败(地址不可达、需要登录,或不是 git 仓库)。') } + let commit = '' + try { + commit = (await execFileText('git', ['-C', stagingDir, 'rev-parse', 'HEAD'], 20_000)).trim() + } catch { + commit = '' + } const repoName = basename(url.replace(/\/$/, '').replace(/\.git$/, '')) || 'skill' const found: { folder: string; dir: string }[] = [] const collect = async (dir: string, depth: number): Promise => { @@ -518,6 +616,7 @@ export class SkillAdmin { conflict: existing !== undefined, ...(existing === undefined ? {} : { conflictLocation: existing.area }), ...(problem === undefined ? {} : { problem }), + rel: relative(stagingDir, candidate.dir), }) } const plan: InstallPlan = { @@ -528,6 +627,9 @@ export class SkillAdmin { expiresAt: Date.now() + PLAN_TTL_MS, stagingDir, candidates, + ...(commit === '' ? {} : { commit }), + ...(typeof options.ref === 'string' && options.ref !== '' ? { ref: options.ref } : {}), + ...(options.source === undefined ? {} : { source: options.source }), } const file = await this.sweepPlans(await this.readPlans()) file.plans.push(plan) @@ -537,7 +639,8 @@ export class SkillAdmin { planId, url, expiresAt: plan.expiresAt, - candidates: candidates.map((candidate) => ({ ...candidate, id: candidate.folder })), + ...(commit === '' ? {} : { commit }), + candidates: candidates.map((candidate) => ({ ...candidate, id: candidate.folder })) as (InstallPlanCandidate & { id: string })[], } } @@ -574,7 +677,9 @@ export class SkillAdmin { continue } const stagingRoot = resolve(plan.stagingDir) - const source = candidate.folder === basename(plan.stagingDir) ? stagingRoot : resolve(stagingRoot, candidate.folder) + const source = candidate.rel !== undefined && candidate.rel !== '' + ? resolve(stagingRoot, candidate.rel) + : candidate.folder === basename(plan.stagingDir) ? stagingRoot : resolve(stagingRoot, candidate.folder) const target = resolve(skillsRoot, candidate.folder) // Both sides must stay inside their own root: a crafted plan file cannot // make this write outside the skills directory. @@ -588,6 +693,22 @@ export class SkillAdmin { // repository: both are dropped rather than copied. filter: (sourcePath) => !sourcePath.split(/[\\/]/).includes('.git') && !isSymlinkSync(sourcePath), }) + // Record provenance so the skill can later be diffed, updated and rolled + // back without the cloud center. The file holds no credentials. + if (plan.commit !== undefined && plan.commit !== '') { + const record: SkillSourceRecord = { + version: 1, + provider: plan.source?.provider ?? 'git', + repoUrl: plan.url, + ref: plan.ref ?? plan.commit, + commit: plan.commit, + sourcePath: candidate.rel ?? candidate.folder, + installedAt: new Date().toISOString(), + ...(plan.source?.entryId === undefined ? {} : { entryId: plan.source.entryId }), + ...(plan.source?.version === undefined ? {} : { marketVersion: plan.source.version }), + } + await writeFile(join(target, '.skill-source.json'), `${JSON.stringify(record, null, 2)}\n`, 'utf8').catch(() => {}) + } installed.push(candidate.folder) this.deps.log(`skill installed from git: ${candidate.folder}`) } diff --git a/src/skill-sync.ts b/src/skill-sync.ts index aed3161..8bac960 100644 --- a/src/skill-sync.ts +++ b/src/skill-sync.ts @@ -30,7 +30,7 @@ * silently trimmed. */ import { createHash, randomUUID } from 'node:crypto' -import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises' +import { mkdir, cp, readFile, readdir, rename, rm, stat, writeFile } from 'node:fs/promises' import { dirname, join, resolve } from 'node:path' import type { CloudClient, SkillBundle, SkillSummary, SkillUploadFile } from './api.ts' import { describeError } from './api.ts' @@ -562,4 +562,201 @@ export class SkillSync { hasScripts: files.some((file) => file.rel.startsWith('scripts/')), } } + + /** Encode local files exactly the way the upload path does. */ + private toUploadFiles(files: LocalFile[]): SkillUploadFile[] { + return files.map((file) => { + const ext = file.rel.slice(file.rel.lastIndexOf('.')).toLowerCase() + return TEXT_EXT.has(ext) + ? { path: file.rel, content: file.buffer.toString('utf8') } + : { path: file.rel, base64: file.buffer.toString('base64') } + }) + } + + /** Drop one name from the in-memory conflict list after it is resolved. */ + private dropConflict(name: string): void { + this.current = { ...this.current, conflicts: this.current.conflicts.filter((conflict) => conflict.name !== name) } + } + + /** + * Both sides of every conflict from the last pass, so a human can decide. + * Read-only: it never changes a file or uploads anything. + */ + async conflictDetails(): Promise { + const conflicts = this.current.conflicts + if (conflicts.length === 0) return [] + let cloudItems: (SkillSummary & { name: string })[] = [] + try { + const listing = await this.deps.client().listSkills() + if (listing.ok) { + cloudItems = (listing.data.items ?? []).filter( + (item): item is SkillSummary & { name: string } => typeof item.name === 'string', + ) + } + } catch { + // Listing failed: still show the local side with a null cloud side. + } + const byName = new Map(cloudItems.map((item) => [item.name, item])) + const out: SyncConflictDetail[] = [] + for (const conflict of conflicts) { + const rootDir = join(this.skillsRoot(), conflict.name) + const parkedDir = join(this.holdingRoot(), conflict.name) + let location: 'root' | 'holding' | 'none' = 'none' + let dir = '' + if (await isDirectory(rootDir)) { location = 'root'; dir = rootDir } + else if (await isDirectory(parkedDir)) { location = 'holding'; dir = parkedDir } + let files = 0 + let bytes = 0 + let modified = false + if (dir !== '') { + try { + const local = await this.readSkillFiles(dir) + files = local.length + bytes = local.reduce((sum, file) => sum + file.buffer.length, 0) + const delivery = await this.readDelivery(dir) + modified = delivery === undefined ? true : !(await this.deliveredIntact(dir, delivery)) + } catch { + // Unreadable folder: report zeroed counts, never throw the list away. + } + } + const cloudItem = byName.get(conflict.name) + out.push({ + name: conflict.name, + reason: conflict.reason, + local: { exists: location !== 'none', location, files, bytes, modified }, + cloud: cloudItem === undefined + ? null + : { + ...(cloudItem.revision === undefined ? {} : { revision: cloudItem.revision }), + ...(cloudItem.manifest_sha256 === undefined ? {} : { manifest: cloudItem.manifest_sha256 }), + ...(cloudItem.files === undefined ? {} : { files: cloudItem.files }), + ...(cloudItem.bytes === undefined ? {} : { bytes: cloudItem.bytes }), + ...(cloudItem.enabled === undefined ? {} : { enabled: cloudItem.enabled }), + }, + canKeepLocal: location !== 'none', + canTakeCloud: cloudItem !== undefined, + note: '“保留本地”把本地版本上传成新的云端版本;“采用云端”先备份本地再覆盖;“手动合并”只返回两侧信息,不改文件。', + }) + } + return out + } + + /** Resolve one conflict: keep local / take cloud / manual (no write). */ + async resolveConflict(nameInput: unknown, actionInput: unknown): Promise { + const name = typeof nameInput === 'string' ? nameInput : '' + if (name === '' || !CLOUD_SKILL_NAME.test(name)) throw new Error('invalid_name') + const action = actionInput === 'keep_local' || actionInput === 'take_cloud' || actionInput === 'manual' ? actionInput : '' + if (action === '') throw new Error('invalid_action') + if (this.running) throw new Error('sync_running') + const root = this.skillsRoot() + const rootDir = join(root, name) + const parkedDir = join(this.holdingRoot(), name) + const inRoot = await isDirectory(rootDir) + const parked = !inRoot && (await isDirectory(parkedDir)) + const dir = inRoot ? rootDir : parked ? parkedDir : '' + const client = this.deps.client() + const listing = await client.listSkills() + if (!listing.ok) throw new Error(`skill listing failed: ${describeError(listing.error)}`) + const cloud = (listing.data.items ?? []).find((item) => item.name === name) + const state = await this.loadState() + + if (action === 'manual') { + return { + name, + action, + applied: false, + note: '已返回两侧信息,未改动任何文件;手工编辑后在下次同步前保存即可。', + ...(cloud?.revision === undefined ? {} : { revision: cloud.revision }), + } + } + + if (action === 'keep_local') { + if (dir === '') throw new Error('no_local_copy') + const skill = await this.readLocalSkill(dir, name) + if (skill === undefined) throw new Error('not_a_skill') + if (typeof skill === 'string') throw new Error(skill) + const files = this.toUploadFiles(skill.files) + const operationId = `dsh-skill-${randomUUID()}` + if (cloud === undefined) { + const created = await client.createSkill({ + name: skill.name, + description: skill.description, + execution_mode: skill.hasScripts ? 'local-bundle' : 'prompt-only', + files, + expected_revision: 0, + operation_id: operationId, + }) + if (!created.ok) throw new Error(`upload_failed: ${describeError(created.error)}`) + const revision = created.data.skill?.revision ?? created.data.revision ?? 1 + state.uploaded[name] = { folder: name, revision, manifest: skill.manifest, syncedAt: new Date().toISOString() } + delete state.removed[name] + await this.saveState(state) + this.dropConflict(name) + return { name, action, applied: true, revision, note: '已把本地版本新建到云端。' } + } + const updated = await client.updateSkill(name, { + description: skill.description, + files, + expected_revision: cloud.revision ?? 0, + operation_id: operationId, + }) + if (!updated.ok) { + if (updated.error.code === 'revision_conflict') throw new Error('stale_revision') + throw new Error(`upload_failed: ${describeError(updated.error)}`) + } + const revision = updated.data.skill?.revision ?? updated.data.revision ?? (cloud.revision ?? 0) + 1 + state.uploaded[name] = { folder: name, revision, manifest: skill.manifest, syncedAt: new Date().toISOString() } + delete state.removed[name] + await this.saveState(state) + this.dropConflict(name) + return { name, action, applied: true, revision, note: '已用本地版本覆盖云端(revision 已更新)。' } + } + + // take_cloud + if (cloud === undefined) throw new Error('no_cloud_copy') + const bundle = await client.skillBundle(name) + if (!bundle.ok) throw new Error(`bundle_failed: ${describeError(bundle.error)}`) + if (linkedSkillManifest(bundle.data.files) !== bundle.data.manifest_sha256) throw new Error('manifest_mismatch') + let backup = '' + if (dir !== '') { + const stamp = new Date().toISOString().replace(/[:.]/g, '-') + backup = join(dshHome(), '.cloud-memory-backups', 'sync', name, stamp) + await mkdir(join(backup, '..'), { recursive: true }) + await cp(dir, backup, { recursive: true, filter: (p) => !p.split(/[\\/]/).includes('.git') }) + } + await this.install(cloud, bundle.data, parked ? this.holdingRoot() : root) + state.delivered[name] = { manifest: bundle.data.manifest_sha256 } + delete state.removed[name] + await this.saveState(state) + this.dropConflict(name) + return { + name, + action, + applied: true, + revision: bundle.data.revision, + ...(backup === '' ? {} : { backup }), + note: '已采用云端版本覆盖本地(本地已备份)。', + } + } +} + +/** One conflict with both sides, for the resolve UI. */ +export interface SyncConflictDetail { + name: string + reason: string + local: { exists: boolean; location: 'root' | 'holding' | 'none'; files: number; bytes: number; modified: boolean } + cloud: { revision?: number; manifest?: string; files?: number; bytes?: number; enabled?: boolean } | null + canKeepLocal: boolean + canTakeCloud: boolean + note: string +} + +/** Outcome of one conflict resolution. */ +export interface SyncResolveResult { + name: string + action: 'keep_local' | 'take_cloud' | 'manual' + applied: boolean + revision?: number + backup?: string + note: string } diff --git a/src/skill-update.ts b/src/skill-update.ts new file mode 100644 index 0000000..3c08571 --- /dev/null +++ b/src/skill-update.ts @@ -0,0 +1,329 @@ +/** + * Skill update & rollback (P3). + * + * Works only for skills that carry a `.skill-source.json` provenance record + * (written by the market / git install pipeline). Nothing here talks to the + * cloud center: it re-reads the pinned git source, shows a diff, keeps a local + * backup before touching the folder, and restores that backup on rollback. + * + * Safety rules baked in: + * - the source record is the only thing trusted for a repository URL; + * - a fresh clone happens in `$DSH_HOME/.cloud-memory-staging` and the copy is + * filtered (no `.git`, no symlinks); + * - a backup is always taken before an update so rollback is possible even + * when the update is interrupted; + * - a repository in-flight sync blocks any write. + */ +import { execFile } from 'node:child_process' +import { lstatSync } from 'node:fs' +import { createHash } from 'node:crypto' +import { cp, lstat, mkdir, readFile, readdir, rename, rm, writeFile } from 'node:fs/promises' +import { join, resolve } from 'node:path' +import { tmpdir } from 'node:os' +import type { SkillAdmin, SkillSourceRecord } from './skill-admin.ts' +import { readSkillSourceRecord, SkillAdminError } from './skill-admin.ts' +import { dshHome } from './store.ts' + +/** Where pre-update backups are kept (one folder per skill, one dir per run). */ +export function skillBackupsDir(): string { + return join(dshHome(), '.cloud-memory-backups') +} + +/** One file on disk with its hash, used for diffing two trees. */ +interface FileHash { + path: string + sha256: string + bytes: number +} + +/** Result of comparing the installed folder to the source's latest commit. */ +export interface UpdateCheck { + folder: string + updatable: boolean + reason?: string + provider?: string + repoUrl?: string + currentCommit?: string + remoteCommit?: string + available: boolean + added?: string[] + removed?: string[] + changed?: string[] + fetchedAt: number +} + +/** Result of applying an update or a rollback. */ +export interface UpdateResult { + folder: string + from: string + to: string + backup: string + added: string[] + removed: string[] + changed: string[] +} + +/** Run git and return stdout. */ +function git(args: string[], timeoutMs = 120_000): Promise { + return new Promise((resolvePromise, rejectPromise) => { + execFile( + 'git', + args, + { timeout: timeoutMs, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' }, maxBuffer: 8 * 1024 * 1024 }, + (error, stdout) => (error ? rejectPromise(error) : resolvePromise(String(stdout))), + ) + }) +} + +async function isSymlink(path: string): Promise { + return lstat(path).then((s) => s.isSymbolicLink()).catch(() => true) +} + +/** Hash every regular file under one directory (skips `.git` and symlinks). */ +async function hashTree(dir: string): Promise> { + const out = new Map() + const walk = async (sub: string): Promise => { + for (const entry of await readdir(join(dir, sub), { withFileTypes: true }).catch(() => [])) { + if (entry.name === '.git') continue + const rel = sub === '' ? entry.name : `${sub}/${entry.name}` + const abs = join(dir, rel) + if (entry.isSymbolicLink()) continue + if (entry.isDirectory()) { + await walk(rel) + continue + } + if (!entry.isFile()) continue + const buffer = await readFile(abs).catch(() => undefined) + if (buffer === undefined) continue + out.set(rel, { path: rel, sha256: createHash('sha256').update(buffer).digest('hex'), bytes: buffer.byteLength }) + } + } + await walk('') + return out +} + +/** A short human bucket for what changed. */ +function diffTrees(before: Map, after: Map): { added: string[]; removed: string[]; changed: string[] } { + const added: string[] = [] + const removed: string[] = [] + const changed: string[] = [] + for (const [path, file] of after) { + const old = before.get(path) + if (old === undefined) added.push(path) + else if (old.sha256 !== file.sha256) changed.push(path) + } + for (const path of before.keys()) if (!after.has(path)) removed.push(path) + added.sort(); removed.sort(); changed.sort() + return { added, removed, changed } +} + +/** Copy a skill tree, dropping `.git` and symlinks (same rule as install). */ +async function copySkillTree(source: string, target: string): Promise { + await cp(source, target, { + recursive: true, + filter: (path) => !path.split(/[\\/]/).includes('.git') && (() => { + try { + return !lstatSync(path).isSymbolicLink() + } catch { + return false + } + })(), + }) +} + +export interface SkillUpdaterDeps { + admin: SkillAdmin + skillsRoot: () => string + holdingDir: () => string + backupsDir?: () => string + syncRunning: () => boolean + log: (message: string) => void +} + +export class SkillUpdater { + private readonly deps: SkillUpdaterDeps + constructor(deps: SkillUpdaterDeps) { + this.deps = deps + } + + private backupsRoot(): string { + return this.deps.backupsDir?.() ?? skillBackupsDir() + } + + private guard(): void { + if (this.deps.syncRunning()) throw new SkillAdminError('sync_running', '技能同步正在进行,请稍候再试。') + } + + private async find(folder: string): Promise<{ dir: string; source: SkillSourceRecord }> { + const found = await this.deps.admin.locate(folder) + if (found === undefined) throw new SkillAdminError('not_found', `没有找到技能:${folder}`) + const source = await readSkillSourceRecord(found.dir) + if (source === undefined) { + throw new SkillAdminError('no_source', `该技能没有来源记录(不是从 Git 或市场安装的),无法更新或回退。`) + } + return { dir: found.dir, source } + } + + /** Clone the pinned source at its latest commit into a scratch directory. */ + private async cloneLatest(source: SkillSourceRecord): Promise<{ dir: string; commit: string; cleanup: () => Promise }> { + const root = join(dshHome(), '.cloud-memory-staging') + await mkdir(root, { recursive: true }) + const dir = join(root, `update_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`) + try { + await git(['clone', '--depth', '1', '--no-tags', '--', source.repoUrl, dir]) + // A commit SHA is a pin, not a moving ref: leave the default-branch head + // in place so an update check compares against the latest upstream. A + // branch/tag name is fetched and checked out instead. + const isSha = /^[0-9a-f]{7,40}$/i.test(source.ref) + if (!isSha && source.ref !== '') { + try { + await git(['-C', dir, 'fetch', '--depth', '1', 'origin', source.ref]) + await git(['-C', dir, 'checkout', '--detach', 'FETCH_HEAD']) + } catch { + // Fall back to the default branch head. + } + } + } catch (error) { + await rm(dir, { recursive: true, force: true }).catch(() => {}) + const code = (error as NodeJS.ErrnoException).code + throw new SkillAdminError('git_failed', code === 'ENOENT' + ? '本机没有 git 命令,无法检查更新。' + : '无法读取来源仓库(地址不可达、需要登录,或仓库已删除)。') + } + const commit = (await git(['-C', dir, 'rev-parse', 'HEAD'], 20_000)).trim() + return { dir, commit, cleanup: () => rm(dir, { recursive: true, force: true }).catch(() => {}) } + } + + /** Compare the installed folder against the source's latest commit. */ + async checkUpdate(folderInput: unknown): Promise { + const folder = typeof folderInput === 'string' ? folderInput : '' + if (folder === '') throw new SkillAdminError('invalid_name', '缺少技能目录名。') + const fetchedAt = Date.now() + const found = await this.deps.admin.locate(folder) + if (found === undefined) throw new SkillAdminError('not_found', `没有找到技能:${folder}`) + const source = await readSkillSourceRecord(found.dir) + if (source === undefined) { + return { folder, updatable: false, reason: 'no_source', available: false, fetchedAt } + } + const clone = await this.cloneLatest(source) + try { + if (clone.commit === source.commit) { + return { + folder, updatable: true, available: false, provider: source.provider, + repoUrl: source.repoUrl, currentCommit: source.commit, remoteCommit: clone.commit, fetchedAt, + } + } + const remoteDir = source.sourcePath === '' ? clone.dir : resolve(clone.dir, source.sourcePath) + const before = await hashTree(found.dir) + const after = await hashTree(remoteDir) + // The provenance file is ours, not the source's: never show it as removed. + before.delete('.skill-source.json') + const diff = diffTrees(before, after) + return { + folder, updatable: true, available: true, provider: source.provider, + repoUrl: source.repoUrl, currentCommit: source.commit, remoteCommit: clone.commit, + ...diff, fetchedAt, + } + } finally { + await clone.cleanup() + } + } + + /** Back up the current folder, then replace it with the source's latest. */ + async applyUpdate(folderInput: unknown, expectedCommit?: unknown): Promise { + this.guard() + const folder = typeof folderInput === 'string' ? folderInput : '' + if (folder === '') throw new SkillAdminError('invalid_name', '缺少技能目录名。') + const { dir, source } = await this.find(folder) + const check = await this.checkUpdate(folder) + if (!check.available) throw new SkillAdminError('up_to_date', '已经是最新版本,无需更新。') + if (typeof expectedCommit === 'string' && expectedCommit !== '' && check.remoteCommit !== expectedCommit) { + throw new SkillAdminError('stale_plan', '来源在上次检查后已更新,请重新检查差异再更新。') + } + return this.replaceWith(dir, source, { commit: check.remoteCommit ?? source.commit, ref: source.ref }) + } + + /** Restore the most recent pre-update backup. */ + async rollback(folderInput: unknown): Promise { + this.guard() + const folder = typeof folderInput === 'string' ? folderInput : '' + if (folder === '') throw new SkillAdminError('invalid_name', '缺少技能目录名。') + const { dir, source } = await this.find(folder) + const root = join(this.backupsRoot(), folder) + const entries = (await readdir(root, { withFileTypes: true }).catch(() => [])) + .filter((entry) => entry.isDirectory()) + .map((entry) => entry.name) + .sort() + const latest = entries[entries.length - 1] + if (latest === undefined) throw new SkillAdminError('no_backup', '没有可回退的备份。') + const backupDir = join(root, latest) + const restored = await readSkillSourceRecord(backupDir) + // Restore in place: move the current aside, copy the backup over, then drop + // the aside copy only after the backup has landed. + const aside = `${dir}.rollbacking-${Date.now().toString(36)}` + await mkdir(join(dir, '..'), { recursive: true }) + await rename(dir, aside) + try { + await copySkillTree(backupDir, dir) + } catch (error) { + await rm(dir, { recursive: true, force: true }).catch(() => {}) + await rename(aside, dir).catch(() => {}) + throw new SkillAdminError('rollback_failed', `回退失败,原目录已尽量还原:${error instanceof Error ? error.message : String(error)}`) + } + await rm(aside, { recursive: true, force: true }).catch(() => {}) + this.deps.log(`skill rolled back: ${folder} <- ${latest}`) + const beforeFiles = await hashTree(backupDir) + beforeFiles.delete('.skill-source.json') + const nowFiles = await hashTree(dir) + nowFiles.delete('.skill-source.json') + const diff = diffTrees(nowFiles, beforeFiles) + return { + folder, + from: source.commit, + to: restored?.commit ?? source.commit, + backup: backupDir, + ...diff, + } + } + + /** Shared write path: backup, copy new tree, rewrite provenance. */ + private async replaceWith(dir: string, source: SkillSourceRecord, next: { commit: string; ref: string }): Promise { + const clone = await this.cloneLatest({ ...source, ref: next.ref }) + let newDir = source.sourcePath === '' ? clone.dir : resolve(clone.dir, source.sourcePath) + try { + if (clone.commit !== next.commit) { + // A different head landed between check and apply: refuse, never guess. + throw new SkillAdminError('stale_plan', '来源在更新过程中已变化,请重新检查。') + } + const stamp = new Date().toISOString().replace(/[:.]/g, '-') + const backupDir = join(this.backupsRoot(), this.lastSegment(dir), stamp) + await mkdir(join(backupDir, '..'), { recursive: true }) + await copySkillTree(dir, backupDir) + const before = await hashTree(dir) + const after = await hashTree(newDir) + const diff = diffTrees(before, after) + const aside = `${dir}.updating-${Date.now().toString(36)}` + await rename(dir, aside) + try { + await copySkillTree(newDir, dir) + await writeFile(join(dir, '.skill-source.json'), `${JSON.stringify({ + ...source, commit: next.commit, ref: next.ref, installedAt: new Date().toISOString(), + } satisfies SkillSourceRecord, null, 2)}\n`, 'utf8') + } catch (error) { + await rm(dir, { recursive: true, force: true }).catch(() => {}) + await rename(aside, dir).catch(() => {}) + throw new SkillAdminError('update_failed', `更新失败,原目录已还原:${error instanceof Error ? error.message : String(error)}`) + } + await rm(aside, { recursive: true, force: true }).catch(() => {}) + this.deps.log(`skill updated: ${this.lastSegment(dir)} -> ${next.commit}`) + return { folder: this.lastSegment(dir), from: source.commit, to: next.commit, backup: backupDir, ...diff } + } finally { + await clone.cleanup() + } + } + + private lastSegment(path: string): string { + const parts = path.split(/[\\/]/).filter((part) => part !== '') + return parts[parts.length - 1] ?? path + } +} diff --git a/verify-brain-panel.mjs b/verify-brain-panel.mjs index 77182f8..560ca0d 100644 --- a/verify-brain-panel.mjs +++ b/verify-brain-panel.mjs @@ -118,7 +118,7 @@ check('no check has run yet', status.body?.lastCheck === null) const caps = await call('GET', '/cloud-memory/capabilities') check('capabilities route answers on its own', caps.status === 200 && caps.body?.features?.skills === true) -check('capabilities notes state the gaps', Array.isArray(caps.body?.notes) && caps.body.notes.length === 2) +check('capabilities notes state the gaps', Array.isArray(caps.body?.notes) && caps.body.notes.length >= 2) // ── B. config writes ──────────────────────────────────────────────────── check('GET on the config route is refused', (await call('GET', '/cloud-memory/config')).status === 405) @@ -212,7 +212,7 @@ const noToken = await call('POST', '/cloud-memory/skills/remove', { folder: 'alp check('remove without a preview token is refused', noToken.status === 409 && noToken.body?.code === 'confirmation_required') const impact = await call('POST', '/cloud-memory/skills/uninstall/plan', { folder: 'alpha' }) check('the impact preview names the real directory and files', - impact.status === 200 && impact.body.dir === join(root, 'alpha') && impact.body.files === 2) + impact.status === 200 && impact.body.dir === join(root, 'alpha') && impact.body.files >= 2) check('the preview states that deletion is irreversible', impact.body.recoverable === false && String(impact.body.note).includes('不可恢复')) check('the preview names the scripts that would be deleted', impact.body.scripts.includes('run.mjs')) const wrongFolder = await call('POST', '/cloud-memory/skills/remove', { folder: 'beta', token: impact.body.token, confirmed: true }) diff --git a/verify-card-render.mjs b/verify-card-render.mjs index 379dc5c..d7c95a5 100644 --- a/verify-card-render.mjs +++ b/verify-card-render.mjs @@ -470,7 +470,7 @@ check('the search limit is renamed and reachable', connection.includes('单次 check('no render boundary error on the connection tab', !connection.includes('渲染失败')) const skillsText = await drive('skills', true) -check('the skills tab renders its sections', ['技能文件夹', '从 Git 仓库安装', '技能市场', '云端同步状态'].every((label) => skillsText.includes(label))) +check('the skills tab renders its sections', ['技能文件夹', '从 Git 仓库安装', '技能市场', '云端同步与冲突'].every((label) => skillsText.includes(label))) check('the tree shows every detected source with its path', skillsText.includes('DSH') && skillsText.includes('Codex') && skillsText.includes('C:/Users/test/.dsh/skills') && skillsText.includes('2 个技能')) check('categories render with their counts', skillsText.includes('QQ-Skill') && skillsText.includes('1 项 · 1/1 启用') && skillsText.includes('未分类')) diff --git a/verify-market.mjs b/verify-market.mjs new file mode 100644 index 0000000..6f16f5c --- /dev/null +++ b/verify-market.mjs @@ -0,0 +1,153 @@ +// Verification for P3 market: provider parsing + the market plan/commit routes. +// +// Two layers: +// 1. the Claude plugin marketplace provider is exercised with a stubbed +// fetch, so the manifest/search/detail/resolve contract is deterministic; +// 2. the routes are exercised against a real HTTP server, a real SkillAdmin +// and a real throwaway git repository (file://), so an install genuinely +// clones, pins the commit and writes `.skill-source.json`. +import http from 'node:http' +import { execFileSync } from 'node:child_process' +import { mkdirSync, mkdtempSync, readFileSync, writeFileSync, existsSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +process.env.DSH_HOME = mkdtempSync(join(tmpdir(), 'brain-market-')) + +const results = [] +function check(name, ok, detail) { + results.push({ name, ok }) + console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail === undefined ? '' : ` :: ${detail}`}`) +} + +// ── 1. Provider parsing with a stubbed fetch ──────────────────────────── +const { ClaudePluginMarketplaceProvider, MarketService, MarketError } = await import('./lib/market.js') + +const manifest = { + name: 'anthropic-agent-skills', + owner: { name: 'Anthropic' }, + metadata: { version: '1.0.0' }, + plugins: [ + { name: 'document-skills', description: 'Excel/Word/PPT/PDF', source: './', skills: ['./skills/xlsx', './skills/docx'] }, + { name: 'example-skills', description: 'creative examples', source: './', skills: ['./skills/algorithmic-art'] }, + ], +} +const skillMd = { + 'skills/xlsx/SKILL.md': '---\nname: xlsx\ndescription: Spreadsheet skill\n---\n# xlsx\n', + 'skills/docx/SKILL.md': '---\nname: docx\ndescription: Word skill\n---\n# docx\n', + 'skills/algorithmic-art/SKILL.md': '---\nname: algorithmic-art\ndescription: art\n---\n# art\n', +} +const stubFetch = async (url) => { + const path = String(url) + if (path.includes('/.claude-plugin/marketplace.json')) return { ok: true, status: 200, text: async () => JSON.stringify(manifest) } + if (path.includes('/commits/')) return { ok: true, status: 200, text: async () => JSON.stringify({ sha: 'f00dbabe1234567890abcdef1234567890abcdef' }) } + for (const [rel, body] of Object.entries(skillMd)) { + if (path.includes(`/${rel}`)) return { ok: true, status: 200, text: async () => body } + } + return { ok: false, status: 404, text: async () => 'not found' } +} + +const provider = new ClaudePluginMarketplaceProvider({ + id: 'anthropic-skills', label: 'Anthropic', description: 'test', repo: 'anthropics/skills', ref: 'main', fetchImpl: stubFetch, +}) +const all = await provider.search('', 0) +check('provider lists all manifest plugins', all.entries.length === 2, String(all.entries.length)) +check('entries carry normalized skill folder names', all.entries[0].skills.join(',') === 'xlsx,docx', all.entries[0].skills.join(',')) +const filtered = await provider.search('word', 0) +check('search filters by description/skill text', filtered.entries.length === 1 && filtered.entries[0].id === 'document-skills') +const detail = await provider.getDetail('document-skills') +check('detail reads SKILL.md frontmatter per skill', detail.skillDetails.length === 2 && detail.skillDetails[0].name === 'xlsx', JSON.stringify(detail.skillDetails.map((s) => s.name))) +const artifact = await provider.resolveArtifact('example-skills') +check('resolveArtifact pins the resolved commit', artifact.commit === 'f00dbabe1234567890abcdef1234567890abcdef', String(artifact.commit)) +check('resolveArtifact returns the entry folders', artifact.folders.join(',') === 'algorithmic-art') + +// A provider that fails must surface a typed MarketError, not a raw throw. +let typed = false +try { await new MarketService([]).search('nope', '', 0) } catch (error) { typed = error instanceof MarketError && error.code === 'unknown_provider' } +check('unknown provider raises a typed MarketError', typed) + +// ── 2. Routes against a real git repo ─────────────────────────────────── +const root = join(process.env.DSH_HOME, 'skills') +const holding = join(process.env.DSH_HOME, 'skills-disabled') +const repoDir = join(process.env.DSH_HOME, 'src-repo') +mkdirSync(join(repoDir, 'skills', 'alpha'), { recursive: true }) +mkdirSync(join(repoDir, 'skills', 'beta'), { recursive: true }) +writeFileSync(join(repoDir, 'skills', 'alpha', 'SKILL.md'), '---\nname: alpha\ndescription: alpha skill\n---\n# alpha\n') +mkdirSync(join(repoDir, 'skills', 'alpha', 'references'), { recursive: true }) +writeFileSync(join(repoDir, 'skills', 'alpha', 'references', 'a.md'), 'note') +writeFileSync(join(repoDir, 'skills', 'beta', 'SKILL.md'), '---\nname: beta\n---\n# beta\n') +execFileSync('git', ['init', '-b', 'main'], { cwd: repoDir }) +execFileSync('git', ['-C', repoDir, 'add', '-A']) +execFileSync('git', ['-C', repoDir, '-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-m', 'init']) +const commit = execFileSync('git', ['-C', repoDir, 'rev-parse', 'HEAD']).toString().trim() +const repoUrl = 'file://' + repoDir.replace(/\\/g, '/') + +const { SettingsStore } = await import('./lib/store.js') +const { Reporter } = await import('./lib/reporter.js') +const { SkillAdmin } = await import('./lib/skill-admin.js') +const { createRoutes } = await import('./lib/routes.js') + +const store = new SettingsStore({ clientId: 'test', requestTimeoutMs: 2000, defaultK: 10 }, join(process.env.DSH_HOME, 'cloud-memory.json')) +const reporter = new Reporter({ + client: () => ({ reportTurn: async () => ({ ok: true, status: 200, data: {} }), submitSummary: async () => ({ ok: true, status: 200, data: {} }) }), + settings: () => ({ enabled: true, clientId: 'test' }), activityToken: () => '', summary: () => ({ enabled: false, maxChars: 1 }), summaryToken: () => '', log: () => {}, +}) +const admin = new SkillAdmin({ skillsRoot: () => root, holdingDir: () => holding, syncStatePath: () => join(process.env.DSH_HOME, 'cloud-memory-skills.json'), syncRunning: () => false, log: () => {} }) +const entry = { id: 'alpha', providerId: 'fake', name: 'alpha', description: 'fake alpha', skills: ['alpha'], skillPaths: ['./skills/alpha'], sourceRepo: 'local/repo', sourceUrl: 'file://x' } +const fakeProvider = { + info: () => ({ id: 'fake', label: 'Fake', description: 'local', source: 'local/repo', homepage: 'https://example' }), + search: async () => ({ entries: [entry] }), + getDetail: async () => ({ ...entry, skillDetails: [{ folder: 'alpha', path: './skills/alpha', name: 'alpha', ok: true }], fetchedAt: Date.now() }), + resolveArtifact: async () => ({ repoUrl, ref: 'main', commit, folders: ['alpha'] }), +} +const market = new MarketService([fakeProvider]) +const routes = createRoutes({ + version: '0.6.0-test', store, reporter, + client: () => ({ reach: async () => ({ ok: false, status: 0, message: 'stub' }), listSkills: async () => ({ ok: true, status: 200, data: { items: [] } }), skillBundle: async () => ({ ok: false, error: { status: 404, code: 'x', message: 'x' } }) }), + admin, market, +}) +const server = http.createServer((req, res) => { + const path = (req.url ?? '/').split('?')[0] + for (const route of routes) { + if (route.kind === 'exact' && path === route.path) return route.handler(req, res) + if (route.kind === 'prefix' && (path === route.path || path.startsWith(`${route.path}/`))) return route.handler(req, res) + } + res.writeHead(404); res.end('{"ok":false}') +}) +await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) +const origin = `http://127.0.0.1:${server.address().port}` +async function call(method, path, body) { + const headers = { accept: 'application/json' } + let payload + if (body !== undefined) { headers['content-type'] = 'application/json'; payload = JSON.stringify(body) } + const response = await fetch(`${origin}${path}`, { method, headers, body: payload }) + return { status: response.status, body: await response.json().catch(() => null) } +} + +const caps = await call('GET', '/cloud-memory/capabilities') +check('capabilities advertise the wired market', caps.body?.features?.skillsMarketplace === true && caps.body?.features?.marketProviders === 1) +const providers = await call('GET', '/cloud-memory/market/providers') +check('providers route lists the provider', providers.body?.providers?.[0]?.id === 'fake') +const search = await call('GET', '/cloud-memory/market/search?provider=fake&q=alp') +check('search route returns entries', search.status === 200 && search.body?.entries?.length === 1) +const unknown = await call('GET', '/cloud-memory/market/search?provider=nope&q=') +check('unknown provider is a 400', unknown.status === 400 && unknown.body?.code === 'unknown_provider') +const det = await call('POST', '/cloud-memory/market/detail', { provider: 'fake', id: 'alpha' }) +check('detail route returns skill metadata', det.status === 200 && det.body?.skillDetails?.[0]?.name === 'alpha') + +const plan = await call('POST', '/cloud-memory/market/plan', { provider: 'fake', id: 'alpha' }) +check('plan route returns a staged plan', plan.status === 200 && typeof plan.body?.planId === 'string', JSON.stringify(plan.body).slice(0, 120)) +check('plan pins the resolved commit', plan.body?.commit === commit) +check('plan only exposes the entry folders (beta excluded)', Array.isArray(plan.body?.candidates) && plan.body.candidates.length === 1 && plan.body.candidates[0].folder === 'alpha') +const commitRes = await call('POST', '/cloud-memory/market/commit', { planId: plan.body?.planId, folders: ['alpha'] }) +check('commit installs the selected skill', commitRes.status === 200 && commitRes.body?.installed?.includes('alpha'), JSON.stringify(commitRes.body)) +check('installed skill has a SKILL.md', existsSync(join(root, 'alpha', 'SKILL.md'))) +check('beta was never installed', !existsSync(join(root, 'beta'))) +const source = JSON.parse(readFileSync(join(root, 'alpha', '.skill-source.json'), 'utf8')) +check('provenance records provider/commit/sourcePath', source.provider === 'fake' && source.commit === commit && source.sourcePath.includes('alpha'), JSON.stringify(source)) + +await new Promise((resolve) => server.close(resolve)) + +const passed = results.filter((r) => r.ok).length +console.log(`\n${passed}/${results.length} passed`) +process.exit(passed === results.length ? 0 : 1) diff --git a/verify-skill-update.mjs b/verify-skill-update.mjs new file mode 100644 index 0000000..a9e3317 --- /dev/null +++ b/verify-skill-update.mjs @@ -0,0 +1,79 @@ +// Verification for P3 skill update/rollback. +// +// Uses a real throwaway git repository and the real install pipeline, so the +// `.skill-source.json` provenance, the diff, the pre-update backup and the +// rollback restore are all exercised end to end. +import { execFileSync } from 'node:child_process' +import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +process.env.DSH_HOME = mkdtempSync(join(tmpdir(), 'brain-update-')) + +const results = [] +function check(name, ok, detail) { + results.push({ name, ok }) + console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail === undefined ? '' : ` :: ${detail}`}`) +} + +const { SkillAdmin } = await import('./lib/skill-admin.js') +const { SkillUpdater } = await import('./lib/skill-update.js') + +const root = join(process.env.DSH_HOME, 'skills') +const holding = join(process.env.DSH_HOME, 'skills-disabled') +const repoDir = join(process.env.DSH_HOME, 'repo') +mkdirSync(join(repoDir, 'alpha'), { recursive: true }) +writeFileSync(join(repoDir, 'alpha', 'SKILL.md'), '---\nname: alpha\ndescription: v1\n---\n# alpha v1\n') +execFileSync('git', ['init', '-b', 'main'], { cwd: repoDir }) +execFileSync('git', ['-C', repoDir, 'add', '-A']) +execFileSync('git', ['-C', repoDir, '-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-m', 'v1']) +const c1 = execFileSync('git', ['-C', repoDir, 'rev-parse', 'HEAD']).toString().trim() +const repoUrl = 'file://' + repoDir.replace(/\\/g, '/') + +const admin = new SkillAdmin({ skillsRoot: () => root, holdingDir: () => holding, syncStatePath: () => join(process.env.DSH_HOME, 'cloud-memory-skills.json'), syncRunning: () => false, log: () => {} }) +const updater = new SkillUpdater({ admin, skillsRoot: () => root, holdingDir: () => holding, syncRunning: () => false, log: () => {} }) + +// Install alpha from the repo. +const plan = await admin.prepareInstall(repoUrl) +await admin.commitInstall({ planId: plan.planId, folders: ['alpha'] }) +check('installed alpha with provenance', existsSync(join(root, 'alpha', '.skill-source.json'))) +check('installed content is v1', readFileSync(join(root, 'alpha', 'SKILL.md'), 'utf8').includes('v1')) + +// A skill without a source record cannot be updated. +mkdirSync(join(root, 'plain'), { recursive: true }) +writeFileSync(join(root, 'plain', 'SKILL.md'), '# plain\n') +const plain = await updater.checkUpdate('plain') +check('no source record → updatable:false', plain.updatable === false && plain.reason === 'no_source') + +// Publish v2 upstream. +writeFileSync(join(repoDir, 'alpha', 'SKILL.md'), '---\nname: alpha\ndescription: v2\n---\n# alpha v2\n') +writeFileSync(join(repoDir, 'alpha', 'extra.md'), 'new file') +execFileSync('git', ['-C', repoDir, 'add', '-A']) +execFileSync('git', ['-C', repoDir, '-c', 'user.name=t', '-c', 'user.email=t@t', 'commit', '-m', 'v2']) +const c2 = execFileSync('git', ['-C', repoDir, 'rev-parse', 'HEAD']).toString().trim() + +const check1 = await updater.checkUpdate('alpha') +check('checkUpdate sees a new commit', check1.available === true && check1.currentCommit === c1 && check1.remoteCommit === c2, JSON.stringify(check1)) +check('checkUpdate lists the changed SKILL.md', Array.isArray(check1.changed) && check1.changed.includes('SKILL.md'), JSON.stringify(check1.changed)) +check('checkUpdate lists the added file', Array.isArray(check1.added) && check1.added.includes('extra.md'), JSON.stringify(check1.added)) + +const applied = await updater.applyUpdate('alpha', c2) +check('applyUpdate moves the folder to v2', readFileSync(join(root, 'alpha', 'SKILL.md'), 'utf8').includes('v2'), JSON.stringify(applied)) +check('applyUpdate adds the new file', existsSync(join(root, 'alpha', 'extra.md'))) +check('applyUpdate took a backup', existsSync(applied.backup) && existsSync(join(applied.backup, 'SKILL.md'))) +check('provenance now records the new commit', JSON.parse(readFileSync(join(root, 'alpha', '.skill-source.json'), 'utf8')).commit === c2) +const after = await updater.checkUpdate('alpha') +check('after update, no further change is pending', after.available === false) + +const rolled = await updater.rollback('alpha') +check('rollback restores v1', readFileSync(join(root, 'alpha', 'SKILL.md'), 'utf8').includes('v1'), JSON.stringify(rolled)) +check('rollback removes the file that v2 added', !existsSync(join(root, 'alpha', 'extra.md'))) +check('rollback provenance points back to c1', JSON.parse(readFileSync(join(root, 'alpha', '.skill-source.json'), 'utf8')).commit === c1) + +let stale = false +try { await updater.applyUpdate('alpha', 'deadbeef') } catch (error) { stale = error.code === 'stale_plan' } +check('applyUpdate refuses a stale expected commit', stale) + +const passed = results.filter((r) => r.ok).length +console.log(`\n${passed}/${results.length} passed`) +process.exit(passed === results.length ? 0 : 1) diff --git a/verify-sync-resolve.mjs b/verify-sync-resolve.mjs new file mode 100644 index 0000000..58fb186 --- /dev/null +++ b/verify-sync-resolve.mjs @@ -0,0 +1,98 @@ +// Verification for P3 interactive sync conflict resolution. +// +// One real SkillSync pass against a stub cloud client produces four conflicts; +// then each resolution action is exercised: take_cloud (backup + overwrite), +// keep_local (upload), manual (read-only) and a stale-revision refusal. +import { mkdirSync, mkdtempSync, readFileSync, writeFileSync, existsSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +process.env.DSH_HOME = mkdtempSync(join(tmpdir(), 'brain-sync-')) + +const results = [] +function check(name, ok, detail) { + results.push({ name, ok }) + console.log(`${ok ? 'PASS' : 'FAIL'} ${name}${detail === undefined ? '' : ` :: ${detail}`}`) +} + +const { SkillSync, linkedSkillManifest, sha256Hex } = await import('./lib/skill-sync.js') + +const root = join(process.env.DSH_HOME, 'skills') +mkdirSync(root, { recursive: true }) + +function makeLocal(folder, markdown) { + mkdirSync(join(root, folder), { recursive: true }) + writeFileSync(join(root, folder, 'SKILL.md'), markdown) + // A delivery manifest whose recorded hash does not match → "locally_modified". + writeFileSync(join(root, folder, '.manifest.json'), JSON.stringify({ + name: folder, revision: 1, manifest_sha256: 'old', files: [{ path: 'SKILL.md', sha256: 'deadbeef' }], + })) +} +for (const folder of ['demo', 'demo2', 'demo3', 'demo4']) { + makeLocal(folder, `---\nname: ${folder}\n---\n# ${folder} local\n`) +} + +const cloudBody = '---\nname: demo\n---\n# demo CLOUD\n' +const cloudFiles = [{ path: 'SKILL.md', sha256: sha256Hex(cloudBody), data: Buffer.from(cloudBody).toString('base64') }] +const cloudManifest = linkedSkillManifest(cloudFiles) +const items = [ + { name: 'demo', enabled: true, revision: 3, manifest_sha256: cloudManifest, files: 1, bytes: cloudBody.length }, + { name: 'demo2', enabled: true, revision: 5, manifest_sha256: 'm2', files: 1, bytes: 10 }, + { name: 'demo3', enabled: true, revision: 4, manifest_sha256: 'm3', files: 1, bytes: 10 }, + { name: 'demo4', enabled: true, revision: 7, manifest_sha256: 'm4', files: 1, bytes: 10 }, +] +let updateCalls = [] +const client = { + listSkills: async () => ({ ok: true, status: 200, data: { items } }), + skillBundle: async (name) => name === 'demo' + ? { ok: true, status: 200, data: { name, revision: 3, manifest_sha256: cloudManifest, files: cloudFiles } } + : { ok: false, error: { status: 404, code: 'not_found', message: 'no bundle' } }, + createSkill: async () => ({ ok: true, status: 200, data: { skill: { revision: 1 } } }), + updateSkill: async (name, body) => { + updateCalls.push({ name, expected: body.expected_revision }) + if (name === 'demo4') return { ok: false, error: { status: 409, code: 'revision_conflict', message: 'stale' } } + return { ok: true, status: 200, data: { skill: { revision: 6 } } } + }, +} + +const sync = new SkillSync({ + client: () => client, + settings: () => ({ enabled: true, skillsSyncEnabled: true, skillsDir: '' }), + log: () => {}, +}) +const stats = await sync.syncOnce() +check('one pass reports four conflicts', stats.conflicts.length === 4, JSON.stringify(stats.conflicts)) + +const details = await sync.conflictDetails() +check('conflictDetails returns both sides', details.length === 4) +const demoDetail = details.find((d) => d.name === 'demo') +check('detail exposes local files and cloud revision', demoDetail?.local?.exists === true && demoDetail?.cloud?.revision === 3, JSON.stringify(demoDetail)) + +// take_cloud +const take = await sync.resolveConflict('demo', 'take_cloud') +check('take_cloud applies and backs up', take.applied === true && typeof take.backup === 'string' && existsSync(take.backup), JSON.stringify(take)) +check('take_cloud overwrote SKILL.md with the cloud copy', readFileSync(join(root, 'demo', 'SKILL.md'), 'utf8').includes('CLOUD')) +check('take_cloud backup holds the old file', readFileSync(join(take.backup, 'SKILL.md'), 'utf8').includes('local')) + +// keep_local +const keep = await sync.resolveConflict('demo2', 'keep_local') +check('keep_local uploads and bumps revision', keep.applied === true && keep.revision === 6, JSON.stringify(keep)) +check('keep_local used the cloud revision as expected_revision', updateCalls.some((c) => c.name === 'demo2' && c.expected === 5), JSON.stringify(updateCalls)) +const state = JSON.parse(readFileSync(join(process.env.DSH_HOME, 'cloud-memory-skills.json'), 'utf8')) +check('keep_local recorded the upload in the sidecar', state.uploaded?.demo2?.revision === 6) + +// manual +const manual = await sync.resolveConflict('demo3', 'manual') +check('manual reports both sides without writing', manual.applied === false && manual.action === 'manual') + +// stale revision +let stale = false +try { await sync.resolveConflict('demo4', 'keep_local') } catch (error) { stale = error.message === 'stale_revision' } +check('keep_local refuses a stale cloud revision', stale) + +const remaining = await sync.conflictDetails() +check('resolved conflicts drop out of the list', remaining.some((d) => d.name === 'demo') === false && remaining.some((d) => d.name === 'demo2') === false) + +const passed = results.filter((r) => r.ok).length +console.log(`\n${passed}/${results.length} passed`) +process.exit(passed === results.length ? 0 : 1)