diff --git a/packages/host/plugin-inventory/README.i18n.yaml b/packages/host/plugin-inventory/README.i18n.yaml index 436960c853..ac0621fd27 100644 --- a/packages/host/plugin-inventory/README.i18n.yaml +++ b/packages/host/plugin-inventory/README.i18n.yaml @@ -24,8 +24,8 @@ en: dfc89b14d4c0d21e zh: 062b233951ed157b /deepseek-ai-dsh-host-plugin-inventory/use-this-package/what-you-can-and-cannot-do-with-it: - en: 0243819e8404c74a - zh: 05dab926c0ecff34 + en: 196540b2ba8bd620 + zh: 909fd29d010a6c00 /deepseek-ai-dsh-host-plugin-inventory/understand-the-implementation: en: 42b15ac6321706aa zh: 4ed0861911abaf5d diff --git a/packages/host/plugin-inventory/README.md b/packages/host/plugin-inventory/README.md index cd9ce7c0fa..d0b63b574a 100644 --- a/packages/host/plugin-inventory/README.md +++ b/packages/host/plugin-inventory/README.md @@ -41,7 +41,7 @@ With a roster composed, `agentPresets` carries one group per preset in roster or The inventory is a snapshot for display and diagnostics: a client can render the roster, flag failed entries, and detect changes by comparing snapshots. It cannot enable, disable, add, or remove plugins, and it carries no history — a fiber that already failed and was removed is absent. Because the service reads the Loader on every call, the answer always reflects the current composition rather than a cached view. -Failed entries include the current startup diagnostic read from their Cordis fiber. Recovery removes it; inventory reads do not wait for loading entries or retain a separate error cache. +Failed entries include the current startup diagnostic read from their Cordis fiber. Entry states are sampled before diagnostics are read concurrently. Recovery removes the diagnostic; inventory reads do not wait for loading entries or retain a separate error cache. ----- diff --git a/packages/host/plugin-inventory/README.zh.md b/packages/host/plugin-inventory/README.zh.md index 8392526a96..0c892c1523 100644 --- a/packages/host/plugin-inventory/README.zh.md +++ b/packages/host/plugin-inventory/README.zh.md @@ -41,7 +41,7 @@ Loader 行与预设行可以携带可选的 `meta`,其中包含标题、描述 该清单是供展示与诊断的快照:客户端可以渲染名单、标出失败条目,并通过比较快照检测变化。它不能启用、停用、添加或移除插件,也不携带历史——已经失败并被移除的 fiber 缺席。由于服务每次调用都读取 Loader,答案总是反映当前组合,而不是缓存视图。 -失败条目包含从 Cordis fiber 读取的当前启动诊断。恢复后移除该诊断;清单查询不等待仍在加载的条目,也不另存错误缓存。 +失败条目包含从 Cordis fiber 读取的当前启动诊断。条目状态在并发读取诊断之前采集。恢复后移除该诊断;清单查询不等待仍在加载的条目,也不另存错误缓存。 ----- diff --git a/packages/host/plugin-inventory/src/index.ts b/packages/host/plugin-inventory/src/index.ts index a47c673ba9..9112b46e4f 100644 --- a/packages/host/plugin-inventory/src/index.ts +++ b/packages/host/plugin-inventory/src/index.ts @@ -1,6 +1,6 @@ /** Read-only projection of the current Cordis Loader plugin entries. */ -import type { Context, FiberState } from '@deepseek-ai/cordis' +import type { Context, Fiber, FiberState } from '@deepseek-ai/cordis' import type {} from '@deepseek-ai/cordis-plugin-loader' // Type-only: the optional agent-preset roster resolved through `ctx.get`. import type {} from '@deepseek-ai/dsh-agent-preset-registry' @@ -77,10 +77,11 @@ export default PluginInventoryGateway /** Read current Loader entries and optional preset compositions. * @param ctx Context with the Loader service. - * @returns Current inventory with optional display metadata and no separate runtime cache. + * @returns Current inventory with entry states sampled before concurrent diagnostic reads. */ export async function readPluginInventory(ctx: Context): Promise { const entries: PluginInventoryEntry[] = [] + const failed: { row: PluginInventoryEntry; fiber: Fiber }[] = [] const packages = ctx.get('pluginPackages') for (const entry of ctx.loader.entries()) { if (entry.options.group) continue @@ -88,18 +89,22 @@ export async function readPluginInventory(ctx: Context): Promise fiber.await().then( + () => ({ row, diagnostic: undefined }), + (error: unknown) => ({ row, diagnostic: error instanceof Error ? error.message : String(error) }), + ))) + for (const { row, diagnostic } of diagnostics) { + if (diagnostic !== undefined) Object.assign(row, { diagnostic }) } const presets = ctx.get('agentPresets') const management = ctx.get('pluginManager') === undefined ? {} : { managementAvailable: true } diff --git a/packages/host/plugin-inventory/tests/inventory.spec.ts b/packages/host/plugin-inventory/tests/inventory.spec.ts index 6e022ea922..b3d41ba5de 100644 --- a/packages/host/plugin-inventory/tests/inventory.spec.ts +++ b/packages/host/plugin-inventory/tests/inventory.spec.ts @@ -1,4 +1,4 @@ -import { afterEach, describe, expect, it } from 'vitest' +import { afterEach, describe, expect, it, vi } from 'vitest' import { lstatSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' @@ -173,6 +173,48 @@ describe('PluginInventoryGateway', () => { }) }) + it.each([false, true])('reads failed diagnostics concurrently and preserves row order (recovery: %s)', async (recover) => { + const { ctx, inventory } = await harness() + ctx.loader.builtins.failing = { + apply(_ctx: Context, config: { fail: boolean; message: string }) { + if (config.fail) throw new Error(config.message) + }, + } + const firstEntry = ctx.loader.resolve(await ctx.loader.create({ name: 'cordis:failing', config: { fail: true, message: 'first failure' } })) + const activeEntry = ctx.loader.resolve(await ctx.loader.create({ name: 'cordis:active' })) + const secondEntry = ctx.loader.resolve(await ctx.loader.create({ name: 'cordis:failing', config: { fail: true, message: 'second failure' } })) + const first = firstEntry.fiber!, second = secondEntry.fiber! + await expect(first.await()).rejects.toThrow('first failure') + await activeEntry.fiber!.await() + await expect(second.await()).rejects.toThrow('second failure') + const firstAwait = first.await.bind(first), secondAwait = second.await.bind(second) + const firstGate = Promise.withResolvers(), secondGate = Promise.withResolvers() + // Hold completion to exercise overlapping reads without scheduler delays. + const firstRead = vi.spyOn(first, 'await').mockImplementation(() => firstGate.promise.then(firstAwait)) + const secondRead = vi.spyOn(second, 'await').mockImplementation(() => secondGate.promise.then(secondAwait)) + const reading = inventory.list() + try { + expect(firstRead).toHaveBeenCalledOnce() + expect(secondRead).toHaveBeenCalledOnce() + if (recover) { + await secondEntry.update({ config: { fail: false, message: 'second failure' } }) + await secondAwait() + } + secondGate.resolve(undefined) + firstGate.resolve(undefined) + expect((await reading).entries).toEqual([ + { entryId: firstEntry.id, moduleName: 'cordis:failing', enabled: true, fiberPhase: 'failed', diagnostic: 'first failure' }, + { entryId: activeEntry.id, moduleName: 'cordis:active', enabled: true, fiberPhase: 'active' }, + { entryId: secondEntry.id, moduleName: 'cordis:failing', enabled: true, fiberPhase: 'failed', + ...recover ? {} : { diagnostic: 'second failure' } }, + ]) + } finally { + firstGate.resolve(undefined) + secondGate.resolve(undefined) + try { await reading } finally { firstRead.mockRestore(); secondRead.mockRestore() } + } + }) + it('projects current non-group Loader entries without a second cache', async () => { const { ctx, inventory } = await harness() const activeId = await ctx.loader.create({ name: 'cordis:active' })