diff --git a/.agents/skills/verify-deepseekbot/features/skill-invocation.md b/.agents/skills/verify-deepseekbot/features/skill-invocation.md index f4840f34d..65883161a 100644 --- a/.agents/skills/verify-deepseekbot/features/skill-invocation.md +++ b/.agents/skills/verify-deepseekbot/features/skill-invocation.md @@ -5,6 +5,7 @@ A Human types `/` in a PersonaBot DM or Group Channel, picks a Skill, and sends ## Sub-features - `menu` lists composer commands first, then Skills: Host Skills and the Project Skills under each conversation member Bot's Memory Repository `.agents/skills` or `.dsh/skills`. Each row shows its owner (Bot avatar or Skill icon), `/name` and a one-line description. +- `live-refresh` invalidates only the changed Bot's Project Skill catalog, or every Bot's Host portion when an unfiltered DSH `skills/change` reveals a changed Host catalog, and pushes `skills/catalog-changed` through the existing roster stream. `skills-list` reads the same fresh catalog as the menu. - `rank` orders Skills by name prefix, then name subsequence, then owning Bot display name or slug substring, then description substring; order within each tier stays stable. Typing `alice` lists all of Alice's Skills. - `chip` inserts the picked Skill as one token; Backspace removes it whole; the draft keeps it across reloads. - `snapshot` stores `{name, owner, path, content}` with the message; editing or deleting the `SKILL.md` later does not change the sent message. @@ -40,9 +41,17 @@ Preconditions: `launch` reported a model credential and `doctor` passes. 6. For a same-named command/Skill, inspect commands-first ordering, exercise the bare command, then explicitly select the Skill row and inspect the snapshot. 7. Through the designated Discord/Lark test destination and the IM browser QA procedure, inspect a mirrored Human message: it shows plain `/qa-echo`, with no brackets, XML or snapshot content. Correlate it with Source Event/Outbox facts. The automated provider-carrier tests alone do not verify browser rendering; do not infer that a Group bridge automatically forwards Human messages. +### Live refresh path + +1. Keep Alice's DM composer and `/` menu open. Add, edit the description of, and delete `/.agents/skills/qa-live/SKILL.md`; repeat under `.dsh/skills`. Each change updates the menu without navigation or a page reload. Bob's unrelated DM does not refetch its Project Skill catalog. +2. Inspect the existing roster stream: `skills/catalog-changed` has `{botSlugs: ["alice"], channelIds: [, ]}`. Read `channelSkillCatalog` and run `deepseekbot skills-list --bot alice` after each event: both show the new description or absence. A Group containing Alice updates too. A subsequent unfiltered DSH Project notification must not broadcast to unrelated Channels when the Host catalog is unchanged. +3. Add/remove or edit a Host Skill: `skills/change` invalidates every Bot's Host catalog portion; Project Skill reads remain cached. An interrupted/reconnected Client roster stream refetches the selected channel on its replacement baseline. +4. Run `deepseekbot memory-move` for Alice, then edit her Skill at the new path. The event and menu still update. Remove Alice through the confirmed Bot deletion flow: her watchers stop. Disposing the isolated Host releases all Skill watchers and pending notifications. +5. Automated coverage: `packages/core/test/skill-catalog-refresh.test.ts` observes per-cwd registry calls, real filesystem add/modify/delete, Memory moves/removal/disposal, CLI/RPC equality, access-filtered SSE/typed push, and real DSH Registry invalidation through its registration control while preserving Agent Scope visibility. `packages/client/test/skill-catalog-live.test.ts` checks event parsing, subscription scope, reconnect baselines and shared-carrier cleanup. + ## Gotchas -- The `/` menu reads the catalog when the conversation opens. A Skill written while the conversation is open appears after reopening it until S3 (live refresh) lands. +- The non-UI S3 slice exposes `actions.subscribeSkillCatalog(channelId, invalidate)` alongside `actions.skillCatalog(channelId)`. The UI owner must wire the mounted composer to refetch on invalidation, prefetch on open, ignore stale async completions, and unsubscribe on channel change/unmount. Until that wiring lands, live menu behavior in steps 1–4 remains unverified. - DSH discovers `/.agents/skills` because the Orchestrator's cwd is the Memory Repository; a Skill placed in `.botharness/` is not found. - Recalling an older message with ArrowUp, or restoring a failed send, keeps `/name` as plain text without the chip. diff --git a/apps/docs/test/docs-reference.test.ts b/apps/docs/test/docs-reference.test.ts index 00d421a42..8452916d3 100644 --- a/apps/docs/test/docs-reference.test.ts +++ b/apps/docs/test/docs-reference.test.ts @@ -110,6 +110,7 @@ describe("generated developer reference", () => { { name: "approval/request", direction: "consumes", operation: "on" }, { name: "botharness/personabot/activity", direction: "emits", operation: "emit" }, { name: "session/event", direction: "consumes", operation: "on" }, + { name: "skills/change", direction: "consumes", operation: "on" }, { name: "subagent/start", direction: "consumes", operation: "on" }, { name: "system-prompt/assemble", direction: "consumes", operation: "on" }, { name: "system-prompt/assemble", direction: "consumes", operation: "on" }, diff --git a/apps/mobile/src/host/wire.ts b/apps/mobile/src/host/wire.ts index 4e38403c7..9a8d35617 100644 --- a/apps/mobile/src/host/wire.ts +++ b/apps/mobile/src/host/wire.ts @@ -402,6 +402,7 @@ export interface WireGroupJoinRequest { } export type RosterLiveFrame = + | { type: 'skills'; change: { botSlugs: string[]; channelIds: string[] } } | { type: 'attention' } | { type: 'baseline'; placements: unknown[] } | { type: 'changed' } diff --git a/changes/4234-live-skill-catalog.md b/changes/4234-live-skill-catalog.md new file mode 100644 index 000000000..6fb3fd915 --- /dev/null +++ b/changes/4234-live-skill-catalog.md @@ -0,0 +1,11 @@ +--- +section: Fixed +--- + +## en + +- Skill catalogs now invalidate on Host Skill changes and changes to `SKILL.md` in a Bot's Memory Repository, with scoped live notifications and fresh `skills-list` results; Client subscriptions can refresh the open conversation without polling ([#4234](https://github.com/BotHarness/BotHarness/issues/4234)). + +## zh + +- Host Skill 或 Bot Memory Repository 中的 `SKILL.md` 变化后,Skill 目录会按范围失效并推送通知,`skills-list` 也会读取最新结果;Client 可通过订阅刷新当前会话,无需轮询([#4234](https://github.com/BotHarness/BotHarness/issues/4234))。 diff --git a/docs/architecture/botharness-architecture.md b/docs/architecture/botharness-architecture.md index f68522607..95350a428 100644 --- a/docs/architecture/botharness-architecture.md +++ b/docs/architecture/botharness-architecture.md @@ -981,6 +981,14 @@ Registry 在 Generation 66 持久化不可恢复的身份关闭状态和确认 Zip 使用既有已认证 RPC 上传 256 KiB 原始数据块,避免整包超过加密网关的单请求边界。手机保留应用内文件、SHA-256 与大小;Host 暂存清单和不可变分块支持重启后核对,完整校验后才通过既有导入 owner 发布。暂存最多八个未过期会话、合计 400 MiB 预留,每个会话自首次接收起 24 小时到期;新上传接收时回收过期内容。规范导入回执独立于暂存期限,已完成操作无需原文件即可查回;未完成操作若需重选文件,必须匹配原哈希和大小。该暂存仅负责传输,不成为 Memory 或 Bot 的第二份权威。 +### Skill catalog live refresh(#4234) + +Skill 文件仍由 [ADR-0345](../adr/0345-project-skills-travel-with-their-directory.md) 与 [ADR-0346](../adr/0346-skill-invocation-is-snapshotted-text-that-addresses-no-one.md) 定义。Host 的 `SkillCatalog` 按 Bot 分开缓存 Project 与 Host 目录:Memory Repository 下 `.agents/skills`、`.dsh/skills` 的原生文件事件只清该 Bot 的 Project 部分;DSH-native `skills/change` 是不区分 Host 与 Project 的通知:先核对已缓存 Host 目录的摘要及文件版本,只有 Host 部分确有变化才清所有 Bot 的 Host 部分并广播,避免把 Project 文件通知扩散到无关会话。监听器只递归 Skill 目录,缺失目录的创建由浅层父目录监听接续;Registry 提交后协调新增、删除与 Memory 移动,Host Plugin/Fiber dispose 关闭所有监听器及合并通知定时器。没有轮询或第二份持久化目录。 + +Memory 文件事件通过兼容层持有的 DSH-native Provider registration control 使原 Registry 的 cwd/scope 缓存失效;该生命周期适配 Provider 不提供 Skill 候选项,目录仍只来自 DSH 的原 Provider,查询保留原 Agent Scope。这样无需跨运行时模块实例创建 Scope 继承关系,也不调用私有缓存方法。应用定义的 `skills/catalog-changed` 通知携带 `{botSlugs, channelIds}`,复用既有 roster SSE 与 typed live stream,并按 Human 的 Channel 访问权过滤。Client action `subscribeSkillCatalog(channelId, invalidate)` 只通知相关会话,在重连 baseline 时补读;`skillCatalog(channelId)` 与 CLI `skills-list` 共用 `channelSkillCatalog`。React composer 的订阅接线由 UI 切片完成。 + +性能边界:无变更时 RPC 复用每 Bot 的目录;文件事件按 Bot 合并 25 ms 后推送,重算仍为按需。无范围的 DSH 通知只核对已缓存视图的 Host 摘要与文件版本,无路径的 runtime Skill 使用内容散列;这项工作发生在首次 Host 读取和事件核对时。回归测试记录 `registry.list` 的 cwd,确保 Memory 失效后的 RPC 只重算该 Bot 的 Project 部分,DSH 通知核对与 Host 变更都不重算 Project,重复读取复用缓存;这不是浏览器或原生帧率验收。 + ## 7 · 关键边界 - 正常运行只认 explicit Session ownership;`cwd` 只可作为迁移/修复提示,不能决定 PersonaBot 身份。 diff --git a/packages/client/src/client/actions.ts b/packages/client/src/client/actions.ts index 4c7f65a67..064b6fdd8 100644 --- a/packages/client/src/client/actions.ts +++ b/packages/client/src/client/actions.ts @@ -1,3 +1,4 @@ +import { subscribeSkillCatalog } from './messaging-defaults-live.js'; import { replyPreviewOf } from '../../../core/src/channels/reply-preview.js'; import { gitImportSource } from '../../../../apps/mobile/src/host/bot-creation.js'; import { approveWorkspaceGrantRequest } from './bridge.js'; @@ -1036,6 +1037,7 @@ export interface BridgeActions { skillInvocations?: ChannelSkillInvocationToken[], ): Promise; skillCatalog?(channelId: string): Promise; + subscribeSkillCatalog?(channelId: string, listener: () => void): () => void; submissions: { subscribe(listener: () => void): () => void; snapshot(channelId: string): SubmissionSnapshot | undefined; @@ -3317,6 +3319,7 @@ export function createActions( await loadSessionsFor(slug, selection); }, allBotPreview: (channelId) => loadAllBotPreview(call, channelId), + subscribeSkillCatalog, skillCatalog: (channelId) => loadChannelSkillCatalog(call, channelId), validateComposerDraft: (channelId, attachments, replyTo) => validateComposerDraft(call, channelId, attachments, replyTo), diff --git a/packages/client/src/client/bot-main.tsx b/packages/client/src/client/bot-main.tsx index be32cff61..55170522a 100644 --- a/packages/client/src/client/bot-main.tsx +++ b/packages/client/src/client/bot-main.tsx @@ -1909,19 +1909,30 @@ function ConversationView({ }, [scopeKey, channelId, previewRoster]); const skillCatalogMount = useMountedResource(() => { let cancelled = false; + let generation = 0; setSkillCatalog(undefined); + const loadSkills = actions.skillCatalog; if ( - ((channel?.type === 'dm' && channel.botSlug !== undefined) || channel?.type === 'group') && - actions.skillCatalog + !((channel?.type === 'dm' && channel.botSlug !== undefined) || channel?.type === 'group') || + loadSkills === undefined ) - void actions - .skillCatalog(channel.id) + return () => { + cancelled = true; + }; + const targetId = channel.id; + const load = (): void => { + const current = ++generation; + void loadSkills(targetId) .then((skills) => { - if (!cancelled) setSkillCatalog(skills); + if (!cancelled && current === generation) setSkillCatalog(skills); }) .catch(() => undefined); + }; + const unsubscribe = actions.subscribeSkillCatalog?.(targetId, load); + load(); return () => { cancelled = true; + unsubscribe?.(); }; }, [scopeKey, channelId, previewRoster]); const skillOptions: ComposerSkillOption[] = (skillCatalog ?? []).map((entry) => ({ diff --git a/packages/client/src/client/bridge.ts b/packages/client/src/client/bridge.ts index 941e46a36..18d940885 100644 --- a/packages/client/src/client/bridge.ts +++ b/packages/client/src/client/bridge.ts @@ -1,3 +1,7 @@ +import { + skillCatalogChangeSchema, + type SkillCatalogChange, +} from '../../../core/src/skills/events.js'; import { MEMBER_METHODS } from '../../../core/src/team/member-methods.js'; import { memberPage } from './member-page.js'; import { @@ -1154,6 +1158,17 @@ function parseSkillInvocations( }); } +export function parseSkillCatalogChange(value: unknown): SkillCatalogChange | undefined { + try { + const parsed = skillCatalogChangeSchema.safeParse( + typeof value === 'string' ? JSON.parse(value) : value, + ); + return parsed.success ? parsed.data : undefined; + } catch { + return undefined; + } +} + export async function loadChannelSkillCatalog( call: BridgeCall, channelId: string, diff --git a/packages/client/src/client/live-snapshots.ts b/packages/client/src/client/live-snapshots.ts index 1df911ebb..dc29e4cd2 100644 --- a/packages/client/src/client/live-snapshots.ts +++ b/packages/client/src/client/live-snapshots.ts @@ -391,6 +391,7 @@ export function createTypedLive( }, update: (frame) => { if (frame.type === 'changed') handlers.changed(); + else if (frame.type === 'skills') handlers.skills?.(frame.change); else if (frame.type === 'attention') handlers.attention?.(); else if (frame.type === 'latest') handlers.latest({ channelId: frame.channelId, message: frame.message }); diff --git a/packages/client/src/client/messaging-defaults-live.ts b/packages/client/src/client/messaging-defaults-live.ts index 2c5b7eb5c..414876d25 100644 --- a/packages/client/src/client/messaging-defaults-live.ts +++ b/packages/client/src/client/messaging-defaults-live.ts @@ -1,6 +1,17 @@ +import { + SKILL_CATALOG_CHANGED_EVENT, + type SkillCatalogChange, +} from '../../../core/src/skills/events.js'; +import { parseSkillCatalogChange } from './bridge.js'; import type { PurgedPlacement } from '../../../core/src/purge/contracts.js'; import { parseContentRedactions } from './content-redactions.js'; +const skillListeners = new Map<() => void, string>(); +const skillsChanged = (change?: SkillCatalogChange): void => { + for (const [listener, channelId] of [...skillListeners]) + if (change === undefined || change.channelIds.includes(channelId)) listener(); +}; + const listeners = new Set<() => void>(); const attentionListeners = new Map<() => void, (healthy: boolean) => void>(); let rosterHealthy = false; @@ -23,6 +34,7 @@ export interface TypedLive { baseline(placements: PurgedPlacement[]): void; changed(): void; attention?(): void; + skills?(change: unknown): void; latest(value: unknown): void; purged(placements: PurgedPlacement[]): void; interrupted(): void; @@ -63,12 +75,16 @@ function publishLatest(value: unknown): void { function syncTyped(): void { if (typed === undefined) return; - if ((listeners.size || attentionListeners.size) && stopRoster === undefined) + if ( + (listeners.size || attentionListeners.size || skillListeners.size) && + stopRoster === undefined + ) stopRoster = typed.roster({ baseline(placements) { redactions.clear(); recordPurged(placements); notify(); + skillsChanged(); attentionHealth(true); attentionChanged(); }, @@ -77,11 +93,20 @@ function syncTyped(): void { attentionChanged(); }, attention: attentionChanged, + skills: (value) => { + const change = parseSkillCatalogChange(value); + if (change !== undefined) skillsChanged(change); + }, latest: publishLatest, purged: recordPurged, interrupted: () => attentionHealth(false), }); - else if (!listeners.size && !attentionListeners.size && stopRoster !== undefined) { + else if ( + !listeners.size && + !attentionListeners.size && + !skillListeners.size && + stopRoster !== undefined + ) { stopRoster(); stopRoster = undefined; rosterHealthy = false; @@ -110,7 +135,7 @@ function releaseUnusedSource(): void { syncTyped(); if ( typed === undefined - ? listeners.size || attentionListeners.size || consumers.size + ? listeners.size || attentionListeners.size || skillListeners.size || consumers.size : channelConsumer !== undefined ) return; @@ -154,7 +179,7 @@ class SharedSource extends EventTarget implements ActivityEventSource { consumers.delete(this); if (channelConsumer === this) channelConsumer = undefined; releaseUnusedSource(); - if (listeners.size || attentionListeners.size || consumers.size) { + if (listeners.size || attentionListeners.size || skillListeners.size || consumers.size) { try { ensureSource(); } catch { @@ -190,9 +215,15 @@ function ensureSource(): void { next.addEventListener('roster/changed', () => { if (source === next) notify(); }); + next.addEventListener(SKILL_CATALOG_CHANGED_EVENT, (event) => { + if (source !== next || !(event instanceof MessageEvent)) return; + const change = parseSkillCatalogChange(event.data); + if (change !== undefined) skillsChanged(change); + }); next.addEventListener('open', () => { if (source === next) { notify(); + skillsChanged(); attentionHealth(true); attentionChanged(); } @@ -266,7 +297,8 @@ export function installTypedLive(next: TypedLive): () => void { attentionHealth(false); activityBaseline = undefined; sourceUrl = undefined; - if (listeners.size || attentionListeners.size || consumers.size) ensureSource(); + if (listeners.size || attentionListeners.size || skillListeners.size || consumers.size) + ensureSource(); else releaseUnusedSource(); for (const consumer of [...consumers]) consumer.publish('error', undefined, 0); }; @@ -384,3 +416,18 @@ export function observeHumanAttention(refresh: () => void): () => void { stop(); }; } + +export function subscribeSkillCatalog(channelId: string, listener: () => void): () => void { + skillListeners.set(listener, channelId); + try { + ensureSource(); + } catch (error) { + skillListeners.delete(listener); + releaseUnusedSource(); + throw error; + } + return () => { + skillListeners.delete(listener); + releaseUnusedSource(); + }; +} diff --git a/packages/client/test/inert-submissions.ts b/packages/client/test/inert-submissions.ts index 004253c71..066882024 100644 --- a/packages/client/test/inert-submissions.ts +++ b/packages/client/test/inert-submissions.ts @@ -1,6 +1,9 @@ import type { BridgeActions } from '../src/client/actions.js'; -export const inertSubmissionActions: Pick = { +export const inertSubmissionActions: Pick< + BridgeActions, + 'submissions' | 'averageReplyPerf' | 'subscribeSkillCatalog' +> = { submissions: { subscribe: () => () => {}, snapshot: () => undefined, @@ -11,4 +14,5 @@ export const inertSubmissionActions: Pick {}, }, averageReplyPerf: async () => undefined, + subscribeSkillCatalog: () => () => {}, }; diff --git a/packages/client/test/skill-catalog-live.test.ts b/packages/client/test/skill-catalog-live.test.ts new file mode 100644 index 000000000..300c1ebe8 --- /dev/null +++ b/packages/client/test/skill-catalog-live.test.ts @@ -0,0 +1,103 @@ +import { afterEach, expect, it, vi } from 'vitest'; +import { parseSkillCatalogChange } from '../src/client/bridge.js'; +import { + installTypedLive, + openSharedChannel, + subscribeMessagingDefaults, + subscribeSkillCatalog, + type TypedLive, +} from '../src/client/messaging-defaults-live.js'; + +const stops: (() => void)[] = []; +afterEach(() => { + for (const stop of stops.splice(0).reverse()) stop(); + vi.unstubAllGlobals(); +}); + +it('parses skill event payloads at the bridge boundary', () => { + const change = { botSlugs: ['ada'], channelIds: ['dm-ada', 'group'] }; + expect(parseSkillCatalogChange(JSON.stringify(change))).toEqual(change); + expect(parseSkillCatalogChange(change)).toEqual(change); + for (const invalid of [ + '{', + null, + {}, + { botSlugs: ['ada'], channelIds: [1] }, + { botSlugs: [''], channelIds: [] }, + ]) + expect(parseSkillCatalogChange(invalid)).toBeUndefined(); +}); + +it('shares the SSE carrier, filters unrelated channels, refreshes on reconnect and cleans up', () => { + const sources: Source[] = []; + class Source extends EventTarget { + readyState = 1; + closed = false; + constructor(readonly url: string) { + super(); + sources.push(this); + } + close() { + this.closed = true; + this.readyState = 2; + } + } + vi.stubGlobal('EventSource', Source); + const roster = vi.fn(); + const ada = vi.fn(); + const bob = vi.fn(); + const stopRoster = subscribeMessagingDefaults(roster); + const stopAda = subscribeSkillCatalog('dm-ada', ada); + const stopBob = subscribeSkillCatalog('dm-bob', bob); + stops.push(stopRoster, stopAda, stopBob); + expect(sources).toHaveLength(1); + const channel = openSharedChannel('/api/botharness/stream?channelId=dm-ada&after=0'); + stops.push(() => channel.close()); + expect(sources.filter((source) => !source.closed)).toHaveLength(1); + const source = sources.at(-1)!; + source.dispatchEvent( + new MessageEvent('skills/catalog-changed', { + data: JSON.stringify({ botSlugs: ['ada'], channelIds: ['dm-ada', 'group'] }), + }), + ); + source.dispatchEvent(new MessageEvent('skills/catalog-changed', { data: '{' })); + expect(ada).toHaveBeenCalledTimes(1); + expect(bob).not.toHaveBeenCalled(); + expect(roster).not.toHaveBeenCalled(); + source.dispatchEvent(new Event('open')); + expect(ada).toHaveBeenCalledTimes(2); + expect(bob).toHaveBeenCalledTimes(1); + stopAda(); + source.dispatchEvent( + new MessageEvent('skills/catalog-changed', { + data: JSON.stringify({ botSlugs: ['ada'], channelIds: ['dm-ada'] }), + }), + ); + expect(ada).toHaveBeenCalledTimes(2); +}); + +it('uses typed roster notifications with no additional stream and refreshes subscriptions on a replacement baseline', () => { + let handlers: Parameters[0] | undefined; + const close = vi.fn(); + const roster = vi.fn((next: Parameters[0]) => { + handlers = next; + return close; + }); + const activity = vi.fn(() => () => {}); + stops.push(installTypedLive({ roster, activity })); + const ada = vi.fn(); + const bob = vi.fn(); + const stopAda = subscribeSkillCatalog('dm-ada', ada); + const stopBob = subscribeSkillCatalog('dm-bob', bob); + stops.push(stopAda, stopBob); + expect(roster).toHaveBeenCalledTimes(1); + expect(activity).not.toHaveBeenCalled(); + handlers!.baseline([]); + handlers!.skills?.({ botSlugs: ['ada'], channelIds: ['dm-ada'] }); + handlers!.skills?.({ botSlugs: ['ada'], channelIds: [false] }); + expect(ada).toHaveBeenCalledTimes(2); + expect(bob).toHaveBeenCalledTimes(1); + stopAda(); + stopBob(); + expect(close).toHaveBeenCalledTimes(1); +}); diff --git a/packages/core/src/channels/live.ts b/packages/core/src/channels/live.ts index 8ead62cd7..54769768a 100644 --- a/packages/core/src/channels/live.ts +++ b/packages/core/src/channels/live.ts @@ -1,3 +1,4 @@ +import { SKILL_CATALOG_CHANGED_EVENT, type SkillCatalogChange } from '../skills/events.js'; import { OWNER_HUMAN_CONTEXT, type HumanContext } from '../humans/context.js'; import type { PersonaBotActivitySnapshot } from '../state/bot-state.js'; import type { PurgedPlacement } from '../purge/contracts.js'; @@ -62,6 +63,7 @@ interface Subscriber { export type RosterLiveFrame = | { type: 'baseline'; placements: PurgedPlacement[] } + | { type: 'skills'; change: SkillCatalogChange } | { type: 'changed' } | { type: 'attention' } | { type: 'latest'; channelId: string; message: ChannelMessage } @@ -193,6 +195,7 @@ export interface ChannelLiveHub { publishAttachments(channelId: string, messageId: string, message: ChannelMessage): void; publishDraft(event: ChannelDraftEvent): void; publishRosterCommitted(): void; + publishSkillCatalogChanged(change: SkillCatalogChange): void; refreshAccess(): void; publishContentPurged(): void; publishHumanIdentityChanged(): void; @@ -392,6 +395,7 @@ export function createChannelLiveHub( }; interface RosterSubscriber { human: HumanContext; + skills(change: SkillCatalogChange): void; attention(): void; push(): void; latest(channelId: string, message: ChannelMessage): void; @@ -434,6 +438,19 @@ export function createChannelLiveHub( }; subscriber = { human, + skills(change) { + if (ended) return; + if ((controller.desiredSize ?? 0) <= -64) return this.close(); + try { + controller.enqueue( + encoder.encode( + `event: ${SKILL_CATALOG_CHANGED_EVENT}\ndata: ${JSON.stringify(change)}\n\n`, + ), + ); + } catch { + this.close(); + } + }, purge(placements) { if (ended) return; try { @@ -532,6 +549,12 @@ export function createChannelLiveHub( let wake: (() => void) | undefined; const subscriber: RosterSubscriber = { human, + skills(change) { + if (ended) return; + queue.push({ type: 'skills', change }); + if (queue.length > ROSTER_LIVE_QUEUE_LIMIT) this.close(); + wake?.(); + }, attention() { if (ended) return; if (!queue.some((frame) => frame.type === 'attention')) queue.push({ type: 'attention' }); @@ -583,7 +606,18 @@ export function createChannelLiveHub( for (;;) { while (!ended && queue.length > 0) { const frame = queue.shift()!; - if (frame.type === 'latest') { + if (frame.type === 'skills') { + const channelIds = frame.change.channelIds.filter((id) => visibleChannel(human, id)); + const members = new Set(channelIds.flatMap((id) => channels.get(id)?.members ?? [])); + if (channelIds.length > 0) + yield { + type: 'skills', + change: { + channelIds, + botSlugs: frame.change.botSlugs.filter((slug) => members.has(slug)), + }, + }; + } else if (frame.type === 'latest') { if (visibleChannel(human, frame.channelId)) yield project(human, frame); } else if (frame.type === 'purged') { const placements = visiblePlacements(human, frame.placements); @@ -1044,6 +1078,17 @@ export function createChannelLiveHub( draftRevisions.set(channelId, revision); for (const subscriber of subscribers.get(channelId) ?? []) subscriber.pushDraft(published); }, + publishSkillCatalogChanged(change) { + for (const subscriber of rosterSubscribers) { + const channelIds = change.channelIds.filter((id) => visibleChannel(subscriber.human, id)); + if (channelIds.length === 0) continue; + const members = new Set(channelIds.flatMap((id) => channels.get(id)?.members ?? [])); + subscriber.skills({ + channelIds, + botSlugs: change.botSlugs.filter((slug) => members.has(slug)), + }); + } + }, publishRosterCommitted() { for (const subscriber of rosterSubscribers) subscriber.push(); }, diff --git a/packages/core/src/plugin.ts b/packages/core/src/plugin.ts index baa21f08b..4b155aaec 100644 --- a/packages/core/src/plugin.ts +++ b/packages/core/src/plugin.ts @@ -247,12 +247,15 @@ import { createMediaVersions, type MediaVersions } from './attachments/media-ver import { loadImageProcessor } from './attachments/thumbnails.js'; import { createChannelLiveHub, CHANNEL_STREAM_PATH, type ChannelLiveHub } from './channels/live.js'; import type { ChannelDraftEvent } from './channels/draft.js'; +import { watchSkillCatalog } from './skills/refresh.js'; import { SkillCatalog, type SkillRegistryPort } from './skills/catalog.js'; import { createSkillViewPort, type SkillAgentPresetsPort } from './skills/agent-view.js'; import { degradeHostCapability, hostAgentPresets, hostSkillRegistry, + installHostSkillInvalidation, + onHostSkillsChange, } from './runtime/native-agent-compat.js'; import { DeveloperModeSkillGate } from './logs/skill.js'; import { openLogDatabase, type LogDatabase } from './logs/log-db.js'; @@ -3271,6 +3274,7 @@ export function apply(ctx: Context, config: BotHarnessConfig): void { drafts: shareDrafts, log: (event) => ctx.logger.info(JSON.stringify(event)), }); + let invalidateNativeSkills: (() => void) | undefined; const skillCatalog = new SkillCatalog({ memoryDirFor: (slug) => core.registry.get(slug) === undefined ? undefined : core.registry.memoryDirFor(slug), @@ -3284,6 +3288,49 @@ export function apply(ctx: Context, config: BotHarnessConfig): void { agentPresets: () => hostAgentPresets>(ctx), }), }); + ctx.effect(() => { + if (core.operationalDatabase.mode !== 'ready') return () => undefined; + const refresh = watchSkillCatalog({ + catalog: skillCatalog, + invalidateNative: () => invalidateNativeSkills?.(), + bots: () => + core.registry.list().flatMap((bot) => { + const memoryDir = core.registry.memoryDirFor(bot.slug); + return memoryDir === undefined + ? [] + : [ + { + slug: bot.slug, + memoryDir, + ...(bot.preset === undefined ? {} : { preset: bot.preset }), + }, + ]; + }), + changed: (botSlugs) => + core.live.publishSkillCatalogChanged({ + botSlugs, + channelIds: core.channels + .list() + .filter( + (channel) => + channel.deletedAt === undefined && + channel.members.some((slug) => botSlugs.includes(slug)), + ) + .map((channel) => channel.id), + }), + diagnostic: (phase, fields) => diagnosticEvents.record('skills', phase, fields), + }); + const offRegistry = core.operationalDatabase.subscribe(({ topics }) => { + if (topics.some((topic) => ['bot-registry', 'member-purged'].includes(topic))) + refresh.reconcile(); + }); + const offHost = onHostSkillsChange(ctx, () => refresh.hostChanged()); + return () => { + offHost(); + offRegistry(); + refresh.close(); + }; + }, 'botharness: live skill catalog'); const bridgeDeps: BridgeMethodsDeps = { team, skills: skillCatalog, @@ -3862,9 +3909,18 @@ export function apply(ctx: Context, config: BotHarnessConfig): void { developerModeTarget.gate = gate; skillsCtx.effect(() => { const registry = hostSkillRegistry(skillsCtx); - return registry === undefined - ? degradeHostCapability(skillsCtx, 'skills.list') - : skillCatalog.attach(registry); + if (registry === undefined) return degradeHostCapability(skillsCtx, 'skills.list'); + const detach = skillCatalog.attach(registry); + const stop = installHostSkillInvalidation(skillsCtx, (invalidate) => { + invalidateNativeSkills = invalidate; + return () => { + if (invalidateNativeSkills === invalidate) invalidateNativeSkills = undefined; + }; + }); + return () => { + stop(); + detach(); + }; }, 'botharness: skill catalog'); skillsCtx.effect( () => diff --git a/packages/core/src/runtime/native-agent-compat.ts b/packages/core/src/runtime/native-agent-compat.ts index 65e5afa88..9705c52a8 100644 --- a/packages/core/src/runtime/native-agent-compat.ts +++ b/packages/core/src/runtime/native-agent-compat.ts @@ -4,6 +4,12 @@ import type { ToolCallId } from '@deepseek-ai/dsh-llm'; import type { ToolExecutionResult } from '@deepseek-ai/dsh-tools'; import { installedDshVersion } from '../telemetry/service.js'; +declare module '@deepseek-ai/cordis' { + interface Events { + 'skills/change'(): void; + } +} + export const HOST_INCOMPATIBLE = 'HOST_INCOMPATIBLE'; export class HostIncompatibleError extends Error { @@ -87,6 +93,44 @@ export function degradeHostCapability( return () => undefined; } +export function installHostSkillInvalidation( + ctx: Context, + attached: (invalidate: () => void) => () => void, +): () => void { + const registry = hostSkillRegistry<{ + registerProvider?: ( + create: (control: { invalidate(): void }) => { + name: string; + list(): Promise; + get(): Promise; + }, + ) => () => void; + }>(ctx); + if (typeof registry?.registerProvider !== 'function') + return requireHostCapability('skills.registerProvider'); + let detach = (): void => {}; + const dispose = registry.registerProvider((control) => { + detach = attached(() => control.invalidate()); + return { + name: 'botharness-memory-skill-changes', + async list() { + return []; + }, + async get() { + return undefined; + }, + }; + }); + return () => { + detach(); + dispose(); + }; +} + +export function onHostSkillsChange(ctx: Context, changed: () => void): () => void { + return ctx.on('skills/change', changed, { global: true }); +} + export function hostSkillRegistry(ctx: Context): Registry | undefined { const skills: unknown = Reflect.get(ctx, 'skills'); return typeof skills === 'object' && diff --git a/packages/core/src/skills/catalog.ts b/packages/core/src/skills/catalog.ts index 3c5be76ac..d1b80c06f 100644 --- a/packages/core/src/skills/catalog.ts +++ b/packages/core/src/skills/catalog.ts @@ -1,4 +1,5 @@ -import { realpathSync } from 'node:fs'; +import { createHash } from 'node:crypto'; +import { realpathSync, statSync } from 'node:fs'; import { isAbsolute, relative, resolve } from 'node:path'; import type { SkillCatalogEntry, SkillOwner } from './entries.js'; @@ -77,10 +78,55 @@ function inside(root: string, path: string | undefined): boolean { return rel.length > 0 && !rel.startsWith('..') && !isAbsolute(rel); } +async function hostVersion( + entries: SkillCatalogEntry[], + load: (name: string) => Promise, +): Promise { + return JSON.stringify( + await Promise.all( + entries.map(async (entry) => { + if (entry.path === undefined) { + const skill = await load(entry.name); + return [ + entry, + skill === undefined ? null : createHash('sha256').update(skill.content).digest('hex'), + ]; + } + try { + const stat = statSync(entry.path, { bigint: true }); + return [entry, stat.mtimeNs.toString(), stat.size.toString()]; + } catch { + return [entry]; + } + }), + ), + ); +} + +function hostEntriesFrom(skills: readonly SkillRegistrySummary[]): SkillCatalogEntry[] { + return skills + .filter((skill) => skill.invocation.userInvocable && !PROJECT_SOURCES.has(skill.source)) + .map((skill) => ({ + name: skill.name, + description: skill.description, + owner: { kind: 'host' }, + ...(skill.path === undefined ? {} : { path: skill.path }), + })); +} + +interface CachedCatalog { + root: string; + project?: SkillCatalogEntry[]; + host?: SkillCatalogEntry[]; + hostVersion?: string; + loading?: Promise; +} + export class SkillCatalog { #registry: SkillRegistryPort | undefined; readonly #memoryDirFor: (botSlug: string) => string | undefined; readonly #viewFor: SkillViewPort | undefined; + readonly #cache = new Map(); constructor(options: { memoryDirFor(botSlug: string): string | undefined; @@ -96,8 +142,12 @@ export class SkillCatalog { attach(registry: SkillRegistryPort): () => void { this.#registry = registry; + this.#cache.clear(); return () => { - if (this.#registry === registry) this.#registry = undefined; + if (this.#registry === registry) { + this.#registry = undefined; + this.#cache.clear(); + } }; } @@ -111,41 +161,99 @@ export class SkillCatalog { return await read(view.registry, view.scope === undefined ? {} : { scope: view.scope }); } + invalidateBot(botSlug: string): void { + this.#cache.delete(botSlug); + } + + invalidateMemory(botSlug: string): void { + const cached = this.#cache.get(botSlug); + if (cached !== undefined) + this.#cache.set(botSlug, { + root: cached.root, + ...(cached.host === undefined + ? {} + : { + host: cached.host, + ...(cached.hostVersion === undefined ? {} : { hostVersion: cached.hostVersion }), + }), + }); + } + + invalidateHost(): void { + for (const [slug, cached] of this.#cache) + this.#cache.set(slug, { + root: cached.root, + ...(cached.project === undefined ? {} : { project: cached.project }), + }); + } + + async refreshHost(): Promise { + const registry = this.#registry; + if (registry === undefined) return false; + const observed = [...this.#cache].filter(([, entry]) => entry.host !== undefined); + if (observed.length === 0) return true; + for (const [slug, entry] of observed) { + const version = await this.#withView(slug, registry, async (view, options) => + hostVersion(hostEntriesFrom(await view.list(options)), (name) => view.get(name, options)), + ); + if (this.#registry !== registry) return false; + if (version !== entry.hostVersion) { + this.invalidateHost(); + return true; + } + } + return false; + } + async list(botSlugs: readonly string[]): Promise { const registry = this.#registry; if (registry === undefined) return []; const entries: SkillCatalogEntry[] = []; - const hostEntries: SkillCatalogEntry[] = []; - let hostListed = false; + const observed = new Map(); + let hostEntries: SkillCatalogEntry[] | undefined; for (const botSlug of new Set(botSlugs)) { const root = this.#memoryDirFor(botSlug); if (root === undefined) continue; - await this.#withView(botSlug, registry, async (registry, options) => { - for (const skill of await registry.list({ ...options, cwd: root })) - if ( - skill.invocation.userInvocable && - PROJECT_SOURCES.has(skill.source) && - inside(root, skill.path) - ) - entries.push({ - name: skill.name, - description: skill.description, - owner: { kind: 'bot', botSlug }, - ...(skill.path === undefined ? {} : { path: skill.path }), - }); - if (hostListed) return; - for (const skill of await registry.list(options)) - if (skill.invocation.userInvocable && !PROJECT_SOURCES.has(skill.source)) - hostEntries.push({ - name: skill.name, - description: skill.description, - owner: { kind: 'host' }, - ...(skill.path === undefined ? {} : { path: skill.path }), - }); - hostListed = true; - }); + let cached = this.#cache.get(botSlug); + if (cached?.root !== root) { + cached = { root }; + this.#cache.set(botSlug, cached); + } + const entry = cached; + observed.set(botSlug, entry); + while (entry.project === undefined || (hostEntries === undefined && entry.host === undefined)) + await (entry.loading ??= this.#withView(botSlug, registry, async (registry, options) => { + if (entry.project === undefined) + entry.project = (await registry.list({ ...options, cwd: root })) + .filter( + (skill) => + skill.invocation.userInvocable && + PROJECT_SOURCES.has(skill.source) && + inside(root, skill.path), + ) + .map((skill) => ({ + name: skill.name, + description: skill.description, + owner: { kind: 'bot', botSlug }, + ...(skill.path === undefined ? {} : { path: skill.path }), + })); + if (hostEntries === undefined && entry.host === undefined) { + const host = hostEntriesFrom(await registry.list(options)); + const version = await hostVersion(host, (name) => registry.get(name, options)); + entry.host = host; + entry.hostVersion = version; + } + }).finally(() => { + delete entry.loading; + })); + if (this.#cache.get(botSlug) !== entry || this.#registry !== registry) + return this.list(botSlugs); + entries.push(...(entry.project ?? [])); + hostEntries ??= entry.host; } - return [...entries, ...hostEntries]; + if ([...observed].some(([slug, entry]) => this.#cache.get(slug) !== entry)) + return this.list(botSlugs); + return [...entries, ...(hostEntries ?? [])]; } async resolve( diff --git a/packages/core/src/skills/events.ts b/packages/core/src/skills/events.ts new file mode 100644 index 000000000..5c28a9983 --- /dev/null +++ b/packages/core/src/skills/events.ts @@ -0,0 +1,10 @@ +import { z } from 'zod'; + +export const SKILL_CATALOG_CHANGED_EVENT = 'skills/catalog-changed'; + +export const skillCatalogChangeSchema = z.object({ + botSlugs: z.array(z.string().min(1)), + channelIds: z.array(z.string().min(1)), +}); + +export type SkillCatalogChange = z.infer; diff --git a/packages/core/src/skills/refresh.ts b/packages/core/src/skills/refresh.ts new file mode 100644 index 000000000..e958bb3e9 --- /dev/null +++ b/packages/core/src/skills/refresh.ts @@ -0,0 +1,187 @@ +import { existsSync, statSync, watch, type FSWatcher } from 'node:fs'; +import { join, normalize, sep } from 'node:path'; +import type { SkillCatalog } from './catalog.js'; + +export interface SkillCatalogBot { + slug: string; + memoryDir: string; + preset?: string; +} + +function watchMemorySkills( + root: string, + changed: () => void, + failed: (error: Error) => void, +): () => void { + const watchers = new Map(); + let closed = false; + const sync = (): void => { + if (closed) return; + const paths = new Map([[root, false]]); + for (const parent of ['.agents', '.dsh']) { + const directory = join(root, parent); + if (existsSync(directory)) paths.set(directory, false); + const skills = join(directory, 'skills'); + if (existsSync(skills)) paths.set(skills, true); + } + const identities = new Map(); + for (const path of paths.keys()) { + try { + const stat = statSync(path, { bigint: true }); + identities.set(path, `${stat.dev}:${stat.ino}`); + } catch { + paths.delete(path); + } + } + for (const [path, entry] of watchers) { + if (paths.has(path) && identities.get(path) === entry.identity) continue; + entry.watcher.close(); + watchers.delete(path); + } + for (const [path, recursive] of paths) { + if (watchers.has(path)) continue; + const watcher = watch(path, { recursive, persistent: false }, (event, name) => { + if (closed || watchers.get(path)?.watcher !== watcher) return; + const parts = name === null ? [] : normalize(name).split(sep); + if ( + name !== null && + (path === root + ? parts[0] !== '.agents' && parts[0] !== '.dsh' + : !recursive + ? parts[0] !== 'skills' + : event !== 'rename' && parts.at(-1) !== 'SKILL.md') + ) + return; + try { + sync(); + } catch (error) { + failed(error instanceof Error ? error : new Error('watch-failed')); + } + changed(); + }); + watcher.on('error', failed); + watchers.set(path, { watcher, identity: identities.get(path)! }); + } + }; + const close = (): void => { + closed = true; + for (const entry of watchers.values()) entry.watcher.close(); + watchers.clear(); + }; + try { + sync(); + } catch (error) { + close(); + throw error; + } + return close; +} + +export function watchSkillCatalog(options: { + catalog: SkillCatalog; + invalidateNative?(): void; + bots(): SkillCatalogBot[]; + changed(botSlugs: string[]): void; + diagnostic(phase: string, fields: { botSlug?: string; reason?: string; count?: number }): void; +}): { reconcile(): void; hostChanged(): Promise; close(): void } { + const watched = new Map(); + let closed = false; + let hostDirty = false; + let hostRefresh: Promise | undefined; + const pending = new Map>(); + const cancel = (slug: string): void => { + clearTimeout(pending.get(slug)); + pending.delete(slug); + }; + const memoryChanged = (slug: string): void => { + options.catalog.invalidateMemory(slug); + options.invalidateNative?.(); + cancel(slug); + pending.set( + slug, + setTimeout(() => { + pending.delete(slug); + if (!closed && watched.has(slug)) options.changed([slug]); + }, 25), + ); + }; + const reconcile = (): void => { + if (closed) return; + const bots = new Map(options.bots().map((bot) => [bot.slug, bot])); + const changed = new Set(); + for (const [slug, entry] of watched) { + const bot = bots.get(slug); + if (bot?.memoryDir === entry.bot.memoryDir && bot.preset === entry.bot.preset) continue; + entry.close(); + cancel(slug); + watched.delete(slug); + options.catalog.invalidateBot(slug); + changed.add(slug); + options.diagnostic('unwatch', { botSlug: slug }); + } + for (const [slug, bot] of bots) { + if (watched.has(slug)) continue; + try { + const entry = { + bot, + close: watchMemorySkills( + bot.memoryDir, + () => { + if (closed || watched.get(slug) !== entry) return; + memoryChanged(slug); + }, + (error) => { + if (closed || watched.get(slug) !== entry) return; + entry.close(); + cancel(slug); + watched.delete(slug); + options.catalog.invalidateMemory(slug); + options.diagnostic('failed', { botSlug: slug, reason: error.name }); + options.changed([slug]); + }, + ), + }; + watched.set(slug, entry); + options.diagnostic('watch', { botSlug: slug }); + changed.add(slug); + } catch (error) { + options.diagnostic('failed', { + botSlug: slug, + reason: error instanceof Error ? error.name : 'unknown', + }); + } + } + if (changed.size > 0) options.changed([...changed]); + }; + reconcile(); + return { + reconcile, + hostChanged() { + if (closed) return Promise.resolve(); + hostDirty = true; + hostRefresh ??= (async () => { + do { + hostDirty = false; + if (await options.catalog.refreshHost()) { + if (!closed) options.changed(options.bots().map((bot) => bot.slug)); + } + } while (hostDirty && !closed); + })().finally(() => { + hostRefresh = undefined; + }); + return hostRefresh; + }, + close() { + if (closed) return; + closed = true; + for (const slug of pending.keys()) cancel(slug); + for (const [slug, entry] of watched) { + entry.close(); + cancel(slug); + options.catalog.invalidateBot(slug); + } + options.diagnostic('closed', { count: watched.size }); + watched.clear(); + }, + }; +} diff --git a/packages/core/test/skill-catalog-refresh.test.ts b/packages/core/test/skill-catalog-refresh.test.ts new file mode 100644 index 000000000..312e58ebc --- /dev/null +++ b/packages/core/test/skill-catalog-refresh.test.ts @@ -0,0 +1,489 @@ +import { Context } from '@deepseek-ai/cordis'; +import { createRequire } from 'node:module'; +import { pathToFileURL } from 'node:url'; +import { createScope } from '@deepseek-ai/dsh-scope'; +import { mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { createBridgeMethods } from '../src/bridge/methods.js'; +import { runSkillCommand } from '../src/bots/cli-skills.js'; +import { createChannelLiveHub } from '../src/channels/live.js'; +import { createChannelStore } from '../src/channels/store.js'; +import { OWNER_HUMAN_CONTEXT, humanContext } from '../src/humans/context.js'; +import { + installHostSkillInvalidation, + hostSkillRegistry, + onHostSkillsChange, +} from '../src/runtime/native-agent-compat.js'; +import { + SkillCatalog, + type SkillRegistryDefinition, + type SkillRegistryPort, +} from '../src/skills/catalog.js'; +import { watchSkillCatalog } from '../src/skills/refresh.js'; +import { createCore } from '../src/plugin.js'; +import { createTempRoot } from './helpers.js'; + +const stops: (() => void)[] = []; +afterEach(() => { + for (const stop of stops.splice(0)) stop(); +}); + +function definition(name: string, path: string, description = name): SkillRegistryDefinition { + return { + name, + path, + description, + content: description, + source: 'project-agents', + invocation: { userInvocable: true }, + }; +} + +function fixture() { + const roots = new Map(['ada', 'bob'].map((slug) => [slug, createTempRoot()])); + const host = [{ ...definition('host', '/host/SKILL.md'), source: 'runtime' }]; + const list = vi.fn(async (options?: { cwd?: string }) => { + if (options?.cwd === undefined) return host; + return ['.agents', '.dsh'].flatMap((parent) => { + const root = join(options.cwd!, parent, 'skills'); + try { + return readdirSync(root).flatMap((name) => { + const path = join(root, name, 'SKILL.md'); + try { + return [definition(name, path, readFileSync(path, 'utf8'))]; + } catch { + return []; + } + }); + } catch { + return []; + } + }); + }); + const registry: SkillRegistryPort = { + list, + async get(name, options) { + return (await list(options)).find((skill) => skill.name === name); + }, + }; + const catalog = new SkillCatalog({ memoryDirFor: (slug) => roots.get(slug) }); + catalog.attach(registry); + const changed = vi.fn<(slugs: string[]) => void>(); + const diagnostic = vi.fn(); + const refresh = watchSkillCatalog({ + catalog, + bots: () => [...roots].map(([slug, memoryDir]) => ({ slug, memoryDir })), + changed, + diagnostic, + }); + stops.push(() => refresh.close()); + changed.mockClear(); + return { roots, host, catalog, changed, diagnostic, refresh, list }; +} + +async function mutate(f: ReturnType, write: () => void) { + let finish: (() => void) | undefined; + const changed = new Promise((resolve) => { + finish = resolve; + }); + f.changed.mockImplementation(() => finish?.()); + await new Promise((resolve) => setTimeout(resolve, 50)); + write(); + let timeout: ReturnType | undefined; + try { + await Promise.race([ + changed, + new Promise((_resolve, reject) => { + timeout = setTimeout( + () => + reject(new Error('watch event missing: ' + JSON.stringify(f.diagnostic.mock.calls))), + 2000, + ); + }), + ]); + } finally { + clearTimeout(timeout); + } + f.changed.mockImplementation(() => {}); +} + +function put(root: string, name: string, body: string, parent = '.agents') { + const folder = join(root, parent, 'skills', name); + mkdirSync(folder, { recursive: true }); + writeFileSync(join(folder, 'SKILL.md'), body); +} + +describe('live Skill catalog', () => { + it.each(['.agents', '.dsh'])( + 'refreshes add/modify/delete under %s while preserving unrelated Bot and Host reads', + async (parent) => { + const f = fixture(); + await f.catalog.list(['ada']); + await f.catalog.list(['bob']); + f.list.mockClear(); + await mutate(f, () => put(f.roots.get('ada')!, 'qa', 'first', parent)); + expect(await f.catalog.list(['ada', 'bob'])).toMatchObject([ + { name: 'qa', description: 'first' }, + { name: 'host' }, + ]); + await mutate(f, () => put(f.roots.get('ada')!, 'qa', 'edited', parent)); + expect(await f.catalog.list(['ada'])).toMatchObject([ + { description: 'edited' }, + { name: 'host' }, + ]); + await mutate(f, () => rmSync(join(f.roots.get('ada')!, parent), { recursive: true })); + expect((await f.catalog.list(['ada'])).map((skill) => skill.name)).toEqual(['host']); + await f.catalog.list(['bob']); + expect(f.list.mock.calls.map(([options]) => options?.cwd)).toEqual( + Array(3).fill(f.roots.get('ada')), + ); + expect( + f.changed.mock.calls.every(([slugs]) => slugs.length === 1 && slugs[0] === 'ada'), + ).toBe(true); + }, + ); + + it('emits scoped live frames for each real Skill file addition, modification and deletion', async () => { + const f = fixture(); + const store = createChannelStore({ rootDir: createTempRoot() }); + const dm = store.getOrCreateDm(OWNER_HUMAN_CONTEXT, 'ada', 'Ada')!; + const hub = createChannelLiveHub(store); + stops.push(() => hub.close()); + f.changed.mockImplementation((botSlugs) => + hub.publishSkillCatalogChanged({ botSlugs, channelIds: [dm.id] }), + ); + const reader = hub + .open(OWNER_HUMAN_CONTEXT, new Request('http://localhost/api/botharness/stream?scope=roster')) + .body!.getReader(); + try { + await reader.read(); + await f.catalog.list(['ada']); + for (const description of ['added', 'modified', undefined]) { + await new Promise((resolve) => setTimeout(resolve, 50)); + if (description === undefined) + rmSync(join(f.roots.get('ada')!, '.agents/skills/qa/SKILL.md')); + else put(f.roots.get('ada')!, 'qa', description); + const frame = new TextDecoder().decode((await reader.read()).value); + expect(frame).toContain('event: skills/catalog-changed'); + expect(JSON.parse(frame.split('data: ')[1]!.trim())).toEqual({ + botSlugs: ['ada'], + channelIds: [dm.id], + }); + expect( + (await f.catalog.list(['ada'])) + .filter((entry) => entry.name === 'qa') + .map((entry) => entry.description), + ).toEqual(description === undefined ? [] : [description]); + } + } finally { + await reader.cancel(); + } + }); + + it('does not broadcast an unfiltered DSH Project notification to unrelated channels', async () => { + const f = fixture(); + await f.catalog.list(['ada']); + await f.catalog.list(['bob']); + await mutate(f, () => put(f.roots.get('ada')!, 'qa', 'added')); + await f.catalog.list(['ada']); + f.changed.mockClear(); + f.list.mockClear(); + await f.refresh.hostChanged(); + expect(f.changed).not.toHaveBeenCalled(); + expect(f.list.mock.calls.every(([options]) => options?.cwd === undefined)).toBe(true); + await f.catalog.list(['bob']); + expect(f.list.mock.calls.every(([options]) => options?.cwd === undefined)).toBe(true); + }); + + it('reattaches when a Skill root is replaced at the same path', async () => { + const f = fixture(); + const memory = f.roots.get('ada')!; + await mutate(f, () => put(memory, 'qa', 'before')); + await f.catalog.list(['ada']); + await mutate(f, () => { + renameSync(join(memory, '.agents/skills'), join(memory, 'previous-skills')); + put(memory, 'qa', 'replaced'); + }); + expect((await f.catalog.list(['ada']))[0]?.description).toBe('replaced'); + await mutate(f, () => put(memory, 'qa', 'edited after replacement')); + expect((await f.catalog.list(['ada']))[0]?.description).toBe('edited after replacement'); + }); + + it('shares a cold Bot catalog read across concurrent channel requests', async () => { + const list = vi.fn(async () => []); + const catalog = new SkillCatalog({ memoryDirFor: () => '/memory/ada' }); + catalog.attach({ + list, + async get() { + return undefined; + }, + }); + await Promise.all([catalog.list(['ada']), catalog.list(['ada'])]); + expect(list).toHaveBeenCalledTimes(2); + await catalog.list(['ada']); + expect(list).toHaveBeenCalledTimes(2); + }); + + it('does not retain or return an observation invalidated while a read is in flight', async () => { + let release: ((skills: SkillRegistryDefinition[]) => void) | undefined; + const first = new Promise((resolve) => { + release = resolve; + }); + const fresh = definition('fresh', '/memory/ada/.agents/skills/fresh/SKILL.md'); + const list = vi.fn(async (options?: { cwd?: string }) => + options?.cwd === undefined ? [] : [fresh], + ); + list.mockImplementationOnce(() => first); + const catalog = new SkillCatalog({ memoryDirFor: () => '/memory/ada' }); + catalog.attach({ + list, + async get() { + return undefined; + }, + }); + const pending = catalog.list(['ada']); + catalog.invalidateMemory('ada'); + release!([]); + expect(await pending).toMatchObject([{ name: 'fresh' }]); + const calls = list.mock.calls.length; + expect(await catalog.list(['ada'])).toMatchObject([{ name: 'fresh' }]); + expect(list).toHaveBeenCalledTimes(calls); + }); + + it('invalidates Host portions across bots without recomputing Project portions', async () => { + const f = fixture(); + put(f.roots.get('ada')!, 'qa', 'first'); + await f.catalog.list(['ada']); + await f.catalog.list(['bob']); + f.list.mockClear(); + f.host[0]!.description = 'host edited'; + await f.refresh.hostChanged(); + expect(f.changed).toHaveBeenCalledWith(['ada', 'bob']); + for (const slug of ['ada', 'bob']) + expect((await f.catalog.list([slug])).at(-1)?.description).toBe('host edited'); + expect(f.list.mock.calls.map(([options]) => options?.cwd)).toEqual([ + undefined, + undefined, + undefined, + ]); + }); + + it('detects Host Skill body changes even when the catalog description is unchanged', async () => { + const f = fixture(); + const path = join(createTempRoot(), 'SKILL.md'); + writeFileSync(path, 'before'); + f.host[0]!.path = path; + await f.catalog.list(['ada']); + f.changed.mockClear(); + writeFileSync(path, 'after with a different size'); + await f.refresh.hostChanged(); + expect(f.changed).toHaveBeenCalledWith(['ada', 'bob']); + }); + + it('detects pathless runtime Host Skill content changes without broadening Project notifications', async () => { + const f = fixture(); + delete f.host[0]!.path; + await f.catalog.list(['ada']); + f.changed.mockClear(); + await f.refresh.hostChanged(); + expect(f.changed).not.toHaveBeenCalled(); + f.host[0]!.content = 'new runtime instructions'; + await f.refresh.hostChanged(); + expect(f.changed).toHaveBeenCalledWith(['ada', 'bob']); + }); + + it('rewatches moved Memory and releases removed bots and Host disposal', async () => { + const f = fixture(); + const from = f.roots.get('ada')!; + put(from, 'qa', 'before'); + await f.catalog.list(['ada']); + const moved = join(createTempRoot(), 'moved'); + renameSync(from, moved); + f.roots.set('ada', moved); + f.refresh.reconcile(); + await mutate(f, () => put(moved, 'qa', 'after')); + expect((await f.catalog.list(['ada']))[0]?.description).toBe('after'); + f.roots.delete('ada'); + f.refresh.reconcile(); + expect(f.diagnostic).toHaveBeenCalledWith('unwatch', { botSlug: 'ada' }); + expect(await f.catalog.list(['ada'])).toEqual([]); + f.refresh.close(); + f.changed.mockClear(); + put(moved, 'qa', 'removed'); + put(f.roots.get('bob')!, 'qa', 'closed'); + await new Promise((resolve) => setTimeout(resolve, 50)); + expect(f.changed).not.toHaveBeenCalled(); + expect(f.diagnostic).toHaveBeenCalledWith('closed', { count: 1 }); + }); + + it('keeps skills-list and the menu RPC fresh after filesystem mutations', async () => { + const f = fixture(); + const core = createCore({ dshHome: createTempRoot() }); + try { + core.registry.create({ slug: 'ada', displayName: 'Ada' }); + const dm = core.channels.getOrCreateDm(OWNER_HUMAN_CONTEXT, 'ada', 'Ada')!; + const methods = createBridgeMethods(OWNER_HUMAN_CONTEXT, { ...core, skills: f.catalog }); + const rpc = async (method: string, payload: Record) => { + expect(method).toBe('channelSkillCatalog'); + const result = await methods.channelSkillCatalog(payload); + if (!result.ok) throw new Error(result.error.message); + return result.value; + }; + const cli = () => runSkillCommand('skills-list', [], { bot: 'ada' }, async () => rpc); + await cli(); + for (const body of ['added', 'modified', undefined]) { + await mutate(f, () => + body === undefined + ? rmSync(join(f.roots.get('ada')!, '.agents'), { recursive: true }) + : put(f.roots.get('ada')!, 'qa', body), + ); + const result = await methods.channelSkillCatalog({ channelId: dm.id }); + expect(result.ok).toBe(true); + if (!result.ok) throw new Error(result.error.message); + expect(await cli()).toEqual({ channelId: dm.id, skills: result.value.skills }); + expect( + result.value.skills + .filter((skill) => skill.name === 'qa') + .map((skill) => skill.description), + ).toEqual(body === undefined ? [] : [body]); + } + } finally { + core.externalMessaging.close(); + core.live.close(); + await core.runtime.close(); + core.operationalDatabase.close(); + } + }); + + it('pushes the affected channels through existing SSE and typed roster carriers with access filtering', async () => { + const store = createChannelStore({ rootDir: createTempRoot() }); + const ada = store.getOrCreateDm(OWNER_HUMAN_CONTEXT, 'ada', 'Ada')!; + const bob = store.getOrCreateDm(OWNER_HUMAN_CONTEXT, 'bob', 'Bob')!; + const group = store.createGroup(OWNER_HUMAN_CONTEXT, { + name: 'Shared', + members: ['ada', 'bob'], + }); + const hub = createChannelLiveHub( + store, + undefined, + undefined, + undefined, + undefined, + undefined, + (_human, channelId) => channelId !== bob.id, + ); + stops.push(() => hub.close()); + const controller = new AbortController(); + const follow = hub + .followRoster(humanContext('member'), controller.signal) + [Symbol.asyncIterator](); + await follow.next(); + const response = hub.open( + humanContext('member'), + new Request('http://localhost/api/botharness/stream?scope=roster'), + ); + const reader = response.body!.getReader(); + await reader.read(); + const change = { botSlugs: ['ada', 'bob'], channelIds: [ada.id, bob.id, group.id] }; + hub.publishSkillCatalogChanged(change); + const expected = { botSlugs: ['ada', 'bob'], channelIds: [ada.id, group.id] }; + expect((await follow.next()).value).toEqual({ type: 'skills', change: expected }); + const frame = new TextDecoder().decode((await reader.read()).value); + expect(frame).toContain('event: skills/catalog-changed'); + expect(JSON.parse(frame.split('data: ')[1]!.trim())).toEqual(expected); + controller.abort(); + await follow.return?.(); + await reader.cancel(); + }); + + it('invalidates through the Host registration control while preserving original Agent visibility', async () => { + const ctx = new Context(); + const dshRequire = createRequire( + createRequire(import.meta.url).resolve('@deepseek-ai/dsh/package.json'), + ); + const { SkillRegistry } = await import( + pathToFileURL(dshRequire.resolve('@deepseek-ai/dsh-skill')).href + ); + const runtime = ctx.plugin(SkillRegistry, {}); + await runtime.await(); + try { + await ctx + .inject(['skills'], async (injected) => { + const agent = {}; + const scope = createScope(injected, agent); + let description = 'before'; + const list = vi.fn(async () => [ + { + name: 'qa', + description, + source: 'custom', + provider: 'test', + invocation: { userInvocable: true, modelInvocable: true }, + rank: 1, + locator: {}, + }, + ]); + const scoped = hostSkillRegistry< + SkillRegistryPort & { + registerProvider( + create: () => { name: string; list: typeof list; get(): Promise }, + ): () => void; + } + >(scope.ctx)!; + const global = hostSkillRegistry< + SkillRegistryPort & { + register(skill: { + name: string; + description: string; + content: string; + source: string; + }): () => void; + } + >(injected)!; + try { + scoped.registerProvider(() => ({ + name: 'test', + list, + async get() { + return undefined; + }, + })); + const changed = vi.fn(); + const off = onHostSkillsChange(injected, changed); + global.register({ + name: 'host', + description: 'host', + content: 'host', + source: 'runtime', + }); + expect(changed).toHaveBeenCalledTimes(1); + let invalidate = (): void => {}; + const stopInvalidation = installHostSkillInvalidation(injected, (next) => { + invalidate = next; + return () => {}; + }); + const before = await scoped.list({ scope: agent }); + description = 'after'; + expect(await scoped.list({ scope: agent })).toEqual(before); + invalidate(); + expect( + (await scoped.list({ scope: agent })).find((skill) => skill.name === 'qa') + ?.description, + ).toBe('after'); + expect((await global.list()).some((skill) => skill.name === 'qa')).toBe(false); + stopInvalidation(); + changed.mockClear(); + invalidate(); + expect(changed).not.toHaveBeenCalled(); + off(); + } finally { + await scope.dispose(); + } + }) + .await(); + } finally { + await ctx.fiber.dispose(); + } + }); +}); diff --git a/packages/core/test/skill-invocation.test.ts b/packages/core/test/skill-invocation.test.ts index 58b214b41..854fff32f 100644 --- a/packages/core/test/skill-invocation.test.ts +++ b/packages/core/test/skill-invocation.test.ts @@ -177,6 +177,7 @@ describe('SkillCatalog', () => { 'lookup failed', ); expect(active).toBe(0); + catalog.invalidateBot('ada'); vi.spyOn(port, 'list').mockRejectedValueOnce(new Error('list failed')); await expect(catalog.list(['ada'])).rejects.toThrow('list failed'); expect(active).toBe(0);