dsh-harness-reflex 0.1.0 → 0.3.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/README.md +20 -6
- package/client.js +3511 -185
- package/dist/attribution.d.ts +266 -0
- package/dist/attribution.js +238 -0
- package/dist/attribution.js.map +1 -0
- package/dist/bridge.d.ts +311 -13
- package/dist/bridge.js +714 -42
- package/dist/bridge.js.map +1 -1
- package/dist/client-bundle.js +4 -1
- package/dist/client-bundle.js.map +1 -1
- package/dist/command.js +10 -0
- package/dist/command.js.map +1 -1
- package/dist/config.d.ts +530 -22
- package/dist/config.js +808 -27
- package/dist/config.js.map +1 -1
- package/dist/delegation-dispatch.d.ts +132 -0
- package/dist/delegation-dispatch.js +186 -0
- package/dist/delegation-dispatch.js.map +1 -0
- package/dist/delegation-health.d.ts +131 -0
- package/dist/delegation-health.js +97 -0
- package/dist/delegation-health.js.map +1 -0
- package/dist/delegation.d.ts +148 -0
- package/dist/delegation.js +186 -0
- package/dist/delegation.js.map +1 -0
- package/dist/effort-delegation.d.ts +144 -0
- package/dist/effort-delegation.js +280 -0
- package/dist/effort-delegation.js.map +1 -0
- package/dist/effort.d.ts +141 -0
- package/dist/effort.js +251 -9
- package/dist/effort.js.map +1 -1
- package/dist/host-patch.d.ts +156 -0
- package/dist/host-patch.js +534 -0
- package/dist/host-patch.js.map +1 -0
- package/dist/index.d.ts +12 -3
- package/dist/index.js +99 -7
- package/dist/index.js.map +1 -1
- package/dist/interventions.d.ts +98 -373
- package/dist/interventions.js +442 -1702
- package/dist/interventions.js.map +1 -1
- package/dist/panel-api.d.ts +328 -4
- package/dist/panel-api.js +322 -15
- package/dist/panel-api.js.map +1 -1
- package/dist/panel-mount.d.ts +81 -22
- package/dist/panel-mount.js +228 -7
- package/dist/panel-mount.js.map +1 -1
- package/dist/routing.d.ts +23 -18
- package/dist/routing.js +471 -153
- package/dist/routing.js.map +1 -1
- package/dist/session-audit.d.ts +70 -0
- package/dist/session-audit.js +73 -0
- package/dist/session-audit.js.map +1 -1
- package/dist/systemone-probe.d.ts +114 -0
- package/dist/systemone-probe.js +383 -0
- package/dist/systemone-probe.js.map +1 -0
- package/dist/testing.d.ts +212 -7
- package/dist/testing.js +333 -27
- package/dist/testing.js.map +1 -1
- package/dist/tools.d.ts +109 -3
- package/dist/tools.js +103 -4
- package/dist/tools.js.map +1 -1
- package/dist/unconfigured-backend.d.ts +72 -0
- package/dist/unconfigured-backend.js +94 -0
- package/dist/unconfigured-backend.js.map +1 -0
- package/dist/vendor/harness-reflex/backend/factory.d.ts +31 -1
- package/dist/vendor/harness-reflex/backend/factory.js +18 -3
- package/dist/vendor/harness-reflex/backend/factory.js.map +1 -1
- package/dist/vendor/harness-reflex/backend/jev.d.ts +20 -0
- package/dist/vendor/harness-reflex/backend/jev.js +11 -4
- package/dist/vendor/harness-reflex/backend/jev.js.map +1 -1
- package/dist/vendor/harness-reflex/backend/simple-jev.d.ts +16 -0
- package/dist/vendor/harness-reflex/backend/simple-jev.js +17 -6
- package/dist/vendor/harness-reflex/backend/simple-jev.js.map +1 -1
- package/dist/vendor/harness-reflex/core/closing.d.ts +90 -0
- package/dist/vendor/harness-reflex/core/closing.js +111 -0
- package/dist/vendor/harness-reflex/core/closing.js.map +1 -0
- package/dist/vendor/harness-reflex/core/evidence.d.ts +119 -0
- package/dist/vendor/harness-reflex/core/evidence.js +209 -0
- package/dist/vendor/harness-reflex/core/evidence.js.map +1 -0
- package/dist/vendor/harness-reflex/core/judge.d.ts +221 -0
- package/dist/vendor/harness-reflex/core/judge.js +251 -0
- package/dist/vendor/harness-reflex/core/judge.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate-closing.d.ts +237 -0
- package/dist/vendor/harness-reflex/core/orchestrate-closing.js +250 -0
- package/dist/vendor/harness-reflex/core/orchestrate-closing.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate-correction.d.ts +209 -0
- package/dist/vendor/harness-reflex/core/orchestrate-correction.js +141 -0
- package/dist/vendor/harness-reflex/core/orchestrate-correction.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate-host.d.ts +225 -0
- package/dist/vendor/harness-reflex/core/orchestrate-host.js +80 -0
- package/dist/vendor/harness-reflex/core/orchestrate-host.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate-stagnation.d.ts +259 -0
- package/dist/vendor/harness-reflex/core/orchestrate-stagnation.js +270 -0
- package/dist/vendor/harness-reflex/core/orchestrate-stagnation.js.map +1 -0
- package/dist/vendor/harness-reflex/core/orchestrate.d.ts +357 -0
- package/dist/vendor/harness-reflex/core/orchestrate.js +418 -0
- package/dist/vendor/harness-reflex/core/orchestrate.js.map +1 -0
- package/dist/vendor/harness-reflex/core/policy.d.ts +122 -0
- package/dist/vendor/harness-reflex/core/policy.js +102 -0
- package/dist/vendor/harness-reflex/core/policy.js.map +1 -0
- package/dist/vendor/harness-reflex/core/prune-plan.d.ts +141 -0
- package/dist/vendor/harness-reflex/core/prune-plan.js +390 -0
- package/dist/vendor/harness-reflex/core/prune-plan.js.map +1 -0
- package/dist/vendor/harness-reflex/core/text.d.ts +36 -0
- package/dist/vendor/harness-reflex/core/text.js +66 -0
- package/dist/vendor/harness-reflex/core/text.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/audit.d.ts +18 -0
- package/dist/vendor/harness-reflex/ports/audit.js +11 -0
- package/dist/vendor/harness-reflex/ports/audit.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/clock.d.ts +14 -0
- package/dist/vendor/harness-reflex/ports/clock.js +11 -0
- package/dist/vendor/harness-reflex/ports/clock.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/decision.d.ts +75 -0
- package/dist/vendor/harness-reflex/ports/decision.js +2 -0
- package/dist/vendor/harness-reflex/ports/decision.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/index.d.ts +14 -0
- package/dist/vendor/harness-reflex/ports/index.js +2 -0
- package/dist/vendor/harness-reflex/ports/index.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/log.d.ts +22 -0
- package/dist/vendor/harness-reflex/ports/log.js +12 -0
- package/dist/vendor/harness-reflex/ports/log.js.map +1 -0
- package/dist/vendor/harness-reflex/ports/state.d.ts +46 -0
- package/dist/vendor/harness-reflex/ports/state.js +13 -0
- package/dist/vendor/harness-reflex/ports/state.js.map +1 -0
- package/package.json +25 -13
package/dist/panel-api.js
CHANGED
|
@@ -1,13 +1,29 @@
|
|
|
1
|
+
import { connectionTarget } from './config.js';
|
|
1
2
|
import { INTERVENTION_CAPABILITIES } from './config.js';
|
|
2
3
|
/** Version reported by the panel; kept in step with `package.json` by a test. */
|
|
3
|
-
export const REFLEX_PLUGIN_VERSION = '0.
|
|
4
|
+
export const REFLEX_PLUGIN_VERSION = '0.3.0';
|
|
4
5
|
/** Namespace every panel route lives under. */
|
|
5
6
|
export const PANEL_BASE = '/dsh-reflex/api';
|
|
6
7
|
/** The route family, as the panel's phases address it. */
|
|
7
8
|
export const PANEL_ROUTES = {
|
|
8
9
|
snapshot: `${PANEL_BASE}/panel`,
|
|
9
10
|
health: `${PANEL_BASE}/health`,
|
|
10
|
-
clearLedger: `${PANEL_BASE}/ledger/clear
|
|
11
|
+
clearLedger: `${PANEL_BASE}/ledger/clear`,
|
|
12
|
+
/**
|
|
13
|
+
* Writes the host `modelSelection` switch into the profile patch.
|
|
14
|
+
*
|
|
15
|
+
* A route of its own, and a `POST`, because it is the only request in this
|
|
16
|
+
* family that changes a file outside this plugin's own settings section.
|
|
17
|
+
*/
|
|
18
|
+
delegation: `${PANEL_BASE}/delegation`,
|
|
19
|
+
/**
|
|
20
|
+
* Probes one System One profile from the server.
|
|
21
|
+
*
|
|
22
|
+
* A `POST` with a JSON body naming the profile, and not a `GET`: it performs
|
|
23
|
+
* outbound work on the user's behalf, and a cached or prefetched GET must never
|
|
24
|
+
* be able to trigger a connection attempt.
|
|
25
|
+
*/
|
|
26
|
+
systemOneTest: `${PANEL_BASE}/systemone/test`
|
|
11
27
|
};
|
|
12
28
|
/** How many recent ledger entries one snapshot carries. Default 25, cap 200. */
|
|
13
29
|
export const PANEL_LEDGER_WINDOW = 25;
|
|
@@ -44,6 +60,14 @@ function projectRow(entry) {
|
|
|
44
60
|
const switched = typeof rawSwitched === 'boolean' ? rawSwitched : undefined;
|
|
45
61
|
const rawContextTokens = details?.contextTokens;
|
|
46
62
|
const contextTokens = typeof rawContextTokens === 'number' && Number.isFinite(rawContextTokens) ? rawContextTokens : undefined;
|
|
63
|
+
// Provenance is projected exactly as recorded: an entry without it (written
|
|
64
|
+
// before g-068) gets nothing, and `null` is kept distinct from absent because
|
|
65
|
+
// "the connection names no model" is a fact, not a missing value.
|
|
66
|
+
const systemOneConnection = text('systemOneConnection');
|
|
67
|
+
const rawSystemOneModel = details?.systemOneModel;
|
|
68
|
+
const systemOneModel = typeof rawSystemOneModel === 'string' || rawSystemOneModel === null ? rawSystemOneModel : undefined;
|
|
69
|
+
const systemOneSource = text('systemOneSource');
|
|
70
|
+
const systemOneDetail = text('systemOneDetail');
|
|
47
71
|
return {
|
|
48
72
|
id: entry.id,
|
|
49
73
|
at: entry.timestamp,
|
|
@@ -62,7 +86,11 @@ function projectRow(entry) {
|
|
|
62
86
|
...(reason === undefined ? {} : { reason }),
|
|
63
87
|
...(currentModel === undefined ? {} : { currentModel }),
|
|
64
88
|
...(switched === undefined ? {} : { switched }),
|
|
65
|
-
...(contextTokens === undefined ? {} : { contextTokens })
|
|
89
|
+
...(contextTokens === undefined ? {} : { contextTokens }),
|
|
90
|
+
...(systemOneConnection === undefined ? {} : { systemOneConnection }),
|
|
91
|
+
...(systemOneModel === undefined ? {} : { systemOneModel }),
|
|
92
|
+
...(systemOneSource === undefined ? {} : { systemOneSource }),
|
|
93
|
+
...(systemOneDetail === undefined ? {} : { systemOneDetail })
|
|
66
94
|
};
|
|
67
95
|
}
|
|
68
96
|
/**
|
|
@@ -145,16 +173,8 @@ export function buildPanelSnapshot(facts, limit = PANEL_LEDGER_WINDOW) {
|
|
|
145
173
|
enabled: { ...config.tools.enabled },
|
|
146
174
|
active: [...facts.toolSurface]
|
|
147
175
|
},
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
active: facts.backendName,
|
|
151
|
-
apiKeyPresent: facts.apiKeyPresent,
|
|
152
|
-
...(facts.systemOneHealth === undefined ? {} : { health: { ...facts.systemOneHealth } }),
|
|
153
|
-
...(config.systemOne.baseUrl === undefined ? {} : { baseUrl: config.systemOne.baseUrl }),
|
|
154
|
-
...(config.systemOne.model === undefined ? {} : { model: config.systemOne.model }),
|
|
155
|
-
...(config.systemOne.apiKeyEnv === undefined ? {} : { apiKeyEnv: config.systemOne.apiKeyEnv }),
|
|
156
|
-
...(config.systemOne.timeoutMs === undefined ? {} : { timeoutMs: config.systemOne.timeoutMs })
|
|
157
|
-
},
|
|
176
|
+
delegation: projectDelegation(config, facts),
|
|
177
|
+
systemOne: projectSystemOne(config, facts),
|
|
158
178
|
ledger: {
|
|
159
179
|
summary: facts.ledger.getSummary(),
|
|
160
180
|
recent,
|
|
@@ -166,6 +186,127 @@ export function buildPanelSnapshot(facts, limit = PANEL_LEDGER_WINDOW) {
|
|
|
166
186
|
...(facts.modelCatalog === undefined ? {} : { modelCatalog: facts.modelCatalog })
|
|
167
187
|
};
|
|
168
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* Project the System One connection block, including every profile.
|
|
191
|
+
*
|
|
192
|
+
* The one rule this function exists to keep: **no key value may reach the
|
|
193
|
+
* payload**. It reads {@link PanelRuntimeFacts.profileKeys} — presence flags the
|
|
194
|
+
* caller gathered — and never `config.systemOne.profiles[*].apiKey`, which does
|
|
195
|
+
* exist in the resolved config because the backend needs it.
|
|
196
|
+
*
|
|
197
|
+
* @param config - the configuration in force.
|
|
198
|
+
* @param facts - gathered runtime facts.
|
|
199
|
+
* @returns the wire projection of the connection block.
|
|
200
|
+
*/
|
|
201
|
+
export function projectSystemOne(config, facts) {
|
|
202
|
+
const { systemOne } = config;
|
|
203
|
+
// Presence, never value: a profile "has" a key when its own secret slot is set,
|
|
204
|
+
// else when one of the variables the chain names holds a non-empty value.
|
|
205
|
+
const keyFacts = (name, profile) => {
|
|
206
|
+
const supplied = facts.profileKeys?.[name];
|
|
207
|
+
if (supplied !== undefined)
|
|
208
|
+
return supplied;
|
|
209
|
+
const own = typeof profile.apiKey === 'string' && profile.apiKey.length > 0;
|
|
210
|
+
return { present: own, fromProfile: own };
|
|
211
|
+
};
|
|
212
|
+
// A row shows the connection it would *actually use*. Since g-066 that is the
|
|
213
|
+
// profile's own fields and nothing else — there is no block beneath it to
|
|
214
|
+
// inherit from — and the model shown is the *selected* one, the same value the
|
|
215
|
+
// runtime sends and the probe tests. A selection pointing outside `modelIds`
|
|
216
|
+
// projects no model plus `modelMissing`, so the row can say why.
|
|
217
|
+
const profiles = Object.entries(systemOne.profiles ?? {}).map(([name, profile]) => {
|
|
218
|
+
const keys = keyFacts(name, profile);
|
|
219
|
+
const projected = connectionTarget(systemOne, name, profile);
|
|
220
|
+
const selectedOutsideList = profile.selectedModel !== undefined && !(profile.modelIds ?? []).includes(profile.selectedModel);
|
|
221
|
+
return {
|
|
222
|
+
name,
|
|
223
|
+
active: systemOne.active === name,
|
|
224
|
+
apiKeyPresent: keys.present,
|
|
225
|
+
apiKeyFromProfile: keys.fromProfile,
|
|
226
|
+
...(keys.variable === undefined ? {} : { apiKeyVariable: keys.variable }),
|
|
227
|
+
...(keys.locked === true ? { apiKeyLocked: true } : {}),
|
|
228
|
+
...(projected.baseUrl === undefined ? {} : { baseUrl: projected.baseUrl }),
|
|
229
|
+
...(projected.model === undefined ? {} : { model: projected.model }),
|
|
230
|
+
...(projected.apiKeyEnv === undefined ? {} : { apiKeyEnv: projected.apiKeyEnv }),
|
|
231
|
+
...(projected.timeoutMs === undefined ? {} : { timeoutMs: projected.timeoutMs }),
|
|
232
|
+
...(projected.backend === undefined ? {} : { backend: projected.backend }),
|
|
233
|
+
...(profile.modelIds === undefined ? {} : { modelIds: [...profile.modelIds] }),
|
|
234
|
+
// The *effective* selection, including the implicit "first listed id", so
|
|
235
|
+
// the page can show which model runs instead of rendering an empty choice.
|
|
236
|
+
// A broken selection projects no model at all (see `modelMissing`).
|
|
237
|
+
...(projected.model === undefined ? {} : { selectedModel: projected.model }),
|
|
238
|
+
...(selectedOutsideList ? { modelMissing: true } : {})
|
|
239
|
+
};
|
|
240
|
+
});
|
|
241
|
+
const resolvedCarriesKey = typeof systemOne.apiKey === 'string' && systemOne.apiKey.length > 0;
|
|
242
|
+
return {
|
|
243
|
+
backend: systemOne.backend,
|
|
244
|
+
active: facts.backendName,
|
|
245
|
+
apiKeyPresent: facts.apiKeyPresent,
|
|
246
|
+
apiKeyFromProfile: resolvedCarriesKey,
|
|
247
|
+
// Provenance travels as a source tag and a variable *name* — the caller
|
|
248
|
+
// resolved them through the one key chain and dropped the value there.
|
|
249
|
+
...(facts.apiKeySource === undefined ? {} : { apiKeySource: facts.apiKeySource }),
|
|
250
|
+
...(facts.apiKeyVariable === undefined ? {} : { apiKeyVariable: facts.apiKeyVariable }),
|
|
251
|
+
...(facts.apiKeyLocked === true ? { apiKeyLocked: true } : {}),
|
|
252
|
+
profiles,
|
|
253
|
+
activeProfileMissing: systemOne.activeProfileMissing === true,
|
|
254
|
+
...(systemOne.unconfigured === undefined ? {} : { unconfigured: systemOne.unconfigured }),
|
|
255
|
+
...(systemOne.activeProfileModelMissing === true ? { activeProfileModelMissing: true } : {}),
|
|
256
|
+
...(systemOne.unconfigured === 'model-selection-unusable' ? { activeProfileModelUnusable: true } : {}),
|
|
257
|
+
...(facts.systemOneHealth === undefined ? {} : { health: { ...facts.systemOneHealth } }),
|
|
258
|
+
...(systemOne.active === undefined ? {} : { activeProfile: systemOne.active }),
|
|
259
|
+
...(systemOne.baseUrl === undefined ? {} : { baseUrl: systemOne.baseUrl }),
|
|
260
|
+
...(systemOne.model === undefined ? {} : { model: systemOne.model }),
|
|
261
|
+
...(systemOne.apiKeyEnv === undefined ? {} : { apiKeyEnv: systemOne.apiKeyEnv }),
|
|
262
|
+
...(systemOne.timeoutMs === undefined ? {} : { timeoutMs: systemOne.timeoutMs })
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Project the delegation switch, the host patch it edits, and the live capability.
|
|
267
|
+
*
|
|
268
|
+
* The "not working" reasons are kept apart on purpose, because the fix differs:
|
|
269
|
+
* `model-selection-off` is a tool that does not publish the level (tick the
|
|
270
|
+
* switch, restart), while `missing-tool` is a host without a delegation tool at
|
|
271
|
+
* all (nothing this plugin can do). `unknown` is neither: it means the capability
|
|
272
|
+
* was not probed, and it must never be rendered as "off".
|
|
273
|
+
*
|
|
274
|
+
* `registered` now follows **configuration** (g-054), so the one contradiction
|
|
275
|
+
* this view exists to prevent — "the switch is ticked, the tool is never
|
|
276
|
+
* registered" — can no longer be produced by the plugin. `restartRequired` is
|
|
277
|
+
* the remaining explanation for "I ticked it and the level still does not take
|
|
278
|
+
* effect": the file asks, but the running host still does not publish the
|
|
279
|
+
* parameter.
|
|
280
|
+
* @param config - the configuration in force.
|
|
281
|
+
* @param facts - gathered runtime facts.
|
|
282
|
+
* @returns the wire projection of the delegation block.
|
|
283
|
+
*/
|
|
284
|
+
export function projectDelegation(config, facts) {
|
|
285
|
+
const state = facts.delegationHealth?.state ?? 'unknown';
|
|
286
|
+
const enabled = config.delegation.enabled;
|
|
287
|
+
const registered = [...facts.toolSurface].some((name) => name.endsWith('delegate'));
|
|
288
|
+
const patchEnabled = facts.hostPatch?.enabled ?? false;
|
|
289
|
+
// The runtime's own reading wins over the file's: a stale file that asks for
|
|
290
|
+
// the field while the tool still refuses is precisely the restart case.
|
|
291
|
+
const broken = state === 'model-selection-off' || state === 'missing-tool';
|
|
292
|
+
const reason = enabled && broken
|
|
293
|
+
? facts.delegationHealth?.reason
|
|
294
|
+
: // An unprobed capability explains itself: without this the panel would
|
|
295
|
+
// fall back to the patch's reason and imply the patch was the problem.
|
|
296
|
+
enabled && state === 'unknown' && registered
|
|
297
|
+
? facts.delegationHealth?.reason
|
|
298
|
+
: facts.hostPatch?.reason;
|
|
299
|
+
return {
|
|
300
|
+
enabled,
|
|
301
|
+
registered,
|
|
302
|
+
state,
|
|
303
|
+
patchEnabled,
|
|
304
|
+
restartRequired: enabled && patchEnabled && state === 'model-selection-off',
|
|
305
|
+
...(facts.hostPatch?.path === undefined ? {} : { patchPath: facts.hostPatch.path }),
|
|
306
|
+
...(facts.hostPatch?.source === undefined ? {} : { patchPathSource: facts.hostPatch.source }),
|
|
307
|
+
...(reason === undefined ? {} : { reason })
|
|
308
|
+
};
|
|
309
|
+
}
|
|
169
310
|
/** Detached copy of one pool entry, so a caller cannot mutate the payload later. */
|
|
170
311
|
function clonePoolEntry(entry) {
|
|
171
312
|
return {
|
|
@@ -281,6 +422,29 @@ export function answerPanelRequest(source, request) {
|
|
|
281
422
|
if (method !== 'GET' && method !== 'HEAD')
|
|
282
423
|
return methodNotAllowed('GET, HEAD');
|
|
283
424
|
return { status: 200, body: { ok: true, name: 'dsh-reflex', version: REFLEX_PLUGIN_VERSION } };
|
|
425
|
+
case PANEL_ROUTES.delegation:
|
|
426
|
+
if (method !== 'POST')
|
|
427
|
+
return methodNotAllowed('POST');
|
|
428
|
+
// The write edits a file the user owns, outside this plugin's settings
|
|
429
|
+
// section. Same fence as the ledger clear: an accidental or cross-site
|
|
430
|
+
// GET/POST must not be enough, so the caller has to name the intent.
|
|
431
|
+
if (readQuery(request.url, 'confirm') !== 'delegation') {
|
|
432
|
+
return {
|
|
433
|
+
status: 400,
|
|
434
|
+
body: {
|
|
435
|
+
ok: false,
|
|
436
|
+
error: 'confirmation-required',
|
|
437
|
+
detail: 'this writes tool-subagent.modelSelection into the profile patch; repeat the POST with ?confirm=delegation'
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
if (source.enableDelegation === undefined) {
|
|
442
|
+
return {
|
|
443
|
+
status: 501,
|
|
444
|
+
body: { ok: false, error: 'not-writable', detail: 'this host cannot write the profile patch; add the row by hand' }
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
return { status: 200, body: { ok: true, ...source.enableDelegation() } };
|
|
284
448
|
case PANEL_ROUTES.clearLedger:
|
|
285
449
|
if (method !== 'POST')
|
|
286
450
|
return methodNotAllowed('POST');
|
|
@@ -301,6 +465,121 @@ export function answerPanelRequest(source, request) {
|
|
|
301
465
|
return { status: 404, body: { ok: false, error: 'not-found', detail: `unknown reflex panel route "${path}"` } };
|
|
302
466
|
}
|
|
303
467
|
}
|
|
468
|
+
/** Largest body the probe route will read, so a request cannot make it buffer the world. */
|
|
469
|
+
export const PANEL_PROBE_BODY_MAX = 4_096;
|
|
470
|
+
/**
|
|
471
|
+
* Read one request body, bounded.
|
|
472
|
+
*
|
|
473
|
+
* @param request - the incoming request.
|
|
474
|
+
* @param max - largest accepted length in bytes.
|
|
475
|
+
* @returns the body text, or `undefined` when it exceeded the bound.
|
|
476
|
+
*/
|
|
477
|
+
async function readBoundedBody(request, max = PANEL_PROBE_BODY_MAX) {
|
|
478
|
+
const stream = request;
|
|
479
|
+
let size = 0;
|
|
480
|
+
const chunks = [];
|
|
481
|
+
for await (const chunk of stream) {
|
|
482
|
+
const text = typeof chunk === 'string' ? chunk : chunk.toString('utf8');
|
|
483
|
+
size += Buffer.byteLength(text, 'utf8');
|
|
484
|
+
if (size > max)
|
|
485
|
+
return undefined;
|
|
486
|
+
chunks.push(text);
|
|
487
|
+
}
|
|
488
|
+
return chunks.join('');
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Resolve one panel request that may need to read a body or probe a connection.
|
|
492
|
+
*
|
|
493
|
+
* The synchronous {@link answerPanelRequest} stays the entry point for every
|
|
494
|
+
* route that needs neither, which is what keeps its existing callers (and their
|
|
495
|
+
* assertions) unchanged. This one handles the probe route, whose body names the
|
|
496
|
+
* profile and whose answer takes a real network round trip.
|
|
497
|
+
*
|
|
498
|
+
* @param source - the live facts.
|
|
499
|
+
* @param request - the incoming request.
|
|
500
|
+
* @returns the answer to serialize.
|
|
501
|
+
*/
|
|
502
|
+
export async function answerPanelRequestAsync(source, request) {
|
|
503
|
+
const path = (request.url ?? '/').split('?')[0] ?? '/';
|
|
504
|
+
if (path !== PANEL_ROUTES.systemOneTest)
|
|
505
|
+
return answerPanelRequest(source, request);
|
|
506
|
+
const method = (request.method ?? 'GET').toUpperCase();
|
|
507
|
+
if (!source.allows(request)) {
|
|
508
|
+
return { status: 403, body: { ok: false, error: 'forbidden', detail: 'the reflex panel answers loopback clients and paired devices only' } };
|
|
509
|
+
}
|
|
510
|
+
if (method !== 'POST')
|
|
511
|
+
return methodNotAllowed('POST');
|
|
512
|
+
if (source.testSystemOne === undefined) {
|
|
513
|
+
return {
|
|
514
|
+
status: 501,
|
|
515
|
+
body: { ok: false, error: 'not-probeable', detail: 'this host cannot reach a System One endpoint from the server' }
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
// An absent body means "the active profile"; anything else must be JSON naming
|
|
519
|
+
// one. A malformed body is refused here rather than guessed at.
|
|
520
|
+
const raw = await readBoundedBody(request);
|
|
521
|
+
if (raw === undefined) {
|
|
522
|
+
return { status: 413, body: { ok: false, error: 'body-too-large', detail: `the request body must stay under ${String(PANEL_PROBE_BODY_MAX)} bytes` } };
|
|
523
|
+
}
|
|
524
|
+
let requested;
|
|
525
|
+
const trimmed = raw.trim();
|
|
526
|
+
if (trimmed.length > 0) {
|
|
527
|
+
let parsed;
|
|
528
|
+
try {
|
|
529
|
+
parsed = JSON.parse(trimmed);
|
|
530
|
+
}
|
|
531
|
+
catch {
|
|
532
|
+
return { status: 400, body: { ok: false, error: 'bad-json', detail: 'the request body must be JSON like {"profile":"name"}' } };
|
|
533
|
+
}
|
|
534
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
535
|
+
return { status: 400, body: { ok: false, error: 'bad-body', detail: 'the request body must be a JSON object' } };
|
|
536
|
+
}
|
|
537
|
+
const value = parsed.profile;
|
|
538
|
+
if (value !== undefined && typeof value !== 'string') {
|
|
539
|
+
return { status: 400, body: { ok: false, error: 'bad-profile', detail: '"profile" must be a string when present' } };
|
|
540
|
+
}
|
|
541
|
+
requested = typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
|
|
542
|
+
}
|
|
543
|
+
// The client may go away mid-probe (panel closed, route unmounted); the probe
|
|
544
|
+
// then stops instead of completing into a destroyed socket.
|
|
545
|
+
const controller = new AbortController();
|
|
546
|
+
request.once?.('close', () => controller.abort());
|
|
547
|
+
try {
|
|
548
|
+
const result = await source.testSystemOne(requested, controller.signal);
|
|
549
|
+
// `result` already carries `ok`, so the spread is the whole payload.
|
|
550
|
+
return { status: 200, body: result };
|
|
551
|
+
}
|
|
552
|
+
catch (error) {
|
|
553
|
+
// A probe is supposed to classify its own failures; reaching here means a bug.
|
|
554
|
+
// Report it as a failed probe rather than a 500, so the row still renders.
|
|
555
|
+
return {
|
|
556
|
+
status: 200,
|
|
557
|
+
body: {
|
|
558
|
+
ok: false,
|
|
559
|
+
status: 'bad-response',
|
|
560
|
+
latencyMs: 0,
|
|
561
|
+
profile: requested ?? null,
|
|
562
|
+
detail: error instanceof Error ? redactProbeText(error.message) : 'the probe failed'
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* Scrub one probe detail before it is put on the wire.
|
|
569
|
+
*
|
|
570
|
+
* The probe already redacts; this is the second gate, at the boundary the payload
|
|
571
|
+
* actually leaves through, so a bug in the first cannot become a leak.
|
|
572
|
+
* @param text - raw detail.
|
|
573
|
+
* @returns a single-line string with credential shapes removed.
|
|
574
|
+
*/
|
|
575
|
+
function redactProbeText(text) {
|
|
576
|
+
return String(text ?? '')
|
|
577
|
+
.replace(/Bearer\s+[^\s"',;}]+/gi, 'Bearer ***')
|
|
578
|
+
.replace(/\b(sk|pk|key)-[A-Za-z0-9._-]{6,}/g, '$1-***')
|
|
579
|
+
.replace(/\s+/g, ' ')
|
|
580
|
+
.trim()
|
|
581
|
+
.slice(0, 240);
|
|
582
|
+
}
|
|
304
583
|
/** Standard 405 answer. */
|
|
305
584
|
function methodNotAllowed(allow) {
|
|
306
585
|
return { status: 405, body: { ok: false, error: 'method-not-allowed', detail: `use ${allow}` }, headers: { allow } };
|
|
@@ -335,17 +614,45 @@ export function sendPanelAnswer(request, response, answer) {
|
|
|
335
614
|
* One handler owns every path in the family, so the method/path decision lives
|
|
336
615
|
* in {@link answerPanelRequest} and cannot drift between routes; the
|
|
337
616
|
* `kind: 'exact'` paths mean the server never routes an unrelated URL here.
|
|
617
|
+
*
|
|
618
|
+
* ## Why the handler catches
|
|
619
|
+
*
|
|
620
|
+
* The Host maps a throw out of a route handler onto a bare `400` with an empty
|
|
621
|
+
* body, which erases the stack — the one thing needed to diagnose it. A panel
|
|
622
|
+
* route that fails must therefore answer for itself: `500` plus the real detail.
|
|
623
|
+
* This is not a substitute for handling a known failure where it happens (a
|
|
624
|
+
* missing service is read defensively, not caught here); it guarantees that an
|
|
625
|
+
* *unknown* one is still visible instead of silently becoming "bad request".
|
|
338
626
|
* @param source - the live facts.
|
|
339
627
|
* @returns the routes to register.
|
|
340
628
|
*/
|
|
341
629
|
export function makePanelRoutes(source) {
|
|
342
630
|
const handler = (request, response) => {
|
|
343
|
-
|
|
631
|
+
void answerPanelRequestAsync(source, request)
|
|
632
|
+
.then((answer) => sendPanelAnswer(request, response, answer))
|
|
633
|
+
.catch((error) => {
|
|
634
|
+
reportHandlerError(response, error);
|
|
635
|
+
});
|
|
636
|
+
};
|
|
637
|
+
const reportHandlerError = (response, error) => {
|
|
638
|
+
const detail = error instanceof Error ? (error.stack ?? error.message) : String(error);
|
|
639
|
+
try {
|
|
640
|
+
response.writeHead(500, {
|
|
641
|
+
'content-type': 'application/json; charset=utf-8',
|
|
642
|
+
'cache-control': 'no-store'
|
|
643
|
+
});
|
|
644
|
+
response.end(JSON.stringify({ ok: false, error: 'panel-handler-threw', detail }));
|
|
645
|
+
}
|
|
646
|
+
catch {
|
|
647
|
+
// The socket is already gone: there is nobody left to report to.
|
|
648
|
+
}
|
|
344
649
|
};
|
|
345
650
|
return [
|
|
346
651
|
{ kind: 'exact', path: PANEL_ROUTES.snapshot, handler },
|
|
347
652
|
{ kind: 'exact', path: PANEL_ROUTES.health, handler },
|
|
348
|
-
{ kind: 'exact', path: PANEL_ROUTES.clearLedger, handler }
|
|
653
|
+
{ kind: 'exact', path: PANEL_ROUTES.clearLedger, handler },
|
|
654
|
+
{ kind: 'exact', path: PANEL_ROUTES.delegation, handler },
|
|
655
|
+
{ kind: 'exact', path: PANEL_ROUTES.systemOneTest, handler }
|
|
349
656
|
];
|
|
350
657
|
}
|
|
351
658
|
/** Every path this family owns, for diagnostics and tests. */
|