@luolingp/request-doctor 0.0.0-stage → 0.11.0
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 +21 -0
- package/README.md +184 -3
- package/client.js +174 -0
- package/cordis.patch.yml +16 -0
- package/docs/00-/346/226/207/346/241/243/347/264/242/345/274/225.md +26 -0
- package/docs/01-/350/275/257/344/273/266/351/234/200/346/261/202/350/247/204/346/240/274/350/257/264/346/230/216.md +191 -0
- package/docs/02-/350/275/257/344/273/266/350/256/276/350/256/241/350/257/264/346/230/216.md +391 -0
- package/docs/03-/350/275/257/344/273/266/346/265/213/350/257/225/346/212/245/345/221/212.md +212 -0
- package/docs/04-/345/256/211/350/243/205/351/203/250/347/275/262/350/257/264/346/230/216.md +176 -0
- package/docs/05-/347/224/250/346/210/267/346/211/213/345/206/214.md +181 -0
- package/docs/06-/350/275/257/344/273/266/350/264/250/351/207/217/345/272/246/351/207/217/346/212/245/345/221/212.md +109 -0
- package/docs/quality-report.json +27 -0
- package/icon.svg +12 -0
- package/index.js +464 -0
- package/lib/arbitrate.js +45 -0
- package/lib/cache-store.js +79 -0
- package/lib/classify.js +389 -0
- package/lib/compat-env.js +490 -0
- package/lib/compat-repair.js +196 -0
- package/lib/compat.js +332 -0
- package/lib/diagnostics.js +77 -0
- package/lib/env-verify.js +160 -0
- package/lib/install-bundle.js +199 -0
- package/lib/install-rescue.js +161 -0
- package/lib/install-support.js +206 -0
- package/lib/model-repair.js +190 -0
- package/lib/package-fetch.js +181 -0
- package/lib/plugin-discovery.js +232 -0
- package/lib/repair-listeners.js +126 -0
- package/lib/retry-ledger.js +67 -0
- package/lib/runtime-probe.js +171 -0
- package/lib/semver.js +159 -0
- package/lib/sleep.js +18 -0
- package/lib/stat-cache.js +67 -0
- package/lib/symbol-alias.js +44 -0
- package/lib/timers.js +39 -0
- package/lib/version-repair.js +495 -0
- package/lib/version-search.js +44 -0
- package/locale/en.json +17 -0
- package/locale/zh.json +17 -0
- package/package.json +51 -3
package/lib/timers.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// 脱离 HMR 事务的定时器。
|
|
2
|
+
//
|
|
3
|
+
// 插件激活时整个流程跑在 hmr.runExclusive 里面,而 AsyncLocalStorage 会把这个上下文
|
|
4
|
+
// 带进激活期间创建的所有定时器。结果就是定时器回调里调用 pluginManager 时,它还认为
|
|
5
|
+
// 自己在事务内,直接报 "HMR transactions cannot be nested"。所以创建定时器之前先退出
|
|
6
|
+
// 这个上下文,回调里就能正常拿锁了。
|
|
7
|
+
export function createTimers(ctx, timers) {
|
|
8
|
+
const detach = (start) => {
|
|
9
|
+
const storage = ctx.get('hmr')?.executing;
|
|
10
|
+
if (storage !== undefined && typeof storage.exit === 'function') {
|
|
11
|
+
try {
|
|
12
|
+
return storage.exit(start);
|
|
13
|
+
} catch {
|
|
14
|
+
// 拿不到就直接调,总比什么都不做强
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return start();
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// 延迟执行,返回取消函数。定时器都 unref,免得拖住进程退出。
|
|
21
|
+
const later = (fn, delayMs) => {
|
|
22
|
+
let timer;
|
|
23
|
+
detach(() => {
|
|
24
|
+
timer = setTimeout(() => {
|
|
25
|
+
timers.delete(timer);
|
|
26
|
+
fn();
|
|
27
|
+
}, delayMs);
|
|
28
|
+
timer.unref?.();
|
|
29
|
+
});
|
|
30
|
+
if (timer !== undefined) timers.add(timer);
|
|
31
|
+
return () => {
|
|
32
|
+
if (timer !== undefined) {
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
timers.delete(timer);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
return { later };
|
|
39
|
+
}
|
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
// 插件与 DSH 的版本修复。
|
|
2
|
+
//
|
|
3
|
+
// 找出所有声明了当前运行时不满足的依赖的插件,逐个审计,把能证明兼容的那些交给兼容环境。
|
|
4
|
+
// 审计是前置条件:插件要用的符号已经不存在了,就绝不硬上。
|
|
5
|
+
import { existsSync, readFileSync, readdirSync } from 'node:fs';
|
|
6
|
+
import { dirname, join } from 'node:path';
|
|
7
|
+
import { unmetPeers } from './compat.js';
|
|
8
|
+
import { auditInstalledPlugin, entryPathOf, installedEntryPath, packageDir } from './compat-repair.js';
|
|
9
|
+
import { alignManifest, installCompatEnvironment } from './compat-env.js';
|
|
10
|
+
import { describeContention } from './arbitrate.js';
|
|
11
|
+
import { clearStagedPreparation, prepareForInstall, readStagedPreparation } from './install-support.js';
|
|
12
|
+
import { verifyEnvironment } from './env-verify.js';
|
|
13
|
+
import { fetchPackage, listVersions } from './package-fetch.js';
|
|
14
|
+
import { orderCandidates } from './version-search.js';
|
|
15
|
+
import { pruneCache, touchAndReturn } from './cache-store.js';
|
|
16
|
+
|
|
17
|
+
export { pruneCache };
|
|
18
|
+
import { collectMismatched } from './plugin-discovery.js';
|
|
19
|
+
|
|
20
|
+
// 激活过程中重排会报这个错
|
|
21
|
+
const NESTED_HMR = /transactions cannot be nested/i;
|
|
22
|
+
|
|
23
|
+
// 造一个版本修复器。
|
|
24
|
+
export function createVersionRepair(input) {
|
|
25
|
+
const { ctx, probe, diagnostics, config } = input;
|
|
26
|
+
const { record, stats } = diagnostics;
|
|
27
|
+
const state = { busy: false };
|
|
28
|
+
|
|
29
|
+
// 拿插件管理器服务,没有就返回 undefined。
|
|
30
|
+
const managerOf = () => probe.get('pluginManager');
|
|
31
|
+
|
|
32
|
+
const deps = { ctx, probe, diagnostics, config, record, stats, warn: input.warn, later: input.later, maxHmrRetries: input.maxHmrRetries, state, scanError: diagnostics.scanError };
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
run: (reason, manager, attempt) => run(reason, manager ?? managerOf(), attempt ?? 0, deps),
|
|
36
|
+
isBusy: () => state.busy,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 把每个不匹配的插件都审一遍。
|
|
41
|
+
//
|
|
42
|
+
// 单个插件读不出来或者 manifest 坏了,不能把整轮扫描带停,所以每个都单独兜住。
|
|
43
|
+
async function auditAll(bundles, facts, plugins, probe) {
|
|
44
|
+
const taken = probe.activeProviders() ?? [];
|
|
45
|
+
const audited = [];
|
|
46
|
+
for (const issue of await collectMismatched(bundles, facts, plugins, probe)) {
|
|
47
|
+
// One plugin's unreadable package or malformed manifest must not abort the
|
|
48
|
+
// scan for every other plugin, so each audit is contained.
|
|
49
|
+
audited.push(await auditOne(issue, facts, probe, taken));
|
|
50
|
+
}
|
|
51
|
+
return audited;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// 审一个候选。抛出来的异常折算成 review,不往上冒。
|
|
55
|
+
async function auditOne(issue, facts, probe, taken) {
|
|
56
|
+
const base = {
|
|
57
|
+
name: issue.name,
|
|
58
|
+
version: issue.version,
|
|
59
|
+
runtimeVersion: issue.runtimeVersion ?? facts.runtimeVersion,
|
|
60
|
+
requirements: issue.requirements ?? {},
|
|
61
|
+
deferred: issue.deferred === true,
|
|
62
|
+
needsEnvironment: issue.needsEnvironment === true,
|
|
63
|
+
};
|
|
64
|
+
try {
|
|
65
|
+
const manifestPath = facts.profileDir === undefined
|
|
66
|
+
? undefined
|
|
67
|
+
: join(packageDir(facts.profileDir, issue.name) ?? '', 'package.json');
|
|
68
|
+
const result = await auditInstalledPlugin({
|
|
69
|
+
profileDir: facts.profileDir,
|
|
70
|
+
name: issue.name,
|
|
71
|
+
runtimeVersion: base.runtimeVersion,
|
|
72
|
+
requirements: base.requirements,
|
|
73
|
+
manifest: manifestPath === undefined ? undefined : probe.readManifestCached(manifestPath),
|
|
74
|
+
runtimeEntryOf: probe.runtimeEntryOf(facts),
|
|
75
|
+
takenProviders: taken,
|
|
76
|
+
assumeCompatible: base.deferred === true || base.needsEnvironment === true,
|
|
77
|
+
});
|
|
78
|
+
return {
|
|
79
|
+
...base,
|
|
80
|
+
verdict: result.verdict,
|
|
81
|
+
reasons: result.reasons,
|
|
82
|
+
missing: result.missing,
|
|
83
|
+
imports: result.imports,
|
|
84
|
+
importMap: result.importMap,
|
|
85
|
+
providers: result.providers,
|
|
86
|
+
conflicts: result.conflicts ?? [],
|
|
87
|
+
};
|
|
88
|
+
} catch (error) {
|
|
89
|
+
return {
|
|
90
|
+
...base,
|
|
91
|
+
verdict: 'review',
|
|
92
|
+
reasons: [`audit failed: ${String(error)}`],
|
|
93
|
+
missing: [],
|
|
94
|
+
imports: [],
|
|
95
|
+
importMap: new Map(),
|
|
96
|
+
providers: [],
|
|
97
|
+
conflicts: [],
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// 已启用插件的花名册,附上每个实际在服务的路由。
|
|
102
|
+
//
|
|
103
|
+
// 源码分析会多算,所以要跟运行时注册表求交集:某个地区没启用,不代表它在抢路由。
|
|
104
|
+
function providerRoster(facts, plugins, deps) {
|
|
105
|
+
const { probe, warn } = deps;
|
|
106
|
+
const roster = [];
|
|
107
|
+
const live = probe.activeProviders();
|
|
108
|
+
for (const entry of plugins ?? []) {
|
|
109
|
+
if (entry?.enabled !== true) continue;
|
|
110
|
+
const entryPath = facts.profileDir === undefined ? undefined : installedEntryPath(facts.profileDir, entry.moduleName);
|
|
111
|
+
if (entryPath === undefined) continue;
|
|
112
|
+
try {
|
|
113
|
+
const declared = probe.factsOf(dirname(entryPath), entryPath, facts.runtimeVersion).providers;
|
|
114
|
+
roster.push({
|
|
115
|
+
name: entry.moduleName,
|
|
116
|
+
version: entry.version ?? '0.0.0',
|
|
117
|
+
active: entry.fiberPhase === 'active',
|
|
118
|
+
providers: live === undefined ? declared : declared.filter((id) => live.includes(id)),
|
|
119
|
+
});
|
|
120
|
+
} catch (error) {
|
|
121
|
+
// A plugin that cannot be read contributes no claim, but the reason is
|
|
122
|
+
// worth one line: a broken install otherwise looks like a clean scan.
|
|
123
|
+
warn(`request-doctor: could not analyse ${entry.moduleName}: ${String(error)}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return roster;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// 本进程里已经确认「所有版本都凑不齐」的组合,就别再搜第二遍了。
|
|
130
|
+
//
|
|
131
|
+
// 不记这一笔的话,每次扫描都会把注册表和下载重走一遍——明明上一次已经证明没有。
|
|
132
|
+
// 进程重启会清空,所以新发布的版本自然会被重新看到。
|
|
133
|
+
const exhaustedSearches = new Set();
|
|
134
|
+
|
|
135
|
+
// 把缺符号的那些包的真实旧版本取回来。
|
|
136
|
+
//
|
|
137
|
+
// 兼容模式的关键就在这里:插件当初是对着某个版本写的,那个版本的代码还在仓库里,
|
|
138
|
+
// 取下来装上就行。这样跑的是它认识的那份实现,不是转发到今天的替身。
|
|
139
|
+
async function fetchNeededVersions(item, facts, deps) {
|
|
140
|
+
const map = new Map();
|
|
141
|
+
const probeResult = installCompatEnvironment({
|
|
142
|
+
pluginDir: facts.profileDir ?? '',
|
|
143
|
+
profileDir: facts.profileDir,
|
|
144
|
+
...commonEnvironmentFields(item),
|
|
145
|
+
runtimeVersion: item.runtimeVersion,
|
|
146
|
+
resolveDir: deps.probe.runtimeDirOf(facts),
|
|
147
|
+
// 只问一句,别真写:这一调用如果落盘,会把 profile 里的真实包盖成垫片。
|
|
148
|
+
dryRun: true,
|
|
149
|
+
});
|
|
150
|
+
if (probeResult.status !== 'needs-old-version') return map;
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
for (const need of probeResult.needed) {
|
|
154
|
+
const found = await findVersionWithSymbols(item, facts, need, deps);
|
|
155
|
+
if (found !== undefined) map.set(need.name, found);
|
|
156
|
+
}
|
|
157
|
+
return map;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 找一个能凑齐符号的版本。
|
|
161
|
+
//
|
|
162
|
+
// 钉住的那个版本未必有插件要的东西——它声明的范围可能覆盖好几个版本,而符号只在其中
|
|
163
|
+
// 某一个里。既然旧代码都还在仓库里,就按顺序试,第一个凑齐的就是答案。
|
|
164
|
+
async function findVersionWithSymbols(item, facts, need, deps) {
|
|
165
|
+
const key = `${need.name}|${(need.symbols ?? []).join(',')}`;
|
|
166
|
+
if (exhaustedSearches.has(key)) return undefined;
|
|
167
|
+
|
|
168
|
+
const versions = await listVersions(need.name);
|
|
169
|
+
const candidates = orderCandidates(versions, item.requirements?.[need.name], need.version);
|
|
170
|
+
if (candidates.length === 0) candidates.push(need.version);
|
|
171
|
+
|
|
172
|
+
for (const version of candidates) {
|
|
173
|
+
const dir = await cachePackage(need.name, version, facts.profileDir, deps);
|
|
174
|
+
if (dir === undefined) continue;
|
|
175
|
+
// 只问这个版本能不能凑齐,别真装。
|
|
176
|
+
const trial = installCompatEnvironment({
|
|
177
|
+
pluginDir: '',
|
|
178
|
+
profileDir: facts.profileDir,
|
|
179
|
+
...environmentInput(item, facts, deps),
|
|
180
|
+
oldVersionDirs: new Map([[need.name, { dir, version }]]),
|
|
181
|
+
dryRun: true,
|
|
182
|
+
});
|
|
183
|
+
if (trial.status === 'installable') {
|
|
184
|
+
return { dir, version, closure: await resolveClosure(need.name, version, facts.profileDir, deps) };
|
|
185
|
+
}
|
|
186
|
+
deps.record('version-skipped', `${need.name}@${version} 凑不齐,试下一个`);
|
|
187
|
+
}
|
|
188
|
+
exhaustedSearches.add(key);
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 把每个包各自的闭包合起来。同一个包出现在多个闭包里时,以先来的为准——
|
|
193
|
+
// 它们通常来自同一批发布,版本一致;真不一致的情况下,冲突本身就该由插件作者解决。
|
|
194
|
+
function mergeClosures(oldVersionDirs) {
|
|
195
|
+
const merged = new Map();
|
|
196
|
+
for (const entry of oldVersionDirs?.values() ?? []) {
|
|
197
|
+
for (const [name, value] of entry.closure ?? new Map()) {
|
|
198
|
+
if (!merged.has(name)) merged.set(name, value);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return merged;
|
|
202
|
+
}
|
|
203
|
+
// 每处都一样的两个字段:插件声明的范围和它实际导入的符号。
|
|
204
|
+
function commonEnvironmentFields(item) {
|
|
205
|
+
return {
|
|
206
|
+
requirements: item.requirements ?? {},
|
|
207
|
+
imports: item.importMap ?? new Map(),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// 建兼容环境要的那几个参数,每处都一样,拼一次就够。
|
|
212
|
+
function environmentInput(item, facts, deps) {
|
|
213
|
+
return {
|
|
214
|
+
profileDir: facts.profileDir,
|
|
215
|
+
...commonEnvironmentFields(item),
|
|
216
|
+
runtimeVersion: item.runtimeVersion,
|
|
217
|
+
resolveDir: deps.probe.runtimeDirOf(facts),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// 一次闭包最多装这么多个包。旧版本的依赖通常几个,多了说明依赖树异常,该停下来。
|
|
222
|
+
const MAX_CLOSURE = 16;
|
|
223
|
+
|
|
224
|
+
// 从一个版本范围里取出具体版本号。取不到就跳过——猜一个版本去装还不如不装。
|
|
225
|
+
function exactVersionOf(range) {
|
|
226
|
+
if (typeof range !== 'string') return undefined;
|
|
227
|
+
return /\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?/.exec(range)?.[0];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 把旧版本自己的运行时依赖也取回来。
|
|
231
|
+
//
|
|
232
|
+
// 这一步不能省:DSH 的包彼此依赖,而且钉的是具体版本。只装它自己一个,加载时
|
|
233
|
+
// 它 import 的兄弟包要么根本找不到,要么拿到新版本——两种都不是插件当初的环境。
|
|
234
|
+
export async function resolveClosure(root, version, profileDir, deps) {
|
|
235
|
+
const closure = new Map();
|
|
236
|
+
const seen = new Set();
|
|
237
|
+
const queue = [{ name: root, version }];
|
|
238
|
+
|
|
239
|
+
while (queue.length > 0 && closure.size < MAX_CLOSURE) {
|
|
240
|
+
const next = queue.shift();
|
|
241
|
+
if (next.name === undefined || seen.has(next.name)) continue;
|
|
242
|
+
seen.add(next.name);
|
|
243
|
+
|
|
244
|
+
const dir = await cachePackage(next.name, next.version, profileDir, deps);
|
|
245
|
+
if (dir === undefined) continue;
|
|
246
|
+
closure.set(next.name, { dir, version: next.version });
|
|
247
|
+
for (const dependency of runtimeDependenciesOf(dir)) {
|
|
248
|
+
if (seen.has(dependency.name)) continue;
|
|
249
|
+
queue.push(dependency);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return closure;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
// 读出一个包自己依赖的运行时包,版本按声明钉死。
|
|
257
|
+
//
|
|
258
|
+
// 只关心 @deepseek-ai 这一谱系:第三方库的版本线跟运行时不是一回事,交给正常的
|
|
259
|
+
// 模块解析去处理。取不到具体版本的一律跳过——猜一个版本去装,还不如不装。
|
|
260
|
+
function runtimeDependenciesOf(dir) {
|
|
261
|
+
let manifest;
|
|
262
|
+
try {
|
|
263
|
+
manifest = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
264
|
+
} catch {
|
|
265
|
+
return [];
|
|
266
|
+
}
|
|
267
|
+
const found = [];
|
|
268
|
+
for (const [name, range] of Object.entries(manifest.dependencies ?? {})) {
|
|
269
|
+
if (!name.startsWith('@deepseek-ai/')) continue;
|
|
270
|
+
const version = exactVersionOf(range);
|
|
271
|
+
if (version !== undefined) found.push({ name, version });
|
|
272
|
+
}
|
|
273
|
+
return found;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// 取一个包到本地缓存目录,已经缓存过就直接用。
|
|
277
|
+
export async function cachePackage(name, version, profileDir, deps) {
|
|
278
|
+
if (typeof profileDir !== 'string' || profileDir === '') return undefined;
|
|
279
|
+
const dir = join(profileDir, '.request-doctor-cache', ...`${name}@${version}`.split('/'));
|
|
280
|
+
try {
|
|
281
|
+
const manifest = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
282
|
+
if (manifest.version === version) return touchAndReturn(dir);
|
|
283
|
+
} catch {
|
|
284
|
+
// 没缓存,去取
|
|
285
|
+
}
|
|
286
|
+
const result = await fetchPackage(name, version, dir);
|
|
287
|
+
if (!result.ok) {
|
|
288
|
+
deps.record('fetch-failed', `${name}@${version} 取不回来:${result.error ?? ''}`);
|
|
289
|
+
return undefined;
|
|
290
|
+
}
|
|
291
|
+
deps.record('fetch', `${name}@${version} 已取回并缓存`);
|
|
292
|
+
return touchAndReturn(dir);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
// 给插件配上它当初的那个环境。
|
|
297
|
+
//
|
|
298
|
+
// 插件目录里的嵌套 node_modules 会遮住 profile 里的新包,每个垫片又转发现行实现,
|
|
299
|
+
// 于是插件看到的是自己期望的版本,跑的是当前代码。manifest 也得跟着对齐,
|
|
300
|
+
// 因为 DSH 在任何导入发生之前就要读它。
|
|
301
|
+
async function adapt(item, dir, facts, deps) {
|
|
302
|
+
const { probe, diagnostics, record, stats, warn, scanError } = deps;
|
|
303
|
+
const packageVersion = `${item.name}@${item.version}`;
|
|
304
|
+
// 缺符号时先把插件期望的真实旧版本取回来,再建环境。
|
|
305
|
+
const oldVersionDirs = await fetchNeededVersions(item, facts, deps);
|
|
306
|
+
const result = installCompatEnvironment({
|
|
307
|
+
pluginDir: dir,
|
|
308
|
+
profileDir: facts.profileDir,
|
|
309
|
+
...commonEnvironmentFields(item),
|
|
310
|
+
runtimeVersion: item.runtimeVersion,
|
|
311
|
+
resolveDir: probe.runtimeDirOf(facts),
|
|
312
|
+
oldVersionDirs,
|
|
313
|
+
closure: mergeClosures(oldVersionDirs),
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
if (result.status === 'refused') {
|
|
318
|
+
deps.scanError(`${packageVersion}: ${result.error}`);
|
|
319
|
+
record('compat-refused', `${packageVersion} cannot run here: ${result.error}`);
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
if (result.status === 'failed') {
|
|
323
|
+
deps.scanError(`${packageVersion}: ${result.error ?? 'unknown failure'}`);
|
|
324
|
+
warn(`request-doctor: compatibility environment failed for ${packageVersion}: ${result.error ?? ''}`);
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// 建好了不等于能用。逐条核对插件要的每个符号环境是否真能给出来,
|
|
329
|
+
// 有问题在这里就暴露,而不是等插件加载那一刻崩给用户看。
|
|
330
|
+
const verdict = verifyEnvironment({ pluginDir: dir, imports: item.importMap ?? new Map() });
|
|
331
|
+
if (!verdict.ok) {
|
|
332
|
+
const detail = verdict.problems.map((problem) => `${problem.name} ${problem.detail}`).join('; ');
|
|
333
|
+
deps.scanError(`${packageVersion}: 环境核对未通过:${detail}`);
|
|
334
|
+
record('compat-unverified', `${packageVersion} 的环境核对未通过:${detail}`);
|
|
335
|
+
return false;
|
|
336
|
+
}
|
|
337
|
+
// 运行时包装在归档里时核对不了,不算失败,但值得记一笔。
|
|
338
|
+
for (const note of verdict.unverified) {
|
|
339
|
+
record('compat-unchecked', `${packageVersion}: ${note.name} ${note.detail}`);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// 环境没变化也要对齐声明。
|
|
343
|
+
//
|
|
344
|
+
// 有些插件一个运行时包都不 import——皮肤、纯客户端界面就是这种。它们没有垫片要建,
|
|
345
|
+
// 环境自然是「没变化」,但 manifest 里照样声明了运行时版本。跳过对齐的话,
|
|
346
|
+
// 这类插件永远过不了管理器的检查,也永远显示不出来。
|
|
347
|
+
// 有垫片的包按运行时版本声明;只声明、没导入的 DSH 包也要一起对齐——
|
|
348
|
+
// 管理器的检查看的就是这份 manifest,漏一个它就不放行。
|
|
349
|
+
const served = Object.fromEntries([
|
|
350
|
+
...(result.packages ?? []).map((pkg) => [pkg, item.runtimeVersion]),
|
|
351
|
+
...Object.keys(item.requirements ?? {}).map((pkg) => [pkg, item.runtimeVersion]),
|
|
352
|
+
]);
|
|
353
|
+
const aligned = alignManifest({ pluginDir: dir, versions: served, runtimeVersion: item.runtimeVersion });
|
|
354
|
+
if (aligned.status === 'failed') {
|
|
355
|
+
deps.scanError(`${packageVersion}: ${aligned.error}`);
|
|
356
|
+
warn(`request-doctor: could not align ${packageVersion}: ${aligned.error}`);
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
if (result.status === 'unchanged' && aligned.status !== 'aligned') return true;
|
|
360
|
+
// 装的时候提前备好的环境,到这里已经落定,临时标记没用了。
|
|
361
|
+
if (item.deferred === true) clearStagedPreparation(dir);
|
|
362
|
+
stats.versionRepairs += 1;
|
|
363
|
+
record('compat', `${packageVersion} given a ${item.runtimeVersion} compatibility environment for ${(result.packages ?? []).join(', ') || 'no shims'}; declared versions aligned`);
|
|
364
|
+
return true;
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
// 审一遍所有插件,把结论写进诊断,并返回分了组的清单。
|
|
368
|
+
//
|
|
369
|
+
// 冲突只报告不动手:两个插件抢同一个 provider 时,谁该让位是人的判断,
|
|
370
|
+
// 插件贸然停用一个,用户看到的就是「莫名其妙少了功能」。
|
|
371
|
+
async function collectAudit(reason, attempt, bundles, facts, plugins, deps) {
|
|
372
|
+
const { diagnostics, probe, record } = deps;
|
|
373
|
+
const audited = await auditAll(bundles, facts, plugins, probe);
|
|
374
|
+
const roster = audited.length === 0 ? [] : providerRoster(facts, plugins, deps);
|
|
375
|
+
|
|
376
|
+
for (const entry of audited) {
|
|
377
|
+
entry.contention = describeContention({
|
|
378
|
+
name: entry.name,
|
|
379
|
+
providers: entry.providers ?? [],
|
|
380
|
+
conflicts: entry.conflicts ?? [],
|
|
381
|
+
roster,
|
|
382
|
+
});
|
|
383
|
+
if (entry.contention.contested.length > 0) {
|
|
384
|
+
record('route-report', `${entry.name}@${entry.version} shares ${entry.contention.contested.join(', ')}`);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
const approved = audited.filter((entry) => entry.verdict === 'compatible');
|
|
390
|
+
const rejected = audited.filter((entry) => entry.verdict !== 'compatible');
|
|
391
|
+
diagnostics.diagnostics.versionScan = {
|
|
392
|
+
reason,
|
|
393
|
+
at: new Date().toISOString(),
|
|
394
|
+
ok: true,
|
|
395
|
+
attempt,
|
|
396
|
+
bundles: bundles.length,
|
|
397
|
+
audited,
|
|
398
|
+
approved: approved.map((entry) => `${entry.name}@${entry.version}`),
|
|
399
|
+
rejected: rejected.map((entry) => ({ name: entry.name, verdict: entry.verdict, reasons: entry.reasons })),
|
|
400
|
+
};
|
|
401
|
+
for (const entry of rejected) {
|
|
402
|
+
record('version-review', `${entry.name}@${entry.version} not approved (${entry.verdict}): ${entry.reasons.slice(-1)[0] ?? ''}`);
|
|
403
|
+
}
|
|
404
|
+
return { approved, rejected };
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
// 重载刚适配好的插件。
|
|
409
|
+
//
|
|
410
|
+
// Loader 是在组合期检查 bundle 的,所以改过 manifest 之后必须重新组合才生效。
|
|
411
|
+
// 只碰这次真正适配过的插件,别人的状态一概不动。
|
|
412
|
+
async function reloadAdapted(manager, fixed, deps) {
|
|
413
|
+
let retry = false;
|
|
414
|
+
for (const packageVersion of fixed) {
|
|
415
|
+
const name = packageVersion.slice(0, packageVersion.lastIndexOf('@'));
|
|
416
|
+
if (name === '') continue;
|
|
417
|
+
try {
|
|
418
|
+
const applied = await manager.setBundleEnabled(name, true);
|
|
419
|
+
if (applied?.application === 'failed') {
|
|
420
|
+
const detail = JSON.stringify(applied.error ?? applied);
|
|
421
|
+
deps.scanError(`${name}: ${detail}`);
|
|
422
|
+
if (NESTED_HMR.test(detail)) retry = true;
|
|
423
|
+
}
|
|
424
|
+
} catch (error) {
|
|
425
|
+
const detail = String(error);
|
|
426
|
+
deps.scanError(`${name}: ${detail}`);
|
|
427
|
+
if (NESTED_HMR.test(detail)) retry = true;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return retry;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
// 跑一轮修复。
|
|
435
|
+
async function run(reason, manager, attempt, deps) {
|
|
436
|
+
if (deps === undefined) throw new Error('NO_DEPS:' + new Error().stack);
|
|
437
|
+
const { diagnostics, config, record, later, scanError } = deps;
|
|
438
|
+
const { probe, stats, warn } = deps;
|
|
439
|
+
if (manager === undefined || manager === null) {
|
|
440
|
+
diagnostics.diagnostics.versionScan = { reason, at: new Date().toISOString(), ok: false, error: 'pluginManager service is unavailable' };
|
|
441
|
+
diagnostics.scheduleWrite();
|
|
442
|
+
return [];
|
|
443
|
+
}
|
|
444
|
+
if (!config().autoVersionRepair || deps.state.busy) return [];
|
|
445
|
+
deps.state.busy = true;
|
|
446
|
+
try {
|
|
447
|
+
const bundles = await manager.listBundles();
|
|
448
|
+
const facts = probe.runtimeFacts();
|
|
449
|
+
// One roster lookup serves both the mismatch scan and the route analysis.
|
|
450
|
+
let plugins;
|
|
451
|
+
try {
|
|
452
|
+
plugins = await manager.listPlugins?.();
|
|
453
|
+
} catch {
|
|
454
|
+
plugins = undefined;
|
|
455
|
+
}
|
|
456
|
+
const { approved, rejected } = await collectAudit(reason, attempt, bundles, facts, plugins, deps);
|
|
457
|
+
|
|
458
|
+
const fixed = [];
|
|
459
|
+
// Every compatible plugin is adapted, not just the first few: a profile can
|
|
460
|
+
// carry a dozen mismatched plugins. The cap is a per-scan safety valve, and
|
|
461
|
+
// `0` means no limit.
|
|
462
|
+
const budget = config().maxVersionRepairs === 0 ? approved.length : config().maxVersionRepairs;
|
|
463
|
+
for (const item of approved.slice(0, budget)) {
|
|
464
|
+
const dir = facts.profileDir === undefined ? undefined : packageDir(facts.profileDir, item.name);
|
|
465
|
+
if (dir === undefined) {
|
|
466
|
+
deps.scanError(`${item.name}@${item.version}: installed directory not found`);
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
if (await adapt(item, dir, facts, deps)) fixed.push(item.name + "@" + item.version);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const retry = await reloadAdapted(manager, fixed, deps);
|
|
473
|
+
|
|
474
|
+
// 扫完顺手清一下缓存。版本搜索会把试过的版本都下载下来,不清理只会越长越大。
|
|
475
|
+
const pruned = pruneCache(facts.profileDir);
|
|
476
|
+
if (pruned > 0) record('cache-pruned', `清掉 ${pruned} 个冷缓存`);
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
diagnostics.scheduleWrite();
|
|
480
|
+
if (retry && attempt < 6) {
|
|
481
|
+
const waitMs = Math.min(2000 * 2 ** attempt, 60000);
|
|
482
|
+
later(() => {
|
|
483
|
+
run(`${reason}-retry`, manager, attempt + 1).catch(() => {});
|
|
484
|
+
}, waitMs);
|
|
485
|
+
}
|
|
486
|
+
return fixed;
|
|
487
|
+
} catch (error) {
|
|
488
|
+
diagnostics.diagnostics.versionScan = { reason, at: new Date().toISOString(), ok: false, error: String(error) };
|
|
489
|
+
diagnostics.scheduleWrite();
|
|
490
|
+
warn(`request-doctor: version scan failed: ${String(error)}`);
|
|
491
|
+
return [];
|
|
492
|
+
} finally {
|
|
493
|
+
deps.state.busy = false;
|
|
494
|
+
}
|
|
495
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// 决定去试哪些版本。
|
|
2
|
+
//
|
|
3
|
+
// 只钉一个版本是不够的:插件声明的范围可能覆盖好几个版本,而它要的那个符号未必在
|
|
4
|
+
// 范围里最新的那个。既然旧代码都还在仓库里,就按顺序挨个试,第一个能凑齐符号的就是答案。
|
|
5
|
+
import { compareVersions, satisfies } from './semver.js';
|
|
6
|
+
|
|
7
|
+
// 一次最多试这么多个版本。够用,也不至于把注册表打爆。
|
|
8
|
+
const MAX_CANDIDATES = 8;
|
|
9
|
+
|
|
10
|
+
// 从旧到新排好序,再去掉重复。
|
|
11
|
+
//
|
|
12
|
+
// 注册表里偶尔有无法解析的版本号。排序不能因此整个垮掉——那会让搜索直接失效,
|
|
13
|
+
// 所以解析不了的一律当作「最旧」,排在后面。
|
|
14
|
+
// 比较两个版本号,解析不了就当作相等——排序不能因为一个坏版本号整个垮掉。
|
|
15
|
+
export function safeCompare(a, b) {
|
|
16
|
+
try {
|
|
17
|
+
return compareVersions(a, b) ?? 0;
|
|
18
|
+
} catch {
|
|
19
|
+
return 0;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 从旧到新排好序,再去掉重复。
|
|
24
|
+
function sorted(versions) {
|
|
25
|
+
return [...new Set(versions)].filter((version) => typeof version === 'string' && version !== '').sort(safeCompare).reverse();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 排出候选顺序。
|
|
29
|
+
//
|
|
30
|
+
// 优先级:声明范围里最新的 → 其余版本最新的。范围是第一顺位,因为那是插件自己说过的
|
|
31
|
+
// 「我认这些」;范围里都没有才退而求其次,毕竟能跑起来比守着声明重要。
|
|
32
|
+
export function orderCandidates(versions, requirement, preferred) {
|
|
33
|
+
const all = sorted(versions);
|
|
34
|
+
const inRange = typeof requirement === 'string' ? all.filter((version) => satisfies(version, requirement) === true) : [];
|
|
35
|
+
const ordered = [];
|
|
36
|
+
const push = (version) => {
|
|
37
|
+
if (typeof version === 'string' && version !== '' && !ordered.includes(version)) ordered.push(version);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
push(preferred);
|
|
41
|
+
for (const version of inRange) push(version);
|
|
42
|
+
for (const version of all) push(version);
|
|
43
|
+
return ordered.slice(0, MAX_CANDIDATES);
|
|
44
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Request Doctor",
|
|
4
|
+
"description": "Automatically repairs request failures caused by the local client (transport, timeout, oversized context, missing adapter) and resolves plugin/DSH version mismatches that block installed bundles."
|
|
5
|
+
},
|
|
6
|
+
"tabLabel": "Incompatible install",
|
|
7
|
+
"intro": "These plugins are refused by the running DSH version. Installing one takes a single step: grant the version exemption the manager asks for, then let it install. Declaration alignment and the compatibility environment are filled in afterwards, so what runs is the current implementation and what the manifest says is the current version.",
|
|
8
|
+
"placeholder": "package@version, e.g. dsh-ssh-ops@0.4.0",
|
|
9
|
+
"installTyped": "Allow and install",
|
|
10
|
+
"fixAndInstall": "Allow and install",
|
|
11
|
+
"requires": "Asks to run on {{runtime}}",
|
|
12
|
+
"none": "Nothing is being refused right now.",
|
|
13
|
+
"working": "Working on {{target}}…",
|
|
14
|
+
"installDone": "{{target}} installed; the compatibility environment follows in the background.",
|
|
15
|
+
"installFailed": "Could not install: {{reason}}",
|
|
16
|
+
"listFailed": "Could not read the plugin list."
|
|
17
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "请求急救员",
|
|
4
|
+
"description": "自动修复由本机客户端造成的请求失败(传输中断、超时、上下文超限、适配器缺失),并自动解决插件与 DSH 版本不一致导致无法加载的问题。"
|
|
5
|
+
},
|
|
6
|
+
"tabLabel": "不兼容安装",
|
|
7
|
+
"intro": "下面是被当前 DSH 版本拒装的插件。装它们只需要一步:先按管理器的规则授权这个版本,再让它自己装。装完之后,声明对齐和兼容环境会自动补上——跑的是当前实现,声明说的也是当前版本。",
|
|
8
|
+
"placeholder": "包名@版本,例如 dsh-ssh-ops@0.4.0",
|
|
9
|
+
"installTyped": "授权并安装",
|
|
10
|
+
"fixAndInstall": "授权并安装",
|
|
11
|
+
"requires": "要求运行在 {{runtime}}",
|
|
12
|
+
"none": "当前没有被拒装的插件。",
|
|
13
|
+
"working": "正在处理 {{target}}…",
|
|
14
|
+
"installDone": "{{target}} 已装好,兼容环境会在后台补上。",
|
|
15
|
+
"installFailed": "没能装上:{{reason}}",
|
|
16
|
+
"listFailed": "读不到插件列表。"
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luolingp/request-doctor",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.11.0",
|
|
4
|
+
"description": "自动修复客户端引起的请求失败,并让版本不匹配的 DSH 插件能装上、能跑起来。",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"dsh-plugin",
|
|
7
|
+
"deepseek-harness",
|
|
8
|
+
"retry",
|
|
9
|
+
"compatibility"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/LuoLingP/@luolingp/request-doctor.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/LuoLingP/@luolingp/request-doctor/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/LuoLingP/@luolingp/request-doctor#readme",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./index.js",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": "./index.js",
|
|
24
|
+
"./client": "./client.js",
|
|
25
|
+
"./package.json": "./package.json",
|
|
26
|
+
"./locale/*.json": "./locale/*.json",
|
|
27
|
+
"./icon": "./icon.svg"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"index.js",
|
|
31
|
+
"client.js",
|
|
32
|
+
"lib",
|
|
33
|
+
"cordis.patch.yml",
|
|
34
|
+
"locale",
|
|
35
|
+
"icon.svg",
|
|
36
|
+
"docs"
|
|
37
|
+
],
|
|
38
|
+
"engines": {
|
|
39
|
+
"node": ">=20"
|
|
40
|
+
},
|
|
41
|
+
"dsh": {
|
|
42
|
+
"bundle": {
|
|
43
|
+
"patch": "./cordis.patch.yml"
|
|
44
|
+
},
|
|
45
|
+
"client": {
|
|
46
|
+
"platform": "web",
|
|
47
|
+
"immediately": true,
|
|
48
|
+
"inject": [
|
|
49
|
+
"@deepseek-ai/dsh-api-remotes",
|
|
50
|
+
"@deepseek-ai/dsh-client-ui-plugin-manager"
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
}
|
|
6
54
|
}
|