@aiwayds/dsh-topics-memory 0.18.1 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +78 -1
- package/README.zh.md +69 -1
- package/lib/commands.js +3 -0
- package/lib/config.d.ts +44 -1
- package/lib/config.js +33 -2
- package/lib/consolidate.d.ts +39 -1
- package/lib/consolidate.js +195 -1
- package/lib/index.d.ts +10 -0
- package/lib/index.js +69 -15
- package/lib/jev/backends.d.ts +31 -0
- package/lib/jev/backends.js +46 -0
- package/lib/jev/client.d.ts +91 -0
- package/lib/jev/client.js +211 -0
- package/lib/jev/gate.d.ts +50 -0
- package/lib/jev/gate.js +125 -0
- package/lib/jev/log.d.ts +65 -0
- package/lib/jev/log.js +119 -0
- package/lib/jev/thresholds.d.ts +21 -0
- package/lib/jev/thresholds.js +27 -0
- package/lib/legacy-import.d.ts +15 -0
- package/lib/legacy-import.js +23 -4
- package/lib/quality.d.ts +60 -2
- package/lib/quality.js +230 -11
- package/package.json +2 -2
- package/src/commands.ts +2 -0
- package/src/config.ts +41 -2
- package/src/consolidate.ts +238 -1
- package/src/index.ts +77 -14
- package/src/jev/backends.ts +66 -0
- package/src/jev/client.ts +318 -0
- package/src/jev/gate.ts +151 -0
- package/src/jev/log.ts +184 -0
- package/src/jev/thresholds.ts +31 -0
- package/src/legacy-import.ts +34 -4
- package/src/quality.ts +269 -11
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* System One decision client — direct HTTP with zero dependencies beyond
|
|
3
|
+
* node:child_process and the global fetch (design 2026-09-25 §3; ADR
|
|
4
|
+
* proposal: direct HTTP over MCP).
|
|
5
|
+
*
|
|
6
|
+
* Semantics (§1/§3.2):
|
|
7
|
+
* - one AbortSignal.timeout per request, NO retry — the calling lane's next
|
|
8
|
+
* cadence is the natural retry (fail-open hard-coded, red line ②);
|
|
9
|
+
* - every failure returns as a structured {ok:false} result, never a throw;
|
|
10
|
+
* - the COMPLETE serialized body is gate-scanned before anything leaves the
|
|
11
|
+
* process (red line ③, design §3.3).
|
|
12
|
+
*
|
|
13
|
+
* @module jev/client
|
|
14
|
+
*/
|
|
15
|
+
import { execFile } from 'node:child_process';
|
|
16
|
+
import { promisify } from 'node:util';
|
|
17
|
+
import { resolveBackend } from "./backends.js";
|
|
18
|
+
import { currentSecrets, scanForSecrets } from "./gate.js";
|
|
19
|
+
import { logCall } from "./log.js";
|
|
20
|
+
/** Guard for non-positive/absent values from bare harnesses. */
|
|
21
|
+
const DEFAULT_TIMEOUT_MS = 3000;
|
|
22
|
+
function isPlainObject(v) {
|
|
23
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
24
|
+
}
|
|
25
|
+
/** 'svc' -> {service:'svc'}; 'svc:acct' -> {service:'svc', account:'acct'} */
|
|
26
|
+
export function parseKeychainSpec(spec) {
|
|
27
|
+
const i = spec.indexOf(':');
|
|
28
|
+
if (i === -1)
|
|
29
|
+
return { service: spec };
|
|
30
|
+
return { service: spec.slice(0, i), account: spec.slice(i + 1) };
|
|
31
|
+
}
|
|
32
|
+
const execFileAsync = promisify(execFile);
|
|
33
|
+
/** Default runner: `security find-generic-password … -w`; errors (missing
|
|
34
|
+
* entry, no binary on non-macOS, access denied) surface as rejections. */
|
|
35
|
+
const securityRunner = async (file, args) => {
|
|
36
|
+
return (await execFileAsync(file, args, { encoding: 'utf8' }));
|
|
37
|
+
};
|
|
38
|
+
/** Runs `security find-generic-password -w`; null on any failure (silent). */
|
|
39
|
+
export async function keychainLookup(spec, runner = securityRunner) {
|
|
40
|
+
const args = ['find-generic-password', '-s', spec.service];
|
|
41
|
+
if (spec.account !== undefined)
|
|
42
|
+
args.push('-a', spec.account);
|
|
43
|
+
try {
|
|
44
|
+
return (await runner('security', [...args, '-w'])).stdout.trim() || null;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/** Resolve the API key for a backend: env var wins, then the keychain spec
|
|
51
|
+
* (explicit JEV_KEYCHAIN, or the backend default for zen only). */
|
|
52
|
+
export async function resolveApiKey(backend, lookup = keychainLookup) {
|
|
53
|
+
const envKey = process.env[backend.keyEnv] ?? '';
|
|
54
|
+
if (envKey !== '')
|
|
55
|
+
return envKey;
|
|
56
|
+
const explicit = process.env.JEV_KEYCHAIN?.trim() ?? '';
|
|
57
|
+
const spec = explicit !== '' ? parseKeychainSpec(explicit) : backend.keychainByDefault ? { service: backend.keychainService } : null;
|
|
58
|
+
if (spec === null)
|
|
59
|
+
return '';
|
|
60
|
+
return (await lookup(spec)) ?? '';
|
|
61
|
+
}
|
|
62
|
+
function where(id) {
|
|
63
|
+
return `questions['${id}']`;
|
|
64
|
+
}
|
|
65
|
+
export function normalizeQuestions(questions, family) {
|
|
66
|
+
if (!isPlainObject(questions) || Object.keys(questions).length === 0) {
|
|
67
|
+
return { ok: false, message: "'questions' must be a non-empty object mapping question id -> {type, instructions, criteria?}." };
|
|
68
|
+
}
|
|
69
|
+
const systemone = family === 'systemone';
|
|
70
|
+
const out = {};
|
|
71
|
+
for (const [id, q] of Object.entries(questions)) {
|
|
72
|
+
if (!isPlainObject(q))
|
|
73
|
+
return { ok: false, message: `${where(id)} must be an object.` };
|
|
74
|
+
if (q.type !== 'noul' && q.type !== 'choice' && q.type !== 'score') {
|
|
75
|
+
return { ok: false, message: `${where(id)}.type must be 'noul' | 'choice' | 'score'.` };
|
|
76
|
+
}
|
|
77
|
+
if (typeof q.instructions !== 'string' || q.instructions === '') {
|
|
78
|
+
return { ok: false, message: `${where(id)}.instructions must be a non-empty string (the plugin always sends instructions).` };
|
|
79
|
+
}
|
|
80
|
+
const wire = { type: q.type, instructions: q.instructions };
|
|
81
|
+
if (q.type === 'noul') {
|
|
82
|
+
if (q.criteria === undefined) {
|
|
83
|
+
wire.criteria = { true: q.instructions, false: `NOT: ${q.instructions}` };
|
|
84
|
+
}
|
|
85
|
+
else if (isPlainObject(q.criteria) && q.criteria.true !== undefined && q.criteria.false !== undefined) {
|
|
86
|
+
if (!systemone && (typeof q.criteria.true !== 'string' || typeof q.criteria.false !== 'string')) {
|
|
87
|
+
return { ok: false, message: `${where(id)}: noul criteria must be the two-sided {true: string, false: string} on the decisions protocol.` };
|
|
88
|
+
}
|
|
89
|
+
wire.criteria = q.criteria;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
return { ok: false, message: `${where(id)}: noul criteria, when given, must be the two-sided {true: ..., false: ...}.` };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else if (q.type === 'choice') {
|
|
96
|
+
if (!isPlainObject(q.criteria)) {
|
|
97
|
+
return { ok: false, message: `${where(id)}: choice criteria must be an OBJECT mapping option -> description.` };
|
|
98
|
+
}
|
|
99
|
+
wire.criteria = q.criteria;
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
// score: 2-10 ordered levels; strings everywhere (plugin convention);
|
|
103
|
+
// the systemone protocol additionally tolerates structured levels.
|
|
104
|
+
if (!Array.isArray(q.criteria) || q.criteria.length < 2 || q.criteria.length > 10) {
|
|
105
|
+
return { ok: false, message: `${where(id)}: score criteria must be an ARRAY of 2-10 level descriptions.` };
|
|
106
|
+
}
|
|
107
|
+
const stringLevels = q.criteria.every((l) => typeof l === 'string' && l.length > 0);
|
|
108
|
+
if (!stringLevels && !systemone) {
|
|
109
|
+
return { ok: false, message: `${where(id)}: score levels must be non-empty strings on the decisions protocol.` };
|
|
110
|
+
}
|
|
111
|
+
wire.criteria = q.criteria;
|
|
112
|
+
}
|
|
113
|
+
out[id] = wire;
|
|
114
|
+
}
|
|
115
|
+
return { ok: true, questions: out };
|
|
116
|
+
}
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
/** Ask the System One decision model. One request for the whole batch;
|
|
119
|
+
* resolves to a structured failure (never throws) so the calling lane can
|
|
120
|
+
* fail open. Writes the decisions.jsonl call-layer row on EVERY path. */
|
|
121
|
+
export async function jevAsk(args) {
|
|
122
|
+
const backend = resolveBackend(args.config.jevBackend);
|
|
123
|
+
// '' is the sentinel; undefined (bare harnesses) resolves to the default too
|
|
124
|
+
const model = args.config.jevModel ? args.config.jevModel : backend.defaultModel;
|
|
125
|
+
const fallback = args.fallback === true; // absent => false: the column is call-time-invariant (fail-open = outcome !== ok)
|
|
126
|
+
const state = typeof args.state === 'string' ? args.state : '';
|
|
127
|
+
const started = Date.now();
|
|
128
|
+
async function emit(outcome, usage) {
|
|
129
|
+
await logCall({
|
|
130
|
+
at: new Date().toISOString(),
|
|
131
|
+
lane: args.lane,
|
|
132
|
+
backend: backend.name,
|
|
133
|
+
model,
|
|
134
|
+
questionCount: isPlainObject(args.questions) ? Object.keys(args.questions).length : 0,
|
|
135
|
+
stateChars: state.length,
|
|
136
|
+
latencyMs: Date.now() - started,
|
|
137
|
+
usage,
|
|
138
|
+
outcome,
|
|
139
|
+
fallback,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
const refuse = async (outcome, message) => {
|
|
143
|
+
await emit(outcome, null);
|
|
144
|
+
return { ok: false, outcome, message, latencyMs: Date.now() - started };
|
|
145
|
+
};
|
|
146
|
+
// 1. Local protocol check — nothing about the request has left the process.
|
|
147
|
+
const normalized = normalizeQuestions(args.questions, backend.family);
|
|
148
|
+
if (!normalized.ok)
|
|
149
|
+
return refuse('invalid_request', normalized.message);
|
|
150
|
+
const wireQuestions = normalized.questions;
|
|
151
|
+
// 2. Secret gate over the COMPLETE outbound body — the exact string below
|
|
152
|
+
// is what goes on the wire, so the gate sees byte-identical traffic.
|
|
153
|
+
// A hit sends NOTHING.
|
|
154
|
+
const secretFile = args.config.jevSecretFile ?? '';
|
|
155
|
+
const wire = JSON.stringify({ model, state, questions: wireQuestions });
|
|
156
|
+
const hit = scanForSecrets(wire, currentSecrets(secretFile, args.logger), secretFile);
|
|
157
|
+
if (hit !== null) {
|
|
158
|
+
return refuse('secret_gate_blocked', `outbound payload matched secret pattern '${hit.pattern}' at offset ${hit.offset}; NOTHING was sent. Remove the secret from the request body (model/state/questions).`);
|
|
159
|
+
}
|
|
160
|
+
// 3. API key — absent key fails before any network activity.
|
|
161
|
+
const key = await resolveApiKey(backend);
|
|
162
|
+
if (key === '') {
|
|
163
|
+
const hint = backend.keychainByDefault
|
|
164
|
+
? ` (set ${backend.keyEnv}, or store the key in the macOS keychain service '${backend.keychainService}')`
|
|
165
|
+
: ` (set ${backend.keyEnv}, or JEV_KEYCHAIN='<service>[:<account>]' for a macOS keychain lookup)`;
|
|
166
|
+
return refuse('missing_key', `no API key for the active backend '${backend.name}'${hint}.`);
|
|
167
|
+
}
|
|
168
|
+
// 4. One request, one timeout, NO retry.
|
|
169
|
+
const timeoutMs = Number(args.config.jevTimeoutMs) > 0 ? Number(args.config.jevTimeoutMs) : DEFAULT_TIMEOUT_MS;
|
|
170
|
+
let res;
|
|
171
|
+
try {
|
|
172
|
+
res = await fetch(backend.endpoint, {
|
|
173
|
+
method: 'POST',
|
|
174
|
+
headers: { 'content-type': 'application/json', authorization: `Bearer ${key}` },
|
|
175
|
+
body: wire,
|
|
176
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
catch (err) {
|
|
180
|
+
const outcome = err instanceof Error && (err.name === 'TimeoutError' || err.name === 'AbortError') ? 'timeout' : 'network';
|
|
181
|
+
return refuse(outcome, `${outcome} error: ${err instanceof Error ? err.message : String(err)}`);
|
|
182
|
+
}
|
|
183
|
+
if (!res.ok) {
|
|
184
|
+
const text = (await res.text().catch(() => '')).slice(0, 300);
|
|
185
|
+
const outcome = res.status >= 500 ? 'http_5xx' : 'http_4xx';
|
|
186
|
+
return refuse(outcome, `${backend.name} responded ${res.status}: ${text}`);
|
|
187
|
+
}
|
|
188
|
+
let json;
|
|
189
|
+
try {
|
|
190
|
+
json = await res.json();
|
|
191
|
+
}
|
|
192
|
+
catch (err) {
|
|
193
|
+
return refuse('bad_json', `${backend.name} returned non-JSON body: ${err instanceof Error ? err.message : String(err)}`);
|
|
194
|
+
}
|
|
195
|
+
// 5. Response shape: answers must be an object covering every question id
|
|
196
|
+
// (a half-answered batch is a bad response, not a partial success).
|
|
197
|
+
if (!isPlainObject(json)) {
|
|
198
|
+
return refuse('bad_json', `${backend.name} returned no answers object.`);
|
|
199
|
+
}
|
|
200
|
+
const answers = json.answers;
|
|
201
|
+
if (!isPlainObject(answers)) {
|
|
202
|
+
return refuse('bad_json', `${backend.name} returned no answers object.`);
|
|
203
|
+
}
|
|
204
|
+
const missing = Object.keys(wireQuestions).filter((id) => answers[id] === undefined);
|
|
205
|
+
if (missing.length > 0) {
|
|
206
|
+
return refuse('bad_json', `${backend.name} returned no answer for question id(s): ${missing.join(', ')}.`);
|
|
207
|
+
}
|
|
208
|
+
const usage = isPlainObject(json.usage) ? json.usage : null;
|
|
209
|
+
await emit('ok', usage);
|
|
210
|
+
return { ok: true, answers: answers, usage, latencyMs: Date.now() - started };
|
|
211
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbound secret gate (design 2026-09-25 §3.3) — self-contained port of the
|
|
3
|
+
* dsh-jev-mcp `server.mjs` mechanism. The COMPLETE serialized request body
|
|
4
|
+
* (model, state, questions — byte-identical to what goes on the wire) is
|
|
5
|
+
* scanned for credential patterns BEFORE anything leaves the process; a hit
|
|
6
|
+
* aborts the call with a structured descriptor that never echoes the secret.
|
|
7
|
+
*
|
|
8
|
+
* SECURITY INVARIANT: hit descriptors carry static metadata only (pattern
|
|
9
|
+
* name, offset, ≤10 printable chars before the hit) — never the matched
|
|
10
|
+
* text, and never text at or after the hit point.
|
|
11
|
+
*
|
|
12
|
+
* @module jev/gate
|
|
13
|
+
*/
|
|
14
|
+
export interface SecretGateHit {
|
|
15
|
+
pattern: string;
|
|
16
|
+
offset: number;
|
|
17
|
+
context: string;
|
|
18
|
+
}
|
|
19
|
+
/** Minimal logger surface (both sinks optional — bare harnesses have none);
|
|
20
|
+
* the gate only ever warns, and a broken logger must not break the gate. */
|
|
21
|
+
export interface GateLogger {
|
|
22
|
+
warn?: (m: string) => void;
|
|
23
|
+
}
|
|
24
|
+
export declare const BUILTIN_PATTERNS: ReadonlyArray<{
|
|
25
|
+
name: string;
|
|
26
|
+
re: RegExp;
|
|
27
|
+
}>;
|
|
28
|
+
/** Literal secrets from the external list file (JEV_SECRET_FILE semantics:
|
|
29
|
+
* one entry per line, ≥8 chars, `#` comments skipped, matched at FULL
|
|
30
|
+
* length — a >512-char line warns but is never truncated, or everything
|
|
31
|
+
* past char 512 would silently escape the gate). */
|
|
32
|
+
export declare function loadExternalSecrets(file: string, log?: GateLogger): string[];
|
|
33
|
+
export declare function currentSecrets(file: string, log?: GateLogger): string[];
|
|
34
|
+
export declare function reloadSecrets(file: string, log?: GateLogger): string[];
|
|
35
|
+
/**
|
|
36
|
+
* Redacted context: at most 10 printable chars BEFORE the hit plus an
|
|
37
|
+
* explicit [REDACTED:<pattern>] marker. Content at or after the hit point is
|
|
38
|
+
* NEVER shown — when a prefix regex under-matches, the secret tail sits
|
|
39
|
+
* right there. Belt and suspenders: if the matched text or any known
|
|
40
|
+
* external secret still appears in the context, the whole context collapses
|
|
41
|
+
* to '[REDACTED]'.
|
|
42
|
+
*/
|
|
43
|
+
export declare function describeHit(pattern: string, offset: number, matched: string, payload: string, externalSecrets?: readonly string[]): SecretGateHit;
|
|
44
|
+
/**
|
|
45
|
+
* Scan the serialized outbound payload for secrets.
|
|
46
|
+
* @param payload JSON.stringify({model, state, questions}) — the exact wire body
|
|
47
|
+
* @param externalSecrets literal secrets from the external list (cached via currentSecrets)
|
|
48
|
+
* @param externalSource label for hit descriptors (the list file path)
|
|
49
|
+
*/
|
|
50
|
+
export declare function scanForSecrets(payload: string, externalSecrets?: readonly string[], externalSource?: string): SecretGateHit | null;
|
package/lib/jev/gate.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbound secret gate (design 2026-09-25 §3.3) — self-contained port of the
|
|
3
|
+
* dsh-jev-mcp `server.mjs` mechanism. The COMPLETE serialized request body
|
|
4
|
+
* (model, state, questions — byte-identical to what goes on the wire) is
|
|
5
|
+
* scanned for credential patterns BEFORE anything leaves the process; a hit
|
|
6
|
+
* aborts the call with a structured descriptor that never echoes the secret.
|
|
7
|
+
*
|
|
8
|
+
* SECURITY INVARIANT: hit descriptors carry static metadata only (pattern
|
|
9
|
+
* name, offset, ≤10 printable chars before the hit) — never the matched
|
|
10
|
+
* text, and never text at or after the hit point.
|
|
11
|
+
*
|
|
12
|
+
* @module jev/gate
|
|
13
|
+
*/
|
|
14
|
+
import { readFileSync } from 'node:fs';
|
|
15
|
+
function safeWarn(log, message) {
|
|
16
|
+
try {
|
|
17
|
+
log?.warn?.(message);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
// contained — diagnostics never take the feature down
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// Prefix-style patterns require a 20+ char continuation so that merely
|
|
24
|
+
// DISCUSSING a prefix (e.g. "set OPENROUTER_API_KEY to an sk-or-v1- key")
|
|
25
|
+
// never trips the gate — only an actual long token does.
|
|
26
|
+
export const BUILTIN_PATTERNS = [
|
|
27
|
+
{ name: 'jwt', re: /eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/ },
|
|
28
|
+
{ name: 'openrouter-key', re: /sk-or-v1-[A-Za-z0-9_-]{20,}/ },
|
|
29
|
+
{ name: 'anthropic-key', re: /sk-ant-[A-Za-z0-9_-]{20,}/ },
|
|
30
|
+
{ name: 'openai-project-key', re: /sk-proj-[A-Za-z0-9_-]{20,}/ },
|
|
31
|
+
{ name: 'openai-sk-key', re: /sk-[A-Za-z0-9]{20,}/ },
|
|
32
|
+
{ name: 'github-token', re: /ghp_[A-Za-z0-9]{20,}/ },
|
|
33
|
+
{ name: 'github-pat', re: /github_pat_[A-Za-z0-9_]{20,}/ },
|
|
34
|
+
{ name: 'gitlab-token', re: /glpat-[A-Za-z0-9_-]{20,}/ },
|
|
35
|
+
{ name: 'slack-token', re: /xox[baprs]-[A-Za-z0-9-]{20,}/ },
|
|
36
|
+
{ name: 'aws-access-key-id', re: /AKIA[0-9A-Z]{16}/ },
|
|
37
|
+
{ name: 'aws-temp-key-id', re: /ASIA[0-9A-Z]{16}/ },
|
|
38
|
+
{ name: 'google-api-key', re: /AIza[A-Za-z0-9_-]{20,}/ },
|
|
39
|
+
{ name: 'private-key-block', re: /-----BEGIN [A-Z ]*PRIVATE KEY-----/ },
|
|
40
|
+
];
|
|
41
|
+
const MAX_SECRET_LINE = 512; // longer lines WARN; matching ALWAYS uses the full line
|
|
42
|
+
const MIN_EXTERNAL_SECRET = 8; // shorter entries would cause runaway false hits
|
|
43
|
+
/** Literal secrets from the external list file (JEV_SECRET_FILE semantics:
|
|
44
|
+
* one entry per line, ≥8 chars, `#` comments skipped, matched at FULL
|
|
45
|
+
* length — a >512-char line warns but is never truncated, or everything
|
|
46
|
+
* past char 512 would silently escape the gate). */
|
|
47
|
+
export function loadExternalSecrets(file, log) {
|
|
48
|
+
if (file === '')
|
|
49
|
+
return [];
|
|
50
|
+
let raw;
|
|
51
|
+
try {
|
|
52
|
+
raw = readFileSync(file, 'utf8');
|
|
53
|
+
}
|
|
54
|
+
catch (err) {
|
|
55
|
+
safeWarn(log, `topics jev: cannot read jevSecretFile (${file}): ${err instanceof Error ? err.message : String(err)}; continuing without it.`);
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
const secrets = [];
|
|
59
|
+
raw.split(/\r?\n/).forEach((line, i) => {
|
|
60
|
+
const s = line.trim();
|
|
61
|
+
if (s === '' || s.startsWith('#'))
|
|
62
|
+
return;
|
|
63
|
+
if (s.length < MIN_EXTERNAL_SECRET) {
|
|
64
|
+
safeWarn(log, `topics jev: jevSecretFile line ${i + 1} shorter than ${MIN_EXTERNAL_SECRET} chars, skipped (false-positive guard).`);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (s.length > MAX_SECRET_LINE) {
|
|
68
|
+
safeWarn(log, `topics jev: jevSecretFile line ${i + 1} is ${s.length} chars (> ${MAX_SECRET_LINE}); matching uses the FULL line (no truncation).`);
|
|
69
|
+
}
|
|
70
|
+
secrets.push(s);
|
|
71
|
+
});
|
|
72
|
+
return secrets;
|
|
73
|
+
}
|
|
74
|
+
// One-time-per-path load, mirroring the reference implementation's
|
|
75
|
+
// load-once-at-startup while staying compatible with the volatile
|
|
76
|
+
// `jevSecretFile` config: an unchanged path is served from the cache (no
|
|
77
|
+
// re-read), a swapped path reloads on the next call. `reloadSecrets` forces
|
|
78
|
+
// a re-read — the `/topics set jev-secret-file` wiring calls it so an
|
|
79
|
+
// in-place content change is picked up too.
|
|
80
|
+
let cache = { file: '', secrets: [] };
|
|
81
|
+
export function currentSecrets(file, log) {
|
|
82
|
+
if (cache.file !== file)
|
|
83
|
+
reloadSecrets(file, log);
|
|
84
|
+
return cache.secrets;
|
|
85
|
+
}
|
|
86
|
+
export function reloadSecrets(file, log) {
|
|
87
|
+
cache = { file, secrets: loadExternalSecrets(file, log) };
|
|
88
|
+
return cache.secrets;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Redacted context: at most 10 printable chars BEFORE the hit plus an
|
|
92
|
+
* explicit [REDACTED:<pattern>] marker. Content at or after the hit point is
|
|
93
|
+
* NEVER shown — when a prefix regex under-matches, the secret tail sits
|
|
94
|
+
* right there. Belt and suspenders: if the matched text or any known
|
|
95
|
+
* external secret still appears in the context, the whole context collapses
|
|
96
|
+
* to '[REDACTED]'.
|
|
97
|
+
*/
|
|
98
|
+
export function describeHit(pattern, offset, matched, payload, externalSecrets = []) {
|
|
99
|
+
const before = payload.slice(Math.max(0, offset - 10), offset).replace(/[^\x20-\x7e]/g, '.'); // keep it printable
|
|
100
|
+
const context = `${before}[REDACTED:${pattern}]`;
|
|
101
|
+
if (context.includes(matched) || externalSecrets.some((s) => s !== '' && context.includes(s))) {
|
|
102
|
+
return { pattern, offset, context: '[REDACTED]' };
|
|
103
|
+
}
|
|
104
|
+
return { pattern, offset, context };
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Scan the serialized outbound payload for secrets.
|
|
108
|
+
* @param payload JSON.stringify({model, state, questions}) — the exact wire body
|
|
109
|
+
* @param externalSecrets literal secrets from the external list (cached via currentSecrets)
|
|
110
|
+
* @param externalSource label for hit descriptors (the list file path)
|
|
111
|
+
*/
|
|
112
|
+
export function scanForSecrets(payload, externalSecrets = [], externalSource = '') {
|
|
113
|
+
for (const { name, re } of BUILTIN_PATTERNS) {
|
|
114
|
+
const m = re.exec(payload);
|
|
115
|
+
if (m)
|
|
116
|
+
return describeHit(name, m.index, m[0], payload, externalSecrets);
|
|
117
|
+
}
|
|
118
|
+
for (const secret of externalSecrets) {
|
|
119
|
+
const idx = payload.indexOf(secret);
|
|
120
|
+
if (idx !== -1) {
|
|
121
|
+
return describeHit(externalSource !== '' ? `external(${externalSource})` : 'external', idx, secret, payload, externalSecrets);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
package/lib/jev/log.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* decisions.jsonl — System One usage/optimization telemetry (design
|
|
3
|
+
* 2026-09-25 §6), always on while jevEnabled is on. Lives beside the ilog at
|
|
4
|
+
* `<bundle>/meta/decisions.jsonl` under the same discipline: 512KB
|
|
5
|
+
* auto-compaction keeping the most recent quarter, line JSON, and write
|
|
6
|
+
* failures swallowed silently — fail-open extends to the stats itself.
|
|
7
|
+
*
|
|
8
|
+
* REDACTION INVARIANT (§6.1): metadata and probabilities only — slug/pair
|
|
9
|
+
* hash/question type/score/latency/token count. NEVER the state text or
|
|
10
|
+
* conclusion bodies. Writers copy an explicit field whitelist, so extra
|
|
11
|
+
* fields on caller objects cannot leak into the file.
|
|
12
|
+
*
|
|
13
|
+
* @module jev/log
|
|
14
|
+
*/
|
|
15
|
+
import type { JevBand } from './thresholds.ts';
|
|
16
|
+
export type JevLane = 'slowlane-rerank' | 'consolidate-prefilter' | 'fastgate-shadow';
|
|
17
|
+
export type JevOutcome = 'ok' | 'timeout' | 'network' | 'http_4xx' | 'http_5xx' | 'bad_json' | 'secret_gate_blocked' | 'missing_key'
|
|
18
|
+
/** Local pre-flight refusal (e.g. questions violate the decisions/openrouter
|
|
19
|
+
* wire contract) — nothing was sent. Reserved outcome beyond §6.2's v1 list. */
|
|
20
|
+
| 'invalid_request';
|
|
21
|
+
export interface JevUsage {
|
|
22
|
+
input_tokens: number;
|
|
23
|
+
output_tokens: number;
|
|
24
|
+
}
|
|
25
|
+
/** Call layer (§6.2): one row per HTTP call, failures included — the
|
|
26
|
+
* fallback rate is the health metric, so failed calls must be counted. */
|
|
27
|
+
export interface JevCallRecord {
|
|
28
|
+
at: string;
|
|
29
|
+
lane: JevLane;
|
|
30
|
+
backend: string;
|
|
31
|
+
model: string;
|
|
32
|
+
questionCount: number;
|
|
33
|
+
stateChars: number;
|
|
34
|
+
latencyMs: number;
|
|
35
|
+
usage: JevUsage | null;
|
|
36
|
+
outcome: JevOutcome;
|
|
37
|
+
fallback: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** Verdict layer (§6.2): one row per question, batch requests expanded.
|
|
40
|
+
* `agree` reconciles the fast-gate shadow against the lexical disposition
|
|
41
|
+
* (wouldBlock = lexical let it through but jev landed in the veto band —
|
|
42
|
+
* the other half of the calibration goldmine). */
|
|
43
|
+
export interface JevVerdictRecord {
|
|
44
|
+
at: string;
|
|
45
|
+
lane: JevLane;
|
|
46
|
+
questionId: string;
|
|
47
|
+
qtype: 'noul' | 'choice' | 'score';
|
|
48
|
+
/** Candidate reference without content: `slug:xxx` | `pair:hash3`. */
|
|
49
|
+
ref: string;
|
|
50
|
+
/** §6.2 digest — stable key for the repeat-decision-rate analysis. */
|
|
51
|
+
digest: string;
|
|
52
|
+
probability: number;
|
|
53
|
+
band: JevBand;
|
|
54
|
+
agree: 'hit' | 'nearFloor' | 'gate-blocked' | 'wouldBlock' | 'n/a';
|
|
55
|
+
}
|
|
56
|
+
export declare function decisionsFile(root: string): string;
|
|
57
|
+
/** digest(state, questionId, instructions): `h1:` + first 16 hex of
|
|
58
|
+
* sha256(state + NUL + questionId + NUL + instructions) — the key the
|
|
59
|
+
* repeat-decision-rate analysis joins on. */
|
|
60
|
+
export declare function digestFor(state: string, questionId: string, instructions: string): string;
|
|
61
|
+
/** Call-layer row. Field whitelist enforced here: anything extra on the
|
|
62
|
+
* caller's object is dropped, so no state/conclusion text can sneak in. */
|
|
63
|
+
export declare function logCall(rec: JevCallRecord): Promise<void>;
|
|
64
|
+
/** Verdict-layer rows (batch expanded by the caller). */
|
|
65
|
+
export declare function logVerdicts(recs: readonly JevVerdictRecord[]): Promise<void>;
|
package/lib/jev/log.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* decisions.jsonl — System One usage/optimization telemetry (design
|
|
3
|
+
* 2026-09-25 §6), always on while jevEnabled is on. Lives beside the ilog at
|
|
4
|
+
* `<bundle>/meta/decisions.jsonl` under the same discipline: 512KB
|
|
5
|
+
* auto-compaction keeping the most recent quarter, line JSON, and write
|
|
6
|
+
* failures swallowed silently — fail-open extends to the stats itself.
|
|
7
|
+
*
|
|
8
|
+
* REDACTION INVARIANT (§6.1): metadata and probabilities only — slug/pair
|
|
9
|
+
* hash/question type/score/latency/token count. NEVER the state text or
|
|
10
|
+
* conclusion bodies. Writers copy an explicit field whitelist, so extra
|
|
11
|
+
* fields on caller objects cannot leak into the file.
|
|
12
|
+
*
|
|
13
|
+
* @module jev/log
|
|
14
|
+
*/
|
|
15
|
+
import { appendFile, mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
16
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
17
|
+
import { dirname, join } from 'node:path';
|
|
18
|
+
import { metaDir, resolveBundleRoot } from "../paths.js";
|
|
19
|
+
const COMPACT_LIMIT = 512 * 1024; // ~512KB cap, same as the ilog sidecars
|
|
20
|
+
export function decisionsFile(root) {
|
|
21
|
+
return join(metaDir(root), 'decisions.jsonl');
|
|
22
|
+
}
|
|
23
|
+
function liveFile() {
|
|
24
|
+
return decisionsFile(resolveBundleRoot());
|
|
25
|
+
}
|
|
26
|
+
/** digest(state, questionId, instructions): `h1:` + first 16 hex of
|
|
27
|
+
* sha256(state + NUL + questionId + NUL + instructions) — the key the
|
|
28
|
+
* repeat-decision-rate analysis joins on. */
|
|
29
|
+
export function digestFor(state, questionId, instructions) {
|
|
30
|
+
const h = createHash('sha256');
|
|
31
|
+
h.update(state);
|
|
32
|
+
h.update('\u0000');
|
|
33
|
+
h.update(questionId);
|
|
34
|
+
h.update('\u0000');
|
|
35
|
+
h.update(instructions);
|
|
36
|
+
return `h1:${h.digest('hex').slice(0, 16)}`;
|
|
37
|
+
}
|
|
38
|
+
// Serialized like the store's write queue: single-record appends are
|
|
39
|
+
// line-atomic anyway, but the compaction rewrite must not interleave with
|
|
40
|
+
// an append.
|
|
41
|
+
let queue = Promise.resolve();
|
|
42
|
+
function enqueue(op) {
|
|
43
|
+
const run = queue.then(op, op);
|
|
44
|
+
queue = run.catch(() => undefined);
|
|
45
|
+
return run;
|
|
46
|
+
}
|
|
47
|
+
/** All write failures die HERE — never upstream. */
|
|
48
|
+
async function appendJsonl(lines) {
|
|
49
|
+
await enqueue(async () => {
|
|
50
|
+
try {
|
|
51
|
+
const file = liveFile();
|
|
52
|
+
await mkdir(dirname(file), { recursive: true });
|
|
53
|
+
await appendFile(file, lines.map((l) => `${l}\n`).join(''), 'utf8');
|
|
54
|
+
await compactIfNeeded(file);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
// fail-open: a broken stats sink must never break the feature (§6.1)
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// store.ts compactInjectionsIfNeeded pattern: ~512KB cap; keep the most
|
|
62
|
+
// recent quarter when exceeded (headroom for the next window).
|
|
63
|
+
async function compactIfNeeded(file) {
|
|
64
|
+
let raw;
|
|
65
|
+
try {
|
|
66
|
+
raw = await readFile(file, 'utf8');
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (raw.length <= COMPACT_LIMIT)
|
|
72
|
+
return;
|
|
73
|
+
const lines = raw.split('\n').filter((l) => l.trim() !== '');
|
|
74
|
+
const keep = lines.slice(-Math.max(1, Math.floor(lines.length / 4)));
|
|
75
|
+
const tmp = `${file}.tmp-${randomUUID()}`;
|
|
76
|
+
try {
|
|
77
|
+
await writeFile(tmp, keep.map((l) => `${l}\n`).join(''), 'utf8');
|
|
78
|
+
await rename(tmp, file);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// swallow — the next append retries the compaction
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** Call-layer row. Field whitelist enforced here: anything extra on the
|
|
85
|
+
* caller's object is dropped, so no state/conclusion text can sneak in. */
|
|
86
|
+
export async function logCall(rec) {
|
|
87
|
+
await appendJsonl([
|
|
88
|
+
JSON.stringify({
|
|
89
|
+
at: rec.at,
|
|
90
|
+
lane: rec.lane,
|
|
91
|
+
backend: rec.backend,
|
|
92
|
+
model: rec.model,
|
|
93
|
+
questionCount: rec.questionCount,
|
|
94
|
+
stateChars: rec.stateChars,
|
|
95
|
+
latencyMs: rec.latencyMs,
|
|
96
|
+
usage: rec.usage === null
|
|
97
|
+
? null
|
|
98
|
+
: { input_tokens: rec.usage.input_tokens, output_tokens: rec.usage.output_tokens },
|
|
99
|
+
outcome: rec.outcome,
|
|
100
|
+
fallback: rec.fallback,
|
|
101
|
+
}),
|
|
102
|
+
]);
|
|
103
|
+
}
|
|
104
|
+
/** Verdict-layer rows (batch expanded by the caller). */
|
|
105
|
+
export async function logVerdicts(recs) {
|
|
106
|
+
if (recs.length === 0)
|
|
107
|
+
return;
|
|
108
|
+
await appendJsonl(recs.map((rec) => JSON.stringify({
|
|
109
|
+
at: rec.at,
|
|
110
|
+
lane: rec.lane,
|
|
111
|
+
questionId: rec.questionId,
|
|
112
|
+
qtype: rec.qtype,
|
|
113
|
+
ref: rec.ref,
|
|
114
|
+
digest: rec.digest,
|
|
115
|
+
probability: rec.probability,
|
|
116
|
+
band: rec.band,
|
|
117
|
+
agree: rec.agree,
|
|
118
|
+
})));
|
|
119
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* System One decision bands (design 2026-09-25 §5).
|
|
3
|
+
*
|
|
4
|
+
* The numbers are CALIBRATED — single-case A/B plus a 383-case threshold
|
|
5
|
+
* sweep (2026-09-25) — and bound to the batch wire protocol (protocol-drift
|
|
6
|
+
* evidence: the same case scores differently single vs batch). Changing the
|
|
7
|
+
* protocol requires a re-sweep; do not tune these in place.
|
|
8
|
+
*
|
|
9
|
+
* @module jev/thresholds
|
|
10
|
+
*/
|
|
11
|
+
/** Slow-lane rerank (候选问): adopt ≥ 0.60, strong-veto < 0.10. */
|
|
12
|
+
export declare const RERANK_ADOPT = 0.6;
|
|
13
|
+
export declare const RERANK_FALLBACK = 0.1;
|
|
14
|
+
/** Consolidation prefilter (pair 问「同一问题吗」): adopt ≥ 0.50, veto < 0.15. */
|
|
15
|
+
export declare const MERGE_PAIR_ADOPT = 0.5;
|
|
16
|
+
export declare const MERGE_PAIR_FALLBACK = 0.15;
|
|
17
|
+
export type JevBand = 'adopt' | 'record' | 'fallback';
|
|
18
|
+
export type JevBandKind = 'rerank' | 'mergePair';
|
|
19
|
+
/** Map a noul probability to its band: adopt → act on it, record → only log
|
|
20
|
+
* it, fallback → hard veto back to the lexical/legacy behavior. */
|
|
21
|
+
export declare function band(probability: number, kind: JevBandKind): JevBand;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* System One decision bands (design 2026-09-25 §5).
|
|
3
|
+
*
|
|
4
|
+
* The numbers are CALIBRATED — single-case A/B plus a 383-case threshold
|
|
5
|
+
* sweep (2026-09-25) — and bound to the batch wire protocol (protocol-drift
|
|
6
|
+
* evidence: the same case scores differently single vs batch). Changing the
|
|
7
|
+
* protocol requires a re-sweep; do not tune these in place.
|
|
8
|
+
*
|
|
9
|
+
* @module jev/thresholds
|
|
10
|
+
*/
|
|
11
|
+
/** Slow-lane rerank (候选问): adopt ≥ 0.60, strong-veto < 0.10. */
|
|
12
|
+
export const RERANK_ADOPT = 0.6;
|
|
13
|
+
export const RERANK_FALLBACK = 0.1;
|
|
14
|
+
/** Consolidation prefilter (pair 问「同一问题吗」): adopt ≥ 0.50, veto < 0.15. */
|
|
15
|
+
export const MERGE_PAIR_ADOPT = 0.5;
|
|
16
|
+
export const MERGE_PAIR_FALLBACK = 0.15;
|
|
17
|
+
/** Map a noul probability to its band: adopt → act on it, record → only log
|
|
18
|
+
* it, fallback → hard veto back to the lexical/legacy behavior. */
|
|
19
|
+
export function band(probability, kind) {
|
|
20
|
+
const floor = kind === 'rerank' ? RERANK_FALLBACK : MERGE_PAIR_FALLBACK;
|
|
21
|
+
const adopt = kind === 'rerank' ? RERANK_ADOPT : MERGE_PAIR_ADOPT;
|
|
22
|
+
if (probability >= adopt)
|
|
23
|
+
return 'adopt';
|
|
24
|
+
if (probability >= floor)
|
|
25
|
+
return 'record';
|
|
26
|
+
return 'fallback';
|
|
27
|
+
}
|
package/lib/legacy-import.d.ts
CHANGED
|
@@ -50,6 +50,21 @@ export interface LegacyImportInput {
|
|
|
50
50
|
logger?: LegacyImportLogger | undefined;
|
|
51
51
|
/** Current effective value for one legacy key (Volatile ref `.get()` at the call site). */
|
|
52
52
|
getCurrent(key: LegacyKey): unknown;
|
|
53
|
+
/**
|
|
54
|
+
* Retry budget for `settings.update`. The host serializes profile writes
|
|
55
|
+
* (settings.update → dsh-config-editor) behind the `<profile>/package.json`
|
|
56
|
+
* writer lock with a 2s per-call deadline, and during BOOT the host itself
|
|
57
|
+
* holds that lock while preparing the profile — a single bare call almost
|
|
58
|
+
* always times out (2026-09-25 incident: the import failed EVERY boot and
|
|
59
|
+
* the migration never converged). Defaults to 5 attempts 2s apart; a
|
|
60
|
+
* permanently orphaned lock (a SIGKILLed host never releases it — orphan
|
|
61
|
+
* recovery is an operator action) still exhausts the budget and stays
|
|
62
|
+
* retry-worthy. Tests shrink this to keep the suite fast.
|
|
63
|
+
*/
|
|
64
|
+
updateRetry?: {
|
|
65
|
+
attempts: number;
|
|
66
|
+
delayMs: number;
|
|
67
|
+
} | undefined;
|
|
53
68
|
}
|
|
54
69
|
export type LegacyImportOutcome = 'imported' | 'no-op' | 'no-legacy' | 'no-section' | 'marker-exists' | 'no-settings' | 'update-failed';
|
|
55
70
|
export interface LegacyImportResult {
|