harness-reflex 0.0.0-stage → 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/CHANGELOG.md +20 -0
- package/README.md +266 -2
- package/USER_GUIDE.md +639 -0
- package/dist/adapters/agy.d.ts +8 -0
- package/dist/adapters/agy.js +80 -0
- package/dist/adapters/agy.js.map +1 -0
- package/dist/adapters/claude.d.ts +13 -0
- package/dist/adapters/claude.js +46 -0
- package/dist/adapters/claude.js.map +1 -0
- package/dist/adapters/codex.d.ts +139 -0
- package/dist/adapters/codex.js +368 -0
- package/dist/adapters/codex.js.map +1 -0
- package/dist/backend/defaults.d.ts +31 -0
- package/dist/backend/defaults.js +32 -0
- package/dist/backend/defaults.js.map +1 -0
- package/dist/backend/factory.d.ts +11 -0
- package/dist/backend/factory.js +40 -0
- package/dist/backend/factory.js.map +1 -0
- package/dist/backend/jev.d.ts +524 -0
- package/dist/backend/jev.js +1486 -0
- package/dist/backend/jev.js.map +1 -0
- package/dist/backend/mock.d.ts +26 -0
- package/dist/backend/mock.js +320 -0
- package/dist/backend/mock.js.map +1 -0
- package/dist/backend/route-prompt.d.ts +37 -0
- package/dist/backend/route-prompt.js +42 -0
- package/dist/backend/route-prompt.js.map +1 -0
- package/dist/backend/simple-jev.d.ts +35 -0
- package/dist/backend/simple-jev.js +273 -0
- package/dist/backend/simple-jev.js.map +1 -0
- package/dist/backend/types.d.ts +270 -0
- package/dist/backend/types.js +5 -0
- package/dist/backend/types.js.map +1 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +278 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/core/calibrate.d.ts +56 -0
- package/dist/core/calibrate.js +144 -0
- package/dist/core/calibrate.js.map +1 -0
- package/dist/core/closing.d.ts +90 -0
- package/dist/core/closing.js +111 -0
- package/dist/core/closing.js.map +1 -0
- package/dist/core/evidence.d.ts +119 -0
- package/dist/core/evidence.js +209 -0
- package/dist/core/evidence.js.map +1 -0
- package/dist/core/gate.d.ts +25 -0
- package/dist/core/gate.js +114 -0
- package/dist/core/gate.js.map +1 -0
- package/dist/core/judge.d.ts +221 -0
- package/dist/core/judge.js +251 -0
- package/dist/core/judge.js.map +1 -0
- package/dist/core/ledger.d.ts +67 -0
- package/dist/core/ledger.js +173 -0
- package/dist/core/ledger.js.map +1 -0
- package/dist/core/orchestrate-closing.d.ts +237 -0
- package/dist/core/orchestrate-closing.js +250 -0
- package/dist/core/orchestrate-closing.js.map +1 -0
- package/dist/core/orchestrate-correction.d.ts +209 -0
- package/dist/core/orchestrate-correction.js +141 -0
- package/dist/core/orchestrate-correction.js.map +1 -0
- package/dist/core/orchestrate-host.d.ts +225 -0
- package/dist/core/orchestrate-host.js +80 -0
- package/dist/core/orchestrate-host.js.map +1 -0
- package/dist/core/orchestrate-stagnation.d.ts +259 -0
- package/dist/core/orchestrate-stagnation.js +270 -0
- package/dist/core/orchestrate-stagnation.js.map +1 -0
- package/dist/core/orchestrate.d.ts +357 -0
- package/dist/core/orchestrate.js +418 -0
- package/dist/core/orchestrate.js.map +1 -0
- package/dist/core/policy.d.ts +122 -0
- package/dist/core/policy.js +102 -0
- package/dist/core/policy.js.map +1 -0
- package/dist/core/prune-plan.d.ts +141 -0
- package/dist/core/prune-plan.js +390 -0
- package/dist/core/prune-plan.js.map +1 -0
- package/dist/core/prune.d.ts +30 -0
- package/dist/core/prune.js +73 -0
- package/dist/core/prune.js.map +1 -0
- package/dist/core/route.d.ts +35 -0
- package/dist/core/route.js +39 -0
- package/dist/core/route.js.map +1 -0
- package/dist/core/score.d.ts +40 -0
- package/dist/core/score.js +49 -0
- package/dist/core/score.js.map +1 -0
- package/dist/core/screen.d.ts +22 -0
- package/dist/core/screen.js +117 -0
- package/dist/core/screen.js.map +1 -0
- package/dist/core/stagnation.d.ts +23 -0
- package/dist/core/stagnation.js +93 -0
- package/dist/core/stagnation.js.map +1 -0
- package/dist/core/text.d.ts +36 -0
- package/dist/core/text.js +66 -0
- package/dist/core/text.js.map +1 -0
- package/dist/hook/cli.d.ts +9 -0
- package/dist/hook/cli.js +52 -0
- package/dist/hook/cli.js.map +1 -0
- package/dist/hook/contract.d.ts +125 -0
- package/dist/hook/contract.js +159 -0
- package/dist/hook/contract.js.map +1 -0
- package/dist/hook/decision.d.ts +97 -0
- package/dist/hook/decision.js +955 -0
- package/dist/hook/decision.js.map +1 -0
- package/dist/hook/memory-store.d.ts +81 -0
- package/dist/hook/memory-store.js +157 -0
- package/dist/hook/memory-store.js.map +1 -0
- package/dist/hook/payload.d.ts +55 -0
- package/dist/hook/payload.js +129 -0
- package/dist/hook/payload.js.map +1 -0
- package/dist/hook/run.d.ts +48 -0
- package/dist/hook/run.js +218 -0
- package/dist/hook/run.js.map +1 -0
- package/dist/hook/store-port.d.ts +72 -0
- package/dist/hook/store-port.js +2 -0
- package/dist/hook/store-port.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/server.d.ts +12 -0
- package/dist/mcp/server.js +368 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/ports/audit.d.ts +18 -0
- package/dist/ports/audit.js +11 -0
- package/dist/ports/audit.js.map +1 -0
- package/dist/ports/clock.d.ts +14 -0
- package/dist/ports/clock.js +11 -0
- package/dist/ports/clock.js.map +1 -0
- package/dist/ports/decision.d.ts +75 -0
- package/dist/ports/decision.js +2 -0
- package/dist/ports/decision.js.map +1 -0
- package/dist/ports/index.d.ts +14 -0
- package/dist/ports/index.js +2 -0
- package/dist/ports/index.js.map +1 -0
- package/dist/ports/log.d.ts +22 -0
- package/dist/ports/log.js +12 -0
- package/dist/ports/log.js.map +1 -0
- package/dist/ports/state.d.ts +46 -0
- package/dist/ports/state.js +13 -0
- package/dist/ports/state.js.map +1 -0
- package/dist/state/clock.d.ts +40 -0
- package/dist/state/clock.js +43 -0
- package/dist/state/clock.js.map +1 -0
- package/dist/state/file-store.d.ts +340 -0
- package/dist/state/file-store.js +849 -0
- package/dist/state/file-store.js.map +1 -0
- package/dist/state/paths.d.ts +41 -0
- package/dist/state/paths.js +73 -0
- package/dist/state/paths.js.map +1 -0
- package/docs/USER_GUIDE.en.md +721 -0
- package/docs/claude-code.md +281 -0
- package/docs/design/v0.2.0-harness-plugins.md +381 -0
- package/docs/design/v0.3.0-orchestration-unification.md +580 -0
- package/docs/dsh-plugin.md +578 -0
- package/docs/reflex-ui-regression-2026-10-20.md +34 -0
- package/docs/release-checklist-v0.1.0.md +315 -0
- package/docs/release-handbook-porting-notes.md +106 -0
- package/docs/release-handbook.md +393 -0
- package/docs/release-notes-v0.1.0.md +63 -0
- package/docs/release-notes-v0.3.0.md +152 -0
- package/docs/research/claude-code-harness.md +607 -0
- package/docs/research/pi-harness.md +621 -0
- package/experiments/data/calibration_sample.json +14 -0
- package/experiments/index.ts +29 -0
- package/experiments/runner.ts +156 -0
- package/package.json +82 -4
|
@@ -0,0 +1,1486 @@
|
|
|
1
|
+
import { CURRENT_MODEL_MARK, buildRouteContext } from './route-prompt.js';
|
|
2
|
+
import { JEV_DEFAULT_API_KEY, JEV_DEFAULT_BASE_URL, JEV_DEFAULT_MODEL, JEV_DEFAULT_TIMEOUT_MS } from './defaults.js';
|
|
3
|
+
/** Path of the one endpoint the current gateway exposes. */
|
|
4
|
+
export const SYSTEMONE_ENDPOINT_PATH = '/v1/systemone';
|
|
5
|
+
/** Endpoint used when neither configuration nor environment supplies a base. */
|
|
6
|
+
export const DEFAULT_SYSTEMONE_ENDPOINT = JEV_DEFAULT_BASE_URL;
|
|
7
|
+
/** Illustration of a choice request, for docs and drift checks. */
|
|
8
|
+
export const CHOICE_REQUEST_EXAMPLE = {
|
|
9
|
+
model: JEV_DEFAULT_MODEL,
|
|
10
|
+
state: {},
|
|
11
|
+
questions: {
|
|
12
|
+
q: { type: 'choice', instructions: 'Which option is better?', criteria: { a: 'the first option', b: 'the second option' } }
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
/** Question id of a plain {@link JevDecisionBackend.choice} call. */
|
|
16
|
+
export const CHOICE_QUESTION_ID = 'choice';
|
|
17
|
+
/** Question id of a {@link JevDecisionBackend.route} call. */
|
|
18
|
+
export const ROUTE_QUESTION_ID = 'route';
|
|
19
|
+
/** Question id of a {@link JevDecisionBackend.prune} call. */
|
|
20
|
+
export const PRUNE_QUESTION_ID = 'prune';
|
|
21
|
+
/** Question id of a {@link JevDecisionBackend.checkStagnation} call. */
|
|
22
|
+
export const STAGNATION_QUESTION_ID = 'stagnation';
|
|
23
|
+
/** Prefix of the per-assertion question ids of a {@link JevDecisionBackend.gate} call. */
|
|
24
|
+
export const GATE_QUESTION_PREFIX = 'assertion-';
|
|
25
|
+
/** Criteria of a gate assertion, in the order ties are broken. */
|
|
26
|
+
export const GATE_VERDICTS = ['verified', 'contradicted', 'not_addressed'];
|
|
27
|
+
/** What each gate verdict means to the model. */
|
|
28
|
+
export const GATE_VERDICT_CRITERIA = {
|
|
29
|
+
verified: 'The evidence directly supports the assertion.',
|
|
30
|
+
contradicted: 'The evidence contradicts the assertion.',
|
|
31
|
+
not_addressed: 'The evidence neither supports nor contradicts the assertion.'
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Minimum probability on `verified` for a strictness level to claim success.
|
|
35
|
+
* Anything below is escalated: a three-way choice at chance is 1/3, so the
|
|
36
|
+
* lenient floor never accepts a distribution the model did not actually prefer.
|
|
37
|
+
*/
|
|
38
|
+
export const GATE_CONFIDENCE_FLOOR = {
|
|
39
|
+
high: 0.7,
|
|
40
|
+
normal: 0.5,
|
|
41
|
+
lenient: 0.34
|
|
42
|
+
};
|
|
43
|
+
/** Pattern criteria, in the order ties are broken. */
|
|
44
|
+
export const STAGNATION_PATTERNS = [
|
|
45
|
+
'repetition',
|
|
46
|
+
'oscillation',
|
|
47
|
+
'failing_retry',
|
|
48
|
+
'zero_progress',
|
|
49
|
+
'none'
|
|
50
|
+
];
|
|
51
|
+
/** What each stagnation pattern means to the model. */
|
|
52
|
+
export const STAGNATION_PATTERN_CRITERIA = {
|
|
53
|
+
repetition: 'The same action with the same input repeats with no new information.',
|
|
54
|
+
oscillation: 'Execution alternates between the same two actions or edits without progress.',
|
|
55
|
+
failing_retry: 'Actions keep failing with recurring errors and are retried unchanged.',
|
|
56
|
+
zero_progress: 'Actions run but nothing observable advances the goal.',
|
|
57
|
+
none: 'The run is progressing; none of the stagnation patterns apply.'
|
|
58
|
+
};
|
|
59
|
+
/** One-line diagnosis of each pattern. */
|
|
60
|
+
export const STAGNATION_DIAGNOSIS = {
|
|
61
|
+
repetition: 'Identical consecutive tool executions with unchanged input.',
|
|
62
|
+
oscillation: 'The run alternates between two identical actions or edits.',
|
|
63
|
+
failing_retry: 'Recent actions keep failing with recurring errors.',
|
|
64
|
+
zero_progress: 'Actions run, but nothing observable advances the goal.',
|
|
65
|
+
none: 'Execution flow appears progressive.'
|
|
66
|
+
};
|
|
67
|
+
/** Corrective action recommended for each stuck pattern. */
|
|
68
|
+
export const STAGNATION_INTERVENTION = {
|
|
69
|
+
repetition: 'Interrupt execution. Change the approach or inspect the previous failure output.',
|
|
70
|
+
oscillation: 'Break the loop: stop alternating, restate the requirement, verify independently.',
|
|
71
|
+
failing_retry: 'Step back and diagnose the root cause before retrying the same action.',
|
|
72
|
+
zero_progress: 'Re-read the goal and pick a step that produces observable evidence of progress.',
|
|
73
|
+
none: 'Continue.'
|
|
74
|
+
};
|
|
75
|
+
/** Minimum probability for a stuck pattern to be reported as a real alert. */
|
|
76
|
+
export const STAGNATION_STUCK_FLOOR = 0.4;
|
|
77
|
+
/* -------------------------------------------------------------------------- */
|
|
78
|
+
/* Pure helpers (exported for tests) */
|
|
79
|
+
/* -------------------------------------------------------------------------- */
|
|
80
|
+
/** Elapsed milliseconds, rounded and never negative. */
|
|
81
|
+
function elapsed(start) {
|
|
82
|
+
return Math.max(0, Math.round(performance.now() - start));
|
|
83
|
+
}
|
|
84
|
+
/** Message of an unknown thrown value. */
|
|
85
|
+
function errorMessage(error) {
|
|
86
|
+
return error instanceof Error ? error.message : String(error);
|
|
87
|
+
}
|
|
88
|
+
/** A probability, clamped into `[0, 1]`; anything unusable reads as 0. */
|
|
89
|
+
function toProbability(value) {
|
|
90
|
+
const numeric = typeof value === 'number' ? value : Number(value);
|
|
91
|
+
if (!Number.isFinite(numeric) || numeric <= 0)
|
|
92
|
+
return 0;
|
|
93
|
+
return Math.min(1, numeric);
|
|
94
|
+
}
|
|
95
|
+
/** A probability rounded to the four decimals the decision surfaces report. */
|
|
96
|
+
function round4(value) {
|
|
97
|
+
return Number(value.toFixed(4));
|
|
98
|
+
}
|
|
99
|
+
/** Comparison key for a criterion: the gateway echoes keys, but be forgiving. */
|
|
100
|
+
function normalizeKey(key) {
|
|
101
|
+
return key.trim().toLowerCase();
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Resolve the endpoint the single request goes to.
|
|
105
|
+
*
|
|
106
|
+
* Every accepted spelling of the base — the full `…/v1/systemone` URL the docs
|
|
107
|
+
* and `JEV_API_BASE` use, a `/v1` root, or a bare host — collapses onto exactly
|
|
108
|
+
* one URL, so a retired per-capability sub-path can never be appended.
|
|
109
|
+
* @param baseUrl - configured or environment base; empty selects the default.
|
|
110
|
+
* @returns the absolute URL of `POST /v1/systemone`.
|
|
111
|
+
*/
|
|
112
|
+
export function normalizeSystemOneEndpoint(baseUrl) {
|
|
113
|
+
const trimmed = (baseUrl ?? '').trim().replace(/\/+$/, '');
|
|
114
|
+
if (trimmed.length === 0)
|
|
115
|
+
return DEFAULT_SYSTEMONE_ENDPOINT;
|
|
116
|
+
if (trimmed.endsWith(SYSTEMONE_ENDPOINT_PATH))
|
|
117
|
+
return trimmed;
|
|
118
|
+
if (trimmed.endsWith('/systemone'))
|
|
119
|
+
return `${trimmed.slice(0, -'/systemone'.length)}${SYSTEMONE_ENDPOINT_PATH}`;
|
|
120
|
+
if (trimmed.endsWith('/v1'))
|
|
121
|
+
return `${trimmed}/systemone`;
|
|
122
|
+
return `${trimmed}${SYSTEMONE_ENDPOINT_PATH}`;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Turn a list of labels into the unique criteria keys of a choice question.
|
|
126
|
+
*
|
|
127
|
+
* Criteria keys are the option labels themselves, which is what makes the
|
|
128
|
+
* answer's probability dictionary readable as option probabilities. Duplicates
|
|
129
|
+
* (two pool entries with the same name) get a numeric suffix so no option can be
|
|
130
|
+
* lost in the dictionary.
|
|
131
|
+
* @param labels - the option labels, in caller order.
|
|
132
|
+
* @returns one unique key per label, in the same order.
|
|
133
|
+
*/
|
|
134
|
+
export function uniqueCriteriaKeys(labels) {
|
|
135
|
+
const used = new Set();
|
|
136
|
+
return labels.map((raw, index) => {
|
|
137
|
+
const label = typeof raw === 'string' ? raw.trim() : '';
|
|
138
|
+
let key = label.length > 0 ? label : `option ${String(index + 1)}`;
|
|
139
|
+
if (used.has(key)) {
|
|
140
|
+
let suffix = 2;
|
|
141
|
+
while (used.has(`${key} (${String(suffix)})`))
|
|
142
|
+
suffix++;
|
|
143
|
+
key = `${key} (${String(suffix)})`;
|
|
144
|
+
}
|
|
145
|
+
used.add(key);
|
|
146
|
+
return key;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Read one answer's distribution against the criteria keys it was asked about.
|
|
151
|
+
* @param probabilities - `answers[id].probabilities`, when present.
|
|
152
|
+
* @param keys - criteria keys of the question, in declared order.
|
|
153
|
+
* @returns every key mapped to its probability (missing keys read 0), or
|
|
154
|
+
* `undefined` when the answer carries no usable distribution at all.
|
|
155
|
+
*/
|
|
156
|
+
export function readDistribution(probabilities, keys) {
|
|
157
|
+
if (!probabilities || typeof probabilities !== 'object')
|
|
158
|
+
return undefined;
|
|
159
|
+
const declared = new Map();
|
|
160
|
+
for (const key of keys)
|
|
161
|
+
declared.set(normalizeKey(key), key);
|
|
162
|
+
const distribution = new Map(keys.map((key) => [key, 0]));
|
|
163
|
+
let matched = false;
|
|
164
|
+
for (const [rawKey, rawValue] of Object.entries(probabilities)) {
|
|
165
|
+
const key = declared.get(normalizeKey(rawKey));
|
|
166
|
+
if (key === undefined)
|
|
167
|
+
continue;
|
|
168
|
+
distribution.set(key, (distribution.get(key) ?? 0) + toProbability(rawValue));
|
|
169
|
+
matched = true;
|
|
170
|
+
}
|
|
171
|
+
return matched ? distribution : undefined;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Highest-probability key of a distribution.
|
|
175
|
+
* @param distribution - probabilities keyed by criteria key.
|
|
176
|
+
* @param order - declared key order, used to break ties deterministically.
|
|
177
|
+
* @returns the winning key, or `undefined` for an empty order.
|
|
178
|
+
*/
|
|
179
|
+
export function argmaxKey(distribution, order) {
|
|
180
|
+
let best;
|
|
181
|
+
let bestValue = -Infinity;
|
|
182
|
+
for (const key of order) {
|
|
183
|
+
const value = distribution.get(key) ?? 0;
|
|
184
|
+
if (value > bestValue) {
|
|
185
|
+
best = key;
|
|
186
|
+
bestValue = value;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return best;
|
|
190
|
+
}
|
|
191
|
+
/** Move `index` to the front when it ties the current leader. */
|
|
192
|
+
function promoteTiedWinner(entries, index, scoreOf) {
|
|
193
|
+
if (index <= 0 || entries.length <= index)
|
|
194
|
+
return;
|
|
195
|
+
const winner = entries[index];
|
|
196
|
+
const leader = entries[0];
|
|
197
|
+
if (scoreOf(winner) !== scoreOf(leader))
|
|
198
|
+
return;
|
|
199
|
+
entries.splice(index, 1);
|
|
200
|
+
entries.unshift(winner);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Build the choice response options from one gateway answer.
|
|
204
|
+
*
|
|
205
|
+
* The distribution is authoritative; the gateway's discrete `choice` only
|
|
206
|
+
* breaks a tie. When the gateway returns no distribution but does name a
|
|
207
|
+
* criterion, the answer is reconstructed around that name and flagged
|
|
208
|
+
* `uncalibrated`, because the probabilities are then ours, not the gateway's.
|
|
209
|
+
* @param labels - option labels in caller order.
|
|
210
|
+
* @param keys - criteria keys, aligned with `labels`.
|
|
211
|
+
* @param distribution - parsed distribution, when the answer had one.
|
|
212
|
+
* @param answer - the raw gateway answer.
|
|
213
|
+
* @returns sorted options plus the uncalibrated flag, or `undefined` when the
|
|
214
|
+
* answer cannot be turned into a distribution at all.
|
|
215
|
+
*/
|
|
216
|
+
export function toChoiceOptions(labels, keys, distribution, answer) {
|
|
217
|
+
const declaredKey = typeof answer?.choice === 'string'
|
|
218
|
+
? keys.find((key) => normalizeKey(key) === normalizeKey(String(answer.choice)))
|
|
219
|
+
: undefined;
|
|
220
|
+
let resolved = distribution;
|
|
221
|
+
let uncalibrated = false;
|
|
222
|
+
if (resolved === undefined) {
|
|
223
|
+
if (declaredKey === undefined)
|
|
224
|
+
return undefined;
|
|
225
|
+
resolved = new Map(keys.map((key) => [key, 0]));
|
|
226
|
+
const confidence = toProbability(answer?.confidence) || 1;
|
|
227
|
+
resolved.set(declaredKey, confidence);
|
|
228
|
+
const others = keys.filter((key) => key !== declaredKey);
|
|
229
|
+
const share = others.length > 0 ? (1 - confidence) / others.length : 0;
|
|
230
|
+
for (const key of others)
|
|
231
|
+
resolved.set(key, share);
|
|
232
|
+
uncalibrated = true;
|
|
233
|
+
}
|
|
234
|
+
const options = labels.map((rawLabel, index) => ({
|
|
235
|
+
label: typeof rawLabel === 'string' ? rawLabel : String(rawLabel),
|
|
236
|
+
probability: round4(resolved?.get(keys[index] ?? '') ?? 0)
|
|
237
|
+
}));
|
|
238
|
+
options.sort((a, b) => b.probability - a.probability);
|
|
239
|
+
if (declaredKey !== undefined) {
|
|
240
|
+
const declaredLabel = labels[keys.indexOf(declaredKey)] ?? '';
|
|
241
|
+
promoteTiedWinner(options, options.findIndex((option) => option.label === declaredLabel), (option) => option.probability);
|
|
242
|
+
}
|
|
243
|
+
return { options, uncalibrated };
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Read the winning criteria index out of a `score` answer.
|
|
247
|
+
* @param answer - the gateway's score answer.
|
|
248
|
+
* @param count - number of criteria the question offered.
|
|
249
|
+
* @returns the argmax index, or `undefined` when the answer is unusable.
|
|
250
|
+
*/
|
|
251
|
+
export function readScoreIndex(answer, count) {
|
|
252
|
+
if (count <= 0)
|
|
253
|
+
return undefined;
|
|
254
|
+
const keys = Array.from({ length: count }, (_, index) => String(index));
|
|
255
|
+
const distribution = readDistribution(answer?.probabilities, keys);
|
|
256
|
+
if (distribution !== undefined) {
|
|
257
|
+
const key = argmaxKey(distribution, keys);
|
|
258
|
+
if (key === undefined || (distribution.get(key) ?? 0) <= 0)
|
|
259
|
+
return undefined;
|
|
260
|
+
return Number(key);
|
|
261
|
+
}
|
|
262
|
+
const raw = typeof answer?.score === 'number' ? answer.score : Number(answer?.score);
|
|
263
|
+
if (!Number.isFinite(raw))
|
|
264
|
+
return undefined;
|
|
265
|
+
return Math.min(count - 1, Math.max(0, Math.round(raw)));
|
|
266
|
+
}
|
|
267
|
+
/** Instruction block of one choice question. */
|
|
268
|
+
function buildChoiceInstructions(question) {
|
|
269
|
+
const trimmed = (question ?? '').trim();
|
|
270
|
+
return trimmed.length > 0 ? trimmed : 'Choose the criterion that best answers the task.';
|
|
271
|
+
}
|
|
272
|
+
/** Instruction block of one gate assertion. */
|
|
273
|
+
function buildGateInstructions(assertion, strictness) {
|
|
274
|
+
const rubric = (assertion.criteria ?? '').trim();
|
|
275
|
+
return [
|
|
276
|
+
'Decide which criterion describes how the STATE evidence relates to the ASSERTION.',
|
|
277
|
+
`ASSERTION: ${assertion.statement}`,
|
|
278
|
+
...(rubric.length > 0 ? [`ASSERTION RUBRIC: ${rubric}`] : []),
|
|
279
|
+
`STRICTNESS: ${strictness}`
|
|
280
|
+
].join('\n');
|
|
281
|
+
}
|
|
282
|
+
/** Instruction block of a stagnation classification. */
|
|
283
|
+
function buildStagnationInstructions() {
|
|
284
|
+
return [
|
|
285
|
+
'Classify the recent agent run in the STATE: choose the criterion that best describes the execution pattern.',
|
|
286
|
+
'Judge the action history itself, not the quality of the tool output.'
|
|
287
|
+
].join('\n');
|
|
288
|
+
}
|
|
289
|
+
/** Instruction block of a route decision; renders the same facts the logits backend scores. */
|
|
290
|
+
export function buildRouteInstructions(req) {
|
|
291
|
+
return [
|
|
292
|
+
buildRouteContext(req),
|
|
293
|
+
'Select the single criterion whose candidate best serves the goal.'
|
|
294
|
+
]
|
|
295
|
+
.filter((line) => line.trim().length > 0)
|
|
296
|
+
.join('\n\n');
|
|
297
|
+
}
|
|
298
|
+
/** Criterion description of one route candidate. */
|
|
299
|
+
function candidateCriterion(candidate) {
|
|
300
|
+
const description = (candidate.description ?? '').trim() || candidate.name;
|
|
301
|
+
const type = candidate.type ? `[${candidate.type}] ` : '';
|
|
302
|
+
const mark = candidate.current ? ` ${CURRENT_MODEL_MARK}` : '';
|
|
303
|
+
return `${type}${description}${mark}`;
|
|
304
|
+
}
|
|
305
|
+
/** Clamp a fragment carried into a decision prompt. */
|
|
306
|
+
function clip(text, limit) {
|
|
307
|
+
const value = typeof text === 'string' ? text : '';
|
|
308
|
+
return value.length <= limit ? value : `${value.slice(0, limit)}…`;
|
|
309
|
+
}
|
|
310
|
+
/** Words a line must share with the intent to count as relevant. */
|
|
311
|
+
function tokenize(text) {
|
|
312
|
+
return text
|
|
313
|
+
.toLowerCase()
|
|
314
|
+
.split(/[^a-z0-9_\u4e00-\u9fff]+/)
|
|
315
|
+
.filter((word) => word.length > 2);
|
|
316
|
+
}
|
|
317
|
+
const PROGRESS_ART = /\[=+[>\s]|█|^\s*\d+%\s|\[K/;
|
|
318
|
+
const NOISE_LINE = /^\s*(?:npm (?:warn|notice)|Downloading|Fetching|Progress:|added \d+ packages|audited \d+ packages)\b/i;
|
|
319
|
+
const DIAGNOSTIC_MARK = /(?:error|fail|exception|panic|traceback|assert|expected|actual|✗|✘|warn|not ok|denied|refused|cannot|undefined is not|stack)/i;
|
|
320
|
+
/** Drop progress art, then collapse repeated lines and blank runs. */
|
|
321
|
+
function condenseLines(lines) {
|
|
322
|
+
const out = [];
|
|
323
|
+
let blanks = 0;
|
|
324
|
+
let previous;
|
|
325
|
+
for (const line of lines) {
|
|
326
|
+
if (PROGRESS_ART.test(line))
|
|
327
|
+
continue;
|
|
328
|
+
const trimmed = line.trim();
|
|
329
|
+
if (trimmed.length === 0) {
|
|
330
|
+
blanks++;
|
|
331
|
+
if (blanks <= 1)
|
|
332
|
+
out.push('');
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
blanks = 0;
|
|
336
|
+
if (trimmed === previous)
|
|
337
|
+
continue;
|
|
338
|
+
previous = trimmed;
|
|
339
|
+
out.push(line.replace(/\s+$/, ''));
|
|
340
|
+
}
|
|
341
|
+
return out;
|
|
342
|
+
}
|
|
343
|
+
/** Cap a candidate to a token budget, keeping its head and its tail. */
|
|
344
|
+
function capToBudget(text, maxTokens) {
|
|
345
|
+
if (typeof maxTokens !== 'number' || !Number.isFinite(maxTokens) || maxTokens <= 0)
|
|
346
|
+
return text;
|
|
347
|
+
const budget = Math.floor(maxTokens) * 4;
|
|
348
|
+
if (text.length <= budget)
|
|
349
|
+
return text;
|
|
350
|
+
const head = Math.floor(budget * 0.7);
|
|
351
|
+
const tail = Math.max(0, budget - head - 64);
|
|
352
|
+
const dropped = text.length - head - tail;
|
|
353
|
+
const marker = `\n… [${String(dropped)} characters elided by the prune budget] …\n`;
|
|
354
|
+
return `${text.slice(0, head)}${marker}${tail > 0 ? text.slice(text.length - tail) : ''}`;
|
|
355
|
+
}
|
|
356
|
+
/** Label of one pruning, including its reduction. */
|
|
357
|
+
function pruneLabel(id, originalChars, prunedChars) {
|
|
358
|
+
const strategy = {
|
|
359
|
+
aggressive: 'keeps only diagnostic and intent-matching lines',
|
|
360
|
+
balanced: 'drops progress noise and irrelevant dependency traces',
|
|
361
|
+
conservative: 'collapses blank runs and repeated lines'
|
|
362
|
+
};
|
|
363
|
+
const reduction = originalChars > 0 ? Math.round(((originalChars - prunedChars) / originalChars) * 100) : 0;
|
|
364
|
+
return `candidate "${id}" (${strategy[id]}, ${String(reduction)}% smaller)`;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Build the deterministic prunings used below the judging threshold and as the
|
|
368
|
+
* fallback when System One cannot judge the segments.
|
|
369
|
+
*
|
|
370
|
+
* These are the legacy line filters: {@link JevDecisionBackend.prune} no longer
|
|
371
|
+
* uses them as its primary decision for large payloads — the per-segment scores
|
|
372
|
+
* are — so `PROGRESS_ART`/`NOISE_LINE` are one input among two, never the sole
|
|
373
|
+
* basis of a reduction.
|
|
374
|
+
* @param rawOutput - the bulk output to reduce.
|
|
375
|
+
* @param intent - what the caller is looking for.
|
|
376
|
+
* @param maxTokens - optional token budget applied to every candidate.
|
|
377
|
+
* @returns distinct candidates, most aggressive first; empty for empty input.
|
|
378
|
+
*/
|
|
379
|
+
export function buildPruneCandidates(rawOutput, intent, maxTokens) {
|
|
380
|
+
if (rawOutput.length === 0)
|
|
381
|
+
return [];
|
|
382
|
+
const intentWords = tokenize(intent);
|
|
383
|
+
const matchesIntent = (line) => {
|
|
384
|
+
const lower = line.toLowerCase();
|
|
385
|
+
return intentWords.some((word) => lower.includes(word));
|
|
386
|
+
};
|
|
387
|
+
const condensed = condenseLines(rawOutput.split('\n'));
|
|
388
|
+
const balanced = condensed.filter((line) => {
|
|
389
|
+
const trimmed = line.trim();
|
|
390
|
+
if (trimmed.length === 0)
|
|
391
|
+
return true;
|
|
392
|
+
if (NOISE_LINE.test(trimmed) && !matchesIntent(trimmed))
|
|
393
|
+
return false;
|
|
394
|
+
if (trimmed.includes('node_modules/') && !matchesIntent(trimmed))
|
|
395
|
+
return false;
|
|
396
|
+
return true;
|
|
397
|
+
});
|
|
398
|
+
const aggressive = balanced.filter((line) => {
|
|
399
|
+
const trimmed = line.trim();
|
|
400
|
+
if (trimmed.length === 0)
|
|
401
|
+
return true;
|
|
402
|
+
return DIAGNOSTIC_MARK.test(trimmed) || matchesIntent(trimmed);
|
|
403
|
+
});
|
|
404
|
+
const drafts = [
|
|
405
|
+
{ id: 'aggressive', text: capToBudget(aggressive.join('\n').trim(), maxTokens) },
|
|
406
|
+
{ id: 'balanced', text: capToBudget(balanced.join('\n').trim(), maxTokens) },
|
|
407
|
+
{ id: 'conservative', text: capToBudget(condensed.join('\n').trim(), maxTokens) }
|
|
408
|
+
];
|
|
409
|
+
const seen = new Set();
|
|
410
|
+
return drafts
|
|
411
|
+
.filter((draft) => {
|
|
412
|
+
if (draft.text.length === 0 || seen.has(draft.text))
|
|
413
|
+
return false;
|
|
414
|
+
seen.add(draft.text);
|
|
415
|
+
return true;
|
|
416
|
+
})
|
|
417
|
+
.map((draft) => ({
|
|
418
|
+
id: draft.id,
|
|
419
|
+
label: pruneLabel(draft.id, rawOutput.length, draft.text.length),
|
|
420
|
+
text: draft.text
|
|
421
|
+
}));
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Candidate used when the ranking is unavailable: the balanced filter, which is
|
|
425
|
+
* the safest reduction that still removes noise.
|
|
426
|
+
* @param candidates - candidates of {@link buildPruneCandidates}.
|
|
427
|
+
* @returns the local default, or `undefined` for an empty list.
|
|
428
|
+
*/
|
|
429
|
+
export function pickLocalPruneCandidate(candidates) {
|
|
430
|
+
const balanced = candidates.find((candidate) => candidate.id === 'balanced');
|
|
431
|
+
if (balanced !== undefined)
|
|
432
|
+
return balanced;
|
|
433
|
+
return candidates[Math.floor(candidates.length / 2)] ?? candidates[0];
|
|
434
|
+
}
|
|
435
|
+
/* -------------------------------------------------------------------------- */
|
|
436
|
+
/* Format-independent template compression (fallback path only) */
|
|
437
|
+
/* -------------------------------------------------------------------------- */
|
|
438
|
+
/**
|
|
439
|
+
* Lines that always survive a template compression verbatim.
|
|
440
|
+
*
|
|
441
|
+
* A degraded run must not lose the line that explains why the run failed, so
|
|
442
|
+
* these are forced anchors next to the first and last line of the output.
|
|
443
|
+
*/
|
|
444
|
+
const TEMPLATE_DIAGNOSTIC_LINE = /error|failed|FAIL|warn|exception|traceback|assert|exit code [1-9]|non-zero/i;
|
|
445
|
+
/** One line standing in for a run of near-identical lines. */
|
|
446
|
+
function templateOmittedMarker(omitted) {
|
|
447
|
+
return `… [${String(omitted)} similar lines omitted] …`;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Reduce one line to the template later lines are compared against.
|
|
451
|
+
*
|
|
452
|
+
* Variable tokens (UUIDs, timestamps, IPs, hex ids, durations, percentages,
|
|
453
|
+
* floats, plain numbers) become placeholders and runs of whitespace collapse,
|
|
454
|
+
* so "progress 001" and "progress 002" share a template while two genuinely
|
|
455
|
+
* different lines do not. The original line is what is printed, never this
|
|
456
|
+
* string.
|
|
457
|
+
* @param line - one line of the output.
|
|
458
|
+
* @returns its shape, whitespace-normalized.
|
|
459
|
+
*/
|
|
460
|
+
export function templateOfLine(line) {
|
|
461
|
+
let template = line.trim();
|
|
462
|
+
template = template.replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, '<uuid>');
|
|
463
|
+
template = template.replace(/\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:[.,]\d+)?(?:Z|[+-]\d{2}:?\d{2})?/g, '<time>');
|
|
464
|
+
template = template.replace(/\b\d{1,3}(?:\.\d{1,3}){3}\b/g, '<ip>');
|
|
465
|
+
template = template.replace(/\b0x[0-9a-f]+\b/gi, '<hex>');
|
|
466
|
+
template = template.replace(/\b[0-9a-f]{8,}\b/gi, '<hex>');
|
|
467
|
+
template = template.replace(/\b\d{1,2}(?::\d{2}){1,2}(?:[.,]\d+)?\b/g, '<time>');
|
|
468
|
+
template = template.replace(/\d+(?:\.\d+)?\s?%/g, '<pct>');
|
|
469
|
+
template = template.replace(/\b\d+(?:\.\d+)?\s?(?:ms|secs?|seconds?|mins?|minutes?|hours?|h)\b/gi, '<duration>');
|
|
470
|
+
template = template.replace(/\d+\.\d+/g, '<float>');
|
|
471
|
+
template = template.replace(/\d+/g, '<num>');
|
|
472
|
+
return template.replace(/\s+/g, ' ').trim();
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Compress a bulk output by its line templates, independent of any format.
|
|
476
|
+
*
|
|
477
|
+
* Consecutive lines whose templates match form a run: a run of four or more
|
|
478
|
+
* keeps its first two and its last line and replaces the rest with one marker.
|
|
479
|
+
* Consecutive blank lines collapse to one. The first line, the last line and
|
|
480
|
+
* every diagnostic line survive verbatim. This is the fallback used when System
|
|
481
|
+
* One cannot judge — it never replaces a real decision.
|
|
482
|
+
* @param rawOutput - the bulk output to reduce.
|
|
483
|
+
* @returns the reduced text; never longer than the input.
|
|
484
|
+
*/
|
|
485
|
+
export function compressByTemplate(rawOutput) {
|
|
486
|
+
if (rawOutput.length === 0)
|
|
487
|
+
return rawOutput;
|
|
488
|
+
const lines = rawOutput.split('\n');
|
|
489
|
+
if (lines.length <= 2)
|
|
490
|
+
return rawOutput;
|
|
491
|
+
const lastIndex = lines.length - 1;
|
|
492
|
+
const out = [];
|
|
493
|
+
let index = 0;
|
|
494
|
+
while (index <= lastIndex) {
|
|
495
|
+
const line = lines[index];
|
|
496
|
+
const blank = line.trim().length === 0;
|
|
497
|
+
if (blank) {
|
|
498
|
+
let end = index;
|
|
499
|
+
while (end + 1 <= lastIndex && lines[end + 1].trim().length === 0)
|
|
500
|
+
end++;
|
|
501
|
+
out.push(index === 0 || end === lastIndex ? line : '');
|
|
502
|
+
index = end + 1;
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
if (index === 0 || index === lastIndex || TEMPLATE_DIAGNOSTIC_LINE.test(line)) {
|
|
506
|
+
out.push(line);
|
|
507
|
+
index += 1;
|
|
508
|
+
continue;
|
|
509
|
+
}
|
|
510
|
+
const template = templateOfLine(line);
|
|
511
|
+
let end = index;
|
|
512
|
+
while (end + 1 < lastIndex &&
|
|
513
|
+
lines[end + 1].trim().length > 0 &&
|
|
514
|
+
!TEMPLATE_DIAGNOSTIC_LINE.test(lines[end + 1]) &&
|
|
515
|
+
templateOfLine(lines[end + 1]) === template) {
|
|
516
|
+
end += 1;
|
|
517
|
+
}
|
|
518
|
+
const run = end - index + 1;
|
|
519
|
+
if (run >= 4) {
|
|
520
|
+
out.push(lines[index], lines[index + 1], templateOmittedMarker(run - 3), lines[end]);
|
|
521
|
+
}
|
|
522
|
+
else {
|
|
523
|
+
for (let cursor = index; cursor <= end; cursor++)
|
|
524
|
+
out.push(lines[cursor]);
|
|
525
|
+
}
|
|
526
|
+
index = end + 1;
|
|
527
|
+
}
|
|
528
|
+
const compressed = out.join('\n');
|
|
529
|
+
return compressed.length < rawOutput.length ? compressed : rawOutput;
|
|
530
|
+
}
|
|
531
|
+
/* -------------------------------------------------------------------------- */
|
|
532
|
+
/* System One segment judging */
|
|
533
|
+
/* -------------------------------------------------------------------------- */
|
|
534
|
+
/**
|
|
535
|
+
* Character count from which {@link JevDecisionBackend.prune} judges segments
|
|
536
|
+
* with System One. Kept in sync with `interventions.pruneJudgeMinChars`.
|
|
537
|
+
*/
|
|
538
|
+
export const PRUNE_JUDGE_MIN_CHARS_DEFAULT = 10000;
|
|
539
|
+
/** A scored segment never exceeds this many characters (a single long line stays whole). */
|
|
540
|
+
const PRUNE_SEGMENT_MAX_CHARS = 1600;
|
|
541
|
+
/** Upper bound on segments judged by one request; the rest are kept deterministically. */
|
|
542
|
+
export const PRUNE_MAX_JUDGED_SEGMENTS = 40;
|
|
543
|
+
/** Prefix of the per-segment question ids of a pruning judgment. */
|
|
544
|
+
export const PRUNE_SEGMENT_QUESTION_PREFIX = 'prune-seg-';
|
|
545
|
+
/** Criterion pair of one segment question; index 1 (keep) is the score we read. */
|
|
546
|
+
const PRUNE_KEEP_CRITERIA = [
|
|
547
|
+
'omit: repetitive, irrelevant, or safe to drop',
|
|
548
|
+
'keep: unique, relevant, or diagnostic content'
|
|
549
|
+
];
|
|
550
|
+
/**
|
|
551
|
+
* Share of the original characters each candidate may spend on scored
|
|
552
|
+
* segments. Forced lines (anchors, diagnostics, intent matches) never compete
|
|
553
|
+
* with this budget.
|
|
554
|
+
*/
|
|
555
|
+
const PRUNE_SCORED_BUDGET_RATIO = {
|
|
556
|
+
aggressive: 0.25,
|
|
557
|
+
balanced: 0.5,
|
|
558
|
+
conservative: 0.7
|
|
559
|
+
};
|
|
560
|
+
/**
|
|
561
|
+
* Lines that must survive verbatim whatever the segment score says: diagnostic
|
|
562
|
+
* evidence, plus the first and last content lines of the payload. Matched
|
|
563
|
+
* case-insensitively.
|
|
564
|
+
*/
|
|
565
|
+
const PRUNE_FORCED_LINE = /error|failed|FAIL|warn|exception|traceback|assert|exit code [1-9]|non-zero/i;
|
|
566
|
+
/**
|
|
567
|
+
* Resolve the segment-judging character threshold from an explicit value, then
|
|
568
|
+
* the environment, then the documented default.
|
|
569
|
+
* @param value - explicit backend configuration, when supplied.
|
|
570
|
+
* @returns a non-negative finite character count.
|
|
571
|
+
*/
|
|
572
|
+
export function resolvePruneJudgeMinChars(value) {
|
|
573
|
+
const raw = value ?? process.env.SYSTEM_ONE_PRUNE_JUDGE_MIN_CHARS;
|
|
574
|
+
const numeric = typeof raw === 'number' ? raw : typeof raw === 'string' && raw.trim().length > 0 ? Number(raw) : NaN;
|
|
575
|
+
if (Number.isFinite(numeric) && numeric >= 0)
|
|
576
|
+
return Math.floor(numeric);
|
|
577
|
+
return PRUNE_JUDGE_MIN_CHARS_DEFAULT;
|
|
578
|
+
}
|
|
579
|
+
/** Whether one line carries diagnostic evidence or matches the intent. */
|
|
580
|
+
function isForcedPruneLine(line, intentWords) {
|
|
581
|
+
if (PRUNE_FORCED_LINE.test(line))
|
|
582
|
+
return true;
|
|
583
|
+
const lower = line.toLowerCase();
|
|
584
|
+
return intentWords.some((word) => lower.includes(word));
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Cut an output into the segments System One judges.
|
|
588
|
+
*
|
|
589
|
+
* Paragraphs are separated by blank lines; a paragraph longer than
|
|
590
|
+
* {@link PRUNE_SEGMENT_MAX_CHARS} is split on line windows, so the model never
|
|
591
|
+
* sees one oversized block and the local replay can reassemble the exact lines.
|
|
592
|
+
* Line order and line numbers are preserved: every segment is a half-open view
|
|
593
|
+
* of the original line array.
|
|
594
|
+
* @param rawOutput - the bulk output.
|
|
595
|
+
* @param intent - what the caller is looking for.
|
|
596
|
+
* @returns segments in original order, with their forced line indexes.
|
|
597
|
+
*/
|
|
598
|
+
export function segmentPruneOutput(rawOutput, intent) {
|
|
599
|
+
const lines = rawOutput.split('\n');
|
|
600
|
+
const intentWords = tokenize(intent);
|
|
601
|
+
const segments = [];
|
|
602
|
+
let firstContent = -1;
|
|
603
|
+
let lastContent = -1;
|
|
604
|
+
for (let index = 0; index < lines.length; index++) {
|
|
605
|
+
if (lines[index].trim().length === 0)
|
|
606
|
+
continue;
|
|
607
|
+
if (firstContent < 0)
|
|
608
|
+
firstContent = index;
|
|
609
|
+
lastContent = index;
|
|
610
|
+
}
|
|
611
|
+
const push = (startLine, endLine) => {
|
|
612
|
+
const forced = [];
|
|
613
|
+
for (let line = startLine; line <= endLine; line++) {
|
|
614
|
+
const text = lines[line];
|
|
615
|
+
if (line === firstContent || line === lastContent || isForcedPruneLine(text, intentWords)) {
|
|
616
|
+
forced.push(line);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
const text = lines.slice(startLine, endLine + 1).join('\n');
|
|
620
|
+
segments.push({ index: segments.length, startLine, endLine, text, chars: text.length, forcedLines: forced });
|
|
621
|
+
};
|
|
622
|
+
let cursor = 0;
|
|
623
|
+
while (cursor < lines.length) {
|
|
624
|
+
while (cursor < lines.length && lines[cursor].trim().length === 0)
|
|
625
|
+
cursor++;
|
|
626
|
+
if (cursor >= lines.length)
|
|
627
|
+
break;
|
|
628
|
+
const blockStart = cursor;
|
|
629
|
+
while (cursor < lines.length && lines[cursor].trim().length > 0)
|
|
630
|
+
cursor++;
|
|
631
|
+
const blockEnd = cursor - 1;
|
|
632
|
+
let windowStart = blockStart;
|
|
633
|
+
let windowChars = lines[blockStart].length;
|
|
634
|
+
for (let line = blockStart + 1; line <= blockEnd; line++) {
|
|
635
|
+
const length = lines[line].length + 1;
|
|
636
|
+
if (windowChars + length > PRUNE_SEGMENT_MAX_CHARS) {
|
|
637
|
+
push(windowStart, line - 1);
|
|
638
|
+
windowStart = line;
|
|
639
|
+
windowChars = lines[line].length;
|
|
640
|
+
}
|
|
641
|
+
else {
|
|
642
|
+
windowChars += length;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
push(windowStart, blockEnd);
|
|
646
|
+
}
|
|
647
|
+
return segments;
|
|
648
|
+
}
|
|
649
|
+
/** Instruction block of one segment question. */
|
|
650
|
+
function buildSegmentInstructions(intent, segment, total) {
|
|
651
|
+
const trimmed = intent.trim() || 'extract diagnostics, relevant results and errors';
|
|
652
|
+
return [
|
|
653
|
+
'Decide whether one segment of a tool output must be kept to serve the INTENT, or may be omitted as noise.',
|
|
654
|
+
`INTENT: ${trimmed}`,
|
|
655
|
+
`SEGMENT: ${String(segment.index + 1)} of ${String(total)} (lines ${String(segment.startLine + 1)}-${String(segment.endLine + 1)}).`,
|
|
656
|
+
'Score 0 when the segment is repetitive or irrelevant; score 1 when it carries information the INTENT needs.'
|
|
657
|
+
].join('\n');
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Read one segment's keep score (0 = omit, 1 = keep) out of its `score` answer.
|
|
661
|
+
* The distribution over the two criteria is authoritative; the weighted `score`
|
|
662
|
+
* is the fallback, and nothing usable reads as `undefined`.
|
|
663
|
+
* @param answer - the gateway's answer for this segment question.
|
|
664
|
+
* @returns the keep score in `[0, 1]`, or `undefined` when unusable.
|
|
665
|
+
*/
|
|
666
|
+
export function readSegmentKeepScore(answer) {
|
|
667
|
+
if (answer === undefined || answer === null)
|
|
668
|
+
return undefined;
|
|
669
|
+
const distribution = readDistribution(answer.probabilities, ['0', '1']);
|
|
670
|
+
if (distribution !== undefined)
|
|
671
|
+
return round4(toProbability(distribution.get('1')));
|
|
672
|
+
const raw = typeof answer.score === 'number' ? answer.score : Number(answer.score);
|
|
673
|
+
if (!Number.isFinite(raw))
|
|
674
|
+
return undefined;
|
|
675
|
+
return round4(Math.min(1, Math.max(0, raw)));
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Pick the segments a budget keeps from their scores.
|
|
679
|
+
*
|
|
680
|
+
* Segments without a usable score are an unjudged safety margin and are always
|
|
681
|
+
* kept; scored segments are taken from the highest score down until the
|
|
682
|
+
* character budget is spent. Ties follow the original order.
|
|
683
|
+
* @param segments - all segments of the output.
|
|
684
|
+
* @param scores - keep score per segment index, absent for unjudged segments.
|
|
685
|
+
* @param originalChars - character count of the unpruned output.
|
|
686
|
+
* @param ratio - share of the original characters scored segments may use.
|
|
687
|
+
* @returns indexes of the segments this candidate keeps in full.
|
|
688
|
+
*/
|
|
689
|
+
export function selectPruneSegments(segments, scores, originalChars, ratio) {
|
|
690
|
+
const selected = new Set();
|
|
691
|
+
let used = 0;
|
|
692
|
+
for (const segment of segments) {
|
|
693
|
+
if (scores.has(segment.index))
|
|
694
|
+
continue;
|
|
695
|
+
selected.add(segment.index);
|
|
696
|
+
used += segment.chars;
|
|
697
|
+
}
|
|
698
|
+
const budget = Math.floor(originalChars * ratio);
|
|
699
|
+
const ranked = segments
|
|
700
|
+
.filter((segment) => scores.has(segment.index))
|
|
701
|
+
.sort((left, right) => {
|
|
702
|
+
const difference = (scores.get(right.index) ?? 0) - (scores.get(left.index) ?? 0);
|
|
703
|
+
return difference !== 0 ? difference : left.index - right.index;
|
|
704
|
+
});
|
|
705
|
+
for (const segment of ranked) {
|
|
706
|
+
// Stop at the budget instead of skipping ahead: a smaller low-score segment
|
|
707
|
+
// must never slip into the space a higher-score one did not fit.
|
|
708
|
+
if (used + segment.chars > budget)
|
|
709
|
+
break;
|
|
710
|
+
selected.add(segment.index);
|
|
711
|
+
used += segment.chars;
|
|
712
|
+
}
|
|
713
|
+
return selected;
|
|
714
|
+
}
|
|
715
|
+
/** Build the marker of one run of omitted segments. */
|
|
716
|
+
function omittedSegmentsMarker(count) {
|
|
717
|
+
return `… [${String(count)} segments omitted] …`;
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Reassemble one candidate in the original line order.
|
|
721
|
+
*
|
|
722
|
+
* A selected segment is kept whole; an unselected one keeps only its forced
|
|
723
|
+
* lines (first/last anchors, diagnostics, intent matches). Consecutive fully
|
|
724
|
+
* omitted segments collapse into one marker that names how many were dropped.
|
|
725
|
+
* Blank runs between kept lines collapse to a single blank line.
|
|
726
|
+
* @param rawOutput - the original output, for its lines.
|
|
727
|
+
* @param segments - segments in original order.
|
|
728
|
+
* @param selected - segment indexes kept whole.
|
|
729
|
+
* @returns the reduced text.
|
|
730
|
+
*/
|
|
731
|
+
export function assemblePruneCandidate(rawOutput, segments, selected) {
|
|
732
|
+
const lines = rawOutput.split('\n');
|
|
733
|
+
const out = [];
|
|
734
|
+
let omitted = 0;
|
|
735
|
+
let lastEmitted = -1;
|
|
736
|
+
const emitLine = (lineIndex) => {
|
|
737
|
+
if (lastEmitted >= 0 && lineIndex > lastEmitted + 1)
|
|
738
|
+
out.push('');
|
|
739
|
+
out.push(lines[lineIndex]);
|
|
740
|
+
lastEmitted = lineIndex;
|
|
741
|
+
};
|
|
742
|
+
const flushOmitted = () => {
|
|
743
|
+
if (omitted === 0)
|
|
744
|
+
return;
|
|
745
|
+
out.push(omittedSegmentsMarker(omitted));
|
|
746
|
+
omitted = 0;
|
|
747
|
+
};
|
|
748
|
+
for (const segment of segments) {
|
|
749
|
+
// A selected segment is kept whole; an unselected one keeps forced lines only.
|
|
750
|
+
const keepWhole = selected.has(segment.index);
|
|
751
|
+
const lineIndexes = [];
|
|
752
|
+
if (keepWhole) {
|
|
753
|
+
for (let line = segment.startLine; line <= segment.endLine; line++)
|
|
754
|
+
lineIndexes.push(line);
|
|
755
|
+
}
|
|
756
|
+
else {
|
|
757
|
+
lineIndexes.push(...segment.forcedLines);
|
|
758
|
+
}
|
|
759
|
+
if (lineIndexes.length === 0) {
|
|
760
|
+
omitted++;
|
|
761
|
+
continue;
|
|
762
|
+
}
|
|
763
|
+
flushOmitted();
|
|
764
|
+
for (const lineIndex of lineIndexes)
|
|
765
|
+
emitLine(lineIndex);
|
|
766
|
+
}
|
|
767
|
+
flushOmitted();
|
|
768
|
+
return out.join('\n').trim();
|
|
769
|
+
}
|
|
770
|
+
/** Label of one score-driven candidate, including its reduction. */
|
|
771
|
+
function scoredPruneLabel(id, originalChars, prunedChars) {
|
|
772
|
+
const strategy = {
|
|
773
|
+
aggressive: 'keeps the segments System One judged essential within a quarter of the original',
|
|
774
|
+
balanced: 'keeps the segments System One judged essential within half of the original',
|
|
775
|
+
conservative: 'keeps the segments System One judged essential within most of the original'
|
|
776
|
+
};
|
|
777
|
+
const reduction = originalChars > 0 ? Math.round(((originalChars - prunedChars) / originalChars) * 100) : 0;
|
|
778
|
+
return `candidate "${id}" (${strategy[id]}, ${String(reduction)}% smaller)`;
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Build the three System One–scored prunings from one segment judgment.
|
|
782
|
+
*
|
|
783
|
+
* The segment keep/drop decision is the model's score; the three ids only
|
|
784
|
+
* differ in their character budget, so the ranking the engine used to make is
|
|
785
|
+
* now made per segment.
|
|
786
|
+
* @param rawOutput - the original output.
|
|
787
|
+
* @param segments - segments of that output in original order.
|
|
788
|
+
* @param scores - keep score per segment index.
|
|
789
|
+
* @param maxTokens - optional hard token ceiling applied to every candidate.
|
|
790
|
+
* @returns candidates ordered from aggressive to conservative.
|
|
791
|
+
*/
|
|
792
|
+
export function buildScoredPruneCandidates(rawOutput, segments, scores, maxTokens) {
|
|
793
|
+
const ids = ['aggressive', 'balanced', 'conservative'];
|
|
794
|
+
return ids.map((id) => {
|
|
795
|
+
const selected = selectPruneSegments(segments, scores, rawOutput.length, PRUNE_SCORED_BUDGET_RATIO[id]);
|
|
796
|
+
const text = capToBudget(assemblePruneCandidate(rawOutput, segments, selected), maxTokens);
|
|
797
|
+
return { id, label: scoredPruneLabel(id, rawOutput.length, text.length), text, keptSegments: selected.size };
|
|
798
|
+
});
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* Pick the first scored candidate strictly smaller than the payload.
|
|
802
|
+
*
|
|
803
|
+
* The balanced budget is tried first because it is the documented default. A
|
|
804
|
+
* payload whose forced lines (anchors, diagnostics, intent matches) already
|
|
805
|
+
* reproduce it verbatim makes even the balanced candidate as large as the
|
|
806
|
+
* original; the tighter and then the wider budget are tried before giving up,
|
|
807
|
+
* so a smaller aggressive candidate is never thrown away by the balanced one.
|
|
808
|
+
* A candidate that does not reduce the payload is never returned as the text:
|
|
809
|
+
* the caller treats that outcome as a judgment that kept everything, unless the
|
|
810
|
+
* judgment produced no usable score at all.
|
|
811
|
+
* @param rawOutput - the original output the candidates were built from.
|
|
812
|
+
* @param candidates - the scored candidates, in any order.
|
|
813
|
+
* @returns the reducing text plus the candidate that produced it, or the raw
|
|
814
|
+
* output plus the reason no candidate was usable.
|
|
815
|
+
*/
|
|
816
|
+
export function pickScoredPruneCandidate(rawOutput, candidates) {
|
|
817
|
+
const order = ['balanced', 'aggressive', 'conservative'];
|
|
818
|
+
for (const id of order) {
|
|
819
|
+
const candidate = candidates.find((entry) => entry.id === id);
|
|
820
|
+
if (candidate !== undefined && candidate.text.length < rawOutput.length)
|
|
821
|
+
return { text: candidate.text, candidate };
|
|
822
|
+
}
|
|
823
|
+
return { text: rawOutput, reason: 'no scored candidate is smaller than the payload' };
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Deterministic stagnation detector over a recent action window.
|
|
827
|
+
*
|
|
828
|
+
* It is the offline half of {@link JevDecisionBackend.checkStagnation}: the
|
|
829
|
+
* gateway classifies the pattern, and this reads the same history with explicit
|
|
830
|
+
* rules, so a dead gateway degrades the verdict instead of losing it.
|
|
831
|
+
* @param recent - the recent action window, oldest first.
|
|
832
|
+
* @returns the locally detected pattern.
|
|
833
|
+
*/
|
|
834
|
+
export function detectStagnationLocally(recent) {
|
|
835
|
+
const stuck = (patternType, confidence) => ({
|
|
836
|
+
isStuck: true,
|
|
837
|
+
patternType,
|
|
838
|
+
confidence,
|
|
839
|
+
diagnosis: `${STAGNATION_DIAGNOSIS[patternType]} [local heuristic]`,
|
|
840
|
+
recommendedIntervention: STAGNATION_INTERVENTION[patternType]
|
|
841
|
+
});
|
|
842
|
+
const signatures = recent.map((step) => `${step.tool}:${(step.input ?? '').trim()}`);
|
|
843
|
+
let identical = 1;
|
|
844
|
+
for (let index = signatures.length - 1; index > 0; index--) {
|
|
845
|
+
if (signatures[index] === signatures[index - 1])
|
|
846
|
+
identical++;
|
|
847
|
+
else
|
|
848
|
+
break;
|
|
849
|
+
}
|
|
850
|
+
if (identical >= 3)
|
|
851
|
+
return stuck('repetition', 0.95);
|
|
852
|
+
const failing = recent.filter((step) => Boolean(step.error) || /(?:error|fail|panic|not ok)/i.test(step.output ?? ''));
|
|
853
|
+
if (failing.length >= 3 && recent.length >= 4)
|
|
854
|
+
return stuck('failing_retry', 0.85);
|
|
855
|
+
if (recent.length >= 4) {
|
|
856
|
+
const last = recent.slice(-4);
|
|
857
|
+
const alternates = last[0]?.tool === last[2]?.tool &&
|
|
858
|
+
last[1]?.tool === last[3]?.tool &&
|
|
859
|
+
last[0]?.input === last[2]?.input &&
|
|
860
|
+
last[1]?.input === last[3]?.input;
|
|
861
|
+
if (alternates)
|
|
862
|
+
return stuck('oscillation', 0.9);
|
|
863
|
+
}
|
|
864
|
+
return {
|
|
865
|
+
isStuck: false,
|
|
866
|
+
patternType: 'none',
|
|
867
|
+
confidence: 0.6,
|
|
868
|
+
diagnosis: `${STAGNATION_DIAGNOSIS.none} [local heuristic]`
|
|
869
|
+
};
|
|
870
|
+
}
|
|
871
|
+
/* -------------------------------------------------------------------------- */
|
|
872
|
+
/* The adapter */
|
|
873
|
+
/* -------------------------------------------------------------------------- */
|
|
874
|
+
/**
|
|
875
|
+
* Jev System One Provider Adapter
|
|
876
|
+
*
|
|
877
|
+
* Speaks only the current gateway contract: one `POST /v1/systemone` carrying
|
|
878
|
+
* `{ model, state, questions }`, where every question is a `choice` between
|
|
879
|
+
* criteria or a `score` over a criteria list. The gateway returns discrete
|
|
880
|
+
* distributions, never free text, so the four composite capabilities
|
|
881
|
+
* (`gate`, `prune`, `checkStagnation`, `route`) are expressed as criteria
|
|
882
|
+
* questions and backed by deterministic local fallbacks.
|
|
883
|
+
*/
|
|
884
|
+
export class JevDecisionBackend {
|
|
885
|
+
name = 'system-one-jev';
|
|
886
|
+
model;
|
|
887
|
+
/** Absolute URL of the single endpoint; no call appends a sub-path to it. */
|
|
888
|
+
endpoint;
|
|
889
|
+
apiKey;
|
|
890
|
+
timeoutMs;
|
|
891
|
+
/** Character count from which pruning judges segments through System One. */
|
|
892
|
+
pruneJudgeMinChars;
|
|
893
|
+
/**
|
|
894
|
+
* Health of the gateway conversation of this backend.
|
|
895
|
+
*
|
|
896
|
+
* Every request updates it; a success clears the failure fields and resets the
|
|
897
|
+
* run. It lives in memory only, so a restarted Host starts healthy.
|
|
898
|
+
*/
|
|
899
|
+
healthState = { available: true, consecutiveFailures: 0 };
|
|
900
|
+
constructor(config) {
|
|
901
|
+
this.endpoint = normalizeSystemOneEndpoint(config?.baseUrl ||
|
|
902
|
+
process.env.SYSTEM_ONE_BASE_URL ||
|
|
903
|
+
process.env.JEV_BASE_URL ||
|
|
904
|
+
process.env.JEV_API_BASE ||
|
|
905
|
+
DEFAULT_SYSTEMONE_ENDPOINT);
|
|
906
|
+
this.apiKey =
|
|
907
|
+
config?.apiKey ||
|
|
908
|
+
process.env.SYSTEM_ONE_API_KEY ||
|
|
909
|
+
process.env.JEV_API_KEY ||
|
|
910
|
+
JEV_DEFAULT_API_KEY;
|
|
911
|
+
this.model =
|
|
912
|
+
config?.model ||
|
|
913
|
+
process.env.SYSTEM_ONE_MODEL ||
|
|
914
|
+
process.env.JEV_MODEL ||
|
|
915
|
+
JEV_DEFAULT_MODEL;
|
|
916
|
+
this.timeoutMs = config?.timeoutMs || JEV_DEFAULT_TIMEOUT_MS;
|
|
917
|
+
this.pruneJudgeMinChars = resolvePruneJudgeMinChars(config?.pruneJudgeMinChars);
|
|
918
|
+
}
|
|
919
|
+
/**
|
|
920
|
+
* Health of the outbound gateway conversation.
|
|
921
|
+
* @returns a copy of the counters, safe to hand to a panel snapshot.
|
|
922
|
+
*/
|
|
923
|
+
health() {
|
|
924
|
+
return { ...this.healthState };
|
|
925
|
+
}
|
|
926
|
+
/** Record one successful request: the backend is available again. */
|
|
927
|
+
noteSuccess() {
|
|
928
|
+
this.healthState = { available: true, consecutiveFailures: 0 };
|
|
929
|
+
}
|
|
930
|
+
/** Record one failed request and its reason. */
|
|
931
|
+
noteFailure(reason) {
|
|
932
|
+
this.healthState = {
|
|
933
|
+
available: false,
|
|
934
|
+
lastFailureReason: reason,
|
|
935
|
+
lastFailureAt: new Date().toISOString(),
|
|
936
|
+
consecutiveFailures: this.healthState.consecutiveFailures + 1
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
/**
|
|
940
|
+
* Send one request to the single endpoint.
|
|
941
|
+
* @param questions - questions keyed by id; answers come back under the same ids.
|
|
942
|
+
* @param state - machine-readable situation the questions reason about.
|
|
943
|
+
* @returns the gateway's answer envelope.
|
|
944
|
+
*/
|
|
945
|
+
async postSystemOne(questions, state) {
|
|
946
|
+
const body = { model: this.model, state, questions };
|
|
947
|
+
const controller = new AbortController();
|
|
948
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
949
|
+
try {
|
|
950
|
+
const resp = await fetch(this.endpoint, {
|
|
951
|
+
method: 'POST',
|
|
952
|
+
headers: {
|
|
953
|
+
'Content-Type': 'application/json',
|
|
954
|
+
...(this.apiKey.length > 0 ? { Authorization: `Bearer ${this.apiKey}` } : {})
|
|
955
|
+
},
|
|
956
|
+
body: JSON.stringify(body),
|
|
957
|
+
signal: controller.signal
|
|
958
|
+
});
|
|
959
|
+
if (!resp.ok) {
|
|
960
|
+
const text = await resp.text();
|
|
961
|
+
throw new Error(`Jev API error (${resp.status} ${resp.statusText}): ${text.slice(0, 500)}`);
|
|
962
|
+
}
|
|
963
|
+
const data = (await resp.json());
|
|
964
|
+
if (!data || typeof data !== 'object' || typeof data.answers !== 'object' || data.answers === null) {
|
|
965
|
+
throw new Error('Jev API response carries no answers object');
|
|
966
|
+
}
|
|
967
|
+
this.noteSuccess();
|
|
968
|
+
return data;
|
|
969
|
+
}
|
|
970
|
+
catch (err) {
|
|
971
|
+
this.noteFailure(errorMessage(err));
|
|
972
|
+
throw err;
|
|
973
|
+
}
|
|
974
|
+
finally {
|
|
975
|
+
clearTimeout(timer);
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Classify one question into a probability distribution over the options.
|
|
980
|
+
*
|
|
981
|
+
* The options become the `criteria` dictionary (key = option label, value =
|
|
982
|
+
* the description the model reads) and `answers[id].probabilities` — keyed by
|
|
983
|
+
* criterion — becomes the returned option distribution. An answer without a
|
|
984
|
+
* usable distribution fails loudly: the caller asked for a decision, and a
|
|
985
|
+
* fabricated one would be worse than an error.
|
|
986
|
+
* @param req - question, options, optional context.
|
|
987
|
+
* @returns options sorted by probability, plus the winning option.
|
|
988
|
+
*/
|
|
989
|
+
async choice(req) {
|
|
990
|
+
const start = performance.now();
|
|
991
|
+
try {
|
|
992
|
+
const labels = Array.isArray(req.options) ? req.options : [];
|
|
993
|
+
if (labels.length === 0)
|
|
994
|
+
throw new Error('at least one option is required');
|
|
995
|
+
const keys = uniqueCriteriaKeys(labels);
|
|
996
|
+
const criteria = {};
|
|
997
|
+
keys.forEach((key, index) => {
|
|
998
|
+
criteria[key] = typeof labels[index] === 'string' ? labels[index] : key;
|
|
999
|
+
});
|
|
1000
|
+
const context = (req.context ?? '').trim();
|
|
1001
|
+
const data = await this.postSystemOne({
|
|
1002
|
+
[CHOICE_QUESTION_ID]: {
|
|
1003
|
+
type: 'choice',
|
|
1004
|
+
instructions: buildChoiceInstructions(req.question),
|
|
1005
|
+
criteria
|
|
1006
|
+
}
|
|
1007
|
+
}, context.length > 0 ? { context } : {});
|
|
1008
|
+
const answer = data.answers?.[CHOICE_QUESTION_ID];
|
|
1009
|
+
const scored = toChoiceOptions(labels, keys, readDistribution(answer?.probabilities, keys), answer);
|
|
1010
|
+
if (scored === undefined) {
|
|
1011
|
+
throw new Error('the gateway returned neither a probability distribution nor a named criterion');
|
|
1012
|
+
}
|
|
1013
|
+
const options = scored.options;
|
|
1014
|
+
return {
|
|
1015
|
+
options,
|
|
1016
|
+
bestOption: options[0]?.label ?? '',
|
|
1017
|
+
confidence: options[0]?.probability ?? 0,
|
|
1018
|
+
uncalibrated: scored.uncalibrated,
|
|
1019
|
+
latencyMs: elapsed(start)
|
|
1020
|
+
};
|
|
1021
|
+
}
|
|
1022
|
+
catch (err) {
|
|
1023
|
+
throw new Error(`[JevBackend.choice] Failed: ${errorMessage(err)}`);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Verify assertions against evidence, fail-closed.
|
|
1028
|
+
*
|
|
1029
|
+
* Every assertion becomes one `choice` question in the same request, and the
|
|
1030
|
+
* answer's distribution over `verified` / `contradicted` / `not_addressed`
|
|
1031
|
+
* becomes the verdict. Anything unusable — a failed call, a missing answer, a
|
|
1032
|
+
* distribution below the strictness floor — escalates that assertion; the
|
|
1033
|
+
* method never returns a pass it could not observe.
|
|
1034
|
+
* @param req - assertions, evidence, strictness.
|
|
1035
|
+
* @returns one result per assertion plus the aggregate verdict.
|
|
1036
|
+
*/
|
|
1037
|
+
async gate(req) {
|
|
1038
|
+
const start = performance.now();
|
|
1039
|
+
const assertions = Array.isArray(req.assertions) ? req.assertions : [];
|
|
1040
|
+
const strictness = req.strictness ?? 'normal';
|
|
1041
|
+
try {
|
|
1042
|
+
if (assertions.length === 0 || (req.evidence ?? '').trim().length === 0) {
|
|
1043
|
+
return this.failClosedGate(assertions, 'Evidence is empty or no assertions were supplied. Fail-closed.');
|
|
1044
|
+
}
|
|
1045
|
+
const ids = assertions.map((_, index) => `${GATE_QUESTION_PREFIX}${String(index)}`);
|
|
1046
|
+
const questions = {};
|
|
1047
|
+
assertions.forEach((assertion, index) => {
|
|
1048
|
+
questions[ids[index]] = {
|
|
1049
|
+
type: 'choice',
|
|
1050
|
+
instructions: buildGateInstructions(assertion, strictness),
|
|
1051
|
+
criteria: { ...GATE_VERDICT_CRITERIA }
|
|
1052
|
+
};
|
|
1053
|
+
});
|
|
1054
|
+
const context = (req.context ?? '').trim();
|
|
1055
|
+
const data = await this.postSystemOne(questions, {
|
|
1056
|
+
evidence: req.evidence,
|
|
1057
|
+
strictness,
|
|
1058
|
+
...(context.length > 0 ? { context } : {})
|
|
1059
|
+
});
|
|
1060
|
+
const results = assertions.map((assertion, index) => this.gateItem(assertion, data.answers?.[ids[index]], strictness));
|
|
1061
|
+
let passed = true;
|
|
1062
|
+
let needsEscalation = false;
|
|
1063
|
+
for (const item of results) {
|
|
1064
|
+
if (item.outcome === 'verified')
|
|
1065
|
+
continue;
|
|
1066
|
+
passed = false;
|
|
1067
|
+
if (item.outcome === 'escalate')
|
|
1068
|
+
needsEscalation = true;
|
|
1069
|
+
else if (item.outcome === 'not_addressed' && strictness !== 'lenient')
|
|
1070
|
+
needsEscalation = true;
|
|
1071
|
+
}
|
|
1072
|
+
return {
|
|
1073
|
+
passed: passed && !needsEscalation,
|
|
1074
|
+
results,
|
|
1075
|
+
needsEscalation,
|
|
1076
|
+
latencyMs: elapsed(start)
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
catch (err) {
|
|
1080
|
+
return this.failClosedGate(assertions, `Jev API call failed: ${errorMessage(err)}. Fail-closed escalation triggered.`);
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
/** One assertion's outcome from its answer; unusable answers escalate. */
|
|
1084
|
+
gateItem(assertion, answer, strictness) {
|
|
1085
|
+
const statement = assertion?.statement ?? '';
|
|
1086
|
+
const choiceAnswer = answer;
|
|
1087
|
+
const distribution = readDistribution(choiceAnswer?.probabilities, GATE_VERDICTS);
|
|
1088
|
+
if (distribution === undefined) {
|
|
1089
|
+
return {
|
|
1090
|
+
statement,
|
|
1091
|
+
outcome: 'escalate',
|
|
1092
|
+
confidence: 0,
|
|
1093
|
+
explanation: 'The gateway returned no usable verdict distribution for this assertion. Fail-closed escalation.'
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
const verdict = argmaxKey(distribution, GATE_VERDICTS);
|
|
1097
|
+
const confidence = round4(distribution.get(verdict) ?? 0);
|
|
1098
|
+
const floor = GATE_CONFIDENCE_FLOOR[strictness];
|
|
1099
|
+
if (confidence < floor) {
|
|
1100
|
+
return {
|
|
1101
|
+
statement,
|
|
1102
|
+
outcome: 'escalate',
|
|
1103
|
+
confidence,
|
|
1104
|
+
explanation: `System One named "${verdict}" at ${(confidence * 100).toFixed(1)}%, below the ${strictness} floor of ${(floor * 100).toFixed(0)}%. Fail-closed escalation.`
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
return {
|
|
1108
|
+
statement,
|
|
1109
|
+
outcome: verdict,
|
|
1110
|
+
confidence,
|
|
1111
|
+
explanation: `System One named "${verdict}" at ${(confidence * 100).toFixed(1)}% (evidence supplied as state).`
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
/** Every assertion escalated for one reason — the fail-closed shape. */
|
|
1115
|
+
failClosedGate(assertions, reason) {
|
|
1116
|
+
const started = performance.now();
|
|
1117
|
+
return {
|
|
1118
|
+
passed: false,
|
|
1119
|
+
needsEscalation: true,
|
|
1120
|
+
results: assertions.map((assertion) => ({
|
|
1121
|
+
statement: assertion?.statement ?? '',
|
|
1122
|
+
outcome: 'escalate',
|
|
1123
|
+
confidence: 0,
|
|
1124
|
+
explanation: reason
|
|
1125
|
+
})),
|
|
1126
|
+
latencyMs: elapsed(started)
|
|
1127
|
+
};
|
|
1128
|
+
}
|
|
1129
|
+
/**
|
|
1130
|
+
* Reduce a bulk output to the segments the intent needs, fail-open.
|
|
1131
|
+
*
|
|
1132
|
+
* Two paths share one contract. A payload below `pruneJudgeMinChars` is
|
|
1133
|
+
* reduced by the deterministic local candidates and sends no request at all.
|
|
1134
|
+
* A payload at/above it is cut into segments, and one `score` question per
|
|
1135
|
+
* segment lets System One decide what to keep; the three budgets only decide
|
|
1136
|
+
* how much of the top-scored material fits. Forced lines (first/last anchors,
|
|
1137
|
+
* diagnostics, intent matches) survive either way. Any failure — call,
|
|
1138
|
+
* transport, unusable answer — falls back to the local candidates, so pruning
|
|
1139
|
+
* never breaks the harness and never returns the raw blob just because the
|
|
1140
|
+
* network blinked.
|
|
1141
|
+
* @param req - raw output, intent, budget, shadow flag.
|
|
1142
|
+
* @returns the pruned text plus its reduction metrics and the path taken.
|
|
1143
|
+
*/
|
|
1144
|
+
async prune(req) {
|
|
1145
|
+
const start = performance.now();
|
|
1146
|
+
const raw = typeof req.rawOutput === 'string' ? req.rawOutput : '';
|
|
1147
|
+
const intent = req.intent ?? '';
|
|
1148
|
+
if (raw.length === 0)
|
|
1149
|
+
return this.pruneResponse(raw, raw, req.shadowMode, start, { judgePath: 'local' });
|
|
1150
|
+
// The caller's resolved config is authoritative; the backend's own value
|
|
1151
|
+
// (explicit config, then environment, then default) is the fallback.
|
|
1152
|
+
const judgeMinChars = req.pruneJudgeMinChars ?? this.pruneJudgeMinChars;
|
|
1153
|
+
if (raw.length < judgeMinChars) {
|
|
1154
|
+
return this.pruneResponse(raw, this.localPruneText(raw, intent, req.maxTokens), req.shadowMode, start, {
|
|
1155
|
+
judgePath: 'local'
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
return this.pruneWithSegments(req, raw, intent, start);
|
|
1159
|
+
}
|
|
1160
|
+
/** The deterministic candidate text, or the raw output when it does not shrink. */
|
|
1161
|
+
localPruneText(raw, intent, maxTokens) {
|
|
1162
|
+
const candidates = buildPruneCandidates(raw, intent, maxTokens);
|
|
1163
|
+
const local = pickLocalPruneCandidate(candidates);
|
|
1164
|
+
if (local === undefined)
|
|
1165
|
+
return raw;
|
|
1166
|
+
return local.text.length < raw.length ? local.text : raw;
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Reduce the payload when System One cannot judge it.
|
|
1170
|
+
*
|
|
1171
|
+
* Template compression is format-independent, so it still shrinks output the
|
|
1172
|
+
* deterministic line filters cannot (progress art they would drop entirely, a
|
|
1173
|
+
* table whose rows differ only by a number). It is only ever used here, on the
|
|
1174
|
+
* degraded path, and the smaller of it and the local candidates wins.
|
|
1175
|
+
* @param raw - the original output.
|
|
1176
|
+
* @param intent - what the caller is looking for.
|
|
1177
|
+
* @param maxTokens - optional token budget.
|
|
1178
|
+
* @returns the reduced text and which strategy produced it.
|
|
1179
|
+
*/
|
|
1180
|
+
fallbackPruneText(raw, intent, maxTokens) {
|
|
1181
|
+
const local = this.localPruneText(raw, intent, maxTokens);
|
|
1182
|
+
const template = compressByTemplate(raw);
|
|
1183
|
+
if (template.length < local.length)
|
|
1184
|
+
return { text: template, compression: 'template' };
|
|
1185
|
+
return { text: local, compression: 'local' };
|
|
1186
|
+
}
|
|
1187
|
+
/** The per-segment System One path, with the local candidates as its fallback. */
|
|
1188
|
+
async pruneWithSegments(req, raw, intent, start) {
|
|
1189
|
+
const segments = segmentPruneOutput(raw, intent);
|
|
1190
|
+
if (segments.length <= 1) {
|
|
1191
|
+
// One segment has nothing to choose between; spending a judgment would
|
|
1192
|
+
// only reproduce the whole payload.
|
|
1193
|
+
return this.pruneResponse(raw, this.localPruneText(raw, intent, req.maxTokens), req.shadowMode, start, {
|
|
1194
|
+
judgePath: 'local',
|
|
1195
|
+
judgedSegments: 0,
|
|
1196
|
+
unjudgedSegments: segments.length
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
const judged = segments.slice(0, PRUNE_MAX_JUDGED_SEGMENTS);
|
|
1200
|
+
const unjudgedSegments = segments.length - judged.length;
|
|
1201
|
+
const diagnostics = { judgedSegments: judged.length, unjudgedSegments };
|
|
1202
|
+
try {
|
|
1203
|
+
const { scores, unparsedScoreAnswers } = await this.scoreSegments(judged, segments.length, intent);
|
|
1204
|
+
// Every segment without a usable score is kept whole as the unjudged
|
|
1205
|
+
// safety margin, so its share of the payload is the first number to read
|
|
1206
|
+
// when a judgment "ran" but the candidate did not shrink.
|
|
1207
|
+
const observation = {
|
|
1208
|
+
...diagnostics,
|
|
1209
|
+
scoredSegments: scores.size,
|
|
1210
|
+
autoKeptSegments: segments.length - scores.size,
|
|
1211
|
+
unparsedScoreAnswers
|
|
1212
|
+
};
|
|
1213
|
+
if (scores.size === 0) {
|
|
1214
|
+
const fallback = this.fallbackPruneText(raw, intent, req.maxTokens);
|
|
1215
|
+
return this.pruneResponse(raw, fallback.text, req.shadowMode, start, {
|
|
1216
|
+
judgePath: 'fallback',
|
|
1217
|
+
fallbackReason: 'System One returned no usable segment score',
|
|
1218
|
+
compression: fallback.compression,
|
|
1219
|
+
...observation
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
const candidates = buildScoredPruneCandidates(raw, segments, scores, req.maxTokens);
|
|
1223
|
+
const charsOf = (id) => candidates.find((candidate) => candidate.id === id)?.text.length ?? raw.length;
|
|
1224
|
+
const sizes = {
|
|
1225
|
+
aggressiveChars: charsOf('aggressive'),
|
|
1226
|
+
balancedChars: charsOf('balanced'),
|
|
1227
|
+
conservativeChars: charsOf('conservative')
|
|
1228
|
+
};
|
|
1229
|
+
// The balanced candidate's selection answers "did the judge keep too much
|
|
1230
|
+
// to shrink anything"; when another budget was installed, its own count is
|
|
1231
|
+
// what survived, so report that one instead.
|
|
1232
|
+
const balanced = candidates.find((candidate) => candidate.id === 'balanced');
|
|
1233
|
+
const pick = pickScoredPruneCandidate(raw, candidates);
|
|
1234
|
+
if (pick.candidate !== undefined) {
|
|
1235
|
+
return this.pruneResponse(raw, pick.text, req.shadowMode, start, {
|
|
1236
|
+
judgePath: 'segmented',
|
|
1237
|
+
...observation,
|
|
1238
|
+
...sizes,
|
|
1239
|
+
keptSegments: pick.candidate.keptSegments
|
|
1240
|
+
});
|
|
1241
|
+
}
|
|
1242
|
+
// The judgment kept as much as the payload: every segment was scored and
|
|
1243
|
+
// kept whole. That is the model's decision, not a service failure, so the
|
|
1244
|
+
// payload is returned untouched instead of being template-compressed over
|
|
1245
|
+
// it. Only a judgment with no usable score at all (handled above) and a
|
|
1246
|
+
// failed call (below) fall back to template compression.
|
|
1247
|
+
return this.pruneResponse(raw, raw, req.shadowMode, start, {
|
|
1248
|
+
judgePath: 'segmented',
|
|
1249
|
+
fallbackReason: 'judged: every segment was kept',
|
|
1250
|
+
compression: 'none',
|
|
1251
|
+
...observation,
|
|
1252
|
+
...sizes,
|
|
1253
|
+
...(balanced === undefined ? {} : { keptSegments: balanced.keptSegments })
|
|
1254
|
+
});
|
|
1255
|
+
}
|
|
1256
|
+
catch (err) {
|
|
1257
|
+
// FAIL-OPEN: template compression keeps the pruned output useful even when
|
|
1258
|
+
// the deterministic line filters have nothing to drop; the harness never
|
|
1259
|
+
// breaks and never returns the raw blob just because the gateway blinked.
|
|
1260
|
+
const fallback = this.fallbackPruneText(raw, intent, req.maxTokens);
|
|
1261
|
+
return this.pruneResponse(raw, fallback.text, req.shadowMode, start, {
|
|
1262
|
+
judgePath: 'fallback',
|
|
1263
|
+
fallbackReason: errorMessage(err),
|
|
1264
|
+
compression: fallback.compression,
|
|
1265
|
+
...diagnostics
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
}
|
|
1269
|
+
/**
|
|
1270
|
+
* Judge every segment in one request: one `score` question per segment, with
|
|
1271
|
+
* the segment text and the intent in the shared state.
|
|
1272
|
+
* @param judged - the segments selected for judging, in original order.
|
|
1273
|
+
* @param totalSegments - segment count of the whole output (for the prompt).
|
|
1274
|
+
* @param intent - what the caller is looking for.
|
|
1275
|
+
* @returns the keep score per segment index (segments without a usable answer
|
|
1276
|
+
* are absent) and how many answers were returned but could not be read.
|
|
1277
|
+
*/
|
|
1278
|
+
async scoreSegments(judged, totalSegments, intent) {
|
|
1279
|
+
const questions = {};
|
|
1280
|
+
const stateSegments = {};
|
|
1281
|
+
for (const segment of judged) {
|
|
1282
|
+
const id = `${PRUNE_SEGMENT_QUESTION_PREFIX}${String(segment.index)}`;
|
|
1283
|
+
questions[id] = {
|
|
1284
|
+
type: 'score',
|
|
1285
|
+
instructions: buildSegmentInstructions(intent, segment, totalSegments),
|
|
1286
|
+
criteria: [...PRUNE_KEEP_CRITERIA]
|
|
1287
|
+
};
|
|
1288
|
+
stateSegments[id] = segment.text;
|
|
1289
|
+
}
|
|
1290
|
+
const data = await this.postSystemOne(questions, {
|
|
1291
|
+
intent,
|
|
1292
|
+
segments: stateSegments,
|
|
1293
|
+
judgedSegments: judged.length,
|
|
1294
|
+
unjudgedSegments: totalSegments - judged.length,
|
|
1295
|
+
segmentMaxChars: PRUNE_SEGMENT_MAX_CHARS
|
|
1296
|
+
});
|
|
1297
|
+
const scores = new Map();
|
|
1298
|
+
let unparsedScoreAnswers = 0;
|
|
1299
|
+
for (const segment of judged) {
|
|
1300
|
+
const id = `${PRUNE_SEGMENT_QUESTION_PREFIX}${String(segment.index)}`;
|
|
1301
|
+
const answer = data.answers?.[id];
|
|
1302
|
+
const score = readSegmentKeepScore(answer);
|
|
1303
|
+
if (score !== undefined) {
|
|
1304
|
+
scores.set(segment.index, score);
|
|
1305
|
+
}
|
|
1306
|
+
else if (answer !== undefined && answer !== null) {
|
|
1307
|
+
// An answer we cannot read is not a high score: it is counted, and the
|
|
1308
|
+
// segment falls into the unjudged safety margin with no answer at all.
|
|
1309
|
+
unparsedScoreAnswers += 1;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
return { scores, unparsedScoreAnswers };
|
|
1313
|
+
}
|
|
1314
|
+
/** Shape one pruning as the shared prune response. */
|
|
1315
|
+
pruneResponse(raw, text, shadow, start, meta) {
|
|
1316
|
+
const originalChars = raw.length;
|
|
1317
|
+
const prunedChars = text.length;
|
|
1318
|
+
return {
|
|
1319
|
+
processedOutput: shadow ? raw : text,
|
|
1320
|
+
originalChars,
|
|
1321
|
+
prunedChars,
|
|
1322
|
+
estimatedTokensSaved: Math.max(0, Math.ceil((originalChars - prunedChars) / 4)),
|
|
1323
|
+
reductionPercentage: originalChars > 0 ? Number((((originalChars - prunedChars) / originalChars) * 100).toFixed(1)) : 0,
|
|
1324
|
+
shadow: Boolean(shadow),
|
|
1325
|
+
latencyMs: elapsed(start),
|
|
1326
|
+
...meta
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Classify the recent action window into one execution pattern.
|
|
1331
|
+
*
|
|
1332
|
+
* One `choice` question over the five patterns; the distribution decides the
|
|
1333
|
+
* verdict and its confidence. When the gateway cannot answer, the same window
|
|
1334
|
+
* is read by {@link detectStagnationLocally}, so a network failure lowers the
|
|
1335
|
+
* quality of the verdict instead of removing it.
|
|
1336
|
+
* @param req - action history and optional goal.
|
|
1337
|
+
* @returns the pattern, whether it is stuck, and the intervention.
|
|
1338
|
+
*/
|
|
1339
|
+
async checkStagnation(req) {
|
|
1340
|
+
const start = performance.now();
|
|
1341
|
+
const history = Array.isArray(req.history) ? req.history : [];
|
|
1342
|
+
const windowSize = typeof req.windowSize === 'number' && req.windowSize > 0 ? req.windowSize : 6;
|
|
1343
|
+
const recent = history.slice(-windowSize);
|
|
1344
|
+
if (recent.length < 3) {
|
|
1345
|
+
return {
|
|
1346
|
+
isStuck: false,
|
|
1347
|
+
patternType: 'none',
|
|
1348
|
+
confidence: 1,
|
|
1349
|
+
diagnosis: 'Action count below stagnation threshold (< 3).',
|
|
1350
|
+
latencyMs: elapsed(start)
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
try {
|
|
1354
|
+
const criteria = {};
|
|
1355
|
+
for (const pattern of STAGNATION_PATTERNS)
|
|
1356
|
+
criteria[pattern] = STAGNATION_PATTERN_CRITERIA[pattern];
|
|
1357
|
+
const data = await this.postSystemOne({
|
|
1358
|
+
[STAGNATION_QUESTION_ID]: {
|
|
1359
|
+
type: 'choice',
|
|
1360
|
+
instructions: buildStagnationInstructions(),
|
|
1361
|
+
criteria
|
|
1362
|
+
}
|
|
1363
|
+
}, {
|
|
1364
|
+
goal: (req.goal ?? '').trim() || null,
|
|
1365
|
+
window: recent.length,
|
|
1366
|
+
actions: recent.map((step) => ({
|
|
1367
|
+
tool: step.tool,
|
|
1368
|
+
input: clip(step.input, 240),
|
|
1369
|
+
...(step.output === undefined ? {} : { output: clip(step.output, 240) }),
|
|
1370
|
+
...(step.error === undefined ? {} : { error: clip(step.error, 240) })
|
|
1371
|
+
}))
|
|
1372
|
+
});
|
|
1373
|
+
const answer = data.answers?.[STAGNATION_QUESTION_ID];
|
|
1374
|
+
const distribution = readDistribution(answer?.probabilities, STAGNATION_PATTERNS);
|
|
1375
|
+
if (distribution === undefined) {
|
|
1376
|
+
return this.localStagnation(recent, 'the gateway returned no usable pattern distribution', start);
|
|
1377
|
+
}
|
|
1378
|
+
const pattern = argmaxKey(distribution, STAGNATION_PATTERNS);
|
|
1379
|
+
const confidence = round4(distribution.get(pattern) ?? 0);
|
|
1380
|
+
const isStuck = pattern !== 'none' && confidence >= STAGNATION_STUCK_FLOOR;
|
|
1381
|
+
return {
|
|
1382
|
+
isStuck,
|
|
1383
|
+
patternType: pattern,
|
|
1384
|
+
confidence,
|
|
1385
|
+
diagnosis: `${STAGNATION_DIAGNOSIS[pattern]} (System One p=${confidence.toFixed(3)})`,
|
|
1386
|
+
...(isStuck ? { recommendedIntervention: STAGNATION_INTERVENTION[pattern] } : {}),
|
|
1387
|
+
latencyMs: elapsed(start)
|
|
1388
|
+
};
|
|
1389
|
+
}
|
|
1390
|
+
catch (err) {
|
|
1391
|
+
return this.localStagnation(recent, errorMessage(err), start);
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
/** The local half of {@link checkStagnation}, annotated with why it ran. */
|
|
1395
|
+
localStagnation(recent, reason, start) {
|
|
1396
|
+
const local = detectStagnationLocally(recent);
|
|
1397
|
+
return {
|
|
1398
|
+
isStuck: local.isStuck,
|
|
1399
|
+
patternType: local.patternType,
|
|
1400
|
+
confidence: local.confidence,
|
|
1401
|
+
diagnosis: `${local.diagnosis} [System One unavailable: ${reason}]`,
|
|
1402
|
+
...(local.recommendedIntervention === undefined
|
|
1403
|
+
? {}
|
|
1404
|
+
: { recommendedIntervention: local.recommendedIntervention }),
|
|
1405
|
+
latencyMs: elapsed(start)
|
|
1406
|
+
};
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* Rank candidates for a goal with one `choice` question.
|
|
1410
|
+
*
|
|
1411
|
+
* The criteria are the candidate names carrying their descriptions and the
|
|
1412
|
+
* incumbent mark, and the answer's distribution is the ranking. The rendered
|
|
1413
|
+
* context block of {@link buildRouteContext} — the same facts the logits
|
|
1414
|
+
* backend scores — rides in the instructions, so cache-affinity facts and the
|
|
1415
|
+
* policy reach the model. A failing gateway falls back to a local keyword
|
|
1416
|
+
* ranking whose reasons say so, keeping routing alive.
|
|
1417
|
+
* @param req - goal, candidates, topK, session facts, policy.
|
|
1418
|
+
* @returns the ranked candidates, best first.
|
|
1419
|
+
*/
|
|
1420
|
+
async route(req) {
|
|
1421
|
+
const start = performance.now();
|
|
1422
|
+
const candidates = Array.isArray(req.candidates) ? req.candidates : [];
|
|
1423
|
+
const topK = typeof req.topK === 'number' && req.topK > 0 ? req.topK : 3;
|
|
1424
|
+
if (candidates.length === 0)
|
|
1425
|
+
return { rankedCandidates: [], latencyMs: elapsed(start) };
|
|
1426
|
+
try {
|
|
1427
|
+
const keys = uniqueCriteriaKeys(candidates.map((candidate) => candidate.name));
|
|
1428
|
+
const criteria = {};
|
|
1429
|
+
keys.forEach((key, index) => {
|
|
1430
|
+
const candidate = candidates[index];
|
|
1431
|
+
criteria[key] = candidateCriterion(candidate);
|
|
1432
|
+
});
|
|
1433
|
+
const data = await this.postSystemOne({
|
|
1434
|
+
[ROUTE_QUESTION_ID]: {
|
|
1435
|
+
type: 'choice',
|
|
1436
|
+
instructions: buildRouteInstructions(req),
|
|
1437
|
+
criteria
|
|
1438
|
+
}
|
|
1439
|
+
}, {
|
|
1440
|
+
goal: req.goal,
|
|
1441
|
+
...(req.state === undefined ? {} : { session: req.state }),
|
|
1442
|
+
topK
|
|
1443
|
+
});
|
|
1444
|
+
const answer = data.answers?.[ROUTE_QUESTION_ID];
|
|
1445
|
+
const distribution = readDistribution(answer?.probabilities, keys);
|
|
1446
|
+
if (distribution === undefined) {
|
|
1447
|
+
return this.localRoute(candidates, req.goal, 'the gateway returned no usable candidate distribution', topK, start);
|
|
1448
|
+
}
|
|
1449
|
+
const ranked = candidates
|
|
1450
|
+
.map((candidate, index) => ({ name: candidate.name, score: round4(distribution.get(keys[index]) ?? 0) }))
|
|
1451
|
+
.sort((a, b) => b.score - a.score);
|
|
1452
|
+
const declaredKey = typeof answer?.choice === 'string'
|
|
1453
|
+
? keys.find((key) => normalizeKey(key) === normalizeKey(String(answer.choice)))
|
|
1454
|
+
: undefined;
|
|
1455
|
+
if (declaredKey !== undefined) {
|
|
1456
|
+
const declaredName = candidates[keys.indexOf(declaredKey)]?.name ?? '';
|
|
1457
|
+
promoteTiedWinner(ranked, ranked.findIndex((entry) => entry.name === declaredName), (entry) => entry.score);
|
|
1458
|
+
}
|
|
1459
|
+
const sliced = ranked.slice(0, topK);
|
|
1460
|
+
return { rankedCandidates: sliced, bestCandidate: sliced[0]?.name, latencyMs: elapsed(start) };
|
|
1461
|
+
}
|
|
1462
|
+
catch (err) {
|
|
1463
|
+
return this.localRoute(candidates, req.goal, errorMessage(err), topK, start);
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
/** Deterministic keyword ranking used when the gateway cannot rank. */
|
|
1467
|
+
localRoute(candidates, goal, reason, topK, start) {
|
|
1468
|
+
const goalWords = tokenize(goal ?? '');
|
|
1469
|
+
const weighted = candidates.map((candidate) => {
|
|
1470
|
+
const text = `${candidate.name} ${candidate.description ?? ''} ${candidate.type ?? ''}`.toLowerCase();
|
|
1471
|
+
const matches = goalWords.filter((word) => text.includes(word)).length;
|
|
1472
|
+
return { name: candidate.name, weight: 0.1 + matches };
|
|
1473
|
+
});
|
|
1474
|
+
const total = weighted.reduce((sum, entry) => sum + entry.weight, 0);
|
|
1475
|
+
const ranked = weighted
|
|
1476
|
+
.map((entry) => ({
|
|
1477
|
+
name: entry.name,
|
|
1478
|
+
score: Number((entry.weight / total).toFixed(4)),
|
|
1479
|
+
reason: `local fallback (${reason})`
|
|
1480
|
+
}))
|
|
1481
|
+
.sort((a, b) => b.score - a.score);
|
|
1482
|
+
const sliced = ranked.slice(0, topK);
|
|
1483
|
+
return { rankedCandidates: sliced, bestCandidate: sliced[0]?.name, latencyMs: elapsed(start) };
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
//# sourceMappingURL=jev.js.map
|