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,273 @@
|
|
|
1
|
+
import { buildRouteContext } from './route-prompt.js';
|
|
2
|
+
import { LOGITS_DEFAULT_API_KEY, LOGITS_DEFAULT_BASE_URL, LOGITS_DEFAULT_MODEL, LOGITS_DEFAULT_TIMEOUT_MS } from './defaults.js';
|
|
3
|
+
/**
|
|
4
|
+
* Logits-based System One Provider (Zero-RLCD, Next-Token Logits Scoring via vLLM / OpenAI API)
|
|
5
|
+
*
|
|
6
|
+
* Implements System One decision API on top of standard open LLM weights (e.g. Qwen2.5 / DeepSeek)
|
|
7
|
+
* using next-token logprobs with max_tokens: 1.
|
|
8
|
+
*/
|
|
9
|
+
export class LogitsSystemOneBackend {
|
|
10
|
+
name = 'system-one-logits';
|
|
11
|
+
model;
|
|
12
|
+
baseUrl;
|
|
13
|
+
apiKey;
|
|
14
|
+
timeoutMs;
|
|
15
|
+
constructor(config) {
|
|
16
|
+
this.baseUrl = (config?.vllmBaseUrl ||
|
|
17
|
+
process.env.SYSTEM_ONE_BASE_URL ||
|
|
18
|
+
process.env.VLLM_BASE_URL ||
|
|
19
|
+
process.env.OPENAI_BASE_URL ||
|
|
20
|
+
LOGITS_DEFAULT_BASE_URL).replace(/\/$/, '');
|
|
21
|
+
this.apiKey =
|
|
22
|
+
config?.apiKey ||
|
|
23
|
+
process.env.SYSTEM_ONE_API_KEY ||
|
|
24
|
+
process.env.VLLM_API_KEY ||
|
|
25
|
+
process.env.OPENAI_API_KEY ||
|
|
26
|
+
LOGITS_DEFAULT_API_KEY;
|
|
27
|
+
this.model =
|
|
28
|
+
config?.model ||
|
|
29
|
+
process.env.SYSTEM_ONE_MODEL ||
|
|
30
|
+
process.env.VLLM_MODEL ||
|
|
31
|
+
LOGITS_DEFAULT_MODEL;
|
|
32
|
+
this.timeoutMs = config?.timeoutMs || LOGITS_DEFAULT_TIMEOUT_MS;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Request next-token logprobs from vLLM /v1/completions
|
|
36
|
+
*/
|
|
37
|
+
async getNextTokenLogprobs(prompt, topLogprobs = 20) {
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
40
|
+
try {
|
|
41
|
+
const resp = await fetch(`${this.baseUrl}/v1/completions`, {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
headers: {
|
|
44
|
+
'Content-Type': 'application/json',
|
|
45
|
+
Authorization: `Bearer ${this.apiKey}`
|
|
46
|
+
},
|
|
47
|
+
body: JSON.stringify({
|
|
48
|
+
model: this.model,
|
|
49
|
+
prompt,
|
|
50
|
+
max_tokens: 1,
|
|
51
|
+
temperature: 0.0,
|
|
52
|
+
logprobs: topLogprobs
|
|
53
|
+
}),
|
|
54
|
+
signal: controller.signal
|
|
55
|
+
});
|
|
56
|
+
if (!resp.ok) {
|
|
57
|
+
throw new Error(`vLLM endpoint error: ${resp.status} ${resp.statusText}`);
|
|
58
|
+
}
|
|
59
|
+
const data = await resp.json();
|
|
60
|
+
const firstChoice = data?.choices?.[0];
|
|
61
|
+
const logprobsObj = firstChoice?.logprobs?.top_logprobs?.[0] || {};
|
|
62
|
+
return logprobsObj;
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
clearTimeout(timer);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Normalizes logprobs across target token labels via Softmax
|
|
70
|
+
*/
|
|
71
|
+
computeNormalizedProbabilities(logprobsMap, labels) {
|
|
72
|
+
// Extract logprobs for each label (accounting for leading spaces)
|
|
73
|
+
const logValues = labels.map((label) => {
|
|
74
|
+
let maxLog = -Infinity;
|
|
75
|
+
for (const [token, logp] of Object.entries(logprobsMap)) {
|
|
76
|
+
if (token.trim().toLowerCase() === label.trim().toLowerCase()) {
|
|
77
|
+
if (logp > maxLog)
|
|
78
|
+
maxLog = logp;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return maxLog === -Infinity ? -20 : maxLog; // baseline floor for unseen tokens
|
|
82
|
+
});
|
|
83
|
+
const maxLogVal = Math.max(...logValues);
|
|
84
|
+
const expValues = logValues.map((v) => Math.exp(v - maxLogVal));
|
|
85
|
+
const sumExp = expValues.reduce((a, b) => a + b, 0);
|
|
86
|
+
return labels.map((label, idx) => ({
|
|
87
|
+
label,
|
|
88
|
+
probability: Number((expValues[idx] / sumExp).toFixed(4))
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
async choice(req) {
|
|
92
|
+
const start = performance.now();
|
|
93
|
+
const optionsLetters = req.options.map((_, i) => String.fromCharCode(65 + i)); // A, B, C...
|
|
94
|
+
const prompt = `Context: ${req.context || 'None'}
|
|
95
|
+
Question: ${req.question}
|
|
96
|
+
Options:
|
|
97
|
+
${req.options.map((opt, i) => `${optionsLetters[i]}. ${opt}`).join('\n')}
|
|
98
|
+
|
|
99
|
+
Answer with the single letter of the best option:`;
|
|
100
|
+
try {
|
|
101
|
+
const topLogprobs = await this.getNextTokenLogprobs(prompt, 20);
|
|
102
|
+
const probabilities = this.computeNormalizedProbabilities(topLogprobs, optionsLetters);
|
|
103
|
+
const mappedOptions = req.options.map((opt, i) => ({
|
|
104
|
+
label: opt,
|
|
105
|
+
probability: probabilities[i].probability
|
|
106
|
+
}));
|
|
107
|
+
mappedOptions.sort((a, b) => b.probability - a.probability);
|
|
108
|
+
return {
|
|
109
|
+
options: mappedOptions,
|
|
110
|
+
bestOption: mappedOptions[0].label,
|
|
111
|
+
confidence: mappedOptions[0].probability,
|
|
112
|
+
uncalibrated: true,
|
|
113
|
+
latencyMs: Math.round(performance.now() - start)
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
throw new Error(`[SimpleJevBackend.choice] Failed: ${err.message}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async gate(req) {
|
|
121
|
+
const start = performance.now();
|
|
122
|
+
const results = [];
|
|
123
|
+
let allPassed = true;
|
|
124
|
+
let needsEscalation = false;
|
|
125
|
+
try {
|
|
126
|
+
for (const assertion of req.assertions) {
|
|
127
|
+
const prompt = `Evidence:
|
|
128
|
+
"""
|
|
129
|
+
${req.evidence.slice(0, 3000)}
|
|
130
|
+
"""
|
|
131
|
+
|
|
132
|
+
Statement: "${assertion.statement}"
|
|
133
|
+
|
|
134
|
+
Is this statement strictly verified by the evidence?
|
|
135
|
+
Options:
|
|
136
|
+
A. Yes (Verified)
|
|
137
|
+
B. No (Contradicted or False)
|
|
138
|
+
C. Unknown (Not addressed in evidence)
|
|
139
|
+
|
|
140
|
+
Best Answer (A, B, or C):`;
|
|
141
|
+
const logprobs = await this.getNextTokenLogprobs(prompt, 10);
|
|
142
|
+
const probs = this.computeNormalizedProbabilities(logprobs, ['A', 'B', 'C']);
|
|
143
|
+
const top = probs.sort((a, b) => b.probability - a.probability)[0];
|
|
144
|
+
let outcome = 'not_addressed';
|
|
145
|
+
if (top.label === 'A')
|
|
146
|
+
outcome = 'verified';
|
|
147
|
+
else if (top.label === 'B')
|
|
148
|
+
outcome = 'contradicted';
|
|
149
|
+
else
|
|
150
|
+
outcome = 'not_addressed';
|
|
151
|
+
if (outcome !== 'verified') {
|
|
152
|
+
allPassed = false;
|
|
153
|
+
}
|
|
154
|
+
results.push({
|
|
155
|
+
statement: assertion.statement,
|
|
156
|
+
outcome,
|
|
157
|
+
confidence: top.probability,
|
|
158
|
+
explanation: `Classified via token logits: P(${top.label}) = ${top.probability}`
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
passed: allPassed,
|
|
163
|
+
results,
|
|
164
|
+
needsEscalation,
|
|
165
|
+
latencyMs: Math.round(performance.now() - start)
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
// Fail-closed to escalate
|
|
170
|
+
return {
|
|
171
|
+
passed: false,
|
|
172
|
+
needsEscalation: true,
|
|
173
|
+
results: req.assertions.map((a) => ({
|
|
174
|
+
statement: a.statement,
|
|
175
|
+
outcome: 'escalate',
|
|
176
|
+
confidence: 0,
|
|
177
|
+
explanation: `SimpleJev backend failed: ${err.message}. Fail-closed escalation.`
|
|
178
|
+
})),
|
|
179
|
+
latencyMs: Math.round(performance.now() - start)
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async prune(req) {
|
|
184
|
+
const start = performance.now();
|
|
185
|
+
// In Simple-Jev, line scoring can be done by heuristic filter + teacher-forcing scoring
|
|
186
|
+
// Here we provide high-throughput line extraction
|
|
187
|
+
const raw = req.rawOutput;
|
|
188
|
+
const lines = raw.split('\n');
|
|
189
|
+
const intentLower = req.intent.toLowerCase();
|
|
190
|
+
const selectedLines = lines.filter((line) => {
|
|
191
|
+
const lower = line.toLowerCase();
|
|
192
|
+
if (lower.includes('error') || lower.includes('fail') || lower.includes('warning'))
|
|
193
|
+
return true;
|
|
194
|
+
if (lower.includes('passed') || lower.includes('complete') || lower.includes('status'))
|
|
195
|
+
return true;
|
|
196
|
+
return intentLower.split(/\s+/).some((term) => term.length > 3 && lower.includes(term));
|
|
197
|
+
});
|
|
198
|
+
const prunedOutput = selectedLines.length > 0 ? selectedLines.join('\n') : lines.slice(0, 30).join('\n');
|
|
199
|
+
const originalChars = raw.length;
|
|
200
|
+
const prunedChars = prunedOutput.length;
|
|
201
|
+
const reduction = originalChars > 0 ? Number((((originalChars - prunedChars) / originalChars) * 100).toFixed(1)) : 0;
|
|
202
|
+
const tokensSaved = Math.max(0, Math.ceil((originalChars - prunedChars) / 4));
|
|
203
|
+
return {
|
|
204
|
+
processedOutput: req.shadowMode ? raw : prunedOutput,
|
|
205
|
+
originalChars,
|
|
206
|
+
prunedChars,
|
|
207
|
+
estimatedTokensSaved: tokensSaved,
|
|
208
|
+
reductionPercentage: reduction,
|
|
209
|
+
shadow: !!req.shadowMode,
|
|
210
|
+
latencyMs: Math.round(performance.now() - start)
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
async checkStagnation(req) {
|
|
214
|
+
const start = performance.now();
|
|
215
|
+
const history = req.history || [];
|
|
216
|
+
if (history.length < 3) {
|
|
217
|
+
return {
|
|
218
|
+
isStuck: false,
|
|
219
|
+
confidence: 0.9,
|
|
220
|
+
diagnosis: 'Action history short.',
|
|
221
|
+
latencyMs: Math.round(performance.now() - start)
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
const summary = history.slice(-5).map((h, i) => `${i + 1}. Tool: ${h.tool} | Input: ${h.input.slice(0, 80)} | Output: ${(h.output || h.error || '').slice(0, 80)}`).join('\n');
|
|
225
|
+
const prompt = `Inspect this recent agent execution trajectory:
|
|
226
|
+
${summary}
|
|
227
|
+
|
|
228
|
+
Is the agent stuck in an unproductive loop (repeating failing actions or oscillating without progress)?
|
|
229
|
+
Options:
|
|
230
|
+
A. Yes, stuck
|
|
231
|
+
B. No, making progress
|
|
232
|
+
|
|
233
|
+
Answer (A or B):`;
|
|
234
|
+
try {
|
|
235
|
+
const logprobs = await this.getNextTokenLogprobs(prompt, 5);
|
|
236
|
+
const probs = this.computeNormalizedProbabilities(logprobs, ['A', 'B']);
|
|
237
|
+
const aProb = probs.find((p) => p.label === 'A')?.probability ?? 0;
|
|
238
|
+
const isStuck = aProb > 0.6;
|
|
239
|
+
return {
|
|
240
|
+
isStuck,
|
|
241
|
+
patternType: isStuck ? 'repetition' : 'none',
|
|
242
|
+
confidence: probs[0].probability,
|
|
243
|
+
diagnosis: isStuck ? 'Logits classified execution trajectory as unproductive repeating loop.' : 'Trajectory classified as progressive.',
|
|
244
|
+
recommendedIntervention: isStuck ? 'Stop repeating this command. Change approach or review inputs.' : undefined,
|
|
245
|
+
latencyMs: Math.round(performance.now() - start)
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
return {
|
|
250
|
+
isStuck: false,
|
|
251
|
+
confidence: 0.5,
|
|
252
|
+
diagnosis: `SimpleJev stagnation check failed: ${err.message}`,
|
|
253
|
+
latencyMs: Math.round(performance.now() - start)
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
async route(req) {
|
|
258
|
+
const start = performance.now();
|
|
259
|
+
const options = req.candidates.map((c) => c.name);
|
|
260
|
+
const choiceRes = await this.choice({
|
|
261
|
+
question: `Which tool or skill is best suited to achieve the goal: "${req.goal}"?`,
|
|
262
|
+
options,
|
|
263
|
+
context: buildRouteContext(req)
|
|
264
|
+
});
|
|
265
|
+
return {
|
|
266
|
+
rankedCandidates: choiceRes.options.map((o) => ({ name: o.label, score: o.probability })),
|
|
267
|
+
bestCandidate: choiceRes.bestOption,
|
|
268
|
+
latencyMs: Math.round(performance.now() - start)
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
export const SimpleJevDecisionBackend = LogitsSystemOneBackend;
|
|
273
|
+
//# sourceMappingURL=simple-jev.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simple-jev.js","sourceRoot":"","sources":["../../src/backend/simple-jev.ts"],"names":[],"mappings":"AAa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,CAAC;SAChC,CAAC,CAAC;QAEH,OAAO;YACL,gBAAgB,EAAE,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;YACzF,aAAa,EAAE,SAAS,CAAC,UAAU;YACnC,SAAS,EAAE,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;SACjD,CAAC;IACJ,CAAC;CACF;AAED,MAAM,CAAC,MAAM,wBAAwB,GAAG,sBAAsB,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} from './types.js';\nimport { buildRouteContext } from './route-prompt.js';\nimport {\n LOGITS_DEFAULT_API_KEY,\n LOGITS_DEFAULT_BASE_URL,\n LOGITS_DEFAULT_MODEL,\n LOGITS_DEFAULT_TIMEOUT_MS\n} from './defaults.js';\n\nexport interface SimpleJevConfig {\n vllmBaseUrl?: string;\n apiKey?: string;\n model?: string;\n timeoutMs?: number;\n}\n\n/**\n * Logits-based System One Provider (Zero-RLCD, Next-Token Logits Scoring via vLLM / OpenAI API)\n *\n * Implements System One decision API on top of standard open LLM weights (e.g. Qwen2.5 / DeepSeek)\n * using next-token logprobs with max_tokens: 1.\n */\nexport class LogitsSystemOneBackend implements DecisionBackend {\n readonly name = 'system-one-logits';\n readonly model: string;\n private readonly baseUrl: string;\n private readonly apiKey: string;\n private readonly timeoutMs: number;\n\n constructor(config?: SimpleJevConfig) {\n this.baseUrl = (\n config?.vllmBaseUrl ||\n process.env.SYSTEM_ONE_BASE_URL ||\n process.env.VLLM_BASE_URL ||\n process.env.OPENAI_BASE_URL ||\n LOGITS_DEFAULT_BASE_URL\n ).replace(/\\/$/, '');\n this.apiKey =\n config?.apiKey ||\n process.env.SYSTEM_ONE_API_KEY ||\n process.env.VLLM_API_KEY ||\n process.env.OPENAI_API_KEY ||\n LOGITS_DEFAULT_API_KEY;\n this.model =\n config?.model ||\n process.env.SYSTEM_ONE_MODEL ||\n process.env.VLLM_MODEL ||\n LOGITS_DEFAULT_MODEL;\n this.timeoutMs = config?.timeoutMs || LOGITS_DEFAULT_TIMEOUT_MS;\n }\n\n /**\n * Request next-token logprobs from vLLM /v1/completions\n */\n private async getNextTokenLogprobs(prompt: string, topLogprobs: number = 20): Promise<Record<string, number>> {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), this.timeoutMs);\n\n try {\n const resp = await fetch(`${this.baseUrl}/v1/completions`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n Authorization: `Bearer ${this.apiKey}`\n },\n body: JSON.stringify({\n model: this.model,\n prompt,\n max_tokens: 1,\n temperature: 0.0,\n logprobs: topLogprobs\n }),\n signal: controller.signal\n });\n\n if (!resp.ok) {\n throw new Error(`vLLM endpoint error: ${resp.status} ${resp.statusText}`);\n }\n\n const data = await resp.json() as any;\n const firstChoice = data?.choices?.[0];\n const logprobsObj = firstChoice?.logprobs?.top_logprobs?.[0] || {};\n return logprobsObj;\n } finally {\n clearTimeout(timer);\n }\n }\n\n /**\n * Normalizes logprobs across target token labels via Softmax\n */\n private computeNormalizedProbabilities(\n logprobsMap: Record<string, number>,\n labels: string[]\n ): Array<{ label: string; probability: number }> {\n // Extract logprobs for each label (accounting for leading spaces)\n const logValues = labels.map((label) => {\n let maxLog = -Infinity;\n for (const [token, logp] of Object.entries(logprobsMap)) {\n if (token.trim().toLowerCase() === label.trim().toLowerCase()) {\n if (logp > maxLog) maxLog = logp;\n }\n }\n return maxLog === -Infinity ? -20 : maxLog; // baseline floor for unseen tokens\n });\n\n const maxLogVal = Math.max(...logValues);\n const expValues = logValues.map((v) => Math.exp(v - maxLogVal));\n const sumExp = expValues.reduce((a, b) => a + b, 0);\n\n return labels.map((label, idx) => ({\n label,\n probability: Number((expValues[idx] / sumExp).toFixed(4))\n }));\n }\n\n async choice(req: ChoiceRequest): Promise<ChoiceResponse> {\n const start = performance.now();\n const optionsLetters = req.options.map((_, i) => String.fromCharCode(65 + i)); // A, B, C...\n\n const prompt = `Context: ${req.context || 'None'}\nQuestion: ${req.question}\nOptions:\n${req.options.map((opt, i) => `${optionsLetters[i]}. ${opt}`).join('\\n')}\n\nAnswer with the single letter of the best option:`;\n\n try {\n const topLogprobs = await this.getNextTokenLogprobs(prompt, 20);\n const probabilities = this.computeNormalizedProbabilities(topLogprobs, optionsLetters);\n\n const mappedOptions = req.options.map((opt, i) => ({\n label: opt,\n probability: probabilities[i].probability\n }));\n\n mappedOptions.sort((a, b) => b.probability - a.probability);\n\n return {\n options: mappedOptions,\n bestOption: mappedOptions[0].label,\n confidence: mappedOptions[0].probability,\n uncalibrated: true,\n latencyMs: Math.round(performance.now() - start)\n };\n } catch (err: any) {\n throw new Error(`[SimpleJevBackend.choice] Failed: ${err.message}`);\n }\n }\n\n async gate(req: GateRequest): Promise<GateResponse> {\n const start = performance.now();\n const results = [];\n let allPassed = true;\n let needsEscalation = false;\n\n try {\n for (const assertion of req.assertions) {\n const prompt = `Evidence:\n\"\"\"\n${req.evidence.slice(0, 3000)}\n\"\"\"\n\nStatement: \"${assertion.statement}\"\n\nIs this statement strictly verified by the evidence?\nOptions:\nA. Yes (Verified)\nB. No (Contradicted or False)\nC. Unknown (Not addressed in evidence)\n\nBest Answer (A, B, or C):`;\n\n const logprobs = await this.getNextTokenLogprobs(prompt, 10);\n const probs = this.computeNormalizedProbabilities(logprobs, ['A', 'B', 'C']);\n const top = probs.sort((a, b) => b.probability - a.probability)[0];\n\n let outcome: 'verified' | 'contradicted' | 'not_addressed' | 'escalate' = 'not_addressed';\n if (top.label === 'A') outcome = 'verified';\n else if (top.label === 'B') outcome = 'contradicted';\n else outcome = 'not_addressed';\n\n if (outcome !== 'verified') {\n allPassed = false;\n }\n\n results.push({\n statement: assertion.statement,\n outcome,\n confidence: top.probability,\n explanation: `Classified via token logits: P(${top.label}) = ${top.probability}`\n });\n }\n\n return {\n passed: allPassed,\n results,\n needsEscalation,\n latencyMs: Math.round(performance.now() - start)\n };\n } catch (err: any) {\n // Fail-closed to escalate\n return {\n passed: false,\n needsEscalation: true,\n results: req.assertions.map((a) => ({\n statement: a.statement,\n outcome: 'escalate' as const,\n confidence: 0,\n explanation: `SimpleJev backend failed: ${err.message}. Fail-closed escalation.`\n })),\n latencyMs: Math.round(performance.now() - start)\n };\n }\n }\n\n async prune(req: PruneRequest): Promise<PruneResponse> {\n const start = performance.now();\n // In Simple-Jev, line scoring can be done by heuristic filter + teacher-forcing scoring\n // Here we provide high-throughput line extraction\n const raw = req.rawOutput;\n const lines = raw.split('\\n');\n const intentLower = req.intent.toLowerCase();\n\n const selectedLines = lines.filter((line) => {\n const lower = line.toLowerCase();\n if (lower.includes('error') || lower.includes('fail') || lower.includes('warning')) return true;\n if (lower.includes('passed') || lower.includes('complete') || lower.includes('status')) return true;\n return intentLower.split(/\\s+/).some((term) => term.length > 3 && lower.includes(term));\n });\n\n const prunedOutput = selectedLines.length > 0 ? selectedLines.join('\\n') : lines.slice(0, 30).join('\\n');\n const originalChars = raw.length;\n const prunedChars = prunedOutput.length;\n const reduction = originalChars > 0 ? Number((((originalChars - prunedChars) / originalChars) * 100).toFixed(1)) : 0;\n const tokensSaved = Math.max(0, Math.ceil((originalChars - prunedChars) / 4));\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 if (history.length < 3) {\n return {\n isStuck: false,\n confidence: 0.9,\n diagnosis: 'Action history short.',\n latencyMs: Math.round(performance.now() - start)\n };\n }\n\n const summary = history.slice(-5).map((h, i) => `${i + 1}. Tool: ${h.tool} | Input: ${h.input.slice(0, 80)} | Output: ${(h.output || h.error || '').slice(0, 80)}`).join('\\n');\n const prompt = `Inspect this recent agent execution trajectory:\n${summary}\n\nIs the agent stuck in an unproductive loop (repeating failing actions or oscillating without progress)?\nOptions:\nA. Yes, stuck\nB. No, making progress\n\nAnswer (A or B):`;\n\n try {\n const logprobs = await this.getNextTokenLogprobs(prompt, 5);\n const probs = this.computeNormalizedProbabilities(logprobs, ['A', 'B']);\n const aProb = probs.find((p) => p.label === 'A')?.probability ?? 0;\n const isStuck = aProb > 0.6;\n\n return {\n isStuck,\n patternType: isStuck ? 'repetition' : 'none',\n confidence: probs[0].probability,\n diagnosis: isStuck ? 'Logits classified execution trajectory as unproductive repeating loop.' : 'Trajectory classified as progressive.',\n recommendedIntervention: isStuck ? 'Stop repeating this command. Change approach or review inputs.' : undefined,\n latencyMs: Math.round(performance.now() - start)\n };\n } catch (err: any) {\n return {\n isStuck: false,\n confidence: 0.5,\n diagnosis: `SimpleJev stagnation check failed: ${err.message}`,\n latencyMs: Math.round(performance.now() - start)\n };\n }\n }\n\n async route(req: RouteRequest): Promise<RouteResponse> {\n const start = performance.now();\n const options = req.candidates.map((c) => c.name);\n const choiceRes = await this.choice({\n question: `Which tool or skill is best suited to achieve the goal: \"${req.goal}\"?`,\n options,\n context: buildRouteContext(req)\n });\n\n return {\n rankedCandidates: choiceRes.options.map((o) => ({ name: o.label, score: o.probability })),\n bestCandidate: choiceRes.bestOption,\n latencyMs: Math.round(performance.now() - start)\n };\n }\n}\n\nexport const SimpleJevDecisionBackend = LogitsSystemOneBackend;\n\n"]}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for System One Decision Runtime & APIs (Jev, vLLM/Simple-Jev, DSPy, etc.)
|
|
3
|
+
*/
|
|
4
|
+
export interface ChoiceOption {
|
|
5
|
+
label: string;
|
|
6
|
+
probability: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ChoiceRequest {
|
|
9
|
+
question: string;
|
|
10
|
+
options: string[];
|
|
11
|
+
context?: string;
|
|
12
|
+
temperature?: number;
|
|
13
|
+
}
|
|
14
|
+
export interface ChoiceResponse {
|
|
15
|
+
options: ChoiceOption[];
|
|
16
|
+
bestOption: string;
|
|
17
|
+
confidence: number;
|
|
18
|
+
uncalibrated?: boolean;
|
|
19
|
+
latencyMs: number;
|
|
20
|
+
}
|
|
21
|
+
export type GateOutcome = 'verified' | 'contradicted' | 'not_addressed' | 'escalate';
|
|
22
|
+
export interface GateAssertion {
|
|
23
|
+
statement: string;
|
|
24
|
+
criteria?: string;
|
|
25
|
+
}
|
|
26
|
+
export interface GateRequest {
|
|
27
|
+
assertions: GateAssertion[];
|
|
28
|
+
evidence: string;
|
|
29
|
+
context?: string;
|
|
30
|
+
strictness?: 'high' | 'normal' | 'lenient';
|
|
31
|
+
}
|
|
32
|
+
export interface GateResultItem {
|
|
33
|
+
statement: string;
|
|
34
|
+
outcome: GateOutcome;
|
|
35
|
+
confidence: number;
|
|
36
|
+
explanation: string;
|
|
37
|
+
}
|
|
38
|
+
export interface GateResponse {
|
|
39
|
+
passed: boolean;
|
|
40
|
+
results: GateResultItem[];
|
|
41
|
+
needsEscalation: boolean;
|
|
42
|
+
latencyMs: number;
|
|
43
|
+
}
|
|
44
|
+
export interface PruneRequest {
|
|
45
|
+
rawOutput: string;
|
|
46
|
+
intent: string;
|
|
47
|
+
maxTokens?: number;
|
|
48
|
+
shadowMode?: boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Character count from which the backend judges segments through System One.
|
|
51
|
+
* The caller's resolved `interventions.pruneJudgeMinChars`; when omitted the
|
|
52
|
+
* backend's own configuration/environment/default applies.
|
|
53
|
+
*/
|
|
54
|
+
pruneJudgeMinChars?: number;
|
|
55
|
+
}
|
|
56
|
+
export interface PruneResponse {
|
|
57
|
+
processedOutput: string;
|
|
58
|
+
originalChars: number;
|
|
59
|
+
prunedChars: number;
|
|
60
|
+
estimatedTokensSaved: number;
|
|
61
|
+
reductionPercentage: number;
|
|
62
|
+
shadow: boolean;
|
|
63
|
+
latencyMs: number;
|
|
64
|
+
/**
|
|
65
|
+
* Which judging path produced the candidate: `segmented` ran the per-segment
|
|
66
|
+
* System One scoring for a payload at/above `interventions.pruneJudgeMinChars`;
|
|
67
|
+
* `local` used the deterministic candidates (small payload, or a fallback).
|
|
68
|
+
* Optional so existing response literals stay valid.
|
|
69
|
+
*/
|
|
70
|
+
judgePath?: 'segmented' | 'local' | 'fallback';
|
|
71
|
+
/** Why the segmented path fell back to the local candidates, when it did. */
|
|
72
|
+
fallbackReason?: string;
|
|
73
|
+
/**
|
|
74
|
+
* How the returned text was reduced, when it was.
|
|
75
|
+
*
|
|
76
|
+
* `none` means the returned text is the payload: a judgment that ran scored
|
|
77
|
+
* every segment and kept everything (honoured as-is), or the degraded path's
|
|
78
|
+
* template compression could not reduce it either. `fallbackReason` and
|
|
79
|
+
* `judgePath` tell the two apart.
|
|
80
|
+
*/
|
|
81
|
+
compression?: 'segmented' | 'local' | 'template' | 'none';
|
|
82
|
+
/** Segments handed to System One in one request (0 when the local path ran). */
|
|
83
|
+
judgedSegments?: number;
|
|
84
|
+
/** Segments beyond the per-request limit, kept deterministically instead. */
|
|
85
|
+
unjudgedSegments?: number;
|
|
86
|
+
/**
|
|
87
|
+
* Segments the composed scored candidate kept whole, out of all segments.
|
|
88
|
+
*
|
|
89
|
+
* The count belongs to the candidate that was installed; when no scored
|
|
90
|
+
* candidate reduced the payload it is the balanced candidate's count, which is
|
|
91
|
+
* what explains "the judge kept five segments and nothing shrank". It counts
|
|
92
|
+
* the segments kept without a score too, which is why
|
|
93
|
+
* {@link autoKeptSegments} is reported beside it. Absent on paths that never
|
|
94
|
+
* selected segments by score (local, or a judgment with no usable score).
|
|
95
|
+
*/
|
|
96
|
+
keptSegments?: number;
|
|
97
|
+
/** Characters of the aggressive-budget candidate, before one is installed. */
|
|
98
|
+
aggressiveChars?: number;
|
|
99
|
+
/** Characters of the balanced-budget candidate, before one is installed. */
|
|
100
|
+
balancedChars?: number;
|
|
101
|
+
/** Characters of the conservative-budget candidate, before one is installed. */
|
|
102
|
+
conservativeChars?: number;
|
|
103
|
+
/**
|
|
104
|
+
* Segments System One really scored (`scores.size`).
|
|
105
|
+
*
|
|
106
|
+
* `judgedSegments` counts the questions sent; this counts the answers that
|
|
107
|
+
* were usable. A gap between the two is a judgment that could not be read.
|
|
108
|
+
*/
|
|
109
|
+
scoredSegments?: number;
|
|
110
|
+
/**
|
|
111
|
+
* Segments kept whole because they carried no score.
|
|
112
|
+
*
|
|
113
|
+
* They are the unjudged safety margin (no answer, an unreadable answer, or a
|
|
114
|
+
* segment beyond the per-request limit), counted over the whole payload. When
|
|
115
|
+
* this is close to the segment count the candidate cannot shrink through the
|
|
116
|
+
* scored budgets, because almost nothing was judged.
|
|
117
|
+
*/
|
|
118
|
+
autoKeptSegments?: number;
|
|
119
|
+
/** Answers returned for a judged segment that carried no usable keep score. */
|
|
120
|
+
unparsedScoreAnswers?: number;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Diagnostic fields of a prune response.
|
|
124
|
+
*
|
|
125
|
+
* Every path reports what it measured and omits the rest, so a reader can tell
|
|
126
|
+
* a field that was not measured from one that measured zero.
|
|
127
|
+
*/
|
|
128
|
+
export type PruneDiagnostics = Pick<PruneResponse, 'judgePath' | 'fallbackReason' | 'compression' | 'judgedSegments' | 'unjudgedSegments' | 'keptSegments' | 'aggressiveChars' | 'balancedChars' | 'conservativeChars' | 'scoredSegments' | 'autoKeptSegments' | 'unparsedScoreAnswers'>;
|
|
129
|
+
/**
|
|
130
|
+
* In-memory health of one System One backend.
|
|
131
|
+
*
|
|
132
|
+
* Every request the backend sends updates it: a success clears the failure
|
|
133
|
+
* fields, a failure records when and why it happened and counts the run. It is
|
|
134
|
+
* deliberately not persisted — a restarted Host starts healthy and discovers
|
|
135
|
+
* the truth with its first call.
|
|
136
|
+
*/
|
|
137
|
+
export interface BackendHealth {
|
|
138
|
+
/** Whether the most recent request succeeded (true before the first request). */
|
|
139
|
+
available: boolean;
|
|
140
|
+
/** Reason of the most recent failure; cleared by a success. */
|
|
141
|
+
lastFailureReason?: string;
|
|
142
|
+
/** ISO timestamp of the most recent failure; cleared by a success. */
|
|
143
|
+
lastFailureAt?: string;
|
|
144
|
+
/** Consecutive failed requests; a single success resets it to 0. */
|
|
145
|
+
consecutiveFailures: number;
|
|
146
|
+
}
|
|
147
|
+
export interface ActionStep {
|
|
148
|
+
tool: string;
|
|
149
|
+
input: string;
|
|
150
|
+
output?: string;
|
|
151
|
+
error?: string;
|
|
152
|
+
timestamp?: number;
|
|
153
|
+
}
|
|
154
|
+
export interface StagnationCheckRequest {
|
|
155
|
+
history: ActionStep[];
|
|
156
|
+
goal?: string;
|
|
157
|
+
windowSize?: number;
|
|
158
|
+
}
|
|
159
|
+
export interface StagnationCheckResponse {
|
|
160
|
+
isStuck: boolean;
|
|
161
|
+
patternType?: 'repetition' | 'oscillation' | 'failing_retry' | 'zero_progress' | 'none';
|
|
162
|
+
confidence: number;
|
|
163
|
+
diagnosis?: string;
|
|
164
|
+
recommendedIntervention?: string;
|
|
165
|
+
latencyMs: number;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Session facts one routing decision reasons about.
|
|
169
|
+
*
|
|
170
|
+
* They exist for prompt-cache affinity: switching a session onto another
|
|
171
|
+
* physical model invalidates the upstream prompt prefix cache, so the decision
|
|
172
|
+
* needs to know which model holds the cached prefix and how much context is
|
|
173
|
+
* already in it. Every field is optional — a caller that knows nothing about
|
|
174
|
+
* the session (a hand-built one-shot call) simply omits the block.
|
|
175
|
+
*/
|
|
176
|
+
export interface RouteRequestState {
|
|
177
|
+
/** Physical `provider:model` the session is currently using, when known. */
|
|
178
|
+
currentModel?: string;
|
|
179
|
+
/** Estimated tokens of the session context accumulated so far. */
|
|
180
|
+
contextTokens?: number;
|
|
181
|
+
/** Character count the token estimate was derived from. */
|
|
182
|
+
contextChars?: number;
|
|
183
|
+
}
|
|
184
|
+
/** One candidate of a {@link RouteRequest}. */
|
|
185
|
+
export interface RouteCandidateItem {
|
|
186
|
+
name: string;
|
|
187
|
+
description: string;
|
|
188
|
+
type?: 'tool' | 'skill' | 'action';
|
|
189
|
+
/**
|
|
190
|
+
* Whether this candidate is the physical model the session already uses.
|
|
191
|
+
* Returning it costs no cache miss; a backend that builds a prompt should
|
|
192
|
+
* say so next to the candidate.
|
|
193
|
+
*/
|
|
194
|
+
current?: boolean;
|
|
195
|
+
}
|
|
196
|
+
export interface RouteRequest {
|
|
197
|
+
goal: string;
|
|
198
|
+
candidates: RouteCandidateItem[];
|
|
199
|
+
topK?: number;
|
|
200
|
+
/** Cache-affinity facts: the model in use and the accumulated context size. */
|
|
201
|
+
state?: RouteRequestState;
|
|
202
|
+
/**
|
|
203
|
+
* Decision instructions the backend must carry into its prompt — the
|
|
204
|
+
* cache-affinity policy ("keep the current model unless clearly outmatched").
|
|
205
|
+
*/
|
|
206
|
+
instructions?: string;
|
|
207
|
+
}
|
|
208
|
+
export interface RouteResponse {
|
|
209
|
+
rankedCandidates: Array<{
|
|
210
|
+
name: string;
|
|
211
|
+
score: number;
|
|
212
|
+
reason?: string;
|
|
213
|
+
}>;
|
|
214
|
+
bestCandidate?: string;
|
|
215
|
+
latencyMs: number;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* One batch of sentences a closing check must classify before it may verify
|
|
219
|
+
* anything.
|
|
220
|
+
*
|
|
221
|
+
* The candidates are chosen deterministically (the last few sentences of the
|
|
222
|
+
* turn), but *which* of them claims completion is a semantic question: a word
|
|
223
|
+
* list would miss a Chinese "全部通过" and read a negated "not complete" as the
|
|
224
|
+
* opposite. A backend that can answer exposes `classifyClaims`; one that cannot
|
|
225
|
+
* leaves the closing check on its fail-open path.
|
|
226
|
+
*/
|
|
227
|
+
export interface ClaimClassificationRequest {
|
|
228
|
+
/** Candidate sentences, in the order the caller will treat them. */
|
|
229
|
+
statements: string[];
|
|
230
|
+
/** What the turn was doing, so the classifier reads the sentences in context. */
|
|
231
|
+
intent: string;
|
|
232
|
+
/** Extra context (the turn's own summary) when the caller has one. */
|
|
233
|
+
context?: string;
|
|
234
|
+
}
|
|
235
|
+
/** One sentence's classification. */
|
|
236
|
+
export interface ClaimClassification {
|
|
237
|
+
statement: string;
|
|
238
|
+
/** Whether the winning criterion says the sentence claims completion. */
|
|
239
|
+
isClaim: boolean;
|
|
240
|
+
/** Probability of the completion-claim criterion, in `[0, 1]`. */
|
|
241
|
+
confidence: number;
|
|
242
|
+
}
|
|
243
|
+
/** The classification answer for one closing candidate batch. */
|
|
244
|
+
export interface ClaimClassificationResponse {
|
|
245
|
+
/** One entry per submitted statement, in submission order when answered. */
|
|
246
|
+
results: ClaimClassification[];
|
|
247
|
+
latencyMs: number;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Common interface for decision backends (Jev official, Simple-Jev, vLLM, mock)
|
|
251
|
+
*/
|
|
252
|
+
export interface DecisionBackend {
|
|
253
|
+
readonly name: string;
|
|
254
|
+
readonly model: string;
|
|
255
|
+
choice(req: ChoiceRequest): Promise<ChoiceResponse>;
|
|
256
|
+
gate(req: GateRequest): Promise<GateResponse>;
|
|
257
|
+
prune(req: PruneRequest): Promise<PruneResponse>;
|
|
258
|
+
checkStagnation(req: StagnationCheckRequest): Promise<StagnationCheckResponse>;
|
|
259
|
+
route(req: RouteRequest): Promise<RouteResponse>;
|
|
260
|
+
/** Classify completion-claim candidates, when the backend can answer at all. */
|
|
261
|
+
classifyClaims?(req: ClaimClassificationRequest): Promise<ClaimClassificationResponse>;
|
|
262
|
+
/**
|
|
263
|
+
* Health of the backend's outbound conversation, when it has one.
|
|
264
|
+
*
|
|
265
|
+
* In-process backends (mock) serve every call locally and simply omit this:
|
|
266
|
+
* absence means "healthy by construction", not "unknown".
|
|
267
|
+
*/
|
|
268
|
+
health?(): BackendHealth;
|
|
269
|
+
}
|
|
270
|
+
export type SystemOneBackend = DecisionBackend;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/backend/types.ts"],"names":[],"mappings":"AAAA;;GAEG","sourcesContent":["/**\n * Core types for System One Decision Runtime & APIs (Jev, vLLM/Simple-Jev, DSPy, etc.)\n */\n\nexport interface ChoiceOption {\n label: string;\n probability: number;\n}\n\nexport interface ChoiceRequest {\n question: string;\n options: string[];\n context?: string;\n temperature?: number;\n}\n\nexport interface ChoiceResponse {\n options: ChoiceOption[];\n bestOption: string;\n confidence: number;\n uncalibrated?: boolean;\n latencyMs: number;\n}\n\nexport type GateOutcome = 'verified' | 'contradicted' | 'not_addressed' | 'escalate';\n\nexport interface GateAssertion {\n statement: string;\n criteria?: string;\n}\n\nexport interface GateRequest {\n assertions: GateAssertion[];\n evidence: string;\n context?: string;\n strictness?: 'high' | 'normal' | 'lenient';\n}\n\nexport interface GateResultItem {\n statement: string;\n outcome: GateOutcome;\n confidence: number;\n explanation: string;\n}\n\nexport interface GateResponse {\n passed: boolean;\n results: GateResultItem[];\n needsEscalation: boolean;\n latencyMs: number;\n}\n\nexport interface PruneRequest {\n rawOutput: string;\n intent: string;\n maxTokens?: number;\n shadowMode?: boolean;\n /**\n * Character count from which the backend judges segments through System One.\n * The caller's resolved `interventions.pruneJudgeMinChars`; when omitted the\n * backend's own configuration/environment/default applies.\n */\n pruneJudgeMinChars?: number;\n}\n\nexport interface PruneResponse {\n processedOutput: string;\n originalChars: number;\n prunedChars: number;\n estimatedTokensSaved: number;\n reductionPercentage: number;\n shadow: boolean;\n latencyMs: number;\n /**\n * Which judging path produced the candidate: `segmented` ran the per-segment\n * System One scoring for a payload at/above `interventions.pruneJudgeMinChars`;\n * `local` used the deterministic candidates (small payload, or a fallback).\n * Optional so existing response literals stay valid.\n */\n judgePath?: 'segmented' | 'local' | 'fallback';\n /** Why the segmented path fell back to the local candidates, when it did. */\n fallbackReason?: string;\n /**\n * How the returned text was reduced, when it was.\n *\n * `none` means the returned text is the payload: a judgment that ran scored\n * every segment and kept everything (honoured as-is), or the degraded path's\n * template compression could not reduce it either. `fallbackReason` and\n * `judgePath` tell the two apart.\n */\n compression?: 'segmented' | 'local' | 'template' | 'none';\n /** Segments handed to System One in one request (0 when the local path ran). */\n judgedSegments?: number;\n /** Segments beyond the per-request limit, kept deterministically instead. */\n unjudgedSegments?: number;\n /**\n * Segments the composed scored candidate kept whole, out of all segments.\n *\n * The count belongs to the candidate that was installed; when no scored\n * candidate reduced the payload it is the balanced candidate's count, which is\n * what explains \"the judge kept five segments and nothing shrank\". It counts\n * the segments kept without a score too, which is why\n * {@link autoKeptSegments} is reported beside it. Absent on paths that never\n * selected segments by score (local, or a judgment with no usable score).\n */\n keptSegments?: number;\n /** Characters of the aggressive-budget candidate, before one is installed. */\n aggressiveChars?: number;\n /** Characters of the balanced-budget candidate, before one is installed. */\n balancedChars?: number;\n /** Characters of the conservative-budget candidate, before one is installed. */\n conservativeChars?: number;\n /**\n * Segments System One really scored (`scores.size`).\n *\n * `judgedSegments` counts the questions sent; this counts the answers that\n * were usable. A gap between the two is a judgment that could not be read.\n */\n scoredSegments?: number;\n /**\n * Segments kept whole because they carried no score.\n *\n * They are the unjudged safety margin (no answer, an unreadable answer, or a\n * segment beyond the per-request limit), counted over the whole payload. When\n * this is close to the segment count the candidate cannot shrink through the\n * scored budgets, because almost nothing was judged.\n */\n autoKeptSegments?: number;\n /** Answers returned for a judged segment that carried no usable keep score. */\n unparsedScoreAnswers?: number;\n}\n\n/**\n * Diagnostic fields of a prune response.\n *\n * Every path reports what it measured and omits the rest, so a reader can tell\n * a field that was not measured from one that measured zero.\n */\nexport type PruneDiagnostics = Pick<\n PruneResponse,\n | 'judgePath'\n | 'fallbackReason'\n | 'compression'\n | 'judgedSegments'\n | 'unjudgedSegments'\n | 'keptSegments'\n | 'aggressiveChars'\n | 'balancedChars'\n | 'conservativeChars'\n | 'scoredSegments'\n | 'autoKeptSegments'\n | 'unparsedScoreAnswers'\n>;\n\n/**\n * In-memory health of one System One backend.\n *\n * Every request the backend sends updates it: a success clears the failure\n * fields, a failure records when and why it happened and counts the run. It is\n * deliberately not persisted — a restarted Host starts healthy and discovers\n * the truth with its first call.\n */\nexport interface BackendHealth {\n /** Whether the most recent request succeeded (true before the first request). */\n available: boolean;\n /** Reason of the most recent failure; cleared by a success. */\n lastFailureReason?: string;\n /** ISO timestamp of the most recent failure; cleared by a success. */\n lastFailureAt?: string;\n /** Consecutive failed requests; a single success resets it to 0. */\n consecutiveFailures: number;\n}\n\nexport interface ActionStep {\n tool: string;\n input: string;\n output?: string;\n error?: string;\n timestamp?: number;\n}\n\nexport interface StagnationCheckRequest {\n history: ActionStep[];\n goal?: string;\n windowSize?: number;\n}\n\nexport interface StagnationCheckResponse {\n isStuck: boolean;\n patternType?: 'repetition' | 'oscillation' | 'failing_retry' | 'zero_progress' | 'none';\n confidence: number;\n diagnosis?: string;\n recommendedIntervention?: string;\n latencyMs: number;\n}\n\n/**\n * Session facts one routing decision reasons about.\n *\n * They exist for prompt-cache affinity: switching a session onto another\n * physical model invalidates the upstream prompt prefix cache, so the decision\n * needs to know which model holds the cached prefix and how much context is\n * already in it. Every field is optional — a caller that knows nothing about\n * the session (a hand-built one-shot call) simply omits the block.\n */\nexport interface RouteRequestState {\n /** Physical `provider:model` the session is currently using, when known. */\n currentModel?: string;\n /** Estimated tokens of the session context accumulated so far. */\n contextTokens?: number;\n /** Character count the token estimate was derived from. */\n contextChars?: number;\n}\n\n/** One candidate of a {@link RouteRequest}. */\nexport interface RouteCandidateItem {\n name: string;\n description: string;\n type?: 'tool' | 'skill' | 'action';\n /**\n * Whether this candidate is the physical model the session already uses.\n * Returning it costs no cache miss; a backend that builds a prompt should\n * say so next to the candidate.\n */\n current?: boolean;\n}\n\nexport interface RouteRequest {\n goal: string;\n candidates: RouteCandidateItem[];\n topK?: number;\n /** Cache-affinity facts: the model in use and the accumulated context size. */\n state?: RouteRequestState;\n /**\n * Decision instructions the backend must carry into its prompt — the\n * cache-affinity policy (\"keep the current model unless clearly outmatched\").\n */\n instructions?: string;\n}\n\nexport interface RouteResponse {\n rankedCandidates: Array<{ name: string; score: number; reason?: string }>;\n bestCandidate?: string;\n latencyMs: number;\n}\n\n/**\n * One batch of sentences a closing check must classify before it may verify\n * anything.\n *\n * The candidates are chosen deterministically (the last few sentences of the\n * turn), but *which* of them claims completion is a semantic question: a word\n * list would miss a Chinese \"全部通过\" and read a negated \"not complete\" as the\n * opposite. A backend that can answer exposes `classifyClaims`; one that cannot\n * leaves the closing check on its fail-open path.\n */\nexport interface ClaimClassificationRequest {\n /** Candidate sentences, in the order the caller will treat them. */\n statements: string[];\n /** What the turn was doing, so the classifier reads the sentences in context. */\n intent: string;\n /** Extra context (the turn's own summary) when the caller has one. */\n context?: string;\n}\n\n/** One sentence's classification. */\nexport interface ClaimClassification {\n statement: string;\n /** Whether the winning criterion says the sentence claims completion. */\n isClaim: boolean;\n /** Probability of the completion-claim criterion, in `[0, 1]`. */\n confidence: number;\n}\n\n/** The classification answer for one closing candidate batch. */\nexport interface ClaimClassificationResponse {\n /** One entry per submitted statement, in submission order when answered. */\n results: ClaimClassification[];\n latencyMs: number;\n}\n\n/**\n * Common interface for decision backends (Jev official, Simple-Jev, vLLM, mock)\n */\nexport interface DecisionBackend {\n readonly name: string;\n readonly model: string;\n \n choice(req: ChoiceRequest): Promise<ChoiceResponse>;\n gate(req: GateRequest): Promise<GateResponse>;\n prune(req: PruneRequest): Promise<PruneResponse>;\n checkStagnation(req: StagnationCheckRequest): Promise<StagnationCheckResponse>;\n route(req: RouteRequest): Promise<RouteResponse>;\n /** Classify completion-claim candidates, when the backend can answer at all. */\n classifyClaims?(req: ClaimClassificationRequest): Promise<ClaimClassificationResponse>;\n /**\n * Health of the backend's outbound conversation, when it has one.\n *\n * In-process backends (mock) serve every call locally and simply omit this:\n * absence means \"healthy by construction\", not \"unknown\".\n */\n health?(): BackendHealth;\n}\n\nexport type SystemOneBackend = DecisionBackend;\n\n"]}
|