@cheemsup/dsh-plugin-console 0.1.1 → 0.2.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.
@@ -0,0 +1,651 @@
1
+ /**
2
+ * 插件禁用效果预演:一个插件被禁用后,谁失去什么。
3
+ *
4
+ * 与 `impact.js` 的关系:那边的结果给「控制」区做一屏确认,只覆盖目标行自己
5
+ * 注册的工具/段与直接供需;这里要回答整页问题,所以补四件事:
6
+ * 1. **分组级联**:禁用 group 行时,按 Loader id 前缀把子行一起算进来;
7
+ * 2. **完整依赖链**:一个插件的服务没了,消费者会停在 pending;消费者自己也提供
8
+ * 服务,它的消费者接着不可用——按「所需服务的提供者全部不可用」迭代到不动点,
9
+ * 而不是只看上下相邻两层;
10
+ * 3. **id 归一**:服务供需给的是 `entryId`,工具/段 owner 给的是 `rowId`,
11
+ * 输出前统一成 `{entryId,rowId,title}`;
12
+ * 4. **不把未知当失去**:注册表里有、owner 扫不到的名字进 `unknown` 桶;名字扫得到、
13
+ * 全局注册表没有的(会话内/条件注册)进独立的 `registryMissing` 桶;两者都不并入
14
+ * `lost`。消费者自己也在不可用集合里时标 `willBeUnavailable`,不说它「在等」。
15
+ *
16
+ * 链路的语义是「禁用并重载/重启后」:已激活的插件可能在下一次重载前仍保留
17
+ * 注册与工具。因此所有链路结论都按推断呈现,不写成运行时实测。
18
+ *
19
+ * 只读:本模块不 import 任何控制/写入路径;执行禁用仍只在插件详情里。
20
+ * @module dsh-plugin-console/host/disable-preview
21
+ */
22
+
23
+ import { expandRegistrationName } from './attribution.js'
24
+ import { collectConfigLayers } from './config-layers.js'
25
+ import { injectionKindsOf } from './injection-owners.js'
26
+ import { readSnapshot } from './snapshot.js'
27
+
28
+ /** 工具/提示词/服务等清单的展示上限;依赖链不套用这个上限。 */
29
+ const MAX_ROWS = 200
30
+
31
+ /** 条目展示名。 */
32
+ function labelOf(entry) {
33
+ if (entry === undefined) return '未知插件'
34
+ return entry.title || entry.packageName || entry.moduleName || entry.entryId || '未知插件'
35
+ }
36
+
37
+ /** entryId / rowId 双向索引;服务供需用前者,工具与段 owner 用后者。 */
38
+ function buildIndex(entries) {
39
+ const byEntryId = new Map()
40
+ const byRowId = new Map()
41
+ for (const entry of entries) {
42
+ byEntryId.set(entry.entryId, entry)
43
+ const list = byRowId.get(entry.rowId) ?? []
44
+ list.push(entry)
45
+ byRowId.set(entry.rowId, list)
46
+ }
47
+ return { byEntryId, byRowId }
48
+ }
49
+
50
+ /** 一条条目引用;服务里拿不到对应行时保留原始 id。 */
51
+ function refOf(entryId, index) {
52
+ const entry = index.byEntryId.get(entryId)
53
+ return {
54
+ entryId,
55
+ rowId: entry?.rowId ?? entryId,
56
+ title: labelOf(entry),
57
+ phase: entry?.phase ?? null,
58
+ }
59
+ }
60
+
61
+ /** 一个名字的归属结论;空的 owner 是未知,不是失去。 */
62
+ function ownership(owners, unavailableRowIds) {
63
+ return {
64
+ lost: owners.length > 0 && owners.every((owner) => unavailableRowIds.has(owner)),
65
+ shared: owners.some((owner) => !unavailableRowIds.has(owner)),
66
+ unknown: owners.length === 0,
67
+ }
68
+ }
69
+
70
+ /** 目标行与它的子行。 */
71
+ function disabledEntriesOf(entries, target) {
72
+ return entries.filter((entry) => (
73
+ entry.entryId === target.entryId
74
+ || entry.entryId.startsWith(target.entryId + ':')
75
+ ))
76
+ }
77
+
78
+ /**
79
+ * 按服务供需推到不动点,得到完整不可用集合。
80
+ *
81
+ * 一条服务只有在「本来有提供者、且提供者全部不可用」时才算被这次禁用带走;
82
+ * 本来就没人提供的服务是既有缺口,不能算到本次头上。条目的距离取它最后失效
83
+ * 的那条必需服务的距离 + 1,这样路径展示的是真正让它停下的那一环。
84
+ * 先求完整失效集合,再统一算距离与归因,避免先命中的浅服务把深层链压平。
85
+ * @param {object} target - 目标条目。
86
+ * @param {Array<object>} baseEntries - 目标与其分组子行(距离 0)。
87
+ * @param {Array<object>} entries - 全部条目。
88
+ * @param {object} services - `snapshot.services`。
89
+ * @returns {{unavailable: Map<string, object>, serviceDistance: Map<string, number>, baseIds: Set<string>}} 闭包。
90
+ */
91
+ function computeClosure(target, baseEntries, entries, services) {
92
+ const unavailable = new Map()
93
+ const baseIds = new Set()
94
+ for (const entry of baseEntries) {
95
+ baseIds.add(entry.entryId)
96
+ unavailable.set(entry.entryId, {
97
+ entry,
98
+ distance: 0,
99
+ kind: entry.entryId === target.entryId ? 'target' : 'group',
100
+ missingServices: [],
101
+ causeService: null,
102
+ viaEntryId: null,
103
+ })
104
+ }
105
+
106
+ // 第一阶段:只算「谁不可用」,不急着归因。
107
+ // 一个条目可能同时缺几条服务,它们在后续轮次才陆续失效;如果在第一次
108
+ // 命中时就冻结距离,链条会被压平(例如 agent → goal → driver 被画成
109
+ // agent → driver 的兄弟节点)。
110
+ const lostServices = new Set()
111
+ let changed = true
112
+ const maxSetRounds = entries.length + Object.keys(services).length + 1
113
+ for (let round = 0; changed && round < maxSetRounds; round++) {
114
+ changed = false
115
+ for (const [name, record] of Object.entries(services)) {
116
+ if (lostServices.has(name)) continue
117
+ const providers = Array.isArray(record?.providers) ? record.providers : []
118
+ if (providers.length === 0) continue
119
+ if (!providers.every((id) => unavailable.has(id))) continue
120
+ lostServices.add(name)
121
+ changed = true
122
+ }
123
+ for (const entry of entries) {
124
+ if (unavailable.has(entry.entryId)) continue
125
+ if (entry.enabled === false) continue
126
+ const missing = (entry.requires ?? []).filter((name) => lostServices.has(name))
127
+ if (missing.length === 0) continue
128
+ unavailable.set(entry.entryId, {
129
+ entry,
130
+ distance: undefined,
131
+ kind: 'blocked',
132
+ missingServices: missing,
133
+ causeService: null,
134
+ viaEntryId: null,
135
+ })
136
+ changed = true
137
+ }
138
+ }
139
+
140
+ // 第二阶段:在完整失效集合上算距离与归因。
141
+ // 距离取「最后失效的必需服务」+ 1;viaEntryId 始终指向距离更小的提供者,
142
+ // 所以即使依赖成环,父链也不会自己咬住自己。
143
+ const serviceDistance = new Map()
144
+ const maxDistanceRounds = entries.length + lostServices.size + 1
145
+ changed = true
146
+ for (let round = 0; changed && round < maxDistanceRounds; round++) {
147
+ changed = false
148
+ for (const name of lostServices) {
149
+ const record = services[name]
150
+ const providers = (record?.providers ?? []).filter((id) => unavailable.has(id))
151
+ if (providers.length === 0) continue
152
+ let distance = 0
153
+ let known = true
154
+ for (const id of providers) {
155
+ const provider = unavailable.get(id)
156
+ if (typeof provider.distance !== 'number') {
157
+ known = false
158
+ break
159
+ }
160
+ distance = Math.max(distance, provider.distance)
161
+ }
162
+ if (!known) continue
163
+ if (distance > (serviceDistance.get(name) ?? -1)) {
164
+ serviceDistance.set(name, distance)
165
+ changed = true
166
+ }
167
+ }
168
+ for (const record of unavailable.values()) {
169
+ if (record.kind !== 'blocked') continue
170
+ const missing = (record.entry.requires ?? []).filter((name) => serviceDistance.has(name))
171
+ if (missing.length === 0) continue
172
+ let primary = missing[0]
173
+ for (const name of missing) {
174
+ if (serviceDistance.get(name) > serviceDistance.get(primary)) primary = name
175
+ }
176
+ const distance = serviceDistance.get(primary) + 1
177
+ if (typeof record.distance === 'number' && distance <= record.distance) continue
178
+ const providers = (services[primary]?.providers ?? []).filter((id) => unavailable.has(id))
179
+ let via = providers[0]
180
+ for (const id of providers) {
181
+ if ((unavailable.get(id)?.distance ?? -1) > (unavailable.get(via)?.distance ?? -1)) via = id
182
+ }
183
+ record.distance = distance
184
+ record.missingServices = missing
185
+ record.causeService = primary
186
+ record.viaEntryId = via
187
+ changed = true
188
+ }
189
+ }
190
+
191
+ // 兜底:只有完全脱离目标的环才可能拿不到距离;给一个可渲染的起点。
192
+ for (const record of unavailable.values()) {
193
+ if (record.kind !== 'blocked' || typeof record.distance === 'number') continue
194
+ const missing = (record.entry.requires ?? []).filter((name) => lostServices.has(name))
195
+ record.distance = 1
196
+ record.missingServices = missing
197
+ record.causeService = missing[0] ?? null
198
+ record.viaEntryId = record.causeService === null
199
+ ? null
200
+ : (services[record.causeService]?.providers ?? []).find((id) => unavailable.has(id)) ?? null
201
+ }
202
+
203
+ return { unavailable, serviceDistance, baseIds }
204
+ }
205
+
206
+ /** 从不可用集合里重建一条因果路径:目标 → 服务 → 消费者 → …。 */
207
+ function pathOf(entryId, unavailable, index, maxNodes) {
208
+ const path = []
209
+ let current = entryId
210
+ for (let guard = 0; current !== undefined && guard < maxNodes; guard++) {
211
+ const record = unavailable.get(current)
212
+ const entry = index.byEntryId.get(current)
213
+ path.unshift({
214
+ kind: 'entry',
215
+ entryId: current,
216
+ rowId: entry?.rowId ?? current,
217
+ title: labelOf(entry),
218
+ })
219
+ if (record === undefined || record.kind === 'target' || record.kind === 'group') break
220
+ if (typeof record.causeService === 'string') path.unshift({ kind: 'service', name: record.causeService })
221
+ current = record.viaEntryId
222
+ }
223
+ return path
224
+ }
225
+
226
+ /** 链路行:谁不可用、缺哪些服务、经由哪条服务、到目标有多远。 */
227
+ function chainRowsOf(unavailable, index, maxNodes) {
228
+ const rows = []
229
+ for (const [entryId, record] of unavailable) {
230
+ if (record.kind !== 'blocked') continue
231
+ const entry = index.byEntryId.get(entryId)
232
+ rows.push({
233
+ entryId,
234
+ rowId: entry?.rowId ?? entryId,
235
+ title: labelOf(entry),
236
+ moduleName: entry?.moduleName ?? null,
237
+ distance: record.distance,
238
+ phase: entry?.phase ?? null,
239
+ alreadyPending: entry?.phase === 'pending',
240
+ missingServices: [...record.missingServices],
241
+ causeService: record.causeService,
242
+ viaEntryId: record.viaEntryId,
243
+ viaTitle: labelOf(index.byEntryId.get(record.viaEntryId)),
244
+ path: pathOf(entryId, unavailable, index, maxNodes),
245
+ })
246
+ }
247
+ return rows.sort((left, right) => left.distance - right.distance || left.title.localeCompare(right.title, 'zh-CN'))
248
+ }
249
+
250
+ /** 工具影响:目标、子行与链路插件的注册工具,按 owner 判断失去 / 共享 / 未知。 */
251
+ function impactTools(snapshot, unavailableEntries, unavailableRowIds, baseRowIds) {
252
+ const attached = Array.isArray(snapshot.tools?.attributed) ? snapshot.tools.attributed : []
253
+ const byName = new Map(attached.map((tool) => [tool.name, tool]))
254
+ const owned = []
255
+ for (const entry of unavailableEntries) {
256
+ for (const name of entry.registrations?.tools ?? []) {
257
+ if (!owned.includes(name)) owned.push(name)
258
+ }
259
+ }
260
+ const rows = owned.map((name) => {
261
+ const tool = byName.get(name)
262
+ const owners = Array.isArray(tool?.owners) ? tool.owners : []
263
+ const status = ownership(owners, unavailableRowIds)
264
+ return {
265
+ name,
266
+ owners,
267
+ scopes: Array.isArray(tool?.scopes) ? tool.scopes : [],
268
+ via: tool?.via ?? 'unknown',
269
+ ...status,
270
+ // 全部 owner 都在链路上失去、且都不是目标/子行:这条工具是「连带失去」。
271
+ chainOnly: status.lost && !owners.some((owner) => baseRowIds.has(owner)),
272
+ }
273
+ })
274
+ const bucket = (predicate) => rows.filter(predicate).map((row) => row.name)
275
+ return {
276
+ owned,
277
+ lost: bucket((row) => row.lost),
278
+ shared: bucket((row) => row.shared),
279
+ unknown: bucket((row) => row.unknown),
280
+ chainOnly: bucket((row) => row.chainOnly),
281
+ rows: rows.slice(0, MAX_ROWS),
282
+ truncated: rows.length > MAX_ROWS,
283
+ }
284
+ }
285
+
286
+ /**
287
+ * 名字扫得到、全局注册表里却没有时的说明。
288
+ *
289
+ * 这不是「失去」,也不是「归属失败」:`agent.ctx.systemPrompt.section()` 这类
290
+ * 会话内注册、条件注册(例如只在某个工具可见时才有正文)以及本次没注册成功,
291
+ * 都会落成这种形态。预演读的是全局注册表,无法进一步区分,所以只陈述事实。
292
+ */
293
+ const REGISTRY_MISSING_REASON = '源码里有注册调用,但当前全局注册表没有这一项;可能是会话内(agent scope)或条件注册,或本次没有注册成功。不计入「失去」。'
294
+
295
+ /** 注册表里有这一项、但源码扫描定位不到 owner 时的说明。 */
296
+ const UNATTRIBUTED_REASON = '注册表里有这一项,但源码扫描没有定位到注册它的包'
297
+
298
+ /** 提示词影响:sections 与 contexts 分开,回答「哪些上下文不再注入」。 */
299
+ function impactPrompt(snapshot, unavailableEntries, unavailableRowIds, baseRowIds) {
300
+ const build = (kind, registry, field) => {
301
+ const byName = new Map((Array.isArray(registry) ? registry : []).map((row) => [row.name, row]))
302
+ const owned = []
303
+ for (const entry of unavailableEntries) {
304
+ for (const name of entry.registrations?.[field] ?? []) {
305
+ for (const actual of expandRegistrationName(name, entry, byName)) {
306
+ if (!owned.includes(actual)) owned.push(actual)
307
+ }
308
+ }
309
+ }
310
+ const rows = owned.map((name) => {
311
+ const row = byName.get(name)
312
+ // 扫得到名字、注册表里没有:多为会话内或条件注册。它和「注册表里有、
313
+ // 但 owner 扫不到」是两件事,不能都塞进「未归属」。
314
+ if (row === undefined) {
315
+ return {
316
+ name,
317
+ kind,
318
+ owners: [],
319
+ lost: false,
320
+ shared: false,
321
+ unknown: false,
322
+ registryMissing: true,
323
+ reason: REGISTRY_MISSING_REASON,
324
+ chainOnly: false,
325
+ }
326
+ }
327
+ const owners = Array.isArray(row.owners) ? row.owners : []
328
+ const status = ownership(owners, unavailableRowIds)
329
+ return {
330
+ name,
331
+ kind,
332
+ owners,
333
+ ...status,
334
+ registryMissing: false,
335
+ ...(status.unknown ? { reason: UNATTRIBUTED_REASON } : {}),
336
+ chainOnly: status.lost && !owners.some((owner) => baseRowIds.has(owner)),
337
+ }
338
+ })
339
+ const bucket = (predicate) => rows.filter(predicate).map((row) => row.name)
340
+ return {
341
+ owned,
342
+ lost: bucket((row) => row.lost),
343
+ shared: bucket((row) => row.shared),
344
+ unknown: bucket((row) => row.unknown),
345
+ registryMissing: bucket((row) => row.registryMissing === true),
346
+ chainOnly: bucket((row) => row.chainOnly),
347
+ rows: rows.slice(0, MAX_ROWS),
348
+ truncated: rows.length > MAX_ROWS,
349
+ }
350
+ }
351
+ return {
352
+ sections: build('section', snapshot.prompt?.sections, 'sections'),
353
+ contexts: build('context', snapshot.prompt?.contexts, 'contexts'),
354
+ }
355
+ }
356
+
357
+ /** 服务影响:唯一提供者失效时,哪些消费者跟着不可用。 */
358
+ function impactServices(snapshot, unavailableEntries, unavailableEntryIds, blockedEntryIds, index) {
359
+ const registry = snapshot.services ?? {}
360
+ const provided = []
361
+ for (const entry of unavailableEntries) {
362
+ for (const name of entry.provides ?? []) if (!provided.includes(name)) provided.push(name)
363
+ }
364
+ const rows = provided.map((name) => {
365
+ const record = registry[name] ?? {}
366
+ const providers = (record.providers ?? []).map((id) => refOf(id, index))
367
+ const consumers = (record.consumers ?? []).map((id) => {
368
+ const ref = refOf(id, index)
369
+ const entry = index.byEntryId.get(id)
370
+ return {
371
+ ...ref,
372
+ alreadyPending: ref.phase === 'pending',
373
+ disabled: entry?.enabled === false,
374
+ willBeUnavailable: unavailableEntryIds.has(id),
375
+ blocked: blockedEntryIds.has(id),
376
+ }
377
+ })
378
+ // provider 为空本来就是既有缺口,不是「禁用导致」;不列为本次影响。
379
+ const soleInSet = providers.length > 0 && providers.every((provider) => unavailableEntryIds.has(provider.entryId))
380
+ return { name, providers, consumers, soleInSet, affected: soleInSet }
381
+ })
382
+
383
+ const pending = new Map()
384
+ for (const row of rows) {
385
+ if (!row.soleInSet) continue
386
+ for (const consumer of row.consumers) {
387
+ if (consumer.willBeUnavailable || consumer.disabled) continue
388
+ const existing = pending.get(consumer.rowId) ?? { ...consumer, services: [] }
389
+ if (!existing.services.includes(row.name)) existing.services.push(row.name)
390
+ pending.set(consumer.rowId, existing)
391
+ }
392
+ }
393
+ const affected = rows.filter((row) => row.soleInSet)
394
+ return {
395
+ rows: rows.slice(0, MAX_ROWS),
396
+ truncated: rows.length > MAX_ROWS,
397
+ soleProviders: affected.map((row) => row.name),
398
+ affectedConsumers: [...new Set(affected.flatMap((row) => row.consumers.filter((consumer) => !consumer.willBeUnavailable && !consumer.disabled).map((consumer) => consumer.rowId)))],
399
+ pending: [...pending.values()],
400
+ }
401
+ }
402
+
403
+ /** 受影响的模式:行 id 或模块名命中不可用集合。 */
404
+ function impactModes(snapshot, unavailableEntryIds, unavailableModules) {
405
+ return (Array.isArray(snapshot.modes) ? snapshot.modes : [])
406
+ .map((mode) => {
407
+ const rows = Array.isArray(mode.rows) ? mode.rows : []
408
+ const byEntry = rows.some((row) => unavailableEntryIds.has(row.entryId))
409
+ const byModule = rows.some((row) => unavailableModules.has(row.moduleName))
410
+ if (!byEntry && !byModule) return null
411
+ return { id: mode.id, name: mode.name ?? mode.id, matchedBy: byEntry ? 'entry' : 'module' }
412
+ })
413
+ .filter((mode) => mode !== null)
414
+ }
415
+
416
+ /** 活会话:因模式或失去的工具而受影响;冷会话只按模式推断。 */
417
+ function impactSessions(snapshot, modeIds, lostTools) {
418
+ const live = []
419
+ for (const agent of snapshot.tools?.liveAgents ?? []) {
420
+ const byMode = modeIds.has(agent.preset)
421
+ const byTool = (agent.tools ?? []).some((name) => lostTools.has(name))
422
+ if (!byMode && !byTool) continue
423
+ live.push({ sessionId: agent.sessionId, preset: agent.preset, reason: byMode ? 'mode' : 'tool' })
424
+ }
425
+ const seen = new Set()
426
+ let cold = 0
427
+ const sessions = [
428
+ ...(Array.isArray(snapshot.workspaces) ? snapshot.workspaces.flatMap((workspace) => workspace.sessions ?? []) : []),
429
+ ...(Array.isArray(snapshot.ungroupedSessions) ? snapshot.ungroupedSessions : []),
430
+ ]
431
+ for (const session of sessions) {
432
+ if (typeof session?.id !== 'string' || seen.has(session.id)) continue
433
+ seen.add(session.id)
434
+ if (session.live === true) continue
435
+ if (modeIds.has(session.preset)) cold++
436
+ }
437
+ return { liveSessions: live, coldSessionCount: cold }
438
+ }
439
+
440
+ /**
441
+ * 计算一个目标(含分组子行与完整依赖链)的影响面。
442
+ *
443
+ * 纯函数,不读文件、不调服务;`readDisablePreview` 在它之上再挂 config 分层。
444
+ * @param {object} snapshot - 当前快照。
445
+ * @param {string} rowId - 目标行 id 或运行时 id。
446
+ * @returns {object} 影响面;找不到时 `{found:false, reason}`。
447
+ */
448
+ export function computeCascadeImpact(snapshot, rowId) {
449
+ const entries = Array.isArray(snapshot?.entries) ? snapshot.entries : []
450
+ const target = entries.find((candidate) => candidate.rowId === rowId)
451
+ ?? entries.find((candidate) => candidate.entryId === rowId)
452
+ if (target === undefined) return { found: false, reason: '找不到这个插件条目' }
453
+
454
+ const index = buildIndex(entries)
455
+ const baseEntries = disabledEntriesOf(entries, target)
456
+ const { unavailable } = computeClosure(target, baseEntries, entries, snapshot.services ?? {})
457
+ const unavailableEntries = entries.filter((entry) => unavailable.has(entry.entryId))
458
+ const unavailableEntryIds = new Set(unavailableEntries.map((entry) => entry.entryId))
459
+ const unavailableRowIds = new Set(unavailableEntries.map((entry) => entry.rowId))
460
+ const baseRowIds = new Set(baseEntries.map((entry) => entry.rowId))
461
+ const blockedEntries = [...unavailable.values()].filter((record) => record.kind === 'blocked')
462
+ const blockedEntryIds = new Set(blockedEntries.map((record) => record.entry.entryId))
463
+ const unavailableModules = new Set(unavailableEntries.map((entry) => entry.moduleName).filter((name) => typeof name === 'string'))
464
+
465
+ const tools = impactTools(snapshot, unavailableEntries, unavailableRowIds, baseRowIds)
466
+ const prompt = impactPrompt(snapshot, unavailableEntries, unavailableRowIds, baseRowIds)
467
+ const services = impactServices(snapshot, unavailableEntries, unavailableEntryIds, blockedEntryIds, index)
468
+ const modes = impactModes(snapshot, unavailableEntryIds, unavailableModules)
469
+ const modeIds = new Set(modes.map((mode) => mode.id))
470
+ const { liveSessions, coldSessionCount } = impactSessions(snapshot, modeIds, new Set(tools.lost))
471
+
472
+ const descendants = baseEntries
473
+ .filter((entry) => entry.entryId !== target.entryId)
474
+ .map((entry) => ({
475
+ rowId: entry.rowId,
476
+ entryId: entry.entryId,
477
+ title: labelOf(entry),
478
+ depth: entry.depth ?? null,
479
+ enabled: entry.enabled !== false,
480
+ phase: entry.phase ?? null,
481
+ }))
482
+
483
+ const blocked = chainRowsOf(unavailable, index, entries.length + 1)
484
+ const chainDepth = blocked.reduce((depth, row) => Math.max(depth, row.distance), 0)
485
+
486
+ const injectionRows = injectionKindsOf(target.packageName, target.moduleName)
487
+ .map((row) => ({ ...row, confidence: 'inference' }))
488
+
489
+ return {
490
+ found: true,
491
+ rowId: target.rowId,
492
+ target: {
493
+ rowId: target.rowId,
494
+ entryId: target.entryId,
495
+ moduleName: target.moduleName ?? null,
496
+ packageName: target.packageName ?? null,
497
+ title: labelOf(target),
498
+ version: target.version ?? null,
499
+ layer: target.layer ?? null,
500
+ layerVersion: target.layerVersion ?? null,
501
+ enabled: target.enabled !== false,
502
+ alreadyDisabled: target.enabled === false,
503
+ phase: target.phase ?? null,
504
+ isGroup: target.isGroup === true,
505
+ modeOnly: target.modeOnly === true,
506
+ requires: target.requires ?? [],
507
+ provides: target.provides ?? [],
508
+ configKeys: target.configKeys ?? [],
509
+ },
510
+ unavailableRowIds: [...unavailableRowIds],
511
+ descendants,
512
+ descendantsTruncated: false,
513
+ chain: {
514
+ depth: chainDepth,
515
+ blocked,
516
+ truncated: false,
517
+ note: '按「所需服务的提供者全部不可用则插件不可用」规则推导',
518
+ },
519
+ tools,
520
+ prompt,
521
+ services,
522
+ modes,
523
+ liveSessions,
524
+ coldSessionCount,
525
+ injections: {
526
+ available: true,
527
+ rows: injectionRows,
528
+ note: injectionRows.length === 0
529
+ ? '未匹配到已知的消息注入来源(本插件暂时覆盖 5 类已知 kind,不代表没有注入)'
530
+ : '归属按包名推断,不是运行时实测',
531
+ },
532
+ scale: {
533
+ descendants: descendants.length,
534
+ blockedEntries: blocked.length,
535
+ chainDepth,
536
+ lostTools: tools.lost.length,
537
+ lostToolsViaChain: tools.chainOnly.length,
538
+ sharedTools: tools.shared.length,
539
+ unknownTools: tools.unknown.length,
540
+ lostSections: prompt.sections.lost.length,
541
+ lostSectionsViaChain: prompt.sections.chainOnly.length,
542
+ sharedSections: prompt.sections.shared.length,
543
+ lostContexts: prompt.contexts.lost.length,
544
+ lostContextsViaChain: prompt.contexts.chainOnly.length,
545
+ sharedContexts: prompt.contexts.shared.length,
546
+ soleServices: services.soleProviders.length,
547
+ pendingConsumers: services.pending.length,
548
+ modes: modes.length,
549
+ liveSessions: liveSessions.length,
550
+ coldSessionCount,
551
+ injectionRows: injectionRows.length,
552
+ },
553
+ }
554
+ }
555
+
556
+ /**
557
+ * 按页面区块写证据与边界。
558
+ *
559
+ * 顺序与页面自上而下一致,读者能在对应区块里找到这句话说的是哪一块。
560
+ */
561
+ const SECTION_NOTES = [
562
+ {
563
+ id: 'target',
564
+ area: '预禁用的插件信息',
565
+ text: '插件、模块与层来自 Loader 行;分组行的子行按 entryId 前缀推断。',
566
+ },
567
+ {
568
+ id: 'reaction',
569
+ area: '会失去的提示词与消息内容',
570
+ text: '工具、提示词段与上下文来自快照注册表,归属由包源码扫描补齐;消息注入按包名推断;「失去」只表示没有其他所有者,不是卸载实测;未归属与不在全局注册表的项单独列出。',
571
+ },
572
+ {
573
+ id: 'chain',
574
+ area: '受影响的下游插件',
575
+ text: '依赖链按 fiber.inject 声明推导到不动点,服务只在提供者全部不可用时失效;结论对应禁用并重载/重启后,已激活插件可能在下一次重载前仍保留注册与工具。',
576
+ },
577
+ {
578
+ id: 'graph',
579
+ area: '依赖关系图',
580
+ text: '上游只展开 2 层、下游按影响链完整展开;边来自 provides / requires 声明,config 引用只认完全相等的值,同组子行按 Loader id 前缀推断。',
581
+ },
582
+ {
583
+ id: 'config',
584
+ area: '配置与覆盖',
585
+ text: '各层声明来自 patch 文件,生效值来自 Loader;读不到的层或不受支持的写法会写原因,诊断只比较、不推断各层如何合并。',
586
+ },
587
+ {
588
+ id: 'page',
589
+ area: '整页',
590
+ text: '只读预演,不执行禁用;执行仍在插件详情控制区,由 plugin-manager 写入并审计。',
591
+ },
592
+ ]
593
+
594
+ /**
595
+ * 关系图所需的最小快照切片。
596
+ *
597
+ * 影响链来自本次 disable-preview 的快照;完整链路若改用页面级的另一份快照,
598
+ * 两次采集之间的插件增减会让两张图对不上。这里把同一份快照的 entries/services
599
+ * 一起返回,两张图共用一份数据。
600
+ * @param {object} snapshot - 本次预演采集的快照。
601
+ * @returns {{entries: Array<object>, services: object}} 图数据。
602
+ */
603
+ export function graphSlice(snapshot) {
604
+ return {
605
+ entries: (Array.isArray(snapshot.entries) ? snapshot.entries : []).map((entry) => ({
606
+ entryId: entry.entryId,
607
+ rowId: entry.rowId,
608
+ title: entry.title,
609
+ moduleName: entry.moduleName,
610
+ packageName: entry.packageName,
611
+ phase: entry.phase,
612
+ enabled: entry.enabled,
613
+ requires: entry.requires ?? [],
614
+ provides: entry.provides ?? [],
615
+ configEntries: entry.configEntries ?? [],
616
+ })),
617
+ services: snapshot.services ?? {},
618
+ }
619
+ }
620
+
621
+ /**
622
+ * 读取一个目标的禁用预演。
623
+ * @param {object} ctx - host 插件上下文。
624
+ * @param {string} rowId - 目标行 id 或运行时 id。
625
+ * @returns {Promise<object>} 影响面 + 依赖链 + config 分层 + 证据与边界。
626
+ */
627
+ export async function readDisablePreview(ctx, rowId) {
628
+ const snapshot = await readSnapshot(ctx, {})
629
+ const impact = computeCascadeImpact(snapshot, rowId)
630
+ if (impact.found !== true) return impact
631
+
632
+ let config
633
+ try {
634
+ config = await collectConfigLayers(ctx, snapshot, impact.rowId)
635
+ } catch (cause) {
636
+ // 配置块独立降级:文件读不到不影响影响面与关系图。
637
+ config = { available: false, reason: cause instanceof Error ? cause.message : String(cause) }
638
+ }
639
+
640
+ return {
641
+ ...impact,
642
+ generatedAt: snapshot.generatedAt,
643
+ // HMR 未知时按「可能需要重启」写;与 impact.js 保持同一口径。
644
+ hmr: ctx.get('hmr') !== undefined,
645
+ readOnly: true,
646
+ config,
647
+ // 影响图与完整链路共用这一份快照,避免与页面级快照的时间差。
648
+ graph: graphSlice(snapshot),
649
+ notes: SECTION_NOTES,
650
+ }
651
+ }
@@ -211,7 +211,7 @@ const CONFIG_VALUE_LIMIT = 2000
211
211
  * @param {unknown} value - `options.config` 里的一个值。
212
212
  * @returns {string} 展示文本。
213
213
  */
214
- function configText(value) {
214
+ export function configText(value) {
215
215
  if (typeof value === 'string') return value
216
216
  if (value === undefined) return 'undefined'
217
217
  if (value === null) return 'null'
@@ -6,6 +6,7 @@
6
6
  * @module dsh-plugin-console/host/impact
7
7
  */
8
8
 
9
+ import { expandRegistrationName } from './attribution.js'
9
10
  import { readSnapshot } from './snapshot.js'
10
11
 
11
12
  /** 取一个条目的展示名。 */
@@ -42,11 +43,16 @@ export function computeImpact(snapshot, rowId) {
42
43
  }
43
44
 
44
45
  const promptRows = []
46
+ const promptByName = new Map(
47
+ [...(snapshot?.prompt?.sections ?? []), ...(snapshot?.prompt?.contexts ?? [])]
48
+ .map((row) => [row.name, row]),
49
+ )
45
50
  for (const name of [...(entry.registrations?.sections ?? []), ...(entry.registrations?.contexts ?? [])]) {
46
- const row = [...(snapshot?.prompt?.sections ?? []), ...(snapshot?.prompt?.contexts ?? [])]
47
- .find((candidate) => candidate.name === name)
48
- const owners = row?.owners ?? []
49
- promptRows.push({ name, ...ownership(owners) })
51
+ for (const actual of expandRegistrationName(name, entry, promptByName)) {
52
+ const row = promptByName.get(actual)
53
+ const owners = row?.owners ?? []
54
+ promptRows.push({ name: actual, ...ownership(owners) })
55
+ }
50
56
  }
51
57
 
52
58
  const services = (entry.provides ?? []).map((name) => {