dsh-taskbridge 1.5.4 → 1.7.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/CHANGELOG.md +80 -59
- package/README.md +51 -50
- package/THIRD_PARTY_NOTICES.txt +23 -0
- package/bin/dsh-delegate-mcp.d.ts +1 -0
- package/bin/dsh-delegate-mcp.js +1 -0
- package/codex/dsh-taskbridge.toml +148 -71
- package/docs/INTRODUCTION.md +44 -22
- package/docs/INTRODUCTION.zh.md +32 -26
- package/docs/codex-integration.md +99 -19
- package/docs/contracts-stage-b.md +4 -4
- package/docs/conversation-placement.md +25 -7
- package/docs/plugin-market.md +1 -1
- package/docs/releases/1.6.0.zh.md +11 -0
- package/docs/releases/1.7.0.zh.md +11 -0
- package/docs/runtime-compatibility.md +10 -11
- package/docs/silent-wait.md +69 -31
- package/docs/taskbridge-upgrade.md +1 -1
- package/docs/worker-permissions.md +11 -21
- package/docs/workspace-grouping.md +126 -0
- package/lib/client.d.ts +581 -0
- package/lib/client.js +332 -3461
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +7 -19
- package/src/client/api.js +895 -90
- package/src/client/index.jsx +1363 -296
- package/src/client/sidebar.jsx +862 -0
- package/src/client/styles.js +340 -0
- package/src/client/task-cleanup.js +304 -0
- package/src/client/task-records-table.jsx +217 -0
- package/src/client/task-view.js +307 -0
- package/src/client/workspace-groups.js +482 -0
- package/src/client/workspaces-browser.jsx +770 -0
- package/src/codex/agent-ownership.js +270 -0
- package/src/codex/installer.js +528 -35
- package/src/codex/seam.js +5 -0
- package/src/config/defaults.js +63 -17
- package/src/config/settings.js +60 -62
- package/src/config/validate.js +126 -71
- package/src/http/execution-plane.js +200 -1
- package/src/http/respond.js +26 -2
- package/src/http/user-plane.js +566 -2
- package/src/index.d.ts +55 -0
- package/src/index.js +58 -42
- package/src/jobs/activity.js +968 -78
- package/src/jobs/engine.js +1010 -230
- package/src/jobs/history.js +224 -12
- package/src/jobs/plan-store.js +1020 -0
- package/src/jobs/record-cleanup.js +196 -0
- package/src/jobs/task-display.js +73 -0
- package/src/jobs/task-tree.js +786 -0
- package/src/jobs/title.js +129 -12
- package/src/mcp/server.js +206 -10
- package/src/mcp/wait.js +64 -5
- package/src/runtime/agent-runner.js +101 -15
- package/src/runtime/worker-prompt.js +53 -0
- package/src/workspace/locks.js +288 -167
- package/docs/assets/taskbridge-workflow.en.png +0 -0
- package/docs/assets/taskbridge-workflow.zh.png +0 -0
- package/docs/releases/1.4.0-token-benchmark.zh.md +0 -40
- package/docs/releases/1.4.0.zh.md +0 -54
- package/docs/releases/1.4.1.zh.md +0 -59
- package/docs/releases/1.4.2.zh.md +0 -23
- package/docs/releases/1.5.0-token-benchmark.zh.md +0 -73
- package/docs/releases/1.5.0.zh.md +0 -51
- package/docs/releases/1.5.1.zh.md +0 -70
- package/docs/releases/1.5.2.zh.md +0 -30
- package/docs/releases/1.5.3-test.1.zh.md +0 -28
- package/docs/releases/1.5.3-test.2.zh.md +0 -24
- package/docs/releases/1.5.3.zh.md +0 -33
- package/docs/releases/1.5.4.zh.md +0 -25
- package/src/review/preset.js +0 -868
- package/src/review/prompts.js +0 -145
- package/src/review/reviewer.js +0 -933
package/src/review/preset.js
DELETED
|
@@ -1,868 +0,0 @@
|
|
|
1
|
-
// The review composition: a dedicated, plugin-owned native agent preset.
|
|
2
|
-
//
|
|
3
|
-
// Why this module exists. An Agent that joins no preset has no tools at all, and
|
|
4
|
-
// the deployment's DEFAULT preset is not a read-only surface: the shipped
|
|
5
|
-
// `standard` composition mounts `dsh-tool-subagent`, whose tool is registered
|
|
6
|
-
// into the composing agent's OWN scope layer before the agent's setup hook runs.
|
|
7
|
-
// A scope-local registration is exactly what `tools.restrict()` cannot remove
|
|
8
|
-
// (see `ToolRuntime.view()`: a restriction filters what a scope INHERITS and
|
|
9
|
-
// never what its own layer registers). A reviewer that joins the default preset
|
|
10
|
-
// therefore advertises `subagent` and cannot honestly be called read-only.
|
|
11
|
-
//
|
|
12
|
-
// The fix is composition, not a wider allow-list: register ONE dedicated preset
|
|
13
|
-
// whose rows are only the filesystem readers, so there is no delegation row to
|
|
14
|
-
// inject anything agent-local in the first place. The scoped restriction in
|
|
15
|
-
// `reviewer.js` still removes `write`/`edit` (which `dsh-tool-fs` registers into
|
|
16
|
-
// the preset plane unless told otherwise) and the post-install proof still
|
|
17
|
-
// re-reads the advertised surface.
|
|
18
|
-
//
|
|
19
|
-
// Readiness is a FINITE STATE, not a boolean:
|
|
20
|
-
//
|
|
21
|
-
// idle -> preparing -> ready
|
|
22
|
-
// preparing -> idle (a failure a later review may retry)
|
|
23
|
-
// any state -> disposed (the plugin unloaded)
|
|
24
|
-
//
|
|
25
|
-
// and the transitions exist to make four race conditions unrepresentable:
|
|
26
|
-
//
|
|
27
|
-
// 1. NOTHING becomes ready before it is VALIDATED. `register()` resolves for a
|
|
28
|
-
// definition whose rows failed to mount (the registry records the diagnostic
|
|
29
|
-
// and `resolve()` reports it), so "registered" is not "usable". A resolve
|
|
30
|
-
// that fails, times out or reports a broken row retires that registration and
|
|
31
|
-
// returns the composition to `idle`; a later review then starts a NEW
|
|
32
|
-
// attempt instead of mounting a composition nothing ever proved.
|
|
33
|
-
// 2. A registration that finishes after the caller that asked for it has given
|
|
34
|
-
// up is retired immediately and never publishes its id. Readiness is
|
|
35
|
-
// published only by the attempt that completes the whole sequence.
|
|
36
|
-
// 3. Concurrent preparers each honour their OWN deadline and signal. They share
|
|
37
|
-
// the registration work, but a caller that times out or is cancelled gets
|
|
38
|
-
// its own distinct refusal without cancelling the other callers' work.
|
|
39
|
-
// 4. A late registration from an abandoned attempt, or one from a registry this
|
|
40
|
-
// instance no longer uses, is retired rather than republishing ownership.
|
|
41
|
-
// 5. A registration that RESOLVES after `dispose()` has completed is retired on
|
|
42
|
-
// arrival: it is never claimed, never validated, never published, and its
|
|
43
|
-
// disposer runs exactly once. Readiness is published only from a claim this
|
|
44
|
-
// instance made while it was still usable.
|
|
45
|
-
//
|
|
46
|
-
// Contract notes this module is shaped by, read from the installed
|
|
47
|
-
// `@deepseek-ai/dsh-agent-preset-registry` (0.2.0-rc.2 and 0.2.1-rc.2, which
|
|
48
|
-
// agree on every call used here):
|
|
49
|
-
// - `register(definition)` validates the id, refuses a duplicate id with
|
|
50
|
-
// `Duplicate agent preset: <id>`, activates the definition eagerly and
|
|
51
|
-
// resolves to an async disposer. A definition whose rows FAIL to mount is not
|
|
52
|
-
// rejected here: `activate()` records the diagnostic and `register` still
|
|
53
|
-
// resolves. So registration is followed by `resolve(id)`, which reports the
|
|
54
|
-
// failure as `broken` and becomes `REVIEW_PRESET_UNUSABLE` here.
|
|
55
|
-
// - `resolve(id?)` rejects with `Unknown agent preset: <id>` when the
|
|
56
|
-
// registration is gone.
|
|
57
|
-
// - `mount(ctx, id)` binds an unpublished Agent to that definition.
|
|
58
|
-
// - `diagnostic()` may wait for the Host Loader tree to settle, and the registry
|
|
59
|
-
// says callers must not run inside a Host row's own activation. Registration
|
|
60
|
-
// is therefore LAZY — it happens on the first review request, from a request
|
|
61
|
-
// handler, never from this plugin's `apply`.
|
|
62
|
-
// - Registration and verification are the two preparation steps that can wait
|
|
63
|
-
// on other plugins, so BOTH are bounded, separately, by the caller's deadline.
|
|
64
|
-
// - Nothing here mutates the registry's configured default, the installed
|
|
65
|
-
// profile YAML, or any user preset: only a new, namespaced declaration is
|
|
66
|
-
// added, and its disposer removes it again on unload.
|
|
67
|
-
|
|
68
|
-
import { randomBytes } from 'node:crypto';
|
|
69
|
-
|
|
70
|
-
import { RunError } from '../runtime/agent-runner.js';
|
|
71
|
-
|
|
72
|
-
/** Codes this module raises. Distinct from every tool-policy code in `reviewer.js`. */
|
|
73
|
-
export const PRESET_CODES = Object.freeze({
|
|
74
|
-
// The registry cannot register a composition at all.
|
|
75
|
-
unsupported: 'REVIEW_PRESET_UNSUPPORTED',
|
|
76
|
-
// `register()` itself refused (duplicate id, invalid definition, ...).
|
|
77
|
-
registerFailed: 'REVIEW_PRESET_REGISTER_FAILED',
|
|
78
|
-
// Registered, but the composition has an unusable row (`resolve()` reported it).
|
|
79
|
-
unusable: 'REVIEW_PRESET_UNUSABLE',
|
|
80
|
-
// The composition rows could not be loaded at all.
|
|
81
|
-
rowsUnavailable: 'REVIEW_PRESET_ROWS_UNAVAILABLE',
|
|
82
|
-
// A mount for an id this plugin does not own was requested.
|
|
83
|
-
notOwned: 'REVIEW_PRESET_NOT_OWNED',
|
|
84
|
-
// Registration/mount preparation exceeded its own bounded deadline.
|
|
85
|
-
prepareTimeout: 'REVIEW_PRESET_PREPARE_TIMEOUT',
|
|
86
|
-
cancelled: 'REVIEW_PRESET_PREPARE_CANCELLED',
|
|
87
|
-
// The plugin lifecycle closed while preparation was still pending.
|
|
88
|
-
disposed: 'REVIEW_PRESET_DISPOSED',
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* The readiness states of one instance's composition.
|
|
93
|
-
*
|
|
94
|
-
* `failed` is not a state: a failure returns the instance to `idle` so the next
|
|
95
|
-
* review retries, and every caller sees the refusal that its own attempt got.
|
|
96
|
-
*/
|
|
97
|
-
export const PRESET_STATES = Object.freeze({
|
|
98
|
-
idle: 'idle',
|
|
99
|
-
preparing: 'preparing',
|
|
100
|
-
ready: 'ready',
|
|
101
|
-
disposing: 'disposing',
|
|
102
|
-
disposed: 'disposed',
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Native packages the review composition is built from.
|
|
107
|
-
*
|
|
108
|
-
* Both are real `@deepseek-ai/dsh-*` entry points and are loaded through this
|
|
109
|
-
* package (or, in a linked install, through the host). They are peer
|
|
110
|
-
* dependencies of the preset registry's environment, not of the Cordis
|
|
111
|
-
* composition: the composition mounts them inside the Agent scope.
|
|
112
|
-
*/
|
|
113
|
-
export const REVIEW_PACKAGES = Object.freeze(Object.freeze({
|
|
114
|
-
fs: '@deepseek-ai/dsh-tool-fs',
|
|
115
|
-
search: '@deepseek-ai/dsh-tool-fs-search',
|
|
116
|
-
}));
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The rows of the dedicated review composition, in mount order.
|
|
120
|
-
*
|
|
121
|
-
* Deliberately tiny. Every row here is a read surface:
|
|
122
|
-
* - `dsh-tool-fs` with `read`/`read_image` on and `write`/`edit` off, so the
|
|
123
|
-
* preset plane never contains a mutation tool at all;
|
|
124
|
-
* - `dsh-tool-fs-search` with `sampleOverCapGlobResults: false`, the same
|
|
125
|
-
* deterministic glob page the deployment uses.
|
|
126
|
-
*
|
|
127
|
-
* Excluded on purpose: delegation (`dsh-tool-subagent`, `dsh-tool-workflow`,
|
|
128
|
-
* `dsh-workflow-ptc`), shell (`dsh-tool-bash`, `dsh-tool-pwsh`), scheduling
|
|
129
|
-
* (`dsh-tool-schedule`), skills, jobs, web, todos, goals, plan mode, compaction
|
|
130
|
-
* and every other mutating or self-directed capability. Session, agent loop,
|
|
131
|
-
* system prompt, working directory and the model route are Host services the
|
|
132
|
-
* Agent already has; a preset does not (and must not) re-provide them.
|
|
133
|
-
*
|
|
134
|
-
* `read_image` needs the deployment's attachment store, which is a Host service
|
|
135
|
-
* the agent scope inherits; `glob`/`grep` need `subprocess`, also Host-supplied.
|
|
136
|
-
* Neither is re-mounted here.
|
|
137
|
-
* @returns {object[]} fresh row list (the registry may keep references).
|
|
138
|
-
*/
|
|
139
|
-
export function reviewCompositionRows() {
|
|
140
|
-
return [
|
|
141
|
-
{
|
|
142
|
-
id: 'tool-fs',
|
|
143
|
-
name: REVIEW_PACKAGES.fs,
|
|
144
|
-
config: {
|
|
145
|
-
read: true,
|
|
146
|
-
readImage: true,
|
|
147
|
-
// Composition-level exclusion, not a policy claim: these tools are never
|
|
148
|
-
// registered inside the review plane, so no restriction has to remove
|
|
149
|
-
// them and no future `restrict()` gap can expose them.
|
|
150
|
-
write: false,
|
|
151
|
-
edit: false,
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
|
-
{
|
|
155
|
-
id: 'tool-fs-search',
|
|
156
|
-
name: REVIEW_PACKAGES.search,
|
|
157
|
-
config: { sampleOverCapGlobResults: false },
|
|
158
|
-
},
|
|
159
|
-
];
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* The preset definition this plugin registers.
|
|
164
|
-
* @param {string} id - the owned, namespaced preset id.
|
|
165
|
-
* @returns {{ id: string, name: string, description: string, order: number, plugins: object[] }} definition.
|
|
166
|
-
*/
|
|
167
|
-
export function reviewPresetDefinition(id) {
|
|
168
|
-
return {
|
|
169
|
-
id,
|
|
170
|
-
// Shown in a roster if an operator ever looks: it must not read as a preset
|
|
171
|
-
// they may select for their own sessions, and it is not the default.
|
|
172
|
-
name: 'dsh-delegate additional review (plugin-owned)',
|
|
173
|
-
description: 'Read-only filesystem composition used by the dsh-delegate independent review.',
|
|
174
|
-
// Sorted after the shipped presets so a roster listing keeps them first.
|
|
175
|
-
order: 1000,
|
|
176
|
-
plugins: reviewCompositionRows(),
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Load the native rows the composition names.
|
|
182
|
-
*
|
|
183
|
-
* A failure here is a deployment fact (the tool packages are absent), not a
|
|
184
|
-
* policy decision, and it is reported before anything is registered.
|
|
185
|
-
*
|
|
186
|
-
* The load itself is bounded: a package import is normally a file read, but a
|
|
187
|
-
* solver seam (a test importer, a host resolver) may be slow or never settle,
|
|
188
|
-
* and preparation must not hang on it. The failure code distinguishes a
|
|
189
|
-
* deadline from an absent package, because the operator fixes them differently.
|
|
190
|
-
* @param {(specifier: string) => Promise<object>} importer - dynamic importer.
|
|
191
|
-
* @param {{ timeoutMs?: number|null, signal?: AbortSignal|null }} [bounds] - preparation bounds.
|
|
192
|
-
* @returns {Promise<Map<string, object>>} package name -> module.
|
|
193
|
-
* @throws {RunError} with `rowsUnavailable`, `prepareTimeout` or `cancelled`.
|
|
194
|
-
*/
|
|
195
|
-
async function loadRows(importer, bounds = {}) {
|
|
196
|
-
const loaded = new Map();
|
|
197
|
-
const missing = [];
|
|
198
|
-
for (const specifier of [REVIEW_PACKAGES.fs, REVIEW_PACKAGES.search]) {
|
|
199
|
-
let pending;
|
|
200
|
-
try {
|
|
201
|
-
pending = Promise.resolve(importer(specifier));
|
|
202
|
-
} catch (error) {
|
|
203
|
-
// An importer that throws synchronously is the same deployment fact as one
|
|
204
|
-
// that rejects: the package could not be loaded.
|
|
205
|
-
missing.push(`${specifier}: ${messageOf(error)}`);
|
|
206
|
-
continue;
|
|
207
|
-
}
|
|
208
|
-
let module;
|
|
209
|
-
try {
|
|
210
|
-
module = await bounded(pending, {
|
|
211
|
-
timeoutMs: bounds.timeoutMs ?? null,
|
|
212
|
-
signal: bounds.signal ?? null,
|
|
213
|
-
code: PRESET_CODES.prepareTimeout,
|
|
214
|
-
message: 'loading the native packages for the read-only review composition did not finish in time',
|
|
215
|
-
});
|
|
216
|
-
} catch (error) {
|
|
217
|
-
// A deadline or a cancellation is the CALLER's outcome, not a missing
|
|
218
|
-
// package: reporting it as `rowsUnavailable` would tell an operator to
|
|
219
|
-
// install something they already have.
|
|
220
|
-
if (error instanceof RunError && (error.code === PRESET_CODES.prepareTimeout || error.code === PRESET_CODES.cancelled)) {
|
|
221
|
-
throw error;
|
|
222
|
-
}
|
|
223
|
-
missing.push(`${specifier}: ${messageOf(error)}`);
|
|
224
|
-
continue;
|
|
225
|
-
}
|
|
226
|
-
if (module === null || typeof module !== 'object' || typeof module.apply !== 'function') {
|
|
227
|
-
missing.push(`${specifier} (no apply export)`);
|
|
228
|
-
continue;
|
|
229
|
-
}
|
|
230
|
-
loaded.set(specifier, module);
|
|
231
|
-
}
|
|
232
|
-
if (missing.length > 0) {
|
|
233
|
-
throw new RunError(
|
|
234
|
-
PRESET_CODES.rowsUnavailable,
|
|
235
|
-
'the read-only review composition needs the native filesystem tool packages, which this deployment does not provide',
|
|
236
|
-
{ missingPackages: missing },
|
|
237
|
-
);
|
|
238
|
-
}
|
|
239
|
-
return loaded;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/** One bounded, redacted-ish line of an error for a fixed-shape detail field. */
|
|
243
|
-
function messageOf(error) {
|
|
244
|
-
return String(error?.message ?? error).slice(0, 200);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Settle a promise under a deadline and an optional cancellation signal.
|
|
249
|
-
*
|
|
250
|
-
* Used for every step of preparation that can wait on something else (a package
|
|
251
|
-
* solve, `register()`, `resolve()`). A caller that runs out of budget gets its
|
|
252
|
-
* own refusal; the abandoned work keeps running and is retired by the attempt
|
|
253
|
-
* that started it, because the registry owns whatever it created.
|
|
254
|
-
*
|
|
255
|
-
* An empty signal (`reason === undefined`) is a legacy abort and is honoured the
|
|
256
|
-
* same way as a named one: abort is abort.
|
|
257
|
-
* @param {Promise<unknown>} promise - work to bound.
|
|
258
|
-
* @param {{ timeoutMs?: number|null, signal?: AbortSignal|null, code?: string, message?: string }} options - bounds.
|
|
259
|
-
* @returns {Promise<unknown>} the work's result.
|
|
260
|
-
* @throws {RunError} when the deadline or the signal fires first.
|
|
261
|
-
*/
|
|
262
|
-
async function bounded(promise, options) {
|
|
263
|
-
const timeoutMs = Number.isInteger(options.timeoutMs) && options.timeoutMs > 0 ? options.timeoutMs : null;
|
|
264
|
-
const signal = options.signal ?? null;
|
|
265
|
-
if (signal?.aborted === true) {
|
|
266
|
-
throw new RunError(PRESET_CODES.cancelled, cancelMessage(options.message), {});
|
|
267
|
-
}
|
|
268
|
-
if (timeoutMs === null && signal === null) return await promise;
|
|
269
|
-
|
|
270
|
-
let timer = null;
|
|
271
|
-
let onAbort = null;
|
|
272
|
-
const guards = [];
|
|
273
|
-
if (timeoutMs !== null) {
|
|
274
|
-
guards.push(new Promise((_resolve, reject) => {
|
|
275
|
-
timer = setTimeout(() => {
|
|
276
|
-
reject(new RunError(options.code ?? PRESET_CODES.prepareTimeout, options.message ?? 'preparing the review composition did not finish in time', { timeoutMs }));
|
|
277
|
-
}, timeoutMs);
|
|
278
|
-
timer.unref?.();
|
|
279
|
-
}));
|
|
280
|
-
}
|
|
281
|
-
if (signal !== null) {
|
|
282
|
-
guards.push(new Promise((_resolve, reject) => {
|
|
283
|
-
onAbort = () => reject(new RunError(PRESET_CODES.cancelled, cancelMessage(options.message), {}));
|
|
284
|
-
signal.addEventListener('abort', onAbort, { once: true });
|
|
285
|
-
}));
|
|
286
|
-
}
|
|
287
|
-
try {
|
|
288
|
-
return await Promise.race([promise, ...guards]);
|
|
289
|
-
} finally {
|
|
290
|
-
if (timer !== null) clearTimeout(timer);
|
|
291
|
-
if (onAbort !== null) signal?.removeEventListener?.('abort', onAbort);
|
|
292
|
-
// A guard that lost the race must never surface as an unhandled rejection
|
|
293
|
-
// when its deadline fires later (the timer is cleared, but a signal that
|
|
294
|
-
// aborts after this point would otherwise reject an unwatched promise).
|
|
295
|
-
for (const guard of guards) guard.catch(() => {});
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
/** Turn a step description into a cancellation message. */
|
|
300
|
-
function cancelMessage(detail) {
|
|
301
|
-
return typeof detail === 'string' && detail !== ''
|
|
302
|
-
? `the review was cancelled while ${detail}`
|
|
303
|
-
: 'the review was cancelled while preparing its composition';
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Install a late continuation on a promise, swallowing both outcomes.
|
|
308
|
-
*
|
|
309
|
-
* This is the shape every abandonable registration/resolve uses: the caller is
|
|
310
|
-
* gone, so nobody may observe the rejection, and nobody may be kept waiting for
|
|
311
|
-
* it either. `settle` is called exactly once, when the work finishes, and is
|
|
312
|
-
* whatever cleanup the attempt owns.
|
|
313
|
-
* @param {Promise<unknown>} promise - work to continue.
|
|
314
|
-
* @param {(outcome: { ok: boolean, value?: unknown, error?: unknown }) => unknown} settle - continuation.
|
|
315
|
-
* @param {object|null} [logger] - logger for a cleanup failure; never throws.
|
|
316
|
-
* @returns {void}
|
|
317
|
-
*/
|
|
318
|
-
function continueLate(promise, settle, logger = null) {
|
|
319
|
-
const run = async (outcome) => {
|
|
320
|
-
try {
|
|
321
|
-
await settle(outcome);
|
|
322
|
-
} catch (error) {
|
|
323
|
-
logger?.warn?.('dsh-delegate: a late review composition cleanup failed', messageOf(error));
|
|
324
|
-
}
|
|
325
|
-
};
|
|
326
|
-
promise.then(
|
|
327
|
-
(value) => run({ ok: true, value }),
|
|
328
|
-
(error) => run({ ok: false, error }),
|
|
329
|
-
);
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
/** Bound on one preparation step performed by the shared attempt. */
|
|
333
|
-
const PREPARE_STEP_TIMEOUT_MS = 30_000;
|
|
334
|
-
|
|
335
|
-
/** Bound on waiting for a registration disposer during unload. */
|
|
336
|
-
const DISPOSE_TIMEOUT_MS = 5_000;
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* A plugin-instance-owned native preset for the independent review.
|
|
340
|
-
*
|
|
341
|
-
* One instance per plugin activation. The composition is registered lazily on
|
|
342
|
-
* the first request that needs it, exactly once, and every concurrent request
|
|
343
|
-
* shares that one registration promise. The instance owns the id it created and
|
|
344
|
-
* refuses to mount anything else.
|
|
345
|
-
*
|
|
346
|
-
* `createdId` is the instance's published ownership: it is non-null only in the
|
|
347
|
-
* `ready` state, i.e. only after a full register -> resolve -> validate sequence
|
|
348
|
-
* succeeded for a registry the instance still uses.
|
|
349
|
-
*/
|
|
350
|
-
export class ReviewPreset {
|
|
351
|
-
/**
|
|
352
|
-
* @param {object} [options] - `registry`, `logger`, `importer`, `id` (test seam).
|
|
353
|
-
*/
|
|
354
|
-
constructor(options = {}) {
|
|
355
|
-
this.registry = options.registry ?? null;
|
|
356
|
-
this.logger = options.logger ?? null;
|
|
357
|
-
// Overridable only so a test can prove the "packages absent" refusal without
|
|
358
|
-
// uninstalling anything.
|
|
359
|
-
this.importer = options.importer ?? ((specifier) => import(specifier));
|
|
360
|
-
this.id = typeof options.id === 'string' && options.id !== '' ? options.id : ownedPresetId();
|
|
361
|
-
// The published ownership. Only the `ready` state sets it.
|
|
362
|
-
this.createdId = null;
|
|
363
|
-
this.state = PRESET_STATES.idle;
|
|
364
|
-
// The registration this instance currently owns (registry + disposer) and the
|
|
365
|
-
// attempt that is producing or produced it. `pending` is null unless the
|
|
366
|
-
// state is `preparing`.
|
|
367
|
-
this.claim = null;
|
|
368
|
-
this.pending = null;
|
|
369
|
-
// Attempt sequence, so a late completion can tell whether it is still the
|
|
370
|
-
// current attempt and whether its registry is still the attached one.
|
|
371
|
-
this.sequence = 0;
|
|
372
|
-
// Callers currently awaiting the shared attempt, and whether the last one
|
|
373
|
-
// already gave up on it.
|
|
374
|
-
this.waiters = 0;
|
|
375
|
-
this.abandoned = false;
|
|
376
|
-
// Resolves when `dispose()` has finished retiring everything; `null` outside
|
|
377
|
-
// `disposing`.
|
|
378
|
-
this.closing = null;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
/** Whether a claim is the live, validated registration of this instance. */
|
|
382
|
-
get ready() {
|
|
383
|
-
return this.state === PRESET_STATES.ready && this.createdId !== null && this.claim !== null;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* Bind another registry to this instance (used when the service appears after
|
|
388
|
-
* the plugin loaded). Never allowed to change the id this instance owns.
|
|
389
|
-
*
|
|
390
|
-
* Replacing the registry RETIRES the previous registration instead of
|
|
391
|
-
* forgetting it: the composition in the old registry is still live, and
|
|
392
|
-
* dropping the disposer would leave it there for the life of the process. The
|
|
393
|
-
* old registration is also marked superseded, so a verification that is still
|
|
394
|
-
* in flight for it retires itself instead of publishing an id that now belongs
|
|
395
|
-
* to a registry no review will mount from.
|
|
396
|
-
* @param {object|null} registry - native preset registry.
|
|
397
|
-
* @returns {void}
|
|
398
|
-
*/
|
|
399
|
-
attach(registry) {
|
|
400
|
-
if (registry === null || registry === undefined) return;
|
|
401
|
-
if (this.registry === registry) return;
|
|
402
|
-
const previous = this.claim;
|
|
403
|
-
// Mark BEFORE reassigning: any in-flight verification compares against
|
|
404
|
-
// `claim.superseded` and against `registry` identity.
|
|
405
|
-
if (previous !== null) previous.superseded = true;
|
|
406
|
-
this.registry = registry;
|
|
407
|
-
this.createdId = null;
|
|
408
|
-
this.claim = null;
|
|
409
|
-
if (this.state === PRESET_STATES.ready || this.state === PRESET_STATES.idle) {
|
|
410
|
-
// The next preparation must run against the new registry, even if the old
|
|
411
|
-
// attempt is still finishing (it is now superseded and will retire itself).
|
|
412
|
-
this.state = PRESET_STATES.idle;
|
|
413
|
-
this.pending = null;
|
|
414
|
-
}
|
|
415
|
-
// The state may be `preparing` (its own attempt owns the transition) or
|
|
416
|
-
// `disposed` (nothing may be republished). Either way the old registration is
|
|
417
|
-
// no longer this instance's, so it is released now, without waiting.
|
|
418
|
-
if (previous !== null) this.release(previous);
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
/** Whether this registry exposes the API a review composition needs. */
|
|
422
|
-
get supported() {
|
|
423
|
-
const registry = this.registry;
|
|
424
|
-
return registry !== null
|
|
425
|
-
&& registry !== undefined
|
|
426
|
-
&& typeof registry.register === 'function'
|
|
427
|
-
&& typeof registry.resolve === 'function'
|
|
428
|
-
&& typeof registry.mount === 'function';
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* The preset id a review should mount, registering the composition if needed.
|
|
433
|
-
*
|
|
434
|
-
* Idempotent and concurrency-safe: the first caller starts the attempt, every
|
|
435
|
-
* other caller awaits the same attempt, and each caller still applies its OWN
|
|
436
|
-
* deadline and signal to that shared work. So one review that gives up never
|
|
437
|
-
* cancels another review's preparation, and a shared attempt that ultimately
|
|
438
|
-
* succeeds still publishes readiness for the reviews that waited.
|
|
439
|
-
*
|
|
440
|
-
* A failure returns the instance to `idle` so a later review can retry instead
|
|
441
|
-
* of being poisoned by a transient one.
|
|
442
|
-
* @param {{ timeoutMs?: number|null, signal?: AbortSignal|null }} [bounds] - this caller's deadline.
|
|
443
|
-
* @returns {Promise<string>} the owned preset id, ready to mount.
|
|
444
|
-
* @throws {RunError} with a stable code from {@link PRESET_CODES}.
|
|
445
|
-
*/
|
|
446
|
-
async preparation(bounds = {}) {
|
|
447
|
-
const caller = {
|
|
448
|
-
timeoutMs: Number.isInteger(bounds?.timeoutMs) && bounds.timeoutMs > 0 ? bounds.timeoutMs : null,
|
|
449
|
-
signal: bounds?.signal ?? null,
|
|
450
|
-
};
|
|
451
|
-
if (this.state === PRESET_STATES.disposed || this.state === PRESET_STATES.disposing) {
|
|
452
|
-
throw new RunError(PRESET_CODES.disposed, 'the plugin was unloaded before the review composition could be prepared', {});
|
|
453
|
-
}
|
|
454
|
-
if (caller.signal?.aborted === true) {
|
|
455
|
-
throw new RunError(PRESET_CODES.cancelled, cancelMessage(null), {});
|
|
456
|
-
}
|
|
457
|
-
if (!this.supported) {
|
|
458
|
-
throw new RunError(
|
|
459
|
-
PRESET_CODES.unsupported,
|
|
460
|
-
'the native agent preset registry cannot register a composition, so a read-only review cannot be composed',
|
|
461
|
-
{},
|
|
462
|
-
);
|
|
463
|
-
}
|
|
464
|
-
if (this.ready) return this.createdId;
|
|
465
|
-
if (this.pending === null) {
|
|
466
|
-
this.state = PRESET_STATES.preparing;
|
|
467
|
-
this.waiters = 0;
|
|
468
|
-
this.pending = this.prepare();
|
|
469
|
-
// The attempt settles itself; this observer only guarantees that a
|
|
470
|
-
// rejection nobody awaited can never become an unhandled one.
|
|
471
|
-
continueLate(this.pending, () => {
|
|
472
|
-
if (this.pending === null) return;
|
|
473
|
-
// Only clear the slot if the attempt did not already do so.
|
|
474
|
-
this.pending = null;
|
|
475
|
-
if (this.state === PRESET_STATES.preparing) this.state = PRESET_STATES.idle;
|
|
476
|
-
}, this.logger);
|
|
477
|
-
}
|
|
478
|
-
// The shared attempt is raced against THIS caller's own bounds. A caller
|
|
479
|
-
// that gives up returns its own refusal and never cancels work other callers
|
|
480
|
-
// are still waiting for — but when the LAST caller gives up, the attempt is
|
|
481
|
-
// marked abandoned, so a registration that completes afterwards retires
|
|
482
|
-
// itself instead of publishing ownership nobody is waiting for.
|
|
483
|
-
this.waiters += 1;
|
|
484
|
-
try {
|
|
485
|
-
return await bounded(this.pending, caller);
|
|
486
|
-
} finally {
|
|
487
|
-
this.waiters -= 1;
|
|
488
|
-
if (this.waiters === 0 && this.state === PRESET_STATES.preparing) this.abandoned = true;
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Run one full preparation attempt against the currently attached registry.
|
|
494
|
-
*
|
|
495
|
-
* The sequence is: load the native rows -> register -> claim the registration
|
|
496
|
-
* -> resolve and validate -> publish readiness. Every step after the claim can
|
|
497
|
-
* fail in a way that must NOT leave the composition marked ready, and a
|
|
498
|
-
* superseded or closed attempt retires its registration instead of publishing.
|
|
499
|
-
* @returns {Promise<string>} the owned preset id.
|
|
500
|
-
*/
|
|
501
|
-
async prepare() {
|
|
502
|
-
const registry = this.registry;
|
|
503
|
-
const attempt = { sequence: this.sequence += 1, superseded: false };
|
|
504
|
-
// This attempt starts with a clean abandonment mark; `preparation` sets it
|
|
505
|
-
// again if the last caller of THIS attempt gives up.
|
|
506
|
-
this.abandoned = false;
|
|
507
|
-
// The attempt owns the `preparing` state: it was entered by `preparation`
|
|
508
|
-
// before this method ran, and every exit below either publishes `ready` or
|
|
509
|
-
// returns to `idle` through `settle(reason)`.
|
|
510
|
-
const stepTimeoutMs = PREPARE_STEP_TIMEOUT_MS;
|
|
511
|
-
|
|
512
|
-
const settle = (error) => {
|
|
513
|
-
// Only the CURRENT attempt may move the instance out of `preparing`; a
|
|
514
|
-
// superseded attempt that finishes later must not overwrite the state of a
|
|
515
|
-
// newer one. `disposed`/`disposing` is terminal: a late step that fails
|
|
516
|
-
// after unload leaves the instance disposed instead of resurrecting `idle`.
|
|
517
|
-
if (this.registry === registry && this.pending !== null) {
|
|
518
|
-
this.pending = null;
|
|
519
|
-
this.waiters = 0;
|
|
520
|
-
this.abandoned = false;
|
|
521
|
-
if (this.state === PRESET_STATES.preparing) this.state = PRESET_STATES.idle;
|
|
522
|
-
}
|
|
523
|
-
throw error;
|
|
524
|
-
};
|
|
525
|
-
const abandon = settle;
|
|
526
|
-
|
|
527
|
-
let rows;
|
|
528
|
-
try {
|
|
529
|
-
rows = await loadRows(this.importer, { timeoutMs: stepTimeoutMs, signal: null });
|
|
530
|
-
} catch (error) {
|
|
531
|
-
return abandon(error);
|
|
532
|
-
}
|
|
533
|
-
void rows;
|
|
534
|
-
if (this.isStale(attempt, registry)) {
|
|
535
|
-
return abandon(new RunError(
|
|
536
|
-
PRESET_CODES.cancelled,
|
|
537
|
-
'the review composition preparation was superseded before it could finish',
|
|
538
|
-
{},
|
|
539
|
-
));
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
// A duplicate id is the one registration failure we must not paper over: it
|
|
543
|
-
// means another owner already uses this namespace, and mounting THEIR
|
|
544
|
-
// composition would silently review on a surface we did not define.
|
|
545
|
-
let disposer;
|
|
546
|
-
let registering;
|
|
547
|
-
try {
|
|
548
|
-
registering = Promise.resolve(registry.register(reviewPresetDefinition(this.id)));
|
|
549
|
-
} catch (error) {
|
|
550
|
-
return abandon(registerRefusal(error));
|
|
551
|
-
}
|
|
552
|
-
try {
|
|
553
|
-
disposer = await bounded(registering, {
|
|
554
|
-
timeoutMs: stepTimeoutMs,
|
|
555
|
-
signal: null,
|
|
556
|
-
code: PRESET_CODES.prepareTimeout,
|
|
557
|
-
message: 'registering the read-only review composition did not finish in time',
|
|
558
|
-
});
|
|
559
|
-
} catch (error) {
|
|
560
|
-
// The registration may still complete after this deadline. If it does, the
|
|
561
|
-
// disposer retires it; nothing here waits for that. This is the case that
|
|
562
|
-
// used to leak a live composition AND republish its ownership later.
|
|
563
|
-
continueLate(registering, (outcome) => {
|
|
564
|
-
if (outcome.ok && typeof outcome.value === 'function') return retireQuietly(outcome.value, this.logger);
|
|
565
|
-
return undefined;
|
|
566
|
-
}, this.logger);
|
|
567
|
-
return abandon(error instanceof RunError ? error : registerRefusal(error));
|
|
568
|
-
}
|
|
569
|
-
if (typeof disposer !== 'function') {
|
|
570
|
-
return abandon(new RunError(
|
|
571
|
-
PRESET_CODES.registerFailed,
|
|
572
|
-
'the native preset registry did not return a disposer for the review composition',
|
|
573
|
-
{},
|
|
574
|
-
));
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
// A registration that arrives after unload began must never become this
|
|
578
|
-
// instance's claim — not even for an instant. `dispose()` snapshotted the
|
|
579
|
-
// claim (null, because nothing was registered yet), cleared `createdId` and
|
|
580
|
-
// marked the attempt superseded, but the claim object did not exist then, so
|
|
581
|
-
// it cannot carry that mark. Retiring it here is the only way its disposer
|
|
582
|
-
// runs exactly once and the registry never keeps a composition nothing owns.
|
|
583
|
-
if (this.state === PRESET_STATES.disposing || this.state === PRESET_STATES.disposed) {
|
|
584
|
-
await retireQuietly(disposer, this.logger);
|
|
585
|
-
return abandon(new RunError(
|
|
586
|
-
PRESET_CODES.disposed,
|
|
587
|
-
'the plugin was unloaded before the review composition finished registering',
|
|
588
|
-
{},
|
|
589
|
-
));
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
// The claim exists from here on: whatever happens, this instance is the owner
|
|
593
|
-
// of this registration and will retire it on failure or on unload.
|
|
594
|
-
const claim = { registry, attempt: attempt.sequence, disposer, superseded: attempt.superseded };
|
|
595
|
-
this.claim = claim;
|
|
596
|
-
|
|
597
|
-
if (this.isStale(attempt, registry, claim)) {
|
|
598
|
-
this.retireClaim(claim);
|
|
599
|
-
return abandon(new RunError(
|
|
600
|
-
PRESET_CODES.cancelled,
|
|
601
|
-
'the review composition preparation was superseded before it could be verified',
|
|
602
|
-
{},
|
|
603
|
-
));
|
|
604
|
-
}
|
|
605
|
-
|
|
606
|
-
// `register()` resolves for a definition whose rows failed to mount — it
|
|
607
|
-
// records the diagnostic instead. `resolve()` is the supported read that
|
|
608
|
-
// reports it, which is what turns "registered" into "usable".
|
|
609
|
-
let resolving;
|
|
610
|
-
try {
|
|
611
|
-
resolving = Promise.resolve(registry.resolve(this.id));
|
|
612
|
-
} catch (error) {
|
|
613
|
-
return this.failVerification(claim, unusableRefusal(error, null));
|
|
614
|
-
}
|
|
615
|
-
let resolved;
|
|
616
|
-
try {
|
|
617
|
-
resolved = await bounded(resolving, {
|
|
618
|
-
timeoutMs: stepTimeoutMs,
|
|
619
|
-
signal: null,
|
|
620
|
-
code: PRESET_CODES.prepareTimeout,
|
|
621
|
-
message: 'verifying the read-only review composition did not finish in time',
|
|
622
|
-
});
|
|
623
|
-
} catch (error) {
|
|
624
|
-
// Same shape as registration: the verification may still settle after the
|
|
625
|
-
// deadline, and its outcome must not be able to publish anything.
|
|
626
|
-
return this.failVerification(
|
|
627
|
-
claim,
|
|
628
|
-
error instanceof RunError ? error : unusableRefusal(error, null),
|
|
629
|
-
resolving,
|
|
630
|
-
);
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
// From here the verdict is final, so a late resolution of a raced deadline is
|
|
634
|
-
// only ever observed, never acted on.
|
|
635
|
-
continueLate(resolving, () => undefined, this.logger);
|
|
636
|
-
if (this.isStale(attempt, registry, claim)) {
|
|
637
|
-
this.retireClaim(claim);
|
|
638
|
-
return abandon(new RunError(
|
|
639
|
-
PRESET_CODES.cancelled,
|
|
640
|
-
'the review composition preparation was superseded before it could be trusted',
|
|
641
|
-
{},
|
|
642
|
-
));
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
const broken = typeof resolved?.broken === 'string' && resolved.broken !== '' ? resolved.broken : null;
|
|
646
|
-
if (broken !== null) {
|
|
647
|
-
return this.failVerification(claim, unusableRefusal(null, broken));
|
|
648
|
-
}
|
|
649
|
-
if (resolved?.id !== undefined && resolved.id !== null && resolved.id !== this.id) {
|
|
650
|
-
// Defensive: a registry that answers about a different definition has not
|
|
651
|
-
// verified this one, and publishing it would claim an unproven surface.
|
|
652
|
-
return this.failVerification(claim, new RunError(
|
|
653
|
-
PRESET_CODES.unusable,
|
|
654
|
-
'the native preset registry resolved a different composition than the one this review registered',
|
|
655
|
-
{},
|
|
656
|
-
));
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
// Validated, current and not superseded: NOW readiness is published.
|
|
660
|
-
claim.validated = true;
|
|
661
|
-
this.state = PRESET_STATES.ready;
|
|
662
|
-
this.createdId = this.id;
|
|
663
|
-
this.pending = null;
|
|
664
|
-
this.waiters = 0;
|
|
665
|
-
this.abandoned = false;
|
|
666
|
-
this.logger?.info?.(`dsh-delegate: registered read-only review preset ${this.id}`);
|
|
667
|
-
return this.id;
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
/**
|
|
671
|
-
* Whether an attempt may no longer publish its result.
|
|
672
|
-
* @param {{ sequence: number, superseded: boolean }} attempt - the attempt.
|
|
673
|
-
* @param {object} registry - the registry the attempt started against.
|
|
674
|
-
* @param {object|null} [claim] - the registration the attempt made, if any.
|
|
675
|
-
* @returns {boolean} true when the result must be retired instead of published.
|
|
676
|
-
*/
|
|
677
|
-
isStale(attempt, registry, claim = null) {
|
|
678
|
-
if (attempt.superseded || this.closing !== null) return true;
|
|
679
|
-
if (this.abandoned) return true;
|
|
680
|
-
if (this.registry !== registry) return true;
|
|
681
|
-
const current = this.claim;
|
|
682
|
-
if (claim !== null && current !== claim) return true;
|
|
683
|
-
if (claim !== null && claim.superseded === true) return true;
|
|
684
|
-
return false;
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
/**
|
|
688
|
-
* Fail a verification: retire the registration and return the instance to idle.
|
|
689
|
-
* @param {object} claim - the registration to retire.
|
|
690
|
-
* @param {RunError} error - the refusal to raise.
|
|
691
|
-
* @param {Promise<unknown>|null} [late] - in-flight verification to observe.
|
|
692
|
-
* @returns {never} always throws.
|
|
693
|
-
*/
|
|
694
|
-
failVerification(claim, error, late = null) {
|
|
695
|
-
if (late !== null) continueLate(late, () => undefined, this.logger);
|
|
696
|
-
// Retiring the claim never publishes readiness, so a verification failure can
|
|
697
|
-
// never leave the instance claiming a composition nothing proved.
|
|
698
|
-
this.retireClaim(claim);
|
|
699
|
-
this.createdId = null;
|
|
700
|
-
if (this.pending !== null) this.pending = null;
|
|
701
|
-
this.waiters = 0;
|
|
702
|
-
this.abandoned = false;
|
|
703
|
-
if (this.state === PRESET_STATES.preparing) this.state = PRESET_STATES.idle;
|
|
704
|
-
throw error;
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* Retire one registration without waiting for it, keeping the instance usable.
|
|
709
|
-
* @param {object} claim - registration to retire.
|
|
710
|
-
* @returns {void}
|
|
711
|
-
*/
|
|
712
|
-
retireClaim(claim) {
|
|
713
|
-
if (this.claim === claim) this.claim = null;
|
|
714
|
-
this.release(claim);
|
|
715
|
-
}
|
|
716
|
-
|
|
717
|
-
/**
|
|
718
|
-
* Call a registration disposer at most once, never throwing and never awaited.
|
|
719
|
-
* @param {object} claim - registration whose disposer to call.
|
|
720
|
-
* @returns {void}
|
|
721
|
-
*/
|
|
722
|
-
release(claim) {
|
|
723
|
-
const disposer = claim.disposer;
|
|
724
|
-
claim.disposer = null;
|
|
725
|
-
if (typeof disposer !== 'function') return;
|
|
726
|
-
let pending;
|
|
727
|
-
try {
|
|
728
|
-
pending = Promise.resolve(disposer());
|
|
729
|
-
} catch (error) {
|
|
730
|
-
this.logger?.warn?.('dsh-delegate: the review preset could not be unregistered', messageOf(error));
|
|
731
|
-
return;
|
|
732
|
-
}
|
|
733
|
-
continueLate(pending, () => undefined, this.logger);
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
/**
|
|
737
|
-
* Whether this instance owns `id` (i.e. the caller may mount it).
|
|
738
|
-
* @param {string} id - requested preset id.
|
|
739
|
-
* @returns {boolean} true when this instance registered AND validated it.
|
|
740
|
-
*/
|
|
741
|
-
owns(id) {
|
|
742
|
-
return typeof id === 'string' && id !== '' && id === this.createdId && this.ready;
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
/**
|
|
746
|
-
* Remove the composition from the registry, if this instance registered one.
|
|
747
|
-
*
|
|
748
|
-
* Unload must not block forever on a registration that a foreign plugin never
|
|
749
|
-
* finishes: the disposer is an external promise. So the early-return is the
|
|
750
|
-
* `disposed` state, and a disposer that has not settled after its own bound is
|
|
751
|
-
* left to a late continuation — the instance is already unusable by then,
|
|
752
|
-
* because `createdId` is cleared synchronously.
|
|
753
|
-
* @returns {Promise<void>} resolves once the instance is released.
|
|
754
|
-
*/
|
|
755
|
-
async dispose() {
|
|
756
|
-
if (this.state === PRESET_STATES.disposed) return;
|
|
757
|
-
if (this.closing !== null) {
|
|
758
|
-
await this.closing;
|
|
759
|
-
return;
|
|
760
|
-
}
|
|
761
|
-
this.state = PRESET_STATES.disposing;
|
|
762
|
-
// Every published fact goes away synchronously: a review that asks while the
|
|
763
|
-
// disposer is still running must refuse, not mount a composition that is
|
|
764
|
-
// being torn down.
|
|
765
|
-
this.createdId = null;
|
|
766
|
-
const claim = this.claim;
|
|
767
|
-
this.claim = null;
|
|
768
|
-
this.pending = null;
|
|
769
|
-
if (claim !== null) claim.superseded = true;
|
|
770
|
-
|
|
771
|
-
let resolveClosing;
|
|
772
|
-
this.closing = new Promise((resolvePromise) => { resolveClosing = resolvePromise; });
|
|
773
|
-
try {
|
|
774
|
-
if (claim !== null) await this.releaseAndWait(claim);
|
|
775
|
-
} finally {
|
|
776
|
-
this.state = PRESET_STATES.disposed;
|
|
777
|
-
this.closing = null;
|
|
778
|
-
resolveClosing();
|
|
779
|
-
}
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
/**
|
|
783
|
-
* Release one claim, waiting for its disposer under a bound.
|
|
784
|
-
* @param {object} claim - registration to retire.
|
|
785
|
-
* @returns {Promise<void>} resolves when retired, or when the bound expires.
|
|
786
|
-
*/
|
|
787
|
-
async releaseAndWait(claim) {
|
|
788
|
-
const disposer = claim.disposer;
|
|
789
|
-
claim.disposer = null;
|
|
790
|
-
if (typeof disposer !== 'function') return;
|
|
791
|
-
let pending;
|
|
792
|
-
try {
|
|
793
|
-
pending = Promise.resolve(disposer());
|
|
794
|
-
} catch (error) {
|
|
795
|
-
this.logger?.warn?.('dsh-delegate: the review preset could not be unregistered', messageOf(error));
|
|
796
|
-
return;
|
|
797
|
-
}
|
|
798
|
-
try {
|
|
799
|
-
await bounded(pending, {
|
|
800
|
-
timeoutMs: DISPOSE_TIMEOUT_MS,
|
|
801
|
-
signal: null,
|
|
802
|
-
code: PRESET_CODES.prepareTimeout,
|
|
803
|
-
message: 'unregistering the read-only review composition did not finish in time',
|
|
804
|
-
});
|
|
805
|
-
} catch (error) {
|
|
806
|
-
this.logger?.warn?.('dsh-delegate: the review preset could not be unregistered', messageOf(error));
|
|
807
|
-
// The disposer is still pending and still owned by the registry: observe it
|
|
808
|
-
// so a late rejection is never unhandled, and stop waiting for it.
|
|
809
|
-
continueLate(pending, () => undefined, this.logger);
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
/** Project a registration refusal into a stable code. */
|
|
815
|
-
function registerRefusal(error) {
|
|
816
|
-
if (error instanceof RunError) return error;
|
|
817
|
-
return new RunError(
|
|
818
|
-
PRESET_CODES.registerFailed,
|
|
819
|
-
'the native preset registry refused the read-only review composition',
|
|
820
|
-
{ cause: messageOf(error) },
|
|
821
|
-
);
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
/** Project a failed or broken verification into a stable code. */
|
|
825
|
-
function unusableRefusal(error, broken) {
|
|
826
|
-
if (error !== null && error !== undefined && !(error instanceof RunError)) {
|
|
827
|
-
return new RunError(
|
|
828
|
-
PRESET_CODES.unusable,
|
|
829
|
-
'the read-only review composition could not be resolved after registration',
|
|
830
|
-
{ cause: messageOf(error) },
|
|
831
|
-
);
|
|
832
|
-
}
|
|
833
|
-
return new RunError(
|
|
834
|
-
PRESET_CODES.unusable,
|
|
835
|
-
'the read-only review composition has an unusable row, so no read-only review can be composed',
|
|
836
|
-
{
|
|
837
|
-
unusable: String(broken ?? '')
|
|
838
|
-
.split('\n')
|
|
839
|
-
.filter((line) => line.trim() !== '')
|
|
840
|
-
.slice(0, 5)
|
|
841
|
-
// Fixed-shape evidence: the rows the registry reported, never a message
|
|
842
|
-
// this module invented, and each line bounded.
|
|
843
|
-
.map((line) => line.trim().slice(0, 200)),
|
|
844
|
-
},
|
|
845
|
-
);
|
|
846
|
-
}
|
|
847
|
-
|
|
848
|
-
/** Retire one registration that a late completion produced, never throwing. */
|
|
849
|
-
async function retireQuietly(disposer, logger) {
|
|
850
|
-
try {
|
|
851
|
-
await disposer();
|
|
852
|
-
} catch (error) {
|
|
853
|
-
logger?.warn?.('dsh-delegate: a late review preset registration could not be retired', messageOf(error));
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
/**
|
|
858
|
-
* A plugin-instance-unique, namespaced preset id.
|
|
859
|
-
*
|
|
860
|
-
* The namespace makes a collision impossible in practice and recognisable in a
|
|
861
|
-
* diagnostic; the suffix keeps two plugin instances in one process apart, and
|
|
862
|
-
* `register()` refuses a duplicate outright rather than letting one instance
|
|
863
|
-
* mount another's composition.
|
|
864
|
-
* @returns {string} preset id.
|
|
865
|
-
*/
|
|
866
|
-
export function ownedPresetId() {
|
|
867
|
-
return `dsh-delegate-review-${randomBytes(6).toString('hex')}`;
|
|
868
|
-
}
|