dsh-live-compact 1.1.14 → 1.1.15
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/CHANGELOG.md +13 -0
- package/README.md +43 -16
- package/adapter.js +43 -4
- package/bin/dsh-live-compact.js +44 -11
- package/bin/shadow-diagnostics.js +181 -0
- package/client.js +21 -3
- package/gate.js +22 -22
- package/index.js +336 -187
- package/package.json +9 -7
- package/plan.js +5 -3
- package/settings.js +40 -1
- package/shadow-search.js +26 -7
- package/shadow.js +95 -105
- package/shell.js +102 -58
package/shell.js
CHANGED
|
@@ -5,9 +5,10 @@
|
|
|
5
5
|
* 预检查只解析配置,校验交给实际引擎;构造失败时释放本次注册,不重试原版盖掉报错。
|
|
6
6
|
* Resolve config before construction; let the engine validate it and preserve its actual error.
|
|
7
7
|
*/
|
|
8
|
-
import { contextProperty, loadBase, loadCompaction, loadSchema, STOCK_ENGINE } from "./adapter.js";
|
|
8
|
+
import { contextProperty, entryOf, loadBase, loadCompaction, loadSchema, STOCK_ENGINE } from "./adapter.js";
|
|
9
9
|
import { configSchema, engineConfig } from "./settings.js";
|
|
10
10
|
import { installPresetGate } from "./gate.js";
|
|
11
|
+
import { appendDecision, shadowDirectory } from "./shadow.js";
|
|
11
12
|
|
|
12
13
|
const CORDIS_INIT = Symbol.for("cordis.init");
|
|
13
14
|
const ISOLATE = Symbol.for("cordis.isolate");
|
|
@@ -45,6 +46,11 @@ try {
|
|
|
45
46
|
if (!loadError) loadError = error instanceof Error ? error : new Error(String(error));
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
if (loadError) {
|
|
50
|
+
const code = typeof loadError.code === "string" ? `${loadError.code} ` : "";
|
|
51
|
+
console.error(`spine: 引擎模块加载失败。${code}${reasonText(loadError)}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
48
54
|
// 官方设置界面按这份 schema 自动生成表单。拿不到 schemastery 就退回 stock 的 schema。
|
|
49
55
|
try {
|
|
50
56
|
const schema = await loadSchema();
|
|
@@ -62,44 +68,48 @@ export function preflightConfig(config = {}) {
|
|
|
62
68
|
}
|
|
63
69
|
|
|
64
70
|
function compactionRecord(ctx) {
|
|
65
|
-
const key = ctx
|
|
66
|
-
return key === undefined ? undefined : ctx
|
|
71
|
+
const key = contextProperty(ctx, ISOLATE)?.compaction;
|
|
72
|
+
return key === undefined ? undefined : contextProperty(ctx, "reflect")?.store?.[key];
|
|
67
73
|
}
|
|
68
74
|
|
|
69
|
-
function releaseCompaction(ctx, previous, key = ctx
|
|
70
|
-
const store = ctx
|
|
75
|
+
function releaseCompaction(ctx, previous, key = contextProperty(ctx, ISOLATE)?.compaction) {
|
|
76
|
+
const store = contextProperty(ctx, "reflect")?.store;
|
|
71
77
|
const current = key === undefined ? undefined : store?.[key];
|
|
72
|
-
const currentFiber = current
|
|
73
|
-
const contextFiber = ctx
|
|
74
|
-
if (!current || current === previous || currentFiber !== contextFiber) return false;
|
|
78
|
+
const currentFiber = contextProperty(current, "fiber") ?? contextProperty(contextProperty(current, "ctx"), "fiber");
|
|
79
|
+
const contextFiber = contextProperty(ctx, "fiber");
|
|
80
|
+
if (!current || current === previous || !contextFiber || currentFiber !== contextFiber) return false;
|
|
75
81
|
delete store[key];
|
|
76
|
-
|
|
82
|
+
const fiberStore = contextProperty(contextFiber, "store");
|
|
83
|
+
if (fiberStore?.compaction === current) delete fiberStore.compaction;
|
|
77
84
|
return true;
|
|
78
85
|
}
|
|
79
86
|
|
|
80
87
|
function detachRootStockCompaction(ctx) {
|
|
81
|
-
const store = ctx
|
|
88
|
+
const store = contextProperty(ctx, "reflect")?.store;
|
|
82
89
|
const current = compactionRecord(ctx);
|
|
83
|
-
const key = ctx
|
|
84
|
-
const fiber = current
|
|
85
|
-
const
|
|
86
|
-
|
|
90
|
+
const key = contextProperty(ctx, ISOLATE)?.compaction;
|
|
91
|
+
const fiber = contextProperty(current, "fiber") ?? contextProperty(contextProperty(current, "ctx"), "fiber");
|
|
92
|
+
const contextFiber = contextProperty(ctx, "fiber");
|
|
93
|
+
const entry = entryOf(fiber);
|
|
94
|
+
const moduleName = contextProperty(entry, "options")?.name;
|
|
95
|
+
if (!current || !contextFiber || !fiber || moduleName !== STOCK_ENGINE || fiber === contextFiber) return false;
|
|
87
96
|
if (compactionRecord(ctx) !== current) return false;
|
|
88
|
-
const fiberStore = fiber
|
|
97
|
+
const fiberStore = contextProperty(fiber, "store");
|
|
89
98
|
if (fiberStore?.compaction === current) delete fiberStore.compaction;
|
|
90
99
|
delete store[key];
|
|
91
|
-
return { store, key, current, fiber,
|
|
100
|
+
return { store, key, current, fiber, entry, fiberStore,
|
|
101
|
+
uid: contextProperty(fiber, "uid"), epoch: contextProperty(fiber, "_runner")?.epoch };
|
|
92
102
|
}
|
|
93
103
|
|
|
94
104
|
function commitRootStockCompaction(replacement) {
|
|
95
105
|
if (!replacement || replacement.committed) return false;
|
|
96
|
-
const options = replacement.
|
|
106
|
+
const options = contextProperty(replacement.entry, "options");
|
|
97
107
|
const disabled = options && Object.getOwnPropertyDescriptor(options, "disabled");
|
|
98
108
|
try {
|
|
99
109
|
// The loader writes unrequested disposals back as disabled. Mark this
|
|
100
110
|
// synchronous notification as intentional without changing its config.
|
|
101
111
|
if (options) options.disabled = true;
|
|
102
|
-
replacement.retirement = Promise.resolve(replacement.fiber
|
|
112
|
+
replacement.retirement = Promise.resolve(contextProperty(replacement.fiber, "dispose")?.call(replacement.fiber));
|
|
103
113
|
replacement.retirement.catch(() => {});
|
|
104
114
|
replacement.committed = true;
|
|
105
115
|
} finally {
|
|
@@ -111,14 +121,17 @@ function commitRootStockCompaction(replacement) {
|
|
|
111
121
|
|
|
112
122
|
function rollbackRootStockCompaction(replacement) {
|
|
113
123
|
if (!replacement || replacement.committed) return false;
|
|
114
|
-
const { fiber, uid, epoch, key } = replacement;
|
|
115
|
-
const
|
|
116
|
-
const
|
|
117
|
-
if (
|
|
118
|
-
(
|
|
119
|
-
(scope && scope.compaction !== key) || entry
|
|
120
|
-
|
|
121
|
-
|
|
124
|
+
const { fiber, entry, uid, epoch, key } = replacement;
|
|
125
|
+
const scope = contextProperty(contextProperty(replacement.current, "ctx"), ISOLATE);
|
|
126
|
+
const state = contextProperty(fiber, "state");
|
|
127
|
+
if (uid === null || contextProperty(fiber, "uid") !== uid || contextProperty(fiber, "_runner")?.epoch !== epoch ||
|
|
128
|
+
(state !== undefined && ![1, 2].includes(state)) ||
|
|
129
|
+
(scope && scope.compaction !== key) || contextProperty(entry, "disabled")) return false;
|
|
130
|
+
const parent = contextProperty(entry, "parent");
|
|
131
|
+
const tree = contextProperty(parent, "tree");
|
|
132
|
+
const entryFiber = contextProperty(entry, "fiber");
|
|
133
|
+
if (tree && (!currentLoaderEntry(entry) || contextProperty(contextProperty(entryFiber, "ctx"), "fiber") !== fiber ||
|
|
134
|
+
![1, 2].includes(loaderFiberState(parent)) || ![1, 2].includes(loaderFiberState(tree)))) return false;
|
|
122
135
|
const current = replacement.store?.[replacement.key];
|
|
123
136
|
if (current !== undefined && current !== replacement.current) return false;
|
|
124
137
|
replacement.store[replacement.key] = replacement.current;
|
|
@@ -145,47 +158,56 @@ export function releaseRootStockCompaction(ctx, { deferDispose = false } = {}) {
|
|
|
145
158
|
}
|
|
146
159
|
|
|
147
160
|
function currentLoaderEntry(entry) {
|
|
148
|
-
return entry && entry
|
|
161
|
+
return entry && contextProperty(contextProperty(entry, "parent"), "tree")?.store?.[contextProperty(entry, "options")?.id] === entry;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function loaderFiberState(loader) {
|
|
165
|
+
return contextProperty(contextProperty(contextProperty(loader, "ctx"), "fiber"), "state");
|
|
149
166
|
}
|
|
150
167
|
|
|
151
168
|
function configuredRootStock(ctx) {
|
|
152
|
-
const owner = ctx
|
|
153
|
-
if (owner
|
|
154
|
-
const stock = owner
|
|
155
|
-
|
|
169
|
+
const owner = entryOf(contextProperty(ctx, "fiber"), ctx);
|
|
170
|
+
if (contextProperty(owner, "options")?.id !== "compaction-spine") return undefined;
|
|
171
|
+
const stock = contextProperty(contextProperty(owner, "parent"), "tree")?.store?.["compaction-basic"];
|
|
172
|
+
const options = contextProperty(stock, "options");
|
|
173
|
+
return options?.name === STOCK_ENGINE && options.isolate?.compaction !== true && !contextProperty(stock, "disabled") ? stock : undefined;
|
|
156
174
|
}
|
|
157
175
|
|
|
158
176
|
function sameCompactionRealm(ctx, entry) {
|
|
159
|
-
|
|
177
|
+
const scope = contextProperty(ctx, ISOLATE);
|
|
178
|
+
const other = contextProperty(contextProperty(entry, "ctx"), ISOLATE);
|
|
179
|
+
return Boolean(scope && other && scope.compaction === other.compaction);
|
|
160
180
|
}
|
|
161
181
|
|
|
162
182
|
function activationCheck(ctx) {
|
|
163
|
-
const fiber = ctx
|
|
164
|
-
const entry = fiber
|
|
165
|
-
const uid = fiber
|
|
166
|
-
const epoch = fiber
|
|
167
|
-
const scope = ctx
|
|
168
|
-
return () => !fiber
|
|
169
|
-
|
|
183
|
+
const fiber = contextProperty(ctx, "fiber");
|
|
184
|
+
const entry = entryOf(fiber, ctx);
|
|
185
|
+
const uid = contextProperty(fiber, "uid");
|
|
186
|
+
const epoch = contextProperty(fiber, "_runner")?.epoch;
|
|
187
|
+
const scope = contextProperty(ctx, ISOLATE);
|
|
188
|
+
return () => !contextProperty(fiber, "runtime") ||
|
|
189
|
+
(uid != null && contextProperty(fiber, "uid") === uid && contextProperty(fiber, "_runner")?.epoch === epoch &&
|
|
190
|
+
contextProperty(ctx, ISOLATE) === scope && (!entry || (currentLoaderEntry(entry) && !contextProperty(entry, "disabled"))));
|
|
170
191
|
}
|
|
171
192
|
|
|
172
193
|
function restoreStockOnUnload(ctx, replacement) {
|
|
173
|
-
const entry = replacement.
|
|
174
|
-
|
|
194
|
+
const entry = replacement.entry;
|
|
195
|
+
const update = contextProperty(entry, "update");
|
|
196
|
+
if (typeof update !== "function") return;
|
|
175
197
|
const installed = compactionRecord(ctx);
|
|
176
198
|
ctx.effect(() => async () => {
|
|
177
199
|
if (!replacement.committed) return;
|
|
178
200
|
await replacement.retirement;
|
|
179
|
-
const parent = entry
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
!
|
|
183
|
-
entry.ctx?.[ISOLATE]?.compaction !== ctx[ISOLATE]?.compaction) return;
|
|
201
|
+
const parent = contextProperty(entry, "parent");
|
|
202
|
+
if (!currentLoaderEntry(entry) || contextProperty(entry, "disabled") || ![1, 2].includes(loaderFiberState(parent)) ||
|
|
203
|
+
![1, 2].includes(loaderFiberState(contextProperty(parent, "tree"))) ||
|
|
204
|
+
!sameCompactionRealm(ctx, entry)) return;
|
|
184
205
|
const current = compactionRecord(ctx);
|
|
185
206
|
if (current && current !== installed) return;
|
|
186
207
|
releaseCompaction(ctx, replacement.current);
|
|
187
|
-
await
|
|
188
|
-
|
|
208
|
+
await update.call(entry, {}, false, true);
|
|
209
|
+
const fiber = contextProperty(entry, "fiber");
|
|
210
|
+
await contextProperty(fiber, "await")?.call(fiber);
|
|
189
211
|
});
|
|
190
212
|
}
|
|
191
213
|
|
|
@@ -193,6 +215,7 @@ function visibleTakeover(ctx, reason) {
|
|
|
193
215
|
const line = `spine: ${MARK}。${reason}`;
|
|
194
216
|
console.error(line);
|
|
195
217
|
try { contextProperty(ctx, "logger")?.warn?.(line); } catch {}
|
|
218
|
+
appendDecision(shadowDirectory(), { phase: "takeover", reason: "activation-failed", detail: reason });
|
|
196
219
|
}
|
|
197
220
|
|
|
198
221
|
const MARK = "本会话未接管";
|
|
@@ -224,14 +247,17 @@ export default class LiveCompactEntry {
|
|
|
224
247
|
constructor(ctx, config) {
|
|
225
248
|
const isCurrent = activationCheck(ctx);
|
|
226
249
|
const stock = configuredRootStock(ctx);
|
|
227
|
-
|
|
250
|
+
const stockFiber = contextProperty(stock, "fiber");
|
|
251
|
+
const init = contextProperty(stock, "_initTask");
|
|
252
|
+
if (stock && (init || (contextProperty(stockFiber, "state") === 1 && sameCompactionRealm(ctx, stock)))) {
|
|
228
253
|
// Loader mounts siblings in parallel. Wait for the configured stock
|
|
229
254
|
// entry to finish publishing before choosing the owner to replace.
|
|
230
255
|
return { [CORDIS_INIT]: async () => {
|
|
231
|
-
try { await
|
|
256
|
+
try { await init; } catch {} // The loader reports import failures.
|
|
232
257
|
if (!isCurrent() || configuredRootStock(ctx) !== stock) return;
|
|
233
258
|
if (sameCompactionRealm(ctx, stock)) {
|
|
234
|
-
|
|
259
|
+
const fiber = contextProperty(stock, "fiber");
|
|
260
|
+
try { await contextProperty(fiber, "await")?.call(fiber); } catch {} // The loader reports activation failures.
|
|
235
261
|
}
|
|
236
262
|
if (!isCurrent() || configuredRootStock(ctx) !== stock) return;
|
|
237
263
|
const engine = this.activate(ctx, config, isCurrent);
|
|
@@ -266,9 +292,27 @@ export default class LiveCompactEntry {
|
|
|
266
292
|
if (!isCurrent()) { rollback(); return; }
|
|
267
293
|
if (deferred) engine.activateAfterInit(resolvedConfig.auto ?? true);
|
|
268
294
|
const commitGate = installPresetGate(ctx, config, { deferCommit: true });
|
|
269
|
-
if (rootStockReplacement)
|
|
270
|
-
|
|
271
|
-
|
|
295
|
+
if (!rootStockReplacement) { commitGate?.(); return; }
|
|
296
|
+
|
|
297
|
+
const installed = compactionRecord(ctx);
|
|
298
|
+
const fiber = contextProperty(ctx, "fiber");
|
|
299
|
+
const installedFiber = contextProperty(installed, "fiber") ?? contextProperty(contextProperty(installed, "ctx"), "fiber");
|
|
300
|
+
if (!installed || installedFiber !== fiber) throw new Error("spine: replacement compaction was not registered");
|
|
301
|
+
const { store, key } = rootStockReplacement;
|
|
302
|
+
restoreStockOnUnload(ctx, rootStockReplacement);
|
|
303
|
+
rootStockReplacement.commit();
|
|
304
|
+
const finishTakeover = () => {
|
|
305
|
+
if (!isCurrent()) return;
|
|
306
|
+
// Older Cordis deletes the shared slot when the retired provider's
|
|
307
|
+
// asynchronous disposer runs. Its replacement still owns its record.
|
|
308
|
+
if (store[key] === undefined && contextProperty(ctx, ISOLATE)?.compaction === key) {
|
|
309
|
+
store[key] = installed;
|
|
310
|
+
const reflect = contextProperty(ctx, "reflect");
|
|
311
|
+
if (contextProperty(fiber, "state") === 2) contextProperty(reflect, "notify")?.call(reflect, ["compaction"]);
|
|
312
|
+
}
|
|
313
|
+
if (compactionRecord(ctx) === installed) commitGate?.();
|
|
314
|
+
};
|
|
315
|
+
return rootStockReplacement.retirement.then(finishTakeover, finishTakeover);
|
|
272
316
|
};
|
|
273
317
|
try {
|
|
274
318
|
rootStockReplacement = releaseRootStockCompaction(ctx, { deferDispose: true });
|
|
@@ -277,11 +321,11 @@ export default class LiveCompactEntry {
|
|
|
277
321
|
const deferActivation = Boolean(rootStockReplacement && typeof Engine.prototype[CORDIS_INIT] === "function");
|
|
278
322
|
const engine = new Engine(ctx, config, resolvedConfig, { strictActivation: true, deferActivation });
|
|
279
323
|
const initialize = engine[CORDIS_INIT];
|
|
280
|
-
if (typeof initialize === "function") {
|
|
324
|
+
if (typeof initialize === "function" || rootStockReplacement) {
|
|
281
325
|
engine[CORDIS_INIT] = async () => {
|
|
282
326
|
try {
|
|
283
|
-
const dispose = await initialize.call(engine);
|
|
284
|
-
commit(engine, deferActivation);
|
|
327
|
+
const dispose = typeof initialize === "function" ? await initialize.call(engine) : undefined;
|
|
328
|
+
await commit(engine, deferActivation);
|
|
285
329
|
return dispose;
|
|
286
330
|
} catch (error) {
|
|
287
331
|
rollback();
|
|
@@ -298,7 +342,7 @@ export default class LiveCompactEntry {
|
|
|
298
342
|
}
|
|
299
343
|
}
|
|
300
344
|
cause ??= new Error("spine: engine missing");
|
|
301
|
-
if (previousCompaction || (constructed && ctx
|
|
345
|
+
if (previousCompaction || (constructed && contextProperty(contextProperty(ctx, "fiber"), "runtime"))) {
|
|
302
346
|
visibleTakeover(ctx, reasonText(cause));
|
|
303
347
|
throw cause;
|
|
304
348
|
}
|