refactor(plugin-inventory): collect startup diagnostics concurrently
This commit is contained in:
1 parent
e979223a76
commit
075e9e4de7
5 files changed
+61
-14
No files matched your search
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
-----
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ Loader 行与预设行可以携带可选的 `meta`,其中包含标题、描述
|
||||
|
||||
该清单是供展示与诊断的快照:客户端可以渲染名单、标出失败条目,并通过比较快照检测变化。它不能启用、停用、添加或移除插件,也不携带历史——已经失败并被移除的 fiber 缺席。由于服务每次调用都读取 Loader,答案总是反映当前组合,而不是缓存视图。
|
||||
|
||||
失败条目包含从 Cordis fiber 读取的当前启动诊断。恢复后移除该诊断;清单查询不等待仍在加载的条目,也不另存错误缓存。
|
||||
失败条目包含从 Cordis fiber 读取的当前启动诊断。条目状态在并发读取诊断之前采集。恢复后移除该诊断;清单查询不等待仍在加载的条目,也不另存错误缓存。
|
||||
|
||||
-----
|
||||
|
||||
|
||||
@@ -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<PluginInventorySnapshot> {
|
||||
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<PluginInventory
|
||||
const meta = base === undefined ? undefined : packages?.metaOf(entry.options.name, base)
|
||||
const fiber = entry.fiber
|
||||
const fiberPhase = fiber === undefined ? null : FIBER_PHASE[fiber.state]
|
||||
let diagnostic: string | undefined
|
||||
if (fiber !== undefined && fiberPhase === 'failed') {
|
||||
try { await fiber.await() } catch (error) { diagnostic = error instanceof Error ? error.message : String(error) }
|
||||
}
|
||||
entries.push({
|
||||
const row: PluginInventoryEntry = {
|
||||
entryId: pluginEntryId(entry.id),
|
||||
moduleName: entry.options.name,
|
||||
enabled: !entry.disabled,
|
||||
fiberPhase,
|
||||
...diagnostic === undefined ? {} : { diagnostic },
|
||||
...meta === undefined ? {} : { meta },
|
||||
})
|
||||
}
|
||||
entries.push(row)
|
||||
if (fiber !== undefined && fiberPhase === 'failed') failed.push({ row, fiber })
|
||||
}
|
||||
const diagnostics = await Promise.all(failed.map(({ row, fiber }) => 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 }
|
||||
|
||||
@@ -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<undefined>(), secondGate = Promise.withResolvers<undefined>()
|
||||
// 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' })
|
||||
|
||||
Reference in new issue
Block a user