dsh-decision-layer 0.5.5 → 0.5.6

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 CHANGED
@@ -40,7 +40,7 @@ dsh plugin --profile web add file:/绝对路径/dsh-decision-layer
40
40
  - **产出自检**:回合结束用评分量表给最终产出打分(0–2,按各档位概率加权,可能是小数)。默认只提示、不打断;可开启「低分补完」,得分偏低且模型有把握时以你的名义追加一次补完提示,同一回合最多补一次。
41
41
  - **任务完成核对**:当用户明确列出多个交付条件(编号 / 项目符号 / 多命令)时,回合结束前用单选逐项判定「已满足 / 未满足 / 证据不足」,工具证据优先、回复自述不算证据;确定性提取、不推断隐含期望。默认只观察、可配 steer,与产出自检并存、指标分列。
42
42
  - **产出质量趋势告警**:建在自检数据上的只读提醒。连续多次高置信低分时,把输入框旁触发按钮的图标从灰变橙、变红,hover 提示可能是任务变难或模型波动、建议调整 prompt 或更换模型。纯提示、不驱动自动干预;高置信门槛 / 连续轮数 / 严重档三个阈值可配(默认 0.6 / 3 / 5),偏保守、待实测校准。
43
- - **工具收窄**:回合开始时用 noul 逐个判断可选工具与本轮任务的相关性,摘掉用不上的(相关性概率低于摘除阈值,默认 0.3、可配),只在宿主已允许的集合内取交集、不扩权。默认真正生效,可切到「仅观察」只记录不改工具集。三重保护:核心工具(读写、编辑、Shell、检索、待办,以及问用户、目标、技能、交付、后台作业、协作等 agent 元能力)始终保留、不参与判定;可选工具超过候选上限(默认 20,可配)时自动降为仅观察;保留下来不足 5 个时视为判定不可信、跳过。**保留工具前缀**白名单里的工具(默认含 `mcp__openviking`、`team_task_`)也始终保留、不参与判定,适合记忆 / 检索、协作这类常驻、按需触发的工具。
43
+ - **工具收窄**:回合开始时用 noul 逐个判断可选工具与本轮任务的相关性,摘掉用不上的(相关性概率低于摘除阈值,默认 0.3、可配),只在宿主已允许的集合内取交集、不扩权。默认真正生效,可切到「仅观察」只记录不改工具集。三重保护:核心工具(读写、编辑、Shell、检索、待办,以及问用户、目标、技能、交付、后台作业、协作等 agent 元能力)始终保留、不参与判定;可选工具超过候选上限(默认 20,可配)时自动降为仅观察;保留下来不足 5 个时视为判定不可信、跳过。**保留工具前缀**白名单里的工具(默认含 `mcp__openviking`、`team_task_`)也始终保留、不参与判定,适合记忆 / 检索、协作这类常驻、按需触发的工具。**分批策略**按后端上下文容量自适应(`batchMode`,默认 `auto`):大上下文后端(如 Jev)一次请求判完全部工具;小上下文后端(如本地 tev1,约 2050 token 单请求上限)在遇到上下文超限错误时自动降级为「每工具一次请求、并发执行」并记住,之后直接走拆分,不再反复探测。也可显式设 `single`(始终整批)或 `split`(始终逐工具)。某个工具的拆分请求失败时该工具保守保留。
44
44
  - **防循环**:确定性判断,不走后端;同一调用连续重复超过阈值即拒绝,打断死循环。始终生效,不在决策点开关范围内。
45
45
  - **会话面板**:输入框下方的 dock 触发器弹出面板,展示自动裁决指标(评估数、失败回退、模型建议与实际结果分列)、会话总开关和决策日志;无自动裁决时显示空态。总开关关闭只暂停自动介入,不影响手动裁决。
46
46
  - **决策分析页**:会话内「决策分析」tab(`conversation.view`,与对话/轨迹平级、天然按会话隔离),基于已落盘日志做只读聚合,展示过程画像(各决策点触发数与结果分布、自检平均分)、质量趋势阈值回测、判断准确度汇总,并可对每条决策标注「判对 / 判错 / 说不准」。标注与介入面板同源:任一处标注都写入同一份日志、按决策 id 关联、取最新。只读日志、只追加标注,不改任何决策行为。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-decision-layer",
3
- "version": "0.5.5",
3
+ "version": "0.5.6",
4
4
  "description": "A structured decision layer for the DeepSeek Harness agent loop: a danger-call gate, output self-check, tool narrowing, and loop guard backed by a pluggable adjudication model.",
5
5
  "keywords": [
6
6
  "dsh",
@@ -97,7 +97,7 @@ function validCheck(value) {
97
97
 
98
98
  function validNarrow(value) {
99
99
  if (!safeObject(value)) return undefined;
100
- if (Object.keys(value).some(key => !['mode', 'threshold', 'keepPrefixes', 'maxCandidates'].includes(key))) throw new Error('Invalid narrowing settings');
100
+ if (Object.keys(value).some(key => !['mode', 'threshold', 'keepPrefixes', 'maxCandidates', 'batchMode'].includes(key))) throw new Error('Invalid narrowing settings');
101
101
  const narrow = {};
102
102
  if (value.mode !== undefined) {
103
103
  if (value.mode !== 'observe' && value.mode !== 'enforce') throw new Error('Invalid narrowing settings');
@@ -124,6 +124,17 @@ function validNarrow(value) {
124
124
  if (!Number.isSafeInteger(value.maxCandidates) || value.maxCandidates < 1 || value.maxCandidates > 200) throw new Error('Invalid narrowing settings');
125
125
  narrow.maxCandidates = value.maxCandidates;
126
126
  }
127
+ // Per-request batching strategy for the relevance pass, chosen by backend
128
+ // context capacity. 'single' sends every tool in one systemone request (best
129
+ // for large-context backends like Jev — one call). 'split' sends one request
130
+ // per tool concurrently (required for small-context backends like tev1, whose
131
+ // ~2050-token prompt limit rejects a many-tool request). 'auto' (default)
132
+ // tries 'single' first and falls back to 'split' only after a context-overflow
133
+ // http-error, so Jev stays at one call and tev1 self-corrects.
134
+ if (value.batchMode !== undefined) {
135
+ if (!['auto', 'single', 'split'].includes(value.batchMode)) throw new Error('Invalid narrowing settings');
136
+ narrow.batchMode = value.batchMode;
137
+ }
127
138
  return narrow;
128
139
  }
129
140
 
@@ -82,15 +82,22 @@ export function relevanceQuestions(state, tools) {
82
82
  // C. if narrowing would leave fewer than `minKeep` tools total, the verdict is
83
83
  // treated as untrustworthy and no restriction is applied.
84
84
  export function createNarrowing({ evaluate, sessions, settings, coreTools = DEFAULT_CORE_TOOLS,
85
- keepPrefixes = DEFAULT_KEEP_PREFIXES, minCandidates = 2, maxCandidates = 20, minKeep = 5 } = {}) {
85
+ keepPrefixes = DEFAULT_KEEP_PREFIXES, minCandidates = 2, maxCandidates = 20, minKeep = 5, splitConcurrency = 8 } = {}) {
86
86
  if (typeof evaluate !== 'function') throw new Error('Narrowing requires an evaluator');
87
87
  const core = new Set(Array.isArray(coreTools) ? coreTools.filter(name => typeof name === 'string') : []);
88
88
  const defaultPrefixes = Array.isArray(keepPrefixes) ? keepPrefixes.filter(prefix => typeof prefix === 'string' && prefix) : [];
89
+ const concurrency = Number.isSafeInteger(splitConcurrency) && splitConcurrency >= 1 ? splitConcurrency : 8;
90
+ // Set once a 'single' (whole-batch) pass hits a context-overflow http-error:
91
+ // from then on 'auto' sends one request per tool. A large-context backend
92
+ // (Jev) never trips this and stays at one call; a small-context backend
93
+ // (tev1, ~2050-token prompt) self-corrects after the first failure.
94
+ let adaptiveSplit = false;
89
95
  const resolveSettings = async () => {
90
96
  let raw = {};
91
97
  try { raw = (typeof settings === 'function' ? await settings() : settings) ?? {}; } catch { raw = {}; }
92
98
  const mode = raw?.mode === 'observe' ? 'observe' : 'enforce';
93
99
  const threshold = typeof raw?.threshold === 'number' && Number.isFinite(raw.threshold) && raw.threshold >= 0 && raw.threshold <= 1 ? raw.threshold : 0.3;
100
+ const batchMode = raw?.batchMode === 'single' || raw?.batchMode === 'split' ? raw.batchMode : 'auto';
94
101
  // An explicit array in settings (including empty) overrides the default set.
95
102
  const prefixes = Array.isArray(raw?.keepPrefixes)
96
103
  ? raw.keepPrefixes.filter(prefix => typeof prefix === 'string' && prefix)
@@ -100,7 +107,7 @@ export function createNarrowing({ evaluate, sessions, settings, coreTools = DEFA
100
107
  // never large enough to be rejected before it is sent.
101
108
  const configuredCap = Number.isSafeInteger(raw?.maxCandidates) && raw.maxCandidates >= 1 ? raw.maxCandidates : maxCandidates;
102
109
  const cap = Math.min(configuredCap, MAX_QUESTIONS);
103
- return { mode, threshold, prefixes, cap };
110
+ return { mode, threshold, prefixes, cap, batchMode };
104
111
  };
105
112
  const record = (turn, outcome, detail = {}) => {
106
113
  const id = turn?.agent?.session?.id;
@@ -111,12 +118,34 @@ export function createNarrowing({ evaluate, sessions, settings, coreTools = DEFA
111
118
  try { sessions.log(id, { kind: 'narrow', outcome, ...detail }); }
112
119
  catch { /* logging is best effort */ }
113
120
  };
121
+ // Send one relevance request per tool with bounded concurrency, then merge the
122
+ // per-tool answers into one result. Each request's prompt is only the task
123
+ // state plus a single question, so it cannot exceed a small-context backend's
124
+ // prompt limit. A tool whose request fails is omitted from the merged answers,
125
+ // which narrowTools treats as "keep" (best effort). Returns null only when
126
+ // every tool failed, so the caller can still report a backend error.
127
+ const evaluateSplit = async (state, tools, signal) => {
128
+ const answers = Object.create(null);
129
+ let anyOk = false;
130
+ for (let i = 0; i < tools.length; i += concurrency) {
131
+ const group = tools.slice(i, i + concurrency);
132
+ const settled = await Promise.all(group.map(async tool => {
133
+ try {
134
+ const reply = await evaluate(relevanceQuestions(state, [tool]), { signal });
135
+ const answer = reply?.answers?.[tool.name];
136
+ return isObject(answer) ? [tool.name, answer] : undefined;
137
+ } catch { return undefined; }
138
+ }));
139
+ for (const pair of settled) { if (pair) { answers[pair[0]] = pair[1]; anyOk = true; } }
140
+ }
141
+ return anyOk ? { model: 'split', answers } : null;
142
+ };
114
143
  const skip = { evaluated: false, applied: false, keep: [], drop: [], mode: 'observe' };
115
144
  return {
116
145
  async review(turn) {
117
146
  const tools = Array.isArray(turn?.tools) ? turn.tools.filter(tool => tool && typeof tool.name === 'string') : [];
118
147
  const names = tools.map(tool => tool.name);
119
- const { mode, threshold, prefixes, cap } = await resolveSettings();
148
+ const { mode, threshold, prefixes, cap, batchMode } = await resolveSettings();
120
149
  // A: core tools and keep-prefix matches are always kept and never sent to
121
150
  // the model for judging (e.g. mcp__openviking memory tools stay resident).
122
151
  const alwaysKeep = name => core.has(name) || prefixes.some(prefix => name.startsWith(prefix));
@@ -139,9 +168,29 @@ export function createNarrowing({ evaluate, sessions, settings, coreTools = DEFA
139
168
  const effectiveMode = overCap ? 'observe' : mode;
140
169
  const judged = overCap ? optional.slice(0, cap) : optional;
141
170
  const judgedNames = judged.map(tool => tool.name);
171
+ // Choose per-request strategy by backend context capacity. 'split' (or an
172
+ // 'auto' pass that already fell back) sends one request per tool so no
173
+ // prompt exceeds a small-context limit. 'single'/'auto' first try one
174
+ // whole-batch request; under 'auto', a context-overflow http-error flips
175
+ // this instance to split from then on and retries the same tools split.
142
176
  let result;
143
- try { result = await evaluate(relevanceQuestions(turn.state, judged), { signal: turn.signal }); }
144
- catch (error) { record(turn, 'error', { reason: typeof error?.reason === 'string' ? error.reason : 'unreachable', mode: effectiveMode }); return { ...skip, keep: names, mode: effectiveMode }; }
177
+ if (batchMode === 'split' || adaptiveSplit) {
178
+ result = await evaluateSplit(turn.state, judged, turn.signal);
179
+ if (!result) { record(turn, 'error', { reason: 'unreachable', mode: effectiveMode }); return { ...skip, keep: names, mode: effectiveMode }; }
180
+ } else {
181
+ try { result = await evaluate(relevanceQuestions(turn.state, judged), { signal: turn.signal }); }
182
+ catch (error) {
183
+ const reason = typeof error?.reason === 'string' ? error.reason : 'unreachable';
184
+ if (batchMode === 'auto' && reason === 'http-error') {
185
+ adaptiveSplit = true;
186
+ result = await evaluateSplit(turn.state, judged, turn.signal);
187
+ if (!result) { record(turn, 'error', { reason, mode: effectiveMode }); return { ...skip, keep: names, mode: effectiveMode }; }
188
+ } else {
189
+ record(turn, 'error', { reason, mode: effectiveMode });
190
+ return { ...skip, keep: names, mode: effectiveMode };
191
+ }
192
+ }
193
+ }
145
194
  const narrowed = narrowTools(judgedNames, result, { threshold });
146
195
  const keep = [...keptNames, ...narrowed.keep];
147
196
  const drop = narrowed.drop;