@allbluecn/dsh-compat 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +24 -2
- package/package.json +34 -3
- package/src/__tests__/package.test.ts +58 -0
- package/src/assemble/__tests__/assemble.test.ts +656 -0
- package/src/assemble/assemble.ts +383 -0
- package/src/assemble/resolve.ts +59 -0
- package/src/compat/__tests__/context.test.ts +211 -0
- package/src/compat/__tests__/events.test.ts +135 -0
- package/src/compat/__tests__/tool-adapter.test.ts +157 -0
- package/src/compat/__tests__/view-bridge.test.ts +166 -0
- package/src/compat/context.ts +496 -0
- package/src/compat/events.ts +189 -0
- package/src/compat/tool-adapter.ts +115 -0
- package/src/compat/view-bridge.ts +160 -0
- package/src/guard/assert-fetchable-url.ts +239 -0
- package/src/index.ts +52 -0
- package/src/patch/__tests__/patch.test.ts +250 -0
- package/src/patch/eval.ts +343 -0
- package/src/patch/merge.ts +45 -0
- package/src/patch/types.ts +37 -0
- package/src/patch/yaml.ts +75 -0
- package/src/plugin-safety/__tests__/build-approval.test.ts +149 -0
- package/src/plugin-safety/__tests__/install-spec.test.ts +148 -0
- package/src/plugin-safety/build-approval.ts +134 -0
- package/src/plugin-safety/index.ts +31 -0
- package/src/plugin-safety/install-spec.ts +189 -0
- package/src/report.ts +47 -0
- package/src/surface.ts +92 -0
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
2
|
+
// assembleProfile 门面测试(规格 §4.1.1 / §4.1.3 / 计划 F1-5)。
|
|
3
|
+
//
|
|
4
|
+
// 用注入式 mock resolver + 合成 CordisPluginModule 覆盖:激活 / 未激活(inject
|
|
5
|
+
// 不满足)/ 失败(apply 抛错,贡献空)/ 停用(disabled + !!js)/ 工具名冲突去重 /
|
|
6
|
+
// surface 版本 + L0 边界断言。
|
|
7
|
+
import { describe, expect, it } from 'vitest'
|
|
8
|
+
import type { HarnessPlugin, PluginSetupContext } from '@tanstack/ai-harness'
|
|
9
|
+
import { definePlugin } from '@tanstack/ai-harness'
|
|
10
|
+
import { assembleProfile } from '../assemble'
|
|
11
|
+
import type { BuiltinFactory } from '../resolve'
|
|
12
|
+
import type { CordisContext, CordisPluginModule } from '../../compat/context'
|
|
13
|
+
import type { PatchLine } from '../../patch/types'
|
|
14
|
+
import type { CordisModuleResolver } from '../resolve'
|
|
15
|
+
import {
|
|
16
|
+
DSH_COMPAT_SURFACE,
|
|
17
|
+
assertL0Surface,
|
|
18
|
+
} from '../../surface'
|
|
19
|
+
|
|
20
|
+
/** 造一个最小官方 setup ctx(runCordisPlugin 只用 resources / logger)。 */
|
|
21
|
+
function fakeOfficial(): PluginSetupContext {
|
|
22
|
+
return {
|
|
23
|
+
resources: {
|
|
24
|
+
acquire: async <T,>(
|
|
25
|
+
open: () => T | Promise<T>,
|
|
26
|
+
_close: (r: T) => unknown,
|
|
27
|
+
) => open(),
|
|
28
|
+
signal: new AbortController().signal,
|
|
29
|
+
},
|
|
30
|
+
logger: { error: () => {}, warn: () => {}, info: () => {}, debug: () => {} },
|
|
31
|
+
} as unknown as PluginSetupContext
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 造一个 DSH 插件模块,apply 里可注册工具 / 抛错。 */
|
|
35
|
+
function mod(
|
|
36
|
+
name: string,
|
|
37
|
+
body: (ctx: CordisContext) => void,
|
|
38
|
+
extra: { inject?: ReadonlyArray<string> } = {},
|
|
39
|
+
): CordisPluginModule {
|
|
40
|
+
return { name, ...extra, apply: (ctx) => body(ctx) }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 工具注册 helper:apply 里注册一个 execute 为 no-op 的工具。 */
|
|
44
|
+
function registerTool(ctx: CordisContext, name: string): void {
|
|
45
|
+
ctx.tools.register({ name, description: `tool ${name}`, execute: () => ({}) })
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 单层 patch:把行数组包成一层。 */
|
|
49
|
+
function layer(lines: PatchLine[]): PatchLine[] {
|
|
50
|
+
return lines
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 断言某插件(按 name)setup 回放的 tools 名单。 */
|
|
54
|
+
async function toolsOf(
|
|
55
|
+
plugins: readonly HarnessPlugin[],
|
|
56
|
+
name: string,
|
|
57
|
+
): Promise<string[]> {
|
|
58
|
+
const plugin = plugins.find((p) => p.name === name)
|
|
59
|
+
expect(plugin, `plugin ${name} 应存在`).toBeDefined()
|
|
60
|
+
// assemble 产出的是 session 插件(setup 参数是 PluginSetupContext,非 HostPluginContext);
|
|
61
|
+
// HarnessPlugin 的 setup 参数类型是 session|host 联合,这里按 session 分支调用。
|
|
62
|
+
const setup = plugin?.setup as
|
|
63
|
+
| ((ctx: PluginSetupContext) => Promise<unknown> | unknown)
|
|
64
|
+
| undefined
|
|
65
|
+
const contributions = (await setup?.(fakeOfficial())) as Record<
|
|
66
|
+
string,
|
|
67
|
+
unknown
|
|
68
|
+
>
|
|
69
|
+
return ((contributions?.tools as Array<{ name: string }>) ?? []).map(
|
|
70
|
+
(t) => t.name,
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
describe('assembleProfile — 激活三态 + 停用 + 冲突去重', () => {
|
|
75
|
+
it('① 激活:inject 满足 → plugins 含该插件 + report.activated', async () => {
|
|
76
|
+
const resolver: CordisModuleResolver = async (name) =>
|
|
77
|
+
name === 'a' ? mod('a', (ctx) => registerTool(ctx, 'toolA')) : undefined
|
|
78
|
+
const { plugins, report } = await assembleProfile({
|
|
79
|
+
layers: [layer([{ id: 'a', name: 'a' }])],
|
|
80
|
+
resolver,
|
|
81
|
+
providedServices: { db: 'DB_HANDLE' },
|
|
82
|
+
})
|
|
83
|
+
expect(report.activated).toEqual([{ id: 'a', name: 'a' }])
|
|
84
|
+
expect(plugins).toHaveLength(1)
|
|
85
|
+
expect(plugins[0]?.name).toBe('a')
|
|
86
|
+
expect(await toolsOf(plugins, 'a')).toEqual(['toolA'])
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('② 未激活(inject 不满足)→ report.deactivated + missing 键 + plugins 不含', async () => {
|
|
90
|
+
const resolver: CordisModuleResolver = async () =>
|
|
91
|
+
mod('needs', () => {}, { inject: ['db', 'secret'] })
|
|
92
|
+
const { plugins, report } = await assembleProfile({
|
|
93
|
+
layers: [layer([{ id: 'n', name: 'needs' }])],
|
|
94
|
+
resolver,
|
|
95
|
+
providedServices: { db: 'DB_HANDLE' },
|
|
96
|
+
})
|
|
97
|
+
expect(report.deactivated).toEqual([
|
|
98
|
+
{ id: 'n', name: 'needs', missing: ['secret'] },
|
|
99
|
+
])
|
|
100
|
+
expect(plugins).toHaveLength(0)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('③ 失败(apply 抛错)→ report.failed + error + 贡献空 + 其余插件照常激活', async () => {
|
|
104
|
+
const resolver: CordisModuleResolver = async (name) => {
|
|
105
|
+
if (name === 'boom') {
|
|
106
|
+
return mod('boom', () => {
|
|
107
|
+
throw new Error('apply exploded')
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
return mod('ok', (ctx) => registerTool(ctx, 'toolOk'))
|
|
111
|
+
}
|
|
112
|
+
const { plugins, report } = await assembleProfile({
|
|
113
|
+
layers: [
|
|
114
|
+
layer([
|
|
115
|
+
{ id: 'boom', name: 'boom' },
|
|
116
|
+
{ id: 'ok', name: 'ok' },
|
|
117
|
+
]),
|
|
118
|
+
],
|
|
119
|
+
resolver,
|
|
120
|
+
})
|
|
121
|
+
expect(report.failed).toEqual([
|
|
122
|
+
{ id: 'boom', name: 'boom', error: 'apply exploded' },
|
|
123
|
+
])
|
|
124
|
+
expect(report.activated).toEqual([{ id: 'ok', name: 'ok' }])
|
|
125
|
+
// failed 插件贡献空(F1-4 I-1)——assemble 只为 activated 条目产出 HarnessPlugin,
|
|
126
|
+
// 故 failed 插件不进 plugins(等价于「贡献空」),且不拖垮 ok。
|
|
127
|
+
expect(plugins.find((p) => p.name === 'boom')).toBeUndefined()
|
|
128
|
+
expect(await toolsOf(plugins, 'ok')).toEqual(['toolOk'])
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('④ 停用(disabled: true)→ report.disabled + plugins 不含', async () => {
|
|
132
|
+
const resolver: CordisModuleResolver = async () =>
|
|
133
|
+
mod('off', (ctx) => registerTool(ctx, 'toolOff'))
|
|
134
|
+
const { plugins, report } = await assembleProfile({
|
|
135
|
+
layers: [layer([{ id: 'off', name: 'off', disabled: true }])],
|
|
136
|
+
resolver,
|
|
137
|
+
})
|
|
138
|
+
expect(report.disabled).toEqual([{ id: 'off' }])
|
|
139
|
+
expect(plugins).toHaveLength(0)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it('⑤ 工具名冲突:后到同名工具被让位 + report.toolConflicts + 两插件都激活 + 最终工具名无重复', async () => {
|
|
143
|
+
const resolver: CordisModuleResolver = async (name) => {
|
|
144
|
+
if (name === 'high') {
|
|
145
|
+
return mod('high', (ctx) => {
|
|
146
|
+
registerTool(ctx, 'shared')
|
|
147
|
+
registerTool(ctx, 'onlyHigh')
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
return mod('low', (ctx) => {
|
|
151
|
+
registerTool(ctx, 'shared')
|
|
152
|
+
registerTool(ctx, 'onlyLow')
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
const { plugins, report } = await assembleProfile({
|
|
156
|
+
// high 先出现 = 高优先层;low 后到,shared 让位。
|
|
157
|
+
layers: [
|
|
158
|
+
layer([
|
|
159
|
+
{ id: 'high', name: 'high' },
|
|
160
|
+
{ id: 'low', name: 'low' },
|
|
161
|
+
]),
|
|
162
|
+
],
|
|
163
|
+
resolver,
|
|
164
|
+
})
|
|
165
|
+
expect(report.activated).toEqual([
|
|
166
|
+
{ id: 'high', name: 'high' },
|
|
167
|
+
{ id: 'low', name: 'low' },
|
|
168
|
+
])
|
|
169
|
+
expect(report.toolConflicts).toEqual([
|
|
170
|
+
{ name: 'shared', owner: 'low', yieldedTo: 'high' },
|
|
171
|
+
])
|
|
172
|
+
// 先出现者保留,后到同名丢弃;其余贡献保留。
|
|
173
|
+
expect(await toolsOf(plugins, 'high')).toEqual(['shared', 'onlyHigh'])
|
|
174
|
+
expect(await toolsOf(plugins, 'low')).toEqual(['onlyLow'])
|
|
175
|
+
// 硬锁定:最终 plugins 里任意两插件工具名不重复。
|
|
176
|
+
const allNames = (
|
|
177
|
+
await Promise.all(plugins.map(async (p) => toolsOf(plugins, p.name)))
|
|
178
|
+
).flat()
|
|
179
|
+
expect(new Set(allNames).size).toBe(allNames.length)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it('⑥ disabled eval(!!js):表达式 truthy → 停用;falsy → 激活', async () => {
|
|
183
|
+
const resolver: CordisModuleResolver = async (name) =>
|
|
184
|
+
mod(name, (ctx) => registerTool(ctx, `tool_${name}`))
|
|
185
|
+
// 表达式 truthy(flag 为 false → !flag = true)→ 停用。
|
|
186
|
+
const off = await assembleProfile({
|
|
187
|
+
layers: [
|
|
188
|
+
layer([
|
|
189
|
+
{
|
|
190
|
+
id: 'gated',
|
|
191
|
+
name: 'gated',
|
|
192
|
+
disabled: "!ctx.get('flag')",
|
|
193
|
+
},
|
|
194
|
+
]),
|
|
195
|
+
],
|
|
196
|
+
resolver,
|
|
197
|
+
config: { flag: false },
|
|
198
|
+
})
|
|
199
|
+
expect(off.report.disabled).toEqual([{ id: 'gated' }])
|
|
200
|
+
expect(off.plugins).toHaveLength(0)
|
|
201
|
+
|
|
202
|
+
// 表达式 falsy(flag 为 true → !flag = false)→ 激活。
|
|
203
|
+
const on = await assembleProfile({
|
|
204
|
+
layers: [
|
|
205
|
+
layer([
|
|
206
|
+
{
|
|
207
|
+
id: 'gated',
|
|
208
|
+
name: 'gated',
|
|
209
|
+
disabled: "!ctx.get('flag')",
|
|
210
|
+
},
|
|
211
|
+
]),
|
|
212
|
+
],
|
|
213
|
+
resolver,
|
|
214
|
+
config: { flag: true },
|
|
215
|
+
})
|
|
216
|
+
expect(on.report.disabled).toEqual([])
|
|
217
|
+
expect(on.report.activated).toEqual([{ id: 'gated', name: 'gated' }])
|
|
218
|
+
expect(on.plugins).toHaveLength(1)
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
describe('assembleProfile — unresolved / deactivated(resolver 未命中)', () => {
|
|
223
|
+
it('无 name 的行 → report.unresolved', async () => {
|
|
224
|
+
const { plugins, report } = await assembleProfile({
|
|
225
|
+
layers: [layer([{ id: 'anon' }])],
|
|
226
|
+
resolver: async () => undefined,
|
|
227
|
+
})
|
|
228
|
+
expect(report.unresolved).toEqual([{ id: 'anon' }])
|
|
229
|
+
expect(plugins).toHaveLength(0)
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
it('resolver 未命中(返回 undefined)→ report.deactivated(missing 空)', async () => {
|
|
233
|
+
const { report } = await assembleProfile({
|
|
234
|
+
layers: [layer([{ id: 'x', name: 'missing-pkg' }])],
|
|
235
|
+
resolver: async () => undefined,
|
|
236
|
+
})
|
|
237
|
+
expect(report.deactivated).toEqual([
|
|
238
|
+
{ id: 'x', name: 'missing-pkg', missing: [] },
|
|
239
|
+
])
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
it('resolver 抛错 → report.failed(error 含 resolver threw)', async () => {
|
|
243
|
+
const { report } = await assembleProfile({
|
|
244
|
+
layers: [layer([{ id: 'x', name: 'bad-pkg' }])],
|
|
245
|
+
resolver: async () => {
|
|
246
|
+
throw new Error('import failed')
|
|
247
|
+
},
|
|
248
|
+
})
|
|
249
|
+
expect(report.failed[0]?.id).toBe('x')
|
|
250
|
+
expect(report.failed[0]?.error).toContain('resolver threw')
|
|
251
|
+
expect(report.failed[0]?.error).toContain('import failed')
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
describe('surface — 版本化兼容面 + L0 边界', () => {
|
|
256
|
+
it('report.surfaceVersion = DSH_COMPAT_SURFACE.version', async () => {
|
|
257
|
+
const { report } = await assembleProfile({ layers: [layer([])] })
|
|
258
|
+
expect(report.surfaceVersion).toBe(DSH_COMPAT_SURFACE.version)
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
it('assertL0Surface:DSH cordis 组装产物(无 agent 贡献)通过', async () => {
|
|
262
|
+
const resolver: CordisModuleResolver = async () =>
|
|
263
|
+
mod('toolonly', (ctx) => registerTool(ctx, 't'))
|
|
264
|
+
const { plugins } = await assembleProfile({
|
|
265
|
+
layers: [layer([{ id: 't', name: 'toolonly' }])],
|
|
266
|
+
resolver,
|
|
267
|
+
})
|
|
268
|
+
expect(() => assertL0Surface(plugins)).not.toThrow()
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
it('assertL0Surface:含 agents / subagents 贡献 → 抛错(越 L0 边界)', () => {
|
|
272
|
+
expect(() =>
|
|
273
|
+
assertL0Surface([
|
|
274
|
+
{ name: 'agentful', contributions: { agents: [{}] } },
|
|
275
|
+
]),
|
|
276
|
+
).toThrow(/L0 边界越界.*agents/)
|
|
277
|
+
expect(() =>
|
|
278
|
+
assertL0Surface([
|
|
279
|
+
{ name: 'sub', contributions: { subagents: [{}] } },
|
|
280
|
+
]),
|
|
281
|
+
).toThrow(/L0 边界越界.*subagents/)
|
|
282
|
+
})
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
/** 造一个内置静态工厂 mock:create 吃 config 返回真实 HarnessPlugin(经 definePlugin,暴露 .name)。 */
|
|
286
|
+
function builtinFactory(
|
|
287
|
+
pluginName: string,
|
|
288
|
+
toolNames: readonly string[],
|
|
289
|
+
capturedConfigs: unknown[] = [],
|
|
290
|
+
): BuiltinFactory {
|
|
291
|
+
return {
|
|
292
|
+
kind: 'builtin',
|
|
293
|
+
create: (config: unknown) => {
|
|
294
|
+
capturedConfigs.push(config)
|
|
295
|
+
return definePlugin({
|
|
296
|
+
name: pluginName,
|
|
297
|
+
lifetime: 'session',
|
|
298
|
+
setup: () => ({
|
|
299
|
+
tools: toolNames.map((tn) => ({
|
|
300
|
+
name: tn,
|
|
301
|
+
description: `builtin ${tn}`,
|
|
302
|
+
execute: () => ({}),
|
|
303
|
+
})),
|
|
304
|
+
}),
|
|
305
|
+
})
|
|
306
|
+
},
|
|
307
|
+
toolNames,
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* 造一个带 `toolNamesFor(config)` 的内置工厂 mock(G2:toolNames 按 config 过滤)。
|
|
313
|
+
*
|
|
314
|
+
* `toolNamesFor` 据 `config.disabledTools` 返回过滤后子集,模拟真实 `@allblue/*`
|
|
315
|
+
* 工厂尊重 `disabledTools` 的行为(被禁用工具不该占据去重池)。
|
|
316
|
+
*/
|
|
317
|
+
function builtinFactoryWithToolNamesFor(
|
|
318
|
+
pluginName: string,
|
|
319
|
+
allNames: readonly string[],
|
|
320
|
+
): BuiltinFactory {
|
|
321
|
+
return {
|
|
322
|
+
kind: 'builtin',
|
|
323
|
+
create: () =>
|
|
324
|
+
definePlugin({
|
|
325
|
+
name: pluginName,
|
|
326
|
+
lifetime: 'session',
|
|
327
|
+
setup: () => ({
|
|
328
|
+
tools: allNames.map((tn) => ({
|
|
329
|
+
name: tn,
|
|
330
|
+
description: `builtin ${tn}`,
|
|
331
|
+
execute: () => ({}),
|
|
332
|
+
})),
|
|
333
|
+
}),
|
|
334
|
+
}),
|
|
335
|
+
toolNames: allNames,
|
|
336
|
+
toolNamesFor: (config: unknown) => {
|
|
337
|
+
const disabledSet = new Set(
|
|
338
|
+
(config as { disabledTools?: readonly string[] })?.disabledTools ?? [],
|
|
339
|
+
)
|
|
340
|
+
return allNames.filter((name) => !disabledSet.has(name))
|
|
341
|
+
},
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
describe('assembleProfile — 内置静态工厂分支(F2-2 resolver union)', () => {
|
|
346
|
+
it('内置激活:resolver 返回 {kind:"builtin"} → create(config) 直挂 plugins + report.activated', async () => {
|
|
347
|
+
const { plugins, report } = await assembleProfile({
|
|
348
|
+
layers: [layer([{ id: 'story', name: '@allblue/story' }])],
|
|
349
|
+
resolver: async (name) =>
|
|
350
|
+
name === '@allblue/story'
|
|
351
|
+
? builtinFactory('allblue/story', ['story_create', 'story_search'])
|
|
352
|
+
: undefined,
|
|
353
|
+
})
|
|
354
|
+
expect(report.activated).toEqual([{ id: 'story', name: 'allblue/story' }])
|
|
355
|
+
expect(plugins).toHaveLength(1)
|
|
356
|
+
expect(plugins[0]?.name).toBe('allblue/story')
|
|
357
|
+
expect(await toolsOf(plugins, 'allblue/story')).toEqual([
|
|
358
|
+
'story_create',
|
|
359
|
+
'story_search',
|
|
360
|
+
])
|
|
361
|
+
// 内置不重建:直挂 create 产出的 plugin,setup 回放保留工具名。
|
|
362
|
+
expect(await toolsOf(plugins, 'allblue/story')).toHaveLength(2)
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
it('向后兼容:裸 cordis 模块(无 kind)仍走 runCordisPlugin 路径', async () => {
|
|
366
|
+
const resolver: CordisModuleResolver = async (name) =>
|
|
367
|
+
name === 'a' ? mod('a', (ctx) => registerTool(ctx, 'toolA')) : undefined
|
|
368
|
+
const { plugins, report } = await assembleProfile({
|
|
369
|
+
layers: [layer([{ id: 'a', name: 'a' }])],
|
|
370
|
+
resolver,
|
|
371
|
+
})
|
|
372
|
+
expect(report.activated).toEqual([{ id: 'a', name: 'a' }])
|
|
373
|
+
expect(plugins).toHaveLength(1)
|
|
374
|
+
expect(plugins[0]?.name).toBe('a')
|
|
375
|
+
expect(await toolsOf(plugins, 'a')).toEqual(['toolA'])
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
it('跨源工具名去重:内置(base 层)先入池,cordis(user 层)撞名让位 + 记冲突', async () => {
|
|
379
|
+
const resolver: CordisModuleResolver = async (name) => {
|
|
380
|
+
if (name === '@allblue/story') {
|
|
381
|
+
return builtinFactory('allblue/story', ['shared', 'builtinOnly'])
|
|
382
|
+
}
|
|
383
|
+
if (name === 'dsh') {
|
|
384
|
+
return mod('dsh', (ctx) => {
|
|
385
|
+
registerTool(ctx, 'shared')
|
|
386
|
+
registerTool(ctx, 'dshOnly')
|
|
387
|
+
})
|
|
388
|
+
}
|
|
389
|
+
return undefined
|
|
390
|
+
}
|
|
391
|
+
const { plugins, report } = await assembleProfile({
|
|
392
|
+
layers: [
|
|
393
|
+
layer([
|
|
394
|
+
{ id: 'story', name: '@allblue/story' },
|
|
395
|
+
{ id: 'dsh', name: 'dsh' },
|
|
396
|
+
]),
|
|
397
|
+
],
|
|
398
|
+
resolver,
|
|
399
|
+
})
|
|
400
|
+
// 内置先激活(base 层)+ cordis 后激活(user 层)。
|
|
401
|
+
expect(report.activated).toEqual([
|
|
402
|
+
{ id: 'story', name: 'allblue/story' },
|
|
403
|
+
{ id: 'dsh', name: 'dsh' },
|
|
404
|
+
])
|
|
405
|
+
// cordis 的 shared 撞内置(base 层)→ 让位 + 记冲突(owner=被让位 dsh,yieldedTo=内置)。
|
|
406
|
+
expect(report.toolConflicts).toEqual([
|
|
407
|
+
{ name: 'shared', owner: 'dsh', yieldedTo: 'allblue/story' },
|
|
408
|
+
])
|
|
409
|
+
// 内置保留 shared + 自有工具;cordis 丢 shared,保留 dshOnly。
|
|
410
|
+
expect(await toolsOf(plugins, 'allblue/story')).toEqual([
|
|
411
|
+
'shared',
|
|
412
|
+
'builtinOnly',
|
|
413
|
+
])
|
|
414
|
+
expect(await toolsOf(plugins, 'dsh')).toEqual(['dshOnly'])
|
|
415
|
+
// 硬锁定:最终 plugins 里任意两插件工具名不重复。
|
|
416
|
+
const allNames = (
|
|
417
|
+
await Promise.all(plugins.map(async (p) => toolsOf(plugins, p.name)))
|
|
418
|
+
).flat()
|
|
419
|
+
expect(new Set(allNames).size).toBe(allNames.length)
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
it('disabled 内置:patch 行 disabled:true → report.disabled + plugins 不含', async () => {
|
|
423
|
+
const { plugins, report } = await assembleProfile({
|
|
424
|
+
layers: [
|
|
425
|
+
layer([
|
|
426
|
+
{
|
|
427
|
+
id: 'story',
|
|
428
|
+
name: '@allblue/story',
|
|
429
|
+
disabled: true,
|
|
430
|
+
},
|
|
431
|
+
]),
|
|
432
|
+
],
|
|
433
|
+
resolver: async () => builtinFactory('allblue/story', ['story_create']),
|
|
434
|
+
})
|
|
435
|
+
expect(report.disabled).toEqual([{ id: 'story' }])
|
|
436
|
+
expect(report.activated).toEqual([])
|
|
437
|
+
expect(plugins).toHaveLength(0)
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
it('config 传递:line.config 原样传给 create', async () => {
|
|
441
|
+
const captured: unknown[] = []
|
|
442
|
+
const cfg = { disabledTools: ['story_delete'], scope: 'team' }
|
|
443
|
+
await assembleProfile({
|
|
444
|
+
layers: [
|
|
445
|
+
layer([
|
|
446
|
+
{
|
|
447
|
+
id: 'story',
|
|
448
|
+
name: '@allblue/story',
|
|
449
|
+
config: cfg,
|
|
450
|
+
},
|
|
451
|
+
]),
|
|
452
|
+
],
|
|
453
|
+
resolver: async () =>
|
|
454
|
+
builtinFactory('allblue/story', ['story_create'], captured),
|
|
455
|
+
})
|
|
456
|
+
expect(captured).toHaveLength(1)
|
|
457
|
+
expect(captured[0]).toEqual(cfg)
|
|
458
|
+
})
|
|
459
|
+
|
|
460
|
+
it('config 缺省:line.config 未设 → create 收到 {}(非 undefined)', async () => {
|
|
461
|
+
const captured: unknown[] = []
|
|
462
|
+
await assembleProfile({
|
|
463
|
+
layers: [layer([{ id: 'story', name: '@allblue/story' }])],
|
|
464
|
+
resolver: async () =>
|
|
465
|
+
builtinFactory('allblue/story', ['story_create'], captured),
|
|
466
|
+
})
|
|
467
|
+
expect(captured[0]).toEqual({})
|
|
468
|
+
})
|
|
469
|
+
|
|
470
|
+
it('toolNamesFor 优先:工厂覆写 toolNamesFor(config) → 去重池用过滤后子集,DSH 撞已禁用内置工具名不被让位(G2)', async () => {
|
|
471
|
+
const resolver: CordisModuleResolver = async (name) => {
|
|
472
|
+
if (name === '@allblue/story') {
|
|
473
|
+
// 全量 toolNames 含 story_get,但工厂 toolNamesFor 尊重 config.disabledTools
|
|
474
|
+
// 过滤掉 story_get → 去重池不含 story_get。
|
|
475
|
+
return builtinFactoryWithToolNamesFor('allblue/story', [
|
|
476
|
+
'story_get',
|
|
477
|
+
'story_create',
|
|
478
|
+
])
|
|
479
|
+
}
|
|
480
|
+
if (name === 'dsh') {
|
|
481
|
+
return mod('dsh', (ctx) => {
|
|
482
|
+
// DSH 提供 story_get(撞内置全量名,但该内置工具已被 disabledTools 禁用)
|
|
483
|
+
// + dshOnly。G2 修复后 story_get 不在去重池 → DSH 保留它,不误丢。
|
|
484
|
+
registerTool(ctx, 'story_get')
|
|
485
|
+
registerTool(ctx, 'dshOnly')
|
|
486
|
+
})
|
|
487
|
+
}
|
|
488
|
+
return undefined
|
|
489
|
+
}
|
|
490
|
+
const { plugins, report } = await assembleProfile({
|
|
491
|
+
layers: [
|
|
492
|
+
layer([
|
|
493
|
+
{
|
|
494
|
+
id: 'story',
|
|
495
|
+
name: '@allblue/story',
|
|
496
|
+
config: { disabledTools: ['story_get'] },
|
|
497
|
+
},
|
|
498
|
+
{ id: 'dsh', name: 'dsh' },
|
|
499
|
+
]),
|
|
500
|
+
],
|
|
501
|
+
resolver,
|
|
502
|
+
})
|
|
503
|
+
// 无冲突:story_get 不在去重池,DSH 不让位。
|
|
504
|
+
expect(report.toolConflicts).toEqual([])
|
|
505
|
+
// DSH 保留 story_get + dshOnly。
|
|
506
|
+
expect(await toolsOf(plugins, 'dsh')).toEqual(['story_get', 'dshOnly'])
|
|
507
|
+
// 内置产出面仍由 create 的 setup 决定(本 mock 未过滤 setup,仅验证去重池路径;
|
|
508
|
+
// 真实工厂 create+toolNamesFor 同源过滤,见 apps/web 侧 assemble-disable.test.ts)。
|
|
509
|
+
})
|
|
510
|
+
|
|
511
|
+
it('toolNamesFor 缺省回落:工厂未覆写 toolNamesFor → 仍用静态 toolNames(既有行为不变)', async () => {
|
|
512
|
+
// builtinFactory 助手不带 toolNamesFor → assembleProfile 应回落 resolved.toolNames,
|
|
513
|
+
// 去重池含全量名,DSH 撞名仍让位(回归守护,确保向后兼容未被破坏)。
|
|
514
|
+
const resolver: CordisModuleResolver = async (name) => {
|
|
515
|
+
if (name === '@allblue/story') {
|
|
516
|
+
return builtinFactory('allblue/story', ['story_get', 'story_create'])
|
|
517
|
+
}
|
|
518
|
+
if (name === 'dsh') {
|
|
519
|
+
return mod('dsh', (ctx) => registerTool(ctx, 'story_get'))
|
|
520
|
+
}
|
|
521
|
+
return undefined
|
|
522
|
+
}
|
|
523
|
+
const { plugins, report } = await assembleProfile({
|
|
524
|
+
layers: [
|
|
525
|
+
layer([
|
|
526
|
+
{ id: 'story', name: '@allblue/story' },
|
|
527
|
+
{ id: 'dsh', name: 'dsh' },
|
|
528
|
+
]),
|
|
529
|
+
],
|
|
530
|
+
resolver,
|
|
531
|
+
})
|
|
532
|
+
// 缺省回落:story_get 在去重池 → DSH 让位 + 记冲突。
|
|
533
|
+
expect(report.toolConflicts).toEqual([
|
|
534
|
+
{ name: 'story_get', owner: 'dsh', yieldedTo: 'allblue/story' },
|
|
535
|
+
])
|
|
536
|
+
expect(await toolsOf(plugins, 'dsh')).toEqual([])
|
|
537
|
+
})
|
|
538
|
+
})
|
|
539
|
+
|
|
540
|
+
describe('assembleProfile — effect/session 生命周期(setup 重跑 apply)', () => {
|
|
541
|
+
/**
|
|
542
|
+
* 带 acquire 跟踪的官方 setup ctx(区别于 fakeOfficial 的 no-op acquire)——记录每次
|
|
543
|
+
* acquire 的 open 结果 + close 注册,供断言「真实 session open 期 effect 资源经此
|
|
544
|
+
* acquire 打开」。
|
|
545
|
+
*/
|
|
546
|
+
function trackingOfficial(): PluginSetupContext & {
|
|
547
|
+
acquired: unknown[]
|
|
548
|
+
disposed: unknown[]
|
|
549
|
+
} {
|
|
550
|
+
const acquired: unknown[] = []
|
|
551
|
+
const disposed: unknown[] = []
|
|
552
|
+
const ctx = {
|
|
553
|
+
acquired,
|
|
554
|
+
disposed,
|
|
555
|
+
resources: {
|
|
556
|
+
acquire: async <T,>(
|
|
557
|
+
open: () => T | Promise<T>,
|
|
558
|
+
close: (r: T) => unknown,
|
|
559
|
+
) => {
|
|
560
|
+
const resource = await open()
|
|
561
|
+
acquired.push(resource)
|
|
562
|
+
// 记录 dispose(真实 host 在 session close 逆序调;这里只验证 acquire 跟踪)。
|
|
563
|
+
void close
|
|
564
|
+
disposed.push(close)
|
|
565
|
+
return resource
|
|
566
|
+
},
|
|
567
|
+
signal: new AbortController().signal,
|
|
568
|
+
},
|
|
569
|
+
logger: {
|
|
570
|
+
error: () => {},
|
|
571
|
+
warn: () => {},
|
|
572
|
+
info: () => {},
|
|
573
|
+
debug: () => {},
|
|
574
|
+
},
|
|
575
|
+
}
|
|
576
|
+
return ctx as unknown as PluginSetupContext & {
|
|
577
|
+
acquired: unknown[]
|
|
578
|
+
disposed: unknown[]
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
it('effect 资源在 setup 重跑(真实 session ctx)时 acquire——非 assemble 期 mock acquire', async () => {
|
|
583
|
+
// DSH 插件 apply 里用 ctx.effect(dispose)(open) 打开一个资源。
|
|
584
|
+
const resolver: CordisModuleResolver = async (name) =>
|
|
585
|
+
name === 'eff'
|
|
586
|
+
? mod('eff', (ctx) => {
|
|
587
|
+
void ctx.effect((r: string) => `disposed:${r}`)(() => 'RESOURCE')
|
|
588
|
+
})
|
|
589
|
+
: undefined
|
|
590
|
+
const { plugins, report } = await assembleProfile({
|
|
591
|
+
layers: [layer([{ id: 'eff', name: 'eff' }])],
|
|
592
|
+
resolver,
|
|
593
|
+
})
|
|
594
|
+
expect(report.activated).toEqual([{ id: 'eff', name: 'eff' }])
|
|
595
|
+
// assemble 期预跑用 mock acquire(assembleOfficial 的 no-op,不跟踪)——不在此断言。
|
|
596
|
+
// setup 重跑(真实 session ctx = trackingOfficial)→ effect 资源经真实 acquire 打开。
|
|
597
|
+
const sessionCtx = trackingOfficial()
|
|
598
|
+
const plugin = plugins.find((p) => p.name === 'eff')
|
|
599
|
+
const setup = plugin?.setup as
|
|
600
|
+
| ((ctx: PluginSetupContext) => Promise<unknown>)
|
|
601
|
+
| undefined
|
|
602
|
+
await setup?.(sessionCtx)
|
|
603
|
+
expect(sessionCtx.acquired).toEqual(['RESOURCE'])
|
|
604
|
+
expect(sessionCtx.disposed).toHaveLength(1)
|
|
605
|
+
})
|
|
606
|
+
|
|
607
|
+
it('setup 重跑按预去重结果过滤工具——撞名让位者在重跑时仍被丢弃(不触发官方同名 throw)', async () => {
|
|
608
|
+
const resolver: CordisModuleResolver = async (name) => {
|
|
609
|
+
if (name === 'high') {
|
|
610
|
+
return mod('high', (ctx) => registerTool(ctx, 'shared'))
|
|
611
|
+
}
|
|
612
|
+
return mod('low', (ctx) => {
|
|
613
|
+
registerTool(ctx, 'shared')
|
|
614
|
+
registerTool(ctx, 'onlyLow')
|
|
615
|
+
})
|
|
616
|
+
}
|
|
617
|
+
const { plugins } = await assembleProfile({
|
|
618
|
+
layers: [
|
|
619
|
+
layer([
|
|
620
|
+
{ id: 'high', name: 'high' },
|
|
621
|
+
{ id: 'low', name: 'low' },
|
|
622
|
+
]),
|
|
623
|
+
],
|
|
624
|
+
resolver,
|
|
625
|
+
})
|
|
626
|
+
// setup 重跑后:low 的 shared 仍被丢弃(预去重让位),只保留 onlyLow。
|
|
627
|
+
expect(await toolsOf(plugins, 'low')).toEqual(['onlyLow'])
|
|
628
|
+
expect(await toolsOf(plugins, 'high')).toEqual(['shared'])
|
|
629
|
+
})
|
|
630
|
+
|
|
631
|
+
it('setup 重跑 apply 抛错 → 空 contributions(failed 插件不贡献,与预跑一致)', async () => {
|
|
632
|
+
// 插件 apply 里登记一个「第二次 apply 才抛错」的状态——模拟 setup 重跑期失败。
|
|
633
|
+
let calls = 0
|
|
634
|
+
const resolver: CordisModuleResolver = async (name) =>
|
|
635
|
+
name === 'flaky'
|
|
636
|
+
? mod('flaky', (ctx) => {
|
|
637
|
+
calls += 1
|
|
638
|
+
registerTool(ctx, `tool_${calls}`)
|
|
639
|
+
if (calls >= 2) throw new Error('setup rerun exploded')
|
|
640
|
+
})
|
|
641
|
+
: undefined
|
|
642
|
+
const { plugins } = await assembleProfile({
|
|
643
|
+
layers: [layer([{ id: 'flaky', name: 'flaky' }])],
|
|
644
|
+
resolver,
|
|
645
|
+
})
|
|
646
|
+
// 预跑(calls=1)成功激活;setup 重跑(calls=2)抛错 → 空 contributions。
|
|
647
|
+
const plugin = plugins.find((p) => p.name === 'flaky')
|
|
648
|
+
const setup = plugin?.setup as
|
|
649
|
+
| ((ctx: PluginSetupContext) => Promise<unknown>)
|
|
650
|
+
| undefined
|
|
651
|
+
const contributions = (await setup?.(fakeOfficial())) as {
|
|
652
|
+
tools?: Array<{ name: string }>
|
|
653
|
+
}
|
|
654
|
+
expect(contributions.tools ?? []).toEqual([])
|
|
655
|
+
})
|
|
656
|
+
})
|