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,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vocabulary one option contributes to the overlap score.
|
|
3
|
+
*
|
|
4
|
+
* ASCII words keep the historical `length > 2` filter. Chinese is written
|
|
5
|
+
* without spaces and nearly every technical term is two characters (并发, 证明,
|
|
6
|
+
* 迁移), so a whitespace split drops the entire vocabulary of a Chinese turn and
|
|
7
|
+
* collapses every option into an indistinguishable tie — which then reads as a
|
|
8
|
+
* confident decision. A Han run therefore contributes its two-character grams as
|
|
9
|
+
* well as the run itself, so an option's cue word matches inside an unspaced
|
|
10
|
+
* sentence.
|
|
11
|
+
*
|
|
12
|
+
* @param text - one candidate option.
|
|
13
|
+
* @returns distinct matchable terms, in option order.
|
|
14
|
+
*/
|
|
15
|
+
function matchTerms(text) {
|
|
16
|
+
const terms = [];
|
|
17
|
+
for (const chunk of text.toLowerCase().split(/[^\p{Script=Han}a-z0-9_]+/u)) {
|
|
18
|
+
if (chunk.length === 0)
|
|
19
|
+
continue;
|
|
20
|
+
if (!/\p{Script=Han}/u.test(chunk)) {
|
|
21
|
+
if (chunk.length > 2)
|
|
22
|
+
terms.push(chunk);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
for (let i = 0; i + 2 <= chunk.length; i++)
|
|
26
|
+
terms.push(chunk.slice(i, i + 2));
|
|
27
|
+
if (chunk.length > 2)
|
|
28
|
+
terms.push(chunk);
|
|
29
|
+
}
|
|
30
|
+
return terms;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Ignore explicit zero counters, not failure words in general. Both count-first
|
|
34
|
+
* (pytest/Jest) and label-first (node:test) summaries occur in captured output.
|
|
35
|
+
* Replace nonzero counters with a cue so another zero counter cannot hide them.
|
|
36
|
+
* This is only a mock heuristic, not semantic verification of arbitrary logs.
|
|
37
|
+
*/
|
|
38
|
+
function hasFailureEvidence(evidence) {
|
|
39
|
+
const withoutCounters = evidence.replace(/\b(\d+(?:[.,]\d+)*|zero)\s+(fail(?:ed|ures?)?|errors?)\b|\b(fail(?:ed|ures?)?|errors?)\s*[:=]?\s*(\d+(?:[.,]\d+)*|zero)\b/g, (_match, before, _beforeLabel, _afterLabel, after) => {
|
|
40
|
+
const count = before ?? after;
|
|
41
|
+
return count === 'zero' || Number(count.replaceAll(',', '')) === 0 ? '' : ' failure ';
|
|
42
|
+
});
|
|
43
|
+
// An explicit error diagnostic is not a summary, even if its message starts with 0.
|
|
44
|
+
return evidence.includes('error:') || /fail|\berrors?\b/.test(withoutCounters);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Mock & Heuristic Decision Backend
|
|
48
|
+
* Provides instant (<5ms) deterministic decision evaluation for testing,
|
|
49
|
+
* local development, and offline baseline comparisons.
|
|
50
|
+
*/
|
|
51
|
+
export class MockDecisionBackend {
|
|
52
|
+
name = 'mock-heuristic-v1';
|
|
53
|
+
model = 'mock-system-one';
|
|
54
|
+
async choice(req) {
|
|
55
|
+
const start = performance.now();
|
|
56
|
+
if (req.options.length === 0) {
|
|
57
|
+
throw new Error('Choice options cannot be empty');
|
|
58
|
+
}
|
|
59
|
+
// Heuristic: rank options based on semantic relevance to question & context
|
|
60
|
+
const qLower = req.question.toLowerCase();
|
|
61
|
+
const ctxLower = (req.context || '').toLowerCase();
|
|
62
|
+
const scores = req.options.map((opt) => {
|
|
63
|
+
let score = 1.0;
|
|
64
|
+
// Match the option's own vocabulary against the question and the context.
|
|
65
|
+
for (const term of matchTerms(opt)) {
|
|
66
|
+
if (qLower.includes(term))
|
|
67
|
+
score += 2.0;
|
|
68
|
+
if (ctxLower.includes(term))
|
|
69
|
+
score += 1.5;
|
|
70
|
+
}
|
|
71
|
+
return score;
|
|
72
|
+
});
|
|
73
|
+
const sum = scores.reduce((a, b) => a + b, 0);
|
|
74
|
+
const options = req.options.map((opt, i) => ({
|
|
75
|
+
label: opt,
|
|
76
|
+
probability: Number((scores[i] / sum).toFixed(4))
|
|
77
|
+
}));
|
|
78
|
+
// Sort descending
|
|
79
|
+
options.sort((a, b) => b.probability - a.probability);
|
|
80
|
+
return {
|
|
81
|
+
options,
|
|
82
|
+
bestOption: options[0].label,
|
|
83
|
+
confidence: options[0].probability,
|
|
84
|
+
uncalibrated: true,
|
|
85
|
+
latencyMs: Math.round(performance.now() - start)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
async gate(req) {
|
|
89
|
+
const start = performance.now();
|
|
90
|
+
const evidenceLower = req.evidence.toLowerCase();
|
|
91
|
+
const hasErrorOrFail = hasFailureEvidence(evidenceLower);
|
|
92
|
+
const results = [];
|
|
93
|
+
let allPassed = true;
|
|
94
|
+
let needsEscalation = false;
|
|
95
|
+
for (const assertion of req.assertions) {
|
|
96
|
+
const stmt = assertion.statement;
|
|
97
|
+
const stmtLower = stmt.toLowerCase();
|
|
98
|
+
// Check for contradiction cues
|
|
99
|
+
const claimsSuccess = stmtLower.includes('success') || stmtLower.includes('pass') || stmtLower.includes('finished') || stmtLower.includes('complete');
|
|
100
|
+
let outcome = 'not_addressed';
|
|
101
|
+
let confidence = 0.85;
|
|
102
|
+
let explanation = 'Evaluated against evidence.';
|
|
103
|
+
if (claimsSuccess && hasErrorOrFail) {
|
|
104
|
+
outcome = 'contradicted';
|
|
105
|
+
explanation = 'Assertion claims success, but evidence contains explicit error or failure output.';
|
|
106
|
+
allPassed = false;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
// Keyword overlap
|
|
110
|
+
const words = stmtLower.replace(/[^a-z0-9_\s]/g, '').split(/\s+/).filter((w) => w.length > 3);
|
|
111
|
+
const matches = words.filter((w) => evidenceLower.includes(w));
|
|
112
|
+
const matchRatio = words.length > 0 ? matches.length / words.length : 0;
|
|
113
|
+
if (matchRatio >= 0.4) {
|
|
114
|
+
outcome = 'verified';
|
|
115
|
+
explanation = `Direct evidence found supporting assertion (matching terms: ${matches.slice(0, 3).join(', ')}).`;
|
|
116
|
+
}
|
|
117
|
+
else if (words.length > 0) {
|
|
118
|
+
outcome = 'not_addressed';
|
|
119
|
+
explanation = 'Evidence does not contain sufficient details to verify this assertion.';
|
|
120
|
+
allPassed = false;
|
|
121
|
+
if (req.strictness === 'high') {
|
|
122
|
+
needsEscalation = true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
outcome = 'escalate';
|
|
127
|
+
explanation = 'Insufficient assertion details to determine truth.';
|
|
128
|
+
needsEscalation = true;
|
|
129
|
+
allPassed = false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
results.push({
|
|
133
|
+
statement: stmt,
|
|
134
|
+
outcome,
|
|
135
|
+
confidence,
|
|
136
|
+
explanation
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
passed: allPassed && !needsEscalation,
|
|
141
|
+
results,
|
|
142
|
+
needsEscalation,
|
|
143
|
+
latencyMs: Math.round(performance.now() - start)
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
async prune(req) {
|
|
147
|
+
const start = performance.now();
|
|
148
|
+
const raw = req.rawOutput;
|
|
149
|
+
const lines = raw.split('\n');
|
|
150
|
+
// Deterministic + Semantic pruning:
|
|
151
|
+
// Drop repetitive progress bars, redundant empty lines, unhelpful stack traces
|
|
152
|
+
const filteredLines = [];
|
|
153
|
+
const intentWords = req.intent.toLowerCase().split(/\s+/).filter((w) => w.length > 2);
|
|
154
|
+
let consecutiveEmpty = 0;
|
|
155
|
+
for (const line of lines) {
|
|
156
|
+
const trimmed = line.trim();
|
|
157
|
+
if (!trimmed) {
|
|
158
|
+
consecutiveEmpty++;
|
|
159
|
+
if (consecutiveEmpty <= 1)
|
|
160
|
+
filteredLines.push(line);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
consecutiveEmpty = 0;
|
|
164
|
+
// Filter progress bar indicators
|
|
165
|
+
if (line.includes('[===') || line.includes('████') || line.includes('\r')) {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
// Filter typical node_modules / site-packages deep internals unless relevant
|
|
169
|
+
if (line.includes('node_modules/') && !intentWords.some((w) => line.toLowerCase().includes(w))) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
filteredLines.push(line);
|
|
173
|
+
}
|
|
174
|
+
const prunedOutput = filteredLines.join('\n');
|
|
175
|
+
const originalChars = raw.length;
|
|
176
|
+
const prunedChars = prunedOutput.length;
|
|
177
|
+
const estOriginalTokens = Math.ceil(originalChars / 4);
|
|
178
|
+
const estPrunedTokens = Math.ceil(prunedChars / 4);
|
|
179
|
+
const tokensSaved = Math.max(0, estOriginalTokens - estPrunedTokens);
|
|
180
|
+
const reduction = originalChars > 0 ? Number((((originalChars - prunedChars) / originalChars) * 100).toFixed(1)) : 0;
|
|
181
|
+
return {
|
|
182
|
+
processedOutput: req.shadowMode ? raw : prunedOutput,
|
|
183
|
+
originalChars,
|
|
184
|
+
prunedChars,
|
|
185
|
+
estimatedTokensSaved: tokensSaved,
|
|
186
|
+
reductionPercentage: reduction,
|
|
187
|
+
shadow: !!req.shadowMode,
|
|
188
|
+
latencyMs: Math.round(performance.now() - start)
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
async checkStagnation(req) {
|
|
192
|
+
const start = performance.now();
|
|
193
|
+
const history = req.history || [];
|
|
194
|
+
const window = req.windowSize || 6;
|
|
195
|
+
const recent = history.slice(-window);
|
|
196
|
+
if (recent.length < 3) {
|
|
197
|
+
return {
|
|
198
|
+
isStuck: false,
|
|
199
|
+
confidence: 0.95,
|
|
200
|
+
diagnosis: 'Action history too short to establish stagnation.',
|
|
201
|
+
latencyMs: Math.round(performance.now() - start)
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
// Check 1: Identical consecutive tool calls
|
|
205
|
+
const tools = recent.map((r) => `${r.tool}:${r.input.trim()}`);
|
|
206
|
+
let identicalCount = 1;
|
|
207
|
+
for (let i = tools.length - 1; i > 0; i--) {
|
|
208
|
+
if (tools[i] === tools[i - 1]) {
|
|
209
|
+
identicalCount++;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (identicalCount >= 3) {
|
|
216
|
+
return {
|
|
217
|
+
isStuck: true,
|
|
218
|
+
patternType: 'repetition',
|
|
219
|
+
confidence: 0.98,
|
|
220
|
+
diagnosis: `Identical tool execution repeated ${identicalCount} times consecutively with no change in input.`,
|
|
221
|
+
recommendedIntervention: 'Interrupt execution. Suggest alternative tool or inspect previous failure output.',
|
|
222
|
+
latencyMs: Math.round(performance.now() - start)
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
// Check 2: Repeated failure / error cycle
|
|
226
|
+
const failingSteps = recent.filter((r) => !!r.error || (r.output && (r.output.includes('Error') || r.output.includes('Failed'))));
|
|
227
|
+
if (failingSteps.length >= 3 && recent.length >= 4) {
|
|
228
|
+
return {
|
|
229
|
+
isStuck: true,
|
|
230
|
+
patternType: 'failing_retry',
|
|
231
|
+
confidence: 0.91,
|
|
232
|
+
diagnosis: `${failingSteps.length} of the last ${recent.length} actions failed with recurring errors.`,
|
|
233
|
+
recommendedIntervention: 'Step back to analyze root cause before attempting the same action again.',
|
|
234
|
+
latencyMs: Math.round(performance.now() - start)
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
// Check 3: Oscillation between two files or two commands
|
|
238
|
+
if (recent.length >= 4) {
|
|
239
|
+
const last4 = recent.slice(-4);
|
|
240
|
+
if (last4[0].tool === last4[2].tool && last4[1].tool === last4[3].tool && last4[0].input === last4[2].input && last4[1].input === last4[3].input) {
|
|
241
|
+
return {
|
|
242
|
+
isStuck: true,
|
|
243
|
+
patternType: 'oscillation',
|
|
244
|
+
confidence: 0.94,
|
|
245
|
+
diagnosis: 'Agent is oscillating between two identical alternating commands/edits.',
|
|
246
|
+
recommendedIntervention: 'Break loop: stop alternating and clarify requirements or test independently.',
|
|
247
|
+
latencyMs: Math.round(performance.now() - start)
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
isStuck: false,
|
|
253
|
+
patternType: 'none',
|
|
254
|
+
confidence: 0.88,
|
|
255
|
+
diagnosis: 'Execution flow appears progressive.',
|
|
256
|
+
latencyMs: Math.round(performance.now() - start)
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
async route(req) {
|
|
260
|
+
const start = performance.now();
|
|
261
|
+
const goalLower = req.goal.toLowerCase();
|
|
262
|
+
const scored = req.candidates.map((cand) => {
|
|
263
|
+
let score = 0.1;
|
|
264
|
+
const text = `${cand.name} ${cand.description}`.toLowerCase();
|
|
265
|
+
const words = goalLower.split(/\s+/).filter((w) => w.length > 2);
|
|
266
|
+
for (const w of words) {
|
|
267
|
+
if (text.includes(w))
|
|
268
|
+
score += 1.0;
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
name: cand.name,
|
|
272
|
+
score: Number(score.toFixed(3)),
|
|
273
|
+
reason: `Matched ${words.filter((w) => text.includes(w)).length} query keywords`
|
|
274
|
+
};
|
|
275
|
+
});
|
|
276
|
+
scored.sort((a, b) => b.score - a.score);
|
|
277
|
+
const topK = req.topK || 3;
|
|
278
|
+
const ranked = scored.slice(0, topK);
|
|
279
|
+
return {
|
|
280
|
+
rankedCandidates: ranked,
|
|
281
|
+
bestCandidate: ranked[0]?.name,
|
|
282
|
+
latencyMs: Math.round(performance.now() - start)
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Deterministic stand-in for the closing-claim classification.
|
|
287
|
+
*
|
|
288
|
+
* The mock exists to keep the offline path honest, not to be the semantic
|
|
289
|
+
* judge: it reads a negated outcome first and only then looks for a completion
|
|
290
|
+
* word, which is enough to cover the English/Chinese and negation cases the
|
|
291
|
+
* hook fixtures exercise.
|
|
292
|
+
* @param req - candidate sentences plus the turn's intent.
|
|
293
|
+
* @returns one classification per candidate, in submission order.
|
|
294
|
+
*/
|
|
295
|
+
async classifyClaims(req) {
|
|
296
|
+
const start = performance.now();
|
|
297
|
+
const statements = Array.isArray(req.statements) ? req.statements : [];
|
|
298
|
+
return {
|
|
299
|
+
results: statements.map(mockClaimClassification),
|
|
300
|
+
latencyMs: Math.round(performance.now() - start)
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/** A negated or unfinished outcome, which is never a completion claim. */
|
|
305
|
+
const MOCK_CLAIM_NEGATION = /(not\s+(?:yet\s+)?(?:complete|completed|done|finished|passing|passed|successful|success)|isn'?t\s+(?:complete|done|finished)|still\s+(?:failing|fails|broken|running)|没有通过|未通过|尚未完成|尚未通过|还没完成|还没通过|并未完成|仍然失败|仍有失败|还有失败)/i;
|
|
306
|
+
/** A completion assertion in English or Chinese. */
|
|
307
|
+
const MOCK_CLAIM_WORD = /(\b(?:pass|passed|passing|green|success|successful|succeed|succeeded|complete|completed|finished|done|verified|works)\b|全部通过|所有测试通过|已完成|完成了|已通过|通过|成功|跑通|全绿|搞定)/i;
|
|
308
|
+
/**
|
|
309
|
+
* Classify one candidate sentence.
|
|
310
|
+
* @param statement - one candidate sentence.
|
|
311
|
+
* @returns whether it reads as a completion claim and with what certainty.
|
|
312
|
+
*/
|
|
313
|
+
function mockClaimClassification(statement) {
|
|
314
|
+
if (MOCK_CLAIM_NEGATION.test(statement))
|
|
315
|
+
return { statement, isClaim: false, confidence: 0.96 };
|
|
316
|
+
if (MOCK_CLAIM_WORD.test(statement))
|
|
317
|
+
return { statement, isClaim: true, confidence: 0.92 };
|
|
318
|
+
return { statement, isClaim: false, confidence: 0.9 };
|
|
319
|
+
}
|
|
320
|
+
//# sourceMappingURL=mock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock.js","sourceRoot":"","sources":["../../src/backend/mock.ts"],"names":[],"mappings":"AAmBA;;;;;;;;;;;;;GAaG;AACH,SAAS,UAAU,CAAC,IAAY;IAC9B,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,4BAA4B,CAAC,EAAE,CAAC;QAC3E,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;YAAE,SAAS;QACjC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACnC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;gBAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YACxC,SAAS;QACX,CAAC;QACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE;YAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC9E,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC;YAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IACD,OAAO,KAAK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EAAE,UAAU,EAAE,GAAG,EAAE,CAAC;AACxD,CAAC","sourcesContent":["import {\n DecisionBackend,\n ChoiceRequest,\n ChoiceResponse,\n GateRequest,\n GateResponse,\n PruneRequest,\n PruneResponse,\n StagnationCheckRequest,\n StagnationCheckResponse,\n RouteRequest,\n RouteResponse,\n GateResultItem,\n GateOutcome,\n ClaimClassification,\n ClaimClassificationRequest,\n ClaimClassificationResponse\n} from './types.js';\n\n/**\n * Vocabulary one option contributes to the overlap score.\n *\n * ASCII words keep the historical `length > 2` filter. Chinese is written\n * without spaces and nearly every technical term is two characters (并发, 证明,\n * 迁移), so a whitespace split drops the entire vocabulary of a Chinese turn and\n * collapses every option into an indistinguishable tie — which then reads as a\n * confident decision. A Han run therefore contributes its two-character grams as\n * well as the run itself, so an option's cue word matches inside an unspaced\n * sentence.\n *\n * @param text - one candidate option.\n * @returns distinct matchable terms, in option order.\n */\nfunction matchTerms(text: string): string[] {\n const terms: string[] = [];\n for (const chunk of text.toLowerCase().split(/[^\\p{Script=Han}a-z0-9_]+/u)) {\n if (chunk.length === 0) continue;\n if (!/\\p{Script=Han}/u.test(chunk)) {\n if (chunk.length > 2) terms.push(chunk);\n continue;\n }\n for (let i = 0; i + 2 <= chunk.length; i++) terms.push(chunk.slice(i, i + 2));\n if (chunk.length > 2) terms.push(chunk);\n }\n return terms;\n}\n\n/**\n * Ignore explicit zero counters, not failure words in general. Both count-first\n * (pytest/Jest) and label-first (node:test) summaries occur in captured output.\n * Replace nonzero counters with a cue so another zero counter cannot hide them.\n * This is only a mock heuristic, not semantic verification of arbitrary logs.\n */\nfunction hasFailureEvidence(evidence: string): boolean {\n const withoutCounters = evidence.replace(\n /\\b(\\d+(?:[.,]\\d+)*|zero)\\s+(fail(?:ed|ures?)?|errors?)\\b|\\b(fail(?:ed|ures?)?|errors?)\\s*[:=]?\\s*(\\d+(?:[.,]\\d+)*|zero)\\b/g,\n (_match, before: string | undefined, _beforeLabel: string | undefined, _afterLabel: string | undefined, after: string | undefined) => {\n const count = before ?? after!;\n return count === 'zero' || Number(count.replaceAll(',', '')) === 0 ? '' : ' failure ';\n }\n );\n // An explicit error diagnostic is not a summary, even if its message starts with 0.\n return evidence.includes('error:') || /fail|\\berrors?\\b/.test(withoutCounters);\n}\n\n/**\n * Mock & Heuristic Decision Backend\n * Provides instant (<5ms) deterministic decision evaluation for testing,\n * local development, and offline baseline comparisons.\n */\nexport class MockDecisionBackend implements DecisionBackend {\n readonly name = 'mock-heuristic-v1';\n readonly model = 'mock-system-one';\n\n async choice(req: ChoiceRequest): Promise<ChoiceResponse> {\n const start = performance.now();\n if (req.options.length === 0) {\n throw new Error('Choice options cannot be empty');\n }\n\n // Heuristic: rank options based on semantic relevance to question & context\n const qLower = req.question.toLowerCase();\n const ctxLower = (req.context || '').toLowerCase();\n\n const scores = req.options.map((opt) => {\n let score = 1.0;\n // Match the option's own vocabulary against the question and the context.\n for (const term of matchTerms(opt)) {\n if (qLower.includes(term)) score += 2.0;\n if (ctxLower.includes(term)) score += 1.5;\n }\n return score;\n });\n\n const sum = scores.reduce((a, b) => a + b, 0);\n const options = req.options.map((opt, i) => ({\n label: opt,\n probability: Number((scores[i] / sum).toFixed(4))\n }));\n\n // Sort descending\n options.sort((a, b) => b.probability - a.probability);\n\n return {\n options,\n bestOption: options[0].label,\n confidence: options[0].probability,\n uncalibrated: true,\n latencyMs: Math.round(performance.now() - start)\n };\n }\n\n async gate(req: GateRequest): Promise<GateResponse> {\n const start = performance.now();\n const evidenceLower = req.evidence.toLowerCase();\n const hasErrorOrFail = hasFailureEvidence(evidenceLower);\n const results: GateResultItem[] = [];\n let allPassed = true;\n let needsEscalation = false;\n\n for (const assertion of req.assertions) {\n const stmt = assertion.statement;\n const stmtLower = stmt.toLowerCase();\n\n // Check for contradiction cues\n const claimsSuccess = stmtLower.includes('success') || stmtLower.includes('pass') || stmtLower.includes('finished') || stmtLower.includes('complete');\n\n let outcome: GateOutcome = 'not_addressed';\n let confidence = 0.85;\n let explanation = 'Evaluated against evidence.';\n\n if (claimsSuccess && hasErrorOrFail) {\n outcome = 'contradicted';\n explanation = 'Assertion claims success, but evidence contains explicit error or failure output.';\n allPassed = false;\n } else {\n // Keyword overlap\n const words = stmtLower.replace(/[^a-z0-9_\\s]/g, '').split(/\\s+/).filter((w) => w.length > 3);\n const matches = words.filter((w) => evidenceLower.includes(w));\n const matchRatio = words.length > 0 ? matches.length / words.length : 0;\n\n if (matchRatio >= 0.4) {\n outcome = 'verified';\n explanation = `Direct evidence found supporting assertion (matching terms: ${matches.slice(0, 3).join(', ')}).`;\n } else if (words.length > 0) {\n outcome = 'not_addressed';\n explanation = 'Evidence does not contain sufficient details to verify this assertion.';\n allPassed = false;\n if (req.strictness === 'high') {\n needsEscalation = true;\n }\n } else {\n outcome = 'escalate';\n explanation = 'Insufficient assertion details to determine truth.';\n needsEscalation = true;\n allPassed = false;\n }\n }\n\n results.push({\n statement: stmt,\n outcome,\n confidence,\n explanation\n });\n }\n\n return {\n passed: allPassed && !needsEscalation,\n results,\n needsEscalation,\n latencyMs: Math.round(performance.now() - start)\n };\n }\n\n async prune(req: PruneRequest): Promise<PruneResponse> {\n const start = performance.now();\n const raw = req.rawOutput;\n const lines = raw.split('\\n');\n\n // Deterministic + Semantic pruning:\n // Drop repetitive progress bars, redundant empty lines, unhelpful stack traces\n const filteredLines: string[] = [];\n const intentWords = req.intent.toLowerCase().split(/\\s+/).filter((w) => w.length > 2);\n\n let consecutiveEmpty = 0;\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed) {\n consecutiveEmpty++;\n if (consecutiveEmpty <= 1) filteredLines.push(line);\n continue;\n }\n consecutiveEmpty = 0;\n\n // Filter progress bar indicators\n if (line.includes('[===') || line.includes('████') || line.includes('\\r')) {\n continue;\n }\n\n // Filter typical node_modules / site-packages deep internals unless relevant\n if (line.includes('node_modules/') && !intentWords.some((w) => line.toLowerCase().includes(w))) {\n continue;\n }\n\n filteredLines.push(line);\n }\n\n const prunedOutput = filteredLines.join('\\n');\n const originalChars = raw.length;\n const prunedChars = prunedOutput.length;\n const estOriginalTokens = Math.ceil(originalChars / 4);\n const estPrunedTokens = Math.ceil(prunedChars / 4);\n const tokensSaved = Math.max(0, estOriginalTokens - estPrunedTokens);\n const reduction = originalChars > 0 ? Number((((originalChars - prunedChars) / originalChars) * 100).toFixed(1)) : 0;\n\n return {\n processedOutput: req.shadowMode ? raw : prunedOutput,\n originalChars,\n prunedChars,\n estimatedTokensSaved: tokensSaved,\n reductionPercentage: reduction,\n shadow: !!req.shadowMode,\n latencyMs: Math.round(performance.now() - start)\n };\n }\n\n async checkStagnation(req: StagnationCheckRequest): Promise<StagnationCheckResponse> {\n const start = performance.now();\n const history = req.history || [];\n const window = req.windowSize || 6;\n const recent = history.slice(-window);\n\n if (recent.length < 3) {\n return {\n isStuck: false,\n confidence: 0.95,\n diagnosis: 'Action history too short to establish stagnation.',\n latencyMs: Math.round(performance.now() - start)\n };\n }\n\n // Check 1: Identical consecutive tool calls\n const tools = recent.map((r) => `${r.tool}:${r.input.trim()}`);\n let identicalCount = 1;\n for (let i = tools.length - 1; i > 0; i--) {\n if (tools[i] === tools[i - 1]) {\n identicalCount++;\n } else {\n break;\n }\n }\n\n if (identicalCount >= 3) {\n return {\n isStuck: true,\n patternType: 'repetition',\n confidence: 0.98,\n diagnosis: `Identical tool execution repeated ${identicalCount} times consecutively with no change in input.`,\n recommendedIntervention: 'Interrupt execution. Suggest alternative tool or inspect previous failure output.',\n latencyMs: Math.round(performance.now() - start)\n };\n }\n\n // Check 2: Repeated failure / error cycle\n const failingSteps = recent.filter((r) => !!r.error || (r.output && (r.output.includes('Error') || r.output.includes('Failed'))));\n if (failingSteps.length >= 3 && recent.length >= 4) {\n return {\n isStuck: true,\n patternType: 'failing_retry',\n confidence: 0.91,\n diagnosis: `${failingSteps.length} of the last ${recent.length} actions failed with recurring errors.`,\n recommendedIntervention: 'Step back to analyze root cause before attempting the same action again.',\n latencyMs: Math.round(performance.now() - start)\n };\n }\n\n // Check 3: Oscillation between two files or two commands\n if (recent.length >= 4) {\n const last4 = recent.slice(-4);\n if (last4[0].tool === last4[2].tool && last4[1].tool === last4[3].tool && last4[0].input === last4[2].input && last4[1].input === last4[3].input) {\n return {\n isStuck: true,\n patternType: 'oscillation',\n confidence: 0.94,\n diagnosis: 'Agent is oscillating between two identical alternating commands/edits.',\n recommendedIntervention: 'Break loop: stop alternating and clarify requirements or test independently.',\n latencyMs: Math.round(performance.now() - start)\n };\n }\n }\n\n return {\n isStuck: false,\n patternType: 'none',\n confidence: 0.88,\n diagnosis: 'Execution flow appears progressive.',\n latencyMs: Math.round(performance.now() - start)\n };\n }\n\n async route(req: RouteRequest): Promise<RouteResponse> {\n const start = performance.now();\n const goalLower = req.goal.toLowerCase();\n \n const scored = req.candidates.map((cand) => {\n let score = 0.1;\n const text = `${cand.name} ${cand.description}`.toLowerCase();\n const words = goalLower.split(/\\s+/).filter((w) => w.length > 2);\n for (const w of words) {\n if (text.includes(w)) score += 1.0;\n }\n return {\n name: cand.name,\n score: Number(score.toFixed(3)),\n reason: `Matched ${words.filter((w) => text.includes(w)).length} query keywords`\n };\n });\n\n scored.sort((a, b) => b.score - a.score);\n const topK = req.topK || 3;\n const ranked = scored.slice(0, topK);\n\n return {\n rankedCandidates: ranked,\n bestCandidate: ranked[0]?.name,\n latencyMs: Math.round(performance.now() - start)\n };\n }\n\n /**\n * Deterministic stand-in for the closing-claim classification.\n *\n * The mock exists to keep the offline path honest, not to be the semantic\n * judge: it reads a negated outcome first and only then looks for a completion\n * word, which is enough to cover the English/Chinese and negation cases the\n * hook fixtures exercise.\n * @param req - candidate sentences plus the turn's intent.\n * @returns one classification per candidate, in submission order.\n */\n async classifyClaims(req: ClaimClassificationRequest): Promise<ClaimClassificationResponse> {\n const start = performance.now();\n const statements = Array.isArray(req.statements) ? req.statements : [];\n return {\n results: statements.map(mockClaimClassification),\n latencyMs: Math.round(performance.now() - start)\n };\n }\n}\n\n/** A negated or unfinished outcome, which is never a completion claim. */\nconst MOCK_CLAIM_NEGATION =\n /(not\\s+(?:yet\\s+)?(?:complete|completed|done|finished|passing|passed|successful|success)|isn'?t\\s+(?:complete|done|finished)|still\\s+(?:failing|fails|broken|running)|没有通过|未通过|尚未完成|尚未通过|还没完成|还没通过|并未完成|仍然失败|仍有失败|还有失败)/i;\n\n/** A completion assertion in English or Chinese. */\nconst MOCK_CLAIM_WORD =\n /(\\b(?:pass|passed|passing|green|success|successful|succeed|succeeded|complete|completed|finished|done|verified|works)\\b|全部通过|所有测试通过|已完成|完成了|已通过|通过|成功|跑通|全绿|搞定)/i;\n\n/**\n * Classify one candidate sentence.\n * @param statement - one candidate sentence.\n * @returns whether it reads as a completion claim and with what certainty.\n */\nfunction mockClaimClassification(statement: string): ClaimClassification {\n if (MOCK_CLAIM_NEGATION.test(statement)) return { statement, isClaim: false, confidence: 0.96 };\n if (MOCK_CLAIM_WORD.test(statement)) return { statement, isClaim: true, confidence: 0.92 };\n return { statement, isClaim: false, confidence: 0.9 };\n}\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prompt rendering for one System 1 route decision.
|
|
3
|
+
*
|
|
4
|
+
* The affinity facts ({@link RouteRequest.state}) and the policy instructions
|
|
5
|
+
* ({@link RouteRequest.instructions}) are part of the decision *prompt*, not of
|
|
6
|
+
* the decision *result*: a backend that builds a text prompt (the logits
|
|
7
|
+
* backend, and the HTTP body of the Jev gateway) renders them with these pure
|
|
8
|
+
* helpers, so what the model reads is unit-testable without a network call.
|
|
9
|
+
*
|
|
10
|
+
* Rendering one request with no state and no instructions reproduces the
|
|
11
|
+
* historical `name: description` context block byte for byte, so an unchanged
|
|
12
|
+
* caller sees an unchanged prompt.
|
|
13
|
+
*
|
|
14
|
+
* @module harness-reflex/backend/route-prompt
|
|
15
|
+
*/
|
|
16
|
+
import type { RouteCandidateItem, RouteRequest, RouteRequestState } from './types.js';
|
|
17
|
+
/** How the incumbent candidate is marked inside a decision prompt. */
|
|
18
|
+
export declare const CURRENT_MODEL_MARK = "[current model \u2014 keeping it preserves the prompt cache]";
|
|
19
|
+
/**
|
|
20
|
+
* Render one candidate line, marking the session's incumbent model.
|
|
21
|
+
* @param candidate - one pool candidate.
|
|
22
|
+
* @returns `name: description`, plus the incumbent mark when the candidate is current.
|
|
23
|
+
*/
|
|
24
|
+
export declare function renderCandidateLine(candidate: RouteCandidateItem): string;
|
|
25
|
+
/**
|
|
26
|
+
* Render the session facts and the policy instructions of one decision.
|
|
27
|
+
* @param state - cache-affinity facts, when the caller supplied any.
|
|
28
|
+
* @param instructions - policy instructions, when the caller supplied any.
|
|
29
|
+
* @returns one line per fact; an empty array when there is nothing to say.
|
|
30
|
+
*/
|
|
31
|
+
export declare function renderRouteStateLines(state: RouteRequestState | undefined, instructions: string | undefined): string[];
|
|
32
|
+
/**
|
|
33
|
+
* Build the context block one System 1 route decision receives.
|
|
34
|
+
* @param req - the route request being decided.
|
|
35
|
+
* @returns candidate lines followed by the session facts and instructions.
|
|
36
|
+
*/
|
|
37
|
+
export declare function buildRouteContext(req: RouteRequest): string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** How the incumbent candidate is marked inside a decision prompt. */
|
|
2
|
+
export const CURRENT_MODEL_MARK = '[current model — keeping it preserves the prompt cache]';
|
|
3
|
+
/**
|
|
4
|
+
* Render one candidate line, marking the session's incumbent model.
|
|
5
|
+
* @param candidate - one pool candidate.
|
|
6
|
+
* @returns `name: description`, plus the incumbent mark when the candidate is current.
|
|
7
|
+
*/
|
|
8
|
+
export function renderCandidateLine(candidate) {
|
|
9
|
+
const mark = candidate.current ? ` ${CURRENT_MODEL_MARK}` : '';
|
|
10
|
+
return `${candidate.name}: ${candidate.description}${mark}`;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Render the session facts and the policy instructions of one decision.
|
|
14
|
+
* @param state - cache-affinity facts, when the caller supplied any.
|
|
15
|
+
* @param instructions - policy instructions, when the caller supplied any.
|
|
16
|
+
* @returns one line per fact; an empty array when there is nothing to say.
|
|
17
|
+
*/
|
|
18
|
+
export function renderRouteStateLines(state, instructions) {
|
|
19
|
+
const lines = [];
|
|
20
|
+
if (state?.currentModel)
|
|
21
|
+
lines.push(`current physical model: ${state.currentModel}`);
|
|
22
|
+
if (typeof state?.contextTokens === 'number' || typeof state?.contextChars === 'number') {
|
|
23
|
+
const tokens = typeof state?.contextTokens === 'number' ? state.contextTokens : 0;
|
|
24
|
+
const chars = typeof state?.contextChars === 'number' ? state.contextChars : 0;
|
|
25
|
+
lines.push(`accumulated session context: about ${String(tokens)} tokens (${String(chars)} characters)`);
|
|
26
|
+
}
|
|
27
|
+
const instruction = instructions?.trim();
|
|
28
|
+
if (instruction)
|
|
29
|
+
lines.push(instruction);
|
|
30
|
+
return lines;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Build the context block one System 1 route decision receives.
|
|
34
|
+
* @param req - the route request being decided.
|
|
35
|
+
* @returns candidate lines followed by the session facts and instructions.
|
|
36
|
+
*/
|
|
37
|
+
export function buildRouteContext(req) {
|
|
38
|
+
return [...req.candidates.map(renderCandidateLine), ...renderRouteStateLines(req.state, req.instructions)]
|
|
39
|
+
.filter((line) => line.length > 0)
|
|
40
|
+
.join('\n');
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=route-prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route-prompt.js","sourceRoot":"","sources":["../../src/backend/route-prompt.ts"],"names":[],"mappings":"AAiBA,sEAAsE;AACtE,MAAM,CAAC,MAAM,kBAAkB,GAAG,yDAAyD,CAAC;AAE5F;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,SAA6B;IAC/D,MAAM,IAAI,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,kBAAkB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAC/D,OAAO,GAAG,SAAS,CAAC,IAAI,KAAK,SAAS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B,CAAC","sourcesContent":["/**\n * Prompt rendering for one System 1 route decision.\n *\n * The affinity facts ({@link RouteRequest.state}) and the policy instructions\n * ({@link RouteRequest.instructions}) are part of the decision *prompt*, not of\n * the decision *result*: a backend that builds a text prompt (the logits\n * backend, and the HTTP body of the Jev gateway) renders them with these pure\n * helpers, so what the model reads is unit-testable without a network call.\n *\n * Rendering one request with no state and no instructions reproduces the\n * historical `name: description` context block byte for byte, so an unchanged\n * caller sees an unchanged prompt.\n *\n * @module harness-reflex/backend/route-prompt\n */\nimport type { RouteCandidateItem, RouteRequest, RouteRequestState } from './types.js';\n\n/** How the incumbent candidate is marked inside a decision prompt. */\nexport const CURRENT_MODEL_MARK = '[current model — keeping it preserves the prompt cache]';\n\n/**\n * Render one candidate line, marking the session's incumbent model.\n * @param candidate - one pool candidate.\n * @returns `name: description`, plus the incumbent mark when the candidate is current.\n */\nexport function renderCandidateLine(candidate: RouteCandidateItem): string {\n const mark = candidate.current ? ` ${CURRENT_MODEL_MARK}` : '';\n return `${candidate.name}: ${candidate.description}${mark}`;\n}\n\n/**\n * Render the session facts and the policy instructions of one decision.\n * @param state - cache-affinity facts, when the caller supplied any.\n * @param instructions - policy instructions, when the caller supplied any.\n * @returns one line per fact; an empty array when there is nothing to say.\n */\nexport function renderRouteStateLines(\n state: RouteRequestState | undefined,\n instructions: string | undefined\n): string[] {\n const lines: string[] = [];\n if (state?.currentModel) lines.push(`current physical model: ${state.currentModel}`);\n\n if (typeof state?.contextTokens === 'number' || typeof state?.contextChars === 'number') {\n const tokens = typeof state?.contextTokens === 'number' ? state.contextTokens : 0;\n const chars = typeof state?.contextChars === 'number' ? state.contextChars : 0;\n lines.push(`accumulated session context: about ${String(tokens)} tokens (${String(chars)} characters)`);\n }\n\n const instruction = instructions?.trim();\n if (instruction) lines.push(instruction);\n return lines;\n}\n\n/**\n * Build the context block one System 1 route decision receives.\n * @param req - the route request being decided.\n * @returns candidate lines followed by the session facts and instructions.\n */\nexport function buildRouteContext(req: RouteRequest): string {\n return [...req.candidates.map(renderCandidateLine), ...renderRouteStateLines(req.state, req.instructions)]\n .filter((line) => line.length > 0)\n .join('\\n');\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { DecisionBackend, ChoiceRequest, ChoiceResponse, GateRequest, GateResponse, PruneRequest, PruneResponse, StagnationCheckRequest, StagnationCheckResponse, RouteRequest, RouteResponse } from './types.js';
|
|
2
|
+
export interface SimpleJevConfig {
|
|
3
|
+
vllmBaseUrl?: string;
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
model?: string;
|
|
6
|
+
timeoutMs?: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Logits-based System One Provider (Zero-RLCD, Next-Token Logits Scoring via vLLM / OpenAI API)
|
|
10
|
+
*
|
|
11
|
+
* Implements System One decision API on top of standard open LLM weights (e.g. Qwen2.5 / DeepSeek)
|
|
12
|
+
* using next-token logprobs with max_tokens: 1.
|
|
13
|
+
*/
|
|
14
|
+
export declare class LogitsSystemOneBackend implements DecisionBackend {
|
|
15
|
+
readonly name = "system-one-logits";
|
|
16
|
+
readonly model: string;
|
|
17
|
+
private readonly baseUrl;
|
|
18
|
+
private readonly apiKey;
|
|
19
|
+
private readonly timeoutMs;
|
|
20
|
+
constructor(config?: SimpleJevConfig);
|
|
21
|
+
/**
|
|
22
|
+
* Request next-token logprobs from vLLM /v1/completions
|
|
23
|
+
*/
|
|
24
|
+
private getNextTokenLogprobs;
|
|
25
|
+
/**
|
|
26
|
+
* Normalizes logprobs across target token labels via Softmax
|
|
27
|
+
*/
|
|
28
|
+
private computeNormalizedProbabilities;
|
|
29
|
+
choice(req: ChoiceRequest): Promise<ChoiceResponse>;
|
|
30
|
+
gate(req: GateRequest): Promise<GateResponse>;
|
|
31
|
+
prune(req: PruneRequest): Promise<PruneResponse>;
|
|
32
|
+
checkStagnation(req: StagnationCheckRequest): Promise<StagnationCheckResponse>;
|
|
33
|
+
route(req: RouteRequest): Promise<RouteResponse>;
|
|
34
|
+
}
|
|
35
|
+
export declare const SimpleJevDecisionBackend: typeof LogitsSystemOneBackend;
|