@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
package/lib/consolidate.js
CHANGED
|
@@ -23,11 +23,21 @@
|
|
|
23
23
|
* - the cadence stamp (meta/consolidate-state.json) advances only when the
|
|
24
24
|
* model actually evaluated at least one cluster — a run that died on the
|
|
25
25
|
* first call leaves the stamp alone, and the next session start retries.
|
|
26
|
+
* - jev prefilter (design 2026-09-25 §2, default-off): with jevEnabled the
|
|
27
|
+
* cluster first answers to ONE cheap System One batch (≤6 questions: the
|
|
28
|
+
* top-5 similar pairs each ask 「同一问题吗」, plus a record-only
|
|
29
|
+
* cluster-level 「值得动吗」). Only when ≥1 pair reaches the adopt band
|
|
30
|
+
* (≥0.50) does the cluster go to the LLM — an all-non-adopt cluster skips
|
|
31
|
+
* the model call entirely. Any jev failure fails open to today's behavior;
|
|
32
|
+
* the merge conclusion generation itself stays LLM.
|
|
26
33
|
*
|
|
27
34
|
* @module consolidate
|
|
28
35
|
*/
|
|
29
36
|
import * as okf from "./okf.js";
|
|
30
37
|
import { parseOps } from "./distill.js";
|
|
38
|
+
import { jevAsk } from "./jev/client.js";
|
|
39
|
+
import { band } from "./jev/thresholds.js";
|
|
40
|
+
import { digestFor, logVerdicts } from "./jev/log.js";
|
|
31
41
|
/** Below this pairwise Jaccard two topics never share a cluster. */
|
|
32
42
|
const CLUSTER_THRESHOLD = 0.3;
|
|
33
43
|
/** Clusters are capped — a giant cluster drowns the model in candidates. */
|
|
@@ -39,6 +49,12 @@ const MAX_OPS_PER_RUN = 12;
|
|
|
39
49
|
/** Conclusion chars fed per topic in a cluster payload. */
|
|
40
50
|
const CONCLUSION_SNIPPET_CHARS = 600;
|
|
41
51
|
const CONSOLIDATE_MAX_TOKENS = 2500;
|
|
52
|
+
/**
|
|
53
|
+
* Pair questions per cluster batch (design §2: k≤5, so with the record-only
|
|
54
|
+
* cluster-level question a batch stays ≤6 问/簇 — the measured cheap end of
|
|
55
|
+
* the batch protocol).
|
|
56
|
+
*/
|
|
57
|
+
const PREFILTER_TOP_PAIRS = 5;
|
|
42
58
|
export const CONSOLIDATE_SYSTEM_PROMPT = [
|
|
43
59
|
'你是 topic 记忆库的整理引擎(园丁)。输入是一个候选簇:若干条可能相近的已有 topic(JSON,含 slug/标题/状态/标签/结论摘要)。',
|
|
44
60
|
'任务:判断簇内有没有值得整理的动作,输出严格 JSON:{"ops":[...]},不要任何其他文字或代码块解释。只允许四类 op:',
|
|
@@ -142,14 +158,90 @@ export function clusterTopics(entries, threshold = CLUSTER_THRESHOLD) {
|
|
|
142
158
|
.sort((a, b) => (degree.get(b) ?? 0) - (degree.get(a) ?? 0))
|
|
143
159
|
.slice(0, MAX_CLUSTER_SIZE);
|
|
144
160
|
}
|
|
161
|
+
// Reuse the clustering edges for the prefilter's pair list: positions map
|
|
162
|
+
// into the (possibly capped) kept order, best similarity first. Edges
|
|
163
|
+
// lost to the cap — or transitively-clustered pairs below threshold — are
|
|
164
|
+
// not pair candidates; a capped cluster with no inner edge left simply
|
|
165
|
+
// has no pairs (the prefilter then behaves as today).
|
|
166
|
+
const position = new Map();
|
|
167
|
+
kept.forEach((original, pos) => position.set(original, pos));
|
|
168
|
+
const pairs = [];
|
|
169
|
+
for (const [key, s] of scores) {
|
|
170
|
+
const i = Math.floor(key / entries.length);
|
|
171
|
+
const j = key % entries.length;
|
|
172
|
+
const pi = position.get(i);
|
|
173
|
+
const pj = position.get(j);
|
|
174
|
+
if (pi === undefined || pj === undefined)
|
|
175
|
+
continue;
|
|
176
|
+
pairs.push({ i: pi, j: pj, similarity: s });
|
|
177
|
+
}
|
|
178
|
+
pairs.sort((a, b) => b.similarity - a.similarity);
|
|
145
179
|
clusters.push({
|
|
146
180
|
entries: kept.map((i) => entries[i]),
|
|
147
181
|
similarity: peak,
|
|
182
|
+
pairs,
|
|
148
183
|
});
|
|
149
184
|
}
|
|
150
185
|
clusters.sort((a, b) => b.similarity - a.similarity);
|
|
151
186
|
return clusters;
|
|
152
187
|
}
|
|
188
|
+
// --- Jev prefilter protocol (design 2026-09-25 §2, sweep-aligned) ------------
|
|
189
|
+
// The merge-pair thresholds (band(..., 'mergePair')) are BOUND to the t4
|
|
190
|
+
// threshold-sweep batch protocol: shared state, two-sided criteria, two
|
|
191
|
+
// 600-char side excerpts per question. Absolute scores are protocol-sensitive
|
|
192
|
+
// (t4 §4: the same case scored 0.15 / 0.60 / 0.71 across protocols), so the
|
|
193
|
+
// texts below are copied VERBATIM from
|
|
194
|
+
// .wayfinder/research/t4-labeling-kit/threshold-sweep.mjs (MRG_STATE /
|
|
195
|
+
// MRG_CRITERIA / mrgInstructions) — changing any of them requires a re-sweep
|
|
196
|
+
// of the lines, do not tune in place.
|
|
197
|
+
const PREFILTER_STATE = [
|
|
198
|
+
'背景:你在为一个个人记忆库做簇内 merge 校准。整理(consolidate)时会把讲同一问题的多个 topic 合并成一份;误并会造成信息丢失,漏并会造成库内冗余与后续漂移。',
|
|
199
|
+
'判定标准:同一问题指两条记录的是同一件事——同一 bug/特性/任务的不同阶段、两面或后续进展,合并保留一份不丢实质信息。相邻但独立不算:两条各自成立、合并会丢失各自信息(如命名澄清 vs 机制对照、修不同 bug 的相邻提交、不同仓库的两次发版)。',
|
|
200
|
+
].join('\n');
|
|
201
|
+
const PAIR_CRITERIA = {
|
|
202
|
+
true: '同一问题——两条记录的是同一件事的不同阶段/两面/后续进展,合并保留一份不丢实质信息',
|
|
203
|
+
false: '相邻但独立——各自成立,合并会丢失各自信息(如命名澄清 vs 机制对照、修不同 bug、不同仓库发版)',
|
|
204
|
+
};
|
|
205
|
+
// Cluster-level 「值得动吗」 — v1 records it only, never gates (design §2:
|
|
206
|
+
// 簇级问只记不门控,无扫描数据).
|
|
207
|
+
const CLUSTER_CRITERIA = {
|
|
208
|
+
true: '值得整理——簇内存在值得动作的条目(重复可并、draft 已稳定、已被取代、元数据失准),动作有实质收益',
|
|
209
|
+
false: '不值得动——条目各自成立,或任何整理动作都不会带来实质收益',
|
|
210
|
+
};
|
|
211
|
+
/** Pair question body — the sweep's mrgInstructions: two sides of
|
|
212
|
+
* title/tags/conclusion excerpt (ClusterEntry.conclusion is already capped
|
|
213
|
+
* at CONCLUSION_SNIPPET_CHARS = the sweep's 600-char protocol). */
|
|
214
|
+
function pairInstructions(a, b) {
|
|
215
|
+
const side = (t, name) => [`## topic ${name}`, `- title: ${t.title}`, `- tags: ${t.tags.join(', ')}`, '- conclusion:', t.conclusion].join('\n');
|
|
216
|
+
return [side(a, 'A'), '', side(b, 'B'), '', '综合以上材料判断:这两个 topic 讲的是同一个问题、应当合并吗?'].join('\n');
|
|
217
|
+
}
|
|
218
|
+
/** Cluster-level question body — titles/status/tags only, no conclusions. */
|
|
219
|
+
function clusterInstructions(cluster) {
|
|
220
|
+
const lines = cluster.entries.map((e) => `- ${e.title}(${e.status},tags: ${e.tags.join(', ')})`);
|
|
221
|
+
return [
|
|
222
|
+
'## 候选簇内的 topic',
|
|
223
|
+
...lines,
|
|
224
|
+
'',
|
|
225
|
+
'综合以上材料判断:这一簇值得一次整理动作吗(merge/promote/deprecate/refresh 任一)?',
|
|
226
|
+
].join('\n');
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* The prefilter's jev config slice — undefined unless jevEnabled is explicitly
|
|
230
|
+
* true (default-off master switch: false ⇒ the whole branch is dead code and
|
|
231
|
+
* today's behavior runs untouched). Optional keys fall back to their
|
|
232
|
+
* documented defaults (config.ts: the bare-harness DEFAULTS literal predates
|
|
233
|
+
* them; consumers treat undefined as the default).
|
|
234
|
+
*/
|
|
235
|
+
function jevConfigOf(cfg) {
|
|
236
|
+
if (cfg.jevEnabled !== true)
|
|
237
|
+
return undefined;
|
|
238
|
+
return {
|
|
239
|
+
jevBackend: cfg.jevBackend ?? 'zen',
|
|
240
|
+
jevModel: cfg.jevModel ?? '',
|
|
241
|
+
jevTimeoutMs: cfg.jevTimeoutMs ?? 3000,
|
|
242
|
+
jevSecretFile: cfg.jevSecretFile ?? '',
|
|
243
|
+
};
|
|
244
|
+
}
|
|
153
245
|
// --- Deprecated-TTL housekeeping (local rule, no model) ----------------------
|
|
154
246
|
/**
|
|
155
247
|
* Drop `deprecated` topics older than `ttlDays` — pure housekeeping, never
|
|
@@ -191,9 +283,15 @@ export class Consolidator {
|
|
|
191
283
|
inFlight;
|
|
192
284
|
service;
|
|
193
285
|
caller;
|
|
194
|
-
|
|
286
|
+
askJev;
|
|
287
|
+
/** Best-effort host-logger sink (warn-style); undefined = stay silent,
|
|
288
|
+
* aligned with the lane's quiet discipline for success/no-clusters. */
|
|
289
|
+
note;
|
|
290
|
+
constructor(service, caller, askJev = jevAsk, note) {
|
|
195
291
|
this.service = service;
|
|
196
292
|
this.caller = caller;
|
|
293
|
+
this.askJev = askJev;
|
|
294
|
+
this.note = note;
|
|
197
295
|
}
|
|
198
296
|
get configured() {
|
|
199
297
|
return this.caller !== undefined;
|
|
@@ -273,9 +371,22 @@ export class Consolidator {
|
|
|
273
371
|
let evaluated = 0;
|
|
274
372
|
let fatal;
|
|
275
373
|
let failureDetail;
|
|
374
|
+
// jev prefilter (design §2): one cheap System One batch decides whether a
|
|
375
|
+
// cluster is worth the LLM call at all. undefined = jevEnabled off ⇒ the
|
|
376
|
+
// whole branch never executes and behavior is exactly as before.
|
|
377
|
+
const jev = jevConfigOf(this.service.cfg);
|
|
378
|
+
let prefilterSkipped = 0;
|
|
276
379
|
for (const cluster of clusters) {
|
|
277
380
|
if (calls >= MAX_MODEL_CALLS_PER_RUN)
|
|
278
381
|
break;
|
|
382
|
+
if (jev !== undefined) {
|
|
383
|
+
const pf = await this.prefilterCluster(jev, cluster);
|
|
384
|
+
if (!pf.proceed) {
|
|
385
|
+
prefilterSkipped += 1;
|
|
386
|
+
this.note?.(`topics consolidate: 前置过滤跳过簇(pair 均未达 adopt 线):${cluster.entries.map((e) => e.slug).join('、')}`);
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
279
390
|
const outcome = await this.runCluster(sessionId, cluster);
|
|
280
391
|
calls += 1;
|
|
281
392
|
if (outcome.fatal !== undefined) {
|
|
@@ -334,6 +445,7 @@ export class Consolidator {
|
|
|
334
445
|
const tail = [
|
|
335
446
|
applied.dropped > 0 ? `丢弃 ${applied.dropped} 个无效 op` : undefined,
|
|
336
447
|
`${calls} 次模型调用(评估 ${evaluated} 簇 / 候选 ${clusters.length} 簇)`,
|
|
448
|
+
prefilterSkipped > 0 ? `前置过滤跳过 ${prefilterSkipped} 簇` : undefined,
|
|
337
449
|
fatal !== undefined ? `中途失败:${failureDetail ?? 'unknown'}` : undefined,
|
|
338
450
|
stampNote === '' ? undefined : stampNote.replace(/^;/, ''),
|
|
339
451
|
]
|
|
@@ -378,6 +490,88 @@ export class Consolidator {
|
|
|
378
490
|
}
|
|
379
491
|
return clusterTopics(entries).slice(0, MAX_MODEL_CALLS_PER_RUN);
|
|
380
492
|
}
|
|
493
|
+
/**
|
|
494
|
+
* One jev request per cluster (design 2026-09-25 §2 整理 lane 前置). The
|
|
495
|
+
* top-k similar pairs (k ≤ {@link PREFILTER_TOP_PAIRS}, edges reused from
|
|
496
|
+
* clustering) each get a pair noul question 「同一问题吗」; one record-only
|
|
497
|
+
* cluster-level 「值得动吗」 rides along (固定 band='record', never gates).
|
|
498
|
+
* Gate: ≥1 pair in the adopt band → the cluster goes to the LLM; all pairs
|
|
499
|
+
* below → skipped (省调用). Any jev failure proceeds fail-open — the
|
|
500
|
+
* call-layer telemetry row (outcome !== 'ok' marks the fail-open) was already written by jevAsk
|
|
501
|
+
* itself; only the verdict layer is ours here (agree='n/a', nothing lexical
|
|
502
|
+
* to reconcile against on this lane).
|
|
503
|
+
*/
|
|
504
|
+
async prefilterCluster(jev, cluster) {
|
|
505
|
+
const pairs = cluster.pairs.slice(0, PREFILTER_TOP_PAIRS);
|
|
506
|
+
// No local similarity evidence (e.g. a capped cluster whose edges all
|
|
507
|
+
// fell outside) — behave exactly as today, zero jev calls.
|
|
508
|
+
if (pairs.length === 0)
|
|
509
|
+
return { proceed: true, failOpen: false };
|
|
510
|
+
const questions = {};
|
|
511
|
+
const metas = [];
|
|
512
|
+
pairs.forEach((p, idx) => {
|
|
513
|
+
const a = cluster.entries[p.i];
|
|
514
|
+
const b = cluster.entries[p.j];
|
|
515
|
+
const instructions = pairInstructions(a, b);
|
|
516
|
+
const questionId = `p${idx + 1}`;
|
|
517
|
+
questions[questionId] = { type: 'noul', instructions, criteria: PAIR_CRITERIA };
|
|
518
|
+
metas.push({ questionId, ref: `pair:${a.slug}|${b.slug}`, instructions, gating: true });
|
|
519
|
+
});
|
|
520
|
+
const clusterQuestion = clusterInstructions(cluster);
|
|
521
|
+
questions['cluster'] = { type: 'noul', instructions: clusterQuestion, criteria: CLUSTER_CRITERIA };
|
|
522
|
+
metas.push({
|
|
523
|
+
questionId: 'cluster',
|
|
524
|
+
ref: `cluster:${cluster.entries[0]?.slug ?? ''}`,
|
|
525
|
+
instructions: clusterQuestion,
|
|
526
|
+
gating: false,
|
|
527
|
+
});
|
|
528
|
+
const r = await this.askJev({
|
|
529
|
+
state: PREFILTER_STATE,
|
|
530
|
+
questions,
|
|
531
|
+
config: jev,
|
|
532
|
+
lane: 'consolidate-prefilter',
|
|
533
|
+
fallback: false,
|
|
534
|
+
// fallback stays FALSE at call time (jevAsk writes before the outcome
|
|
535
|
+
// is known); fail-open occurrence = outcome !== 'ok' — this lane's
|
|
536
|
+
// "cluster goes to the LLM as today" is deterministic per ADR 0018.
|
|
537
|
+
});
|
|
538
|
+
if (!r.ok)
|
|
539
|
+
return { proceed: true, failOpen: true };
|
|
540
|
+
const at = new Date().toISOString();
|
|
541
|
+
const recs = [];
|
|
542
|
+
let adopt = false;
|
|
543
|
+
let failOpen = false;
|
|
544
|
+
for (const meta of metas) {
|
|
545
|
+
// Answer shape per the sweep protocol: noul answers carry the
|
|
546
|
+
// probability on the `noul` field; anything else is unusable.
|
|
547
|
+
const raw = r.answers[meta.questionId]?.noul;
|
|
548
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw)) {
|
|
549
|
+
// A gate question without a usable score must never veto the cluster.
|
|
550
|
+
if (meta.gating)
|
|
551
|
+
failOpen = true;
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
const probability = Math.min(1, Math.max(0, raw));
|
|
555
|
+
const verdictBand = meta.gating ? band(probability, 'mergePair') : 'record';
|
|
556
|
+
if (verdictBand === 'adopt')
|
|
557
|
+
adopt = true;
|
|
558
|
+
recs.push({
|
|
559
|
+
at,
|
|
560
|
+
lane: 'consolidate-prefilter',
|
|
561
|
+
questionId: meta.questionId,
|
|
562
|
+
qtype: 'noul',
|
|
563
|
+
ref: meta.ref,
|
|
564
|
+
digest: digestFor(PREFILTER_STATE, meta.questionId, meta.instructions),
|
|
565
|
+
probability,
|
|
566
|
+
band: verdictBand,
|
|
567
|
+
agree: 'n/a',
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
await logVerdicts(recs);
|
|
571
|
+
if (failOpen)
|
|
572
|
+
return { proceed: true, failOpen: true };
|
|
573
|
+
return { proceed: adopt, failOpen: false };
|
|
574
|
+
}
|
|
381
575
|
/** One model call over one cluster, ops shape-validated but not yet applied. */
|
|
382
576
|
async runCluster(sessionId, cluster) {
|
|
383
577
|
const caller = this.caller;
|
package/lib/index.d.ts
CHANGED
|
@@ -113,6 +113,11 @@ export declare const Config: import("@deepseek-ai/schemastery").default<Schemast
|
|
|
113
113
|
deprecatedTtlDays: import("@deepseek-ai/schemastery").default<number, number, "volatile-defined">;
|
|
114
114
|
usageBoost: import("@deepseek-ai/schemastery").default<number, number, "volatile-defined">;
|
|
115
115
|
pushDebounceSeconds: import("@deepseek-ai/schemastery").default<number, number, "volatile-defined">;
|
|
116
|
+
jevEnabled: import("@deepseek-ai/schemastery").default<boolean, boolean, "volatile-defined">;
|
|
117
|
+
jevBackend: import("@deepseek-ai/schemastery").default<string, string, "volatile-defined">;
|
|
118
|
+
jevModel: import("@deepseek-ai/schemastery").default<string, string, "volatile-defined">;
|
|
119
|
+
jevTimeoutMs: import("@deepseek-ai/schemastery").default<number, number, "volatile-defined">;
|
|
120
|
+
jevSecretFile: import("@deepseek-ai/schemastery").default<string, string, "volatile-defined">;
|
|
116
121
|
}>>, Schemastery.ObjectT<NoInfer<{
|
|
117
122
|
repo: import("@deepseek-ai/schemastery").default<string, string, "volatile-defined">;
|
|
118
123
|
autoInject: import("@deepseek-ai/schemastery").default<boolean, boolean, "volatile-defined">;
|
|
@@ -140,6 +145,11 @@ export declare const Config: import("@deepseek-ai/schemastery").default<Schemast
|
|
|
140
145
|
deprecatedTtlDays: import("@deepseek-ai/schemastery").default<number, number, "volatile-defined">;
|
|
141
146
|
usageBoost: import("@deepseek-ai/schemastery").default<number, number, "volatile-defined">;
|
|
142
147
|
pushDebounceSeconds: import("@deepseek-ai/schemastery").default<number, number, "volatile-defined">;
|
|
148
|
+
jevEnabled: import("@deepseek-ai/schemastery").default<boolean, boolean, "volatile-defined">;
|
|
149
|
+
jevBackend: import("@deepseek-ai/schemastery").default<string, string, "volatile-defined">;
|
|
150
|
+
jevModel: import("@deepseek-ai/schemastery").default<string, string, "volatile-defined">;
|
|
151
|
+
jevTimeoutMs: import("@deepseek-ai/schemastery").default<number, number, "volatile-defined">;
|
|
152
|
+
jevSecretFile: import("@deepseek-ai/schemastery").default<string, string, "volatile-defined">;
|
|
143
153
|
}>>, "plain">;
|
|
144
154
|
/** Live config reference as a 0.1.7 host passes it (cron / mcp-adapter
|
|
145
155
|
* spelling): `.get()` snapshots the current value — the host swaps the
|
package/lib/index.js
CHANGED
|
@@ -317,6 +317,7 @@ export async function apply(ctx, config = {}) {
|
|
|
317
317
|
},
|
|
318
318
|
});
|
|
319
319
|
const agents = () => ctx.agents;
|
|
320
|
+
const fastGateBySession = new Map();
|
|
320
321
|
// SYNCHRONOUS retrieval: the spliced event dispatches before prompt
|
|
321
322
|
// assembly, and the context() provider reads the assembled digest
|
|
322
323
|
// synchronously — an async round would always lose this race.
|
|
@@ -366,12 +367,52 @@ export async function apply(ctx, config = {}) {
|
|
|
366
367
|
for (const slug of r.slowIncluded)
|
|
367
368
|
marked.add(slug);
|
|
368
369
|
}
|
|
370
|
+
// Fastgate shadow capture (design §2 row 3): this round's candidates with
|
|
371
|
+
// their lexical dispositions (retrieval.ts split: gate-blocked rides the
|
|
372
|
+
// near-miss reasons). Pure metadata on an already-built result — no hot
|
|
373
|
+
// path cost beyond the array literal, and hard-gated on the master switch.
|
|
374
|
+
if (cfg.jevEnabled === true && r.outcome.rosterSize > 0) {
|
|
375
|
+
fastGateBySession.set(sessionId, {
|
|
376
|
+
query,
|
|
377
|
+
candidates: [
|
|
378
|
+
...r.outcome.hits.map((h) => ({ slug: h.slug, disposition: 'hit' })),
|
|
379
|
+
...r.outcome.nearMisses.map((nm) => ({
|
|
380
|
+
slug: nm.slug,
|
|
381
|
+
disposition: nm.reasons.includes('gate-blocked') ? 'gate-blocked' : 'nearFloor',
|
|
382
|
+
})),
|
|
383
|
+
],
|
|
384
|
+
});
|
|
385
|
+
}
|
|
369
386
|
state.injectionText = r.text;
|
|
370
387
|
}
|
|
371
388
|
catch {
|
|
372
389
|
// Contained: a retrieval failure must never break the turn.
|
|
373
390
|
}
|
|
374
391
|
}
|
|
392
|
+
/**
|
|
393
|
+
* Fastgate shadow trigger: consumes the spliced-time capture and hands it to
|
|
394
|
+
* the lane's shadow entry (which applies the jevEnabled / cadence gates and
|
|
395
|
+
* serializes behind a same-session produce pipeline). One audit per round —
|
|
396
|
+
* the capture is consumed here.
|
|
397
|
+
*/
|
|
398
|
+
function dispatchFastGateShadow(sessionId) {
|
|
399
|
+
try {
|
|
400
|
+
if (cfgNow().jevEnabled !== true)
|
|
401
|
+
return;
|
|
402
|
+
const captured = fastGateBySession.get(sessionId);
|
|
403
|
+
if (captured === undefined)
|
|
404
|
+
return;
|
|
405
|
+
fastGateBySession.delete(sessionId);
|
|
406
|
+
slowLane.dispatchFastGateShadow(sessionId, {
|
|
407
|
+
query: captured.query,
|
|
408
|
+
candidates: captured.candidates,
|
|
409
|
+
turnId: observer.turnCountOf(sessionId),
|
|
410
|
+
});
|
|
411
|
+
}
|
|
412
|
+
catch {
|
|
413
|
+
// contained — the shadow must never break the event handler
|
|
414
|
+
}
|
|
415
|
+
}
|
|
375
416
|
/**
|
|
376
417
|
* Slow-lane production trigger at turn/end (v4 §4.2). Host-side gates live
|
|
377
418
|
* here: subagent sessions never run the lane (hard guard, independent of
|
|
@@ -387,8 +428,12 @@ export async function apply(ctx, config = {}) {
|
|
|
387
428
|
return;
|
|
388
429
|
if (isDelegated(agents()?.get(sessionId)))
|
|
389
430
|
return;
|
|
390
|
-
if (distiller.hasPending(sessionId))
|
|
431
|
+
if (distiller.hasPending(sessionId)) {
|
|
432
|
+
// The lane yields to the distill run — but the shadow shares no LLM
|
|
433
|
+
// route, so it still fires on this sampled trigger.
|
|
434
|
+
dispatchFastGateShadow(sessionId);
|
|
391
435
|
return;
|
|
436
|
+
}
|
|
392
437
|
// Same trigger-time capture the distill trigger uses: the agent's
|
|
393
438
|
// scoped llm is alive NOW, and the lane's caller reads the captured
|
|
394
439
|
// entry lazily inside its async pipeline (released via the
|
|
@@ -418,6 +463,10 @@ export async function apply(ctx, config = {}) {
|
|
|
418
463
|
return merged;
|
|
419
464
|
})(),
|
|
420
465
|
});
|
|
466
|
+
// Same trigger, after the dispatch call: the shadow serializes behind
|
|
467
|
+
// the produce pipeline (rerank 完成后) or runs standalone when the
|
|
468
|
+
// dispatch produced nothing (无慢道任务时独立跑).
|
|
469
|
+
dispatchFastGateShadow(sessionId);
|
|
421
470
|
}
|
|
422
471
|
catch {
|
|
423
472
|
// contained — the lane must never break the event handler
|
|
@@ -485,7 +534,7 @@ export async function apply(ctx, config = {}) {
|
|
|
485
534
|
// Consolidation lane (整理): same caller/route as distill, global single
|
|
486
535
|
// flight inside. Reads the pool and merges/promotes/deprecates/refreshes;
|
|
487
536
|
// cadence-gated at session start, manual via /topics consolidate.
|
|
488
|
-
const consolidator = new Consolidator(service, caller);
|
|
537
|
+
const consolidator = new Consolidator(service, caller, undefined, warn);
|
|
489
538
|
// ---- Observer (M2) ----
|
|
490
539
|
// Trigger runs are fire-and-forget everywhere now — including the exit
|
|
491
540
|
// one. Observations are write-through on disk, so a run the process exits
|
|
@@ -617,6 +666,7 @@ export async function apply(ctx, config = {}) {
|
|
|
617
666
|
// fresh process has no dedup registry, so allow re-injection.
|
|
618
667
|
injectedBySession.delete(sessionId);
|
|
619
668
|
slowLane.clear(sessionId);
|
|
669
|
+
fastGateBySession.delete(sessionId);
|
|
620
670
|
}
|
|
621
671
|
observer.onSessionEvent(sessionId, event.type, event.data);
|
|
622
672
|
// AFTER the observer processed the turn (turnCount already bumped, so
|
|
@@ -651,6 +701,7 @@ export async function apply(ctx, config = {}) {
|
|
|
651
701
|
observer.onSessionEvent(sessionId, name, undefined);
|
|
652
702
|
injectedBySession.delete(sessionId);
|
|
653
703
|
slowLane.clear(sessionId);
|
|
704
|
+
fastGateBySession.delete(sessionId);
|
|
654
705
|
// A teardown-triggered run (session-end distill) reads the payload
|
|
655
706
|
// capture lazily: drop the entry here only when no run can still read
|
|
656
707
|
// it. The slow lane's in-flight pipeline reads it too (shared caller);
|
|
@@ -898,23 +949,26 @@ export async function apply(ctx, config = {}) {
|
|
|
898
949
|
// The 0.1.7 host imports the old settings.yaml ONCE by "section name =
|
|
899
950
|
// entry id" and renames it settings.yaml.imported — this plugin's legacy
|
|
900
951
|
// section was named `topics`, so the host's import silently dropped it.
|
|
901
|
-
// Recover it here (see legacy-import.ts).
|
|
902
|
-
//
|
|
903
|
-
//
|
|
904
|
-
//
|
|
905
|
-
//
|
|
906
|
-
//
|
|
907
|
-
|
|
908
|
-
|
|
952
|
+
// Recover it here (see legacy-import.ts). Wired through
|
|
953
|
+
// ctx.inject(['settings'], …) instead of a direct `ctx.settings` read: a
|
|
954
|
+
// real boot can run apply() before the settings service is mounted, the
|
|
955
|
+
// direct read came up undefined, and the import then took the silent
|
|
956
|
+
// no-settings exit EVERY boot (no marker, nothing ever recovered). The
|
|
957
|
+
// inject callback fires the moment the service exists (immediately when
|
|
958
|
+
// it already does — fake-ctx tests stay synchronous), fire-and-forget so
|
|
959
|
+
// activation never waits on it. Fully contained: any failure only warns
|
|
960
|
+
// and leaves the audit marker unwritten, so the next boot retries;
|
|
961
|
+
// activation never depends on this.
|
|
962
|
+
ctx.inject(['settings'], (sctx) => {
|
|
963
|
+
void runLegacySettingsImport({
|
|
909
964
|
home: paths.resolveDshHome(),
|
|
910
|
-
settings:
|
|
965
|
+
settings: sctx.settings,
|
|
911
966
|
logger: ctx.logger,
|
|
912
967
|
getCurrent: (key) => cfgNow()[key],
|
|
968
|
+
}).catch((error) => {
|
|
969
|
+
warn(`dsh-topics-memory 旧 settings 迁移失败(不影响启动,下次启动重试):${error instanceof Error ? error.message : String(error)}`);
|
|
913
970
|
});
|
|
914
|
-
}
|
|
915
|
-
catch (error) {
|
|
916
|
-
warn(`dsh-topics-memory 旧 settings 迁移失败(不影响启动,下次启动重试):${error instanceof Error ? error.message : String(error)}`);
|
|
917
|
-
}
|
|
971
|
+
});
|
|
918
972
|
}
|
|
919
973
|
/** Config defaults for harnesses that skip schemastery's default application. */
|
|
920
974
|
const DEFAULTS = {
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backend table for the System One integration (design 2026-09-25 §3.1),
|
|
3
|
+
* aligned with dsh-jev-mcp `backends.mjs` (three-endpoint field evidence,
|
|
4
|
+
* zero changes there). Model ids are PINNED versioned ids: aliases
|
|
5
|
+
* (jev-latest & friends) silently move on every vendor release, so routing
|
|
6
|
+
* thresholds and eval numbers stop meaning anything without a deliberate
|
|
7
|
+
* model bump.
|
|
8
|
+
*
|
|
9
|
+
* @module jev/backends
|
|
10
|
+
*/
|
|
11
|
+
export type JevBackendName = 'zen' | 'native' | 'openrouter';
|
|
12
|
+
export type JevFamily = 'systemone' | 'decisions';
|
|
13
|
+
export interface JevBackend {
|
|
14
|
+
readonly name: JevBackendName;
|
|
15
|
+
/** Wire protocol family: systemone (instructions required, noul criteria
|
|
16
|
+
* optional, structured score levels allowed) vs decisions (noul criteria
|
|
17
|
+
* REQUIRED two-sided, score levels strings only). */
|
|
18
|
+
readonly family: JevFamily;
|
|
19
|
+
readonly endpoint: string;
|
|
20
|
+
readonly defaultModel: string;
|
|
21
|
+
readonly keyEnv: string;
|
|
22
|
+
readonly keychainService: string;
|
|
23
|
+
/** Only zen falls back to its default keychain service when JEV_KEYCHAIN
|
|
24
|
+
* is unset; native/openrouter expose a service for onboarding but still
|
|
25
|
+
* require an explicit JEV_KEYCHAIN (dsh-jev-mcp server.mjs parity). */
|
|
26
|
+
readonly keychainByDefault: boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare const BACKENDS: Readonly<Record<JevBackendName, JevBackend>>;
|
|
29
|
+
/** Resolve a config value to a backend; anything but a known name (bare
|
|
30
|
+
* harnesses, future drift) lands on the config default `zen`. */
|
|
31
|
+
export declare function resolveBackend(name: string | undefined): JevBackend;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Backend table for the System One integration (design 2026-09-25 §3.1),
|
|
3
|
+
* aligned with dsh-jev-mcp `backends.mjs` (three-endpoint field evidence,
|
|
4
|
+
* zero changes there). Model ids are PINNED versioned ids: aliases
|
|
5
|
+
* (jev-latest & friends) silently move on every vendor release, so routing
|
|
6
|
+
* thresholds and eval numbers stop meaning anything without a deliberate
|
|
7
|
+
* model bump.
|
|
8
|
+
*
|
|
9
|
+
* @module jev/backends
|
|
10
|
+
*/
|
|
11
|
+
export const BACKENDS = {
|
|
12
|
+
zen: {
|
|
13
|
+
name: 'zen',
|
|
14
|
+
family: 'systemone',
|
|
15
|
+
endpoint: 'https://opencode.ai/zen/v1/systemone',
|
|
16
|
+
defaultModel: 'jev-1.13-free',
|
|
17
|
+
keyEnv: 'JEV_ZEN_API_KEY',
|
|
18
|
+
keychainService: 'opencode-zen-inference',
|
|
19
|
+
keychainByDefault: true,
|
|
20
|
+
},
|
|
21
|
+
native: {
|
|
22
|
+
name: 'native',
|
|
23
|
+
family: 'systemone',
|
|
24
|
+
endpoint: 'https://api.typesafe.ai/v1/systemone',
|
|
25
|
+
defaultModel: 'jev-1.13.0',
|
|
26
|
+
keyEnv: 'TYPESAFE_API_KEY',
|
|
27
|
+
keychainService: 'jev-typesafe',
|
|
28
|
+
keychainByDefault: false,
|
|
29
|
+
},
|
|
30
|
+
openrouter: {
|
|
31
|
+
name: 'openrouter',
|
|
32
|
+
family: 'decisions',
|
|
33
|
+
endpoint: 'https://openrouter.ai/api/alpha/decisions',
|
|
34
|
+
defaultModel: 'typesafe/jev-1.13',
|
|
35
|
+
keyEnv: 'OPENROUTER_API_KEY',
|
|
36
|
+
keychainService: 'openrouter-inference',
|
|
37
|
+
keychainByDefault: false,
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
/** Resolve a config value to a backend; anything but a known name (bare
|
|
41
|
+
* harnesses, future drift) lands on the config default `zen`. */
|
|
42
|
+
export function resolveBackend(name) {
|
|
43
|
+
if (name === 'native' || name === 'openrouter')
|
|
44
|
+
return BACKENDS[name];
|
|
45
|
+
return BACKENDS.zen;
|
|
46
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
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 type { JevFamily } from './backends.ts';
|
|
16
|
+
import type { GateLogger } from './gate.ts';
|
|
17
|
+
import type { JevLane, JevOutcome, JevUsage } from './log.ts';
|
|
18
|
+
export type JevQuestionType = 'noul' | 'choice' | 'score';
|
|
19
|
+
export interface JevQuestion {
|
|
20
|
+
type: JevQuestionType;
|
|
21
|
+
/** The plugin ALWAYS sends instructions (systemone requires them). */
|
|
22
|
+
instructions: string;
|
|
23
|
+
criteria?: unknown;
|
|
24
|
+
}
|
|
25
|
+
/** The config slice jevAsk needs (pass straight from TopicsConfigValue). */
|
|
26
|
+
export interface JevAskConfig {
|
|
27
|
+
jevBackend: string;
|
|
28
|
+
jevModel: string;
|
|
29
|
+
jevTimeoutMs: number;
|
|
30
|
+
jevSecretFile: string;
|
|
31
|
+
}
|
|
32
|
+
export interface JevAskArgs {
|
|
33
|
+
state: string;
|
|
34
|
+
questions: Record<string, JevQuestion>;
|
|
35
|
+
config: JevAskConfig;
|
|
36
|
+
/** Seam identity for the decisions.jsonl call-layer row (§6.2) — required
|
|
37
|
+
* so telemetry cannot be silently skipped. */
|
|
38
|
+
lane: JevLane;
|
|
39
|
+
/** Whether the caller falls back to the legacy path on failure (§6.2
|
|
40
|
+
* `fallback` — the fail-open health metric). */
|
|
41
|
+
fallback?: boolean;
|
|
42
|
+
/** Host logger (optional, warn-only, best-effort). */
|
|
43
|
+
logger?: GateLogger;
|
|
44
|
+
}
|
|
45
|
+
export type JevAnswer = Record<string, unknown>;
|
|
46
|
+
export type JevAskOk = {
|
|
47
|
+
ok: true;
|
|
48
|
+
answers: Record<string, JevAnswer>;
|
|
49
|
+
usage: JevUsage | null;
|
|
50
|
+
latencyMs: number;
|
|
51
|
+
};
|
|
52
|
+
export type JevAskFailure = {
|
|
53
|
+
ok: false;
|
|
54
|
+
outcome: Exclude<JevOutcome, 'ok'>;
|
|
55
|
+
message: string;
|
|
56
|
+
latencyMs: number;
|
|
57
|
+
};
|
|
58
|
+
export type JevAskResult = JevAskOk | JevAskFailure;
|
|
59
|
+
export interface KeychainSpec {
|
|
60
|
+
service: string;
|
|
61
|
+
account?: string;
|
|
62
|
+
}
|
|
63
|
+
/** 'svc' -> {service:'svc'}; 'svc:acct' -> {service:'svc', account:'acct'} */
|
|
64
|
+
export declare function parseKeychainSpec(spec: string): KeychainSpec;
|
|
65
|
+
export type KeychainRunner = (file: string, args: string[]) => Promise<{
|
|
66
|
+
stdout: string;
|
|
67
|
+
}>;
|
|
68
|
+
/** Runs `security find-generic-password -w`; null on any failure (silent). */
|
|
69
|
+
export declare function keychainLookup(spec: KeychainSpec, runner?: KeychainRunner): Promise<string | null>;
|
|
70
|
+
/** Resolve the API key for a backend: env var wins, then the keychain spec
|
|
71
|
+
* (explicit JEV_KEYCHAIN, or the backend default for zen only). */
|
|
72
|
+
export declare function resolveApiKey(backend: {
|
|
73
|
+
keyEnv: string;
|
|
74
|
+
keychainService: string;
|
|
75
|
+
keychainByDefault: boolean;
|
|
76
|
+
}, lookup?: (spec: KeychainSpec) => Promise<string | null>): Promise<string>;
|
|
77
|
+
type WireQuestions = Record<string, Record<string, unknown>>;
|
|
78
|
+
interface NormalizeOk {
|
|
79
|
+
ok: true;
|
|
80
|
+
questions: WireQuestions;
|
|
81
|
+
}
|
|
82
|
+
interface NormalizeRefusal {
|
|
83
|
+
ok: false;
|
|
84
|
+
message: string;
|
|
85
|
+
}
|
|
86
|
+
export declare function normalizeQuestions(questions: Record<string, JevQuestion>, family: JevFamily): NormalizeOk | NormalizeRefusal;
|
|
87
|
+
/** Ask the System One decision model. One request for the whole batch;
|
|
88
|
+
* resolves to a structured failure (never throws) so the calling lane can
|
|
89
|
+
* fail open. Writes the decisions.jsonl call-layer row on EVERY path. */
|
|
90
|
+
export declare function jevAsk(args: JevAskArgs): Promise<JevAskResult>;
|
|
91
|
+
export {};
|