@goodandready/dsh-agent-loop-guard 0.3.6 → 0.3.7
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/lib/client.js +11 -1
- package/lib/index.js +51 -11
- package/lib/target-quota-guard.js +39 -5
- package/package.json +1 -1
package/lib/client.js
CHANGED
|
@@ -76,6 +76,7 @@ window.__ModuleLoader__.load({
|
|
|
76
76
|
progressToolNames: 'Progress tool names (comma-separated)', pseudoToolCallThreshold: 'Pseudo tool-call loop threshold (turns, 0 = disabled)',
|
|
77
77
|
oscillationDetection: 'Oscillation loop guard (ping-pong cycles)', maxOscillationRepeats: 'Max oscillation repeats (2-4 step cycles)',
|
|
78
78
|
payloadBloatGuard: 'Payload bloat guard (truncate huge outputs)', maxToolResultChars: 'Max tool result characters (0 = disabled)',
|
|
79
|
+
stopPatterns: 'Stop patterns (user message regexes, comma separated)', pseudoToolCallPatterns: 'Pseudo tool-call patterns (regex, comma separated)',
|
|
79
80
|
errorThrashingGuard: 'Error thrashing guard (fail-loop detection)', maxConsecutiveFailures: 'Max consecutive failures before thrashing block',
|
|
80
81
|
adaptiveSteering: 'Smart steering (guide agent before hard cancel)', targetQuotaGuard: 'Target quota guard (per-file loop protection)',
|
|
81
82
|
maxUnchangedReadsPerTarget: 'Max unchanged reads per target file', maxReadsPerTarget: 'Max reads per target file per turn',
|
|
@@ -103,6 +104,7 @@ window.__ModuleLoader__.load({
|
|
|
103
104
|
progressToolNames: '进度标记工具(逗号分隔)', pseudoToolCallThreshold: '伪工具调用空转拦截阈值(连续回合数,0 表示禁用)',
|
|
104
105
|
oscillationDetection: '交替循环拦截(乒乓震荡检测)', maxOscillationRepeats: '最大交替循环重复次数(2-4 步序列)',
|
|
105
106
|
payloadBloatGuard: '超大结果防膨胀拦截(截断巨大输出)', maxToolResultChars: '工具结果字符上限(0 表示禁用)',
|
|
107
|
+
stopPatterns: '停止词模式(用户消息正则,逗号分隔)', pseudoToolCallPatterns: '伪工具调用模式(正则,逗号分隔)',
|
|
106
108
|
errorThrashingGuard: '工具重复报错拦截(错误循环保护)', maxConsecutiveFailures: '连续报错熔断阈值',
|
|
107
109
|
adaptiveSteering: '智能自纠正(熔断前引导修正)', targetQuotaGuard: '目标配额守卫(单文件/目标防死循环)',
|
|
108
110
|
maxUnchangedReadsPerTarget: '单文件无变化最大重复读取次数', maxReadsPerTarget: '单文件最大读取次数',
|
|
@@ -117,7 +119,7 @@ window.__ModuleLoader__.load({
|
|
|
117
119
|
oscillationDetection: true, maxOscillationRepeats: 3, payloadBloatGuard: true, maxToolResultChars: 65536,
|
|
118
120
|
errorThrashingGuard: true, maxConsecutiveFailures: 4, adaptiveSteering: true,
|
|
119
121
|
targetQuotaGuard: true, maxUnchangedReadsPerTarget: 3, maxReadsPerTarget: 6,
|
|
120
|
-
maxEditsPerTarget: 8, maxTargetAttempts: 12,
|
|
122
|
+
maxEditsPerTarget: 8, maxTargetAttempts: 12, stopPatterns: [], pseudoToolCallPatterns: [],
|
|
121
123
|
};
|
|
122
124
|
|
|
123
125
|
const CLIENT_PRESETS = {
|
|
@@ -251,6 +253,8 @@ window.__ModuleLoader__.load({
|
|
|
251
253
|
...d, ...DEFAULTS, ...vals,
|
|
252
254
|
progressToolNamesText: names.join(', '),
|
|
253
255
|
strictToolsText: strict.join(', '),
|
|
256
|
+
stopPatternsText: (Array.isArray(vals.stopPatterns) ? vals.stopPatterns : DEFAULTS.stopPatterns).join(', '),
|
|
257
|
+
pseudoToolCallPatternsText: (Array.isArray(vals.pseudoToolCallPatterns) ? vals.pseudoToolCallPatterns : DEFAULTS.pseudoToolCallPatterns).join(', '),
|
|
254
258
|
}));
|
|
255
259
|
}
|
|
256
260
|
setStatus('ready');
|
|
@@ -320,6 +324,8 @@ window.__ModuleLoader__.load({
|
|
|
320
324
|
setMsg('');
|
|
321
325
|
const names = draft.progressToolNamesText.split(',').map((s) => s.trim()).filter(Boolean);
|
|
322
326
|
const strictList = draft.strictToolsText.split(',').map((s) => s.trim()).filter(Boolean);
|
|
327
|
+
const stopList = String(draft.stopPatternsText || '').split(/[,\n]/).map((s) => s.trim()).filter(Boolean);
|
|
328
|
+
const pseudoList = String(draft.pseudoToolCallPatternsText || '').split(/[,\n]/).map((s) => s.trim()).filter(Boolean);
|
|
323
329
|
const payload = {
|
|
324
330
|
maxToolAttemptsPerTurn: Number(draft.maxToolAttemptsPerTurn) || 0, maxProgressToolCallsPerTurn: Number(draft.maxProgressToolCallsPerTurn) || 16,
|
|
325
331
|
maxCallsPerRepeatGroup: Number(draft.maxCallsPerRepeatGroup) || 5, strictTools: strictList, strictToolLimit: Number(draft.strictToolLimit) || 3,
|
|
@@ -336,6 +342,8 @@ window.__ModuleLoader__.load({
|
|
|
336
342
|
maxReadsPerTarget: Number(draft.maxReadsPerTarget) || 6,
|
|
337
343
|
maxEditsPerTarget: Number(draft.maxEditsPerTarget) || 8,
|
|
338
344
|
maxTargetAttempts: Number(draft.maxTargetAttempts) || 12,
|
|
345
|
+
stopPatterns: stopList,
|
|
346
|
+
pseudoToolCallPatterns: pseudoList,
|
|
339
347
|
};
|
|
340
348
|
const errs = [];
|
|
341
349
|
for (const [k, v] of Object.entries(payload)) {
|
|
@@ -479,6 +487,8 @@ window.__ModuleLoader__.load({
|
|
|
479
487
|
field('maxAssistantBlockChars', num('maxAssistantBlockChars')),
|
|
480
488
|
field('progressToolNames', str('progressToolNamesText')),
|
|
481
489
|
field('pseudoToolCallThreshold', num('pseudoToolCallThreshold')),
|
|
490
|
+
field('stopPatterns', str('stopPatternsText')),
|
|
491
|
+
field('pseudoToolCallPatterns', str('pseudoToolCallPatternsText')),
|
|
482
492
|
check('oscillationDetection'),
|
|
483
493
|
field('maxOscillationRepeats', num('maxOscillationRepeats')),
|
|
484
494
|
check('payloadBloatGuard'),
|
package/lib/index.js
CHANGED
|
@@ -28,6 +28,48 @@ export function truncateText(text, maxChars = 65536, headRatio = 0.7) {
|
|
|
28
28
|
return head + banner + tail;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
const DEEP_TRUNCATE_MAX_DEPTH = 6;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Truncates oversized strings anywhere inside a JSON-like value (#177).
|
|
35
|
+
* Plain objects and arrays are cloned only along the changed path; cycles,
|
|
36
|
+
* non-plain objects (Buffer, Date, class instances) and the depth limit are
|
|
37
|
+
* left untouched.
|
|
38
|
+
*/
|
|
39
|
+
function truncateDeep(value, maxChars, depth = 0, seen = new WeakSet()) {
|
|
40
|
+
if (typeof value === 'string') {
|
|
41
|
+
return value.length > maxChars ? truncateText(value, maxChars) : value;
|
|
42
|
+
}
|
|
43
|
+
if (value === null || typeof value !== 'object' || depth >= DEEP_TRUNCATE_MAX_DEPTH) return value;
|
|
44
|
+
if (seen.has(value)) return value;
|
|
45
|
+
seen.add(value);
|
|
46
|
+
|
|
47
|
+
if (Array.isArray(value)) {
|
|
48
|
+
let clone = null;
|
|
49
|
+
for (let i = 0; i < value.length; i++) {
|
|
50
|
+
const next = truncateDeep(value[i], maxChars, depth + 1, seen);
|
|
51
|
+
if (next !== value[i]) {
|
|
52
|
+
if (!clone) clone = value.slice();
|
|
53
|
+
clone[i] = next;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return clone || value;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const proto = Object.getPrototypeOf(value);
|
|
60
|
+
if (proto !== Object.prototype && proto !== null) return value;
|
|
61
|
+
|
|
62
|
+
let clone = null;
|
|
63
|
+
for (const key of Object.keys(value)) {
|
|
64
|
+
const next = truncateDeep(value[key], maxChars, depth + 1, seen);
|
|
65
|
+
if (next !== value[key]) {
|
|
66
|
+
if (!clone) clone = { ...value };
|
|
67
|
+
clone[key] = next;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return clone || value;
|
|
71
|
+
}
|
|
72
|
+
|
|
31
73
|
export function truncateExecutionResult(result, maxChars, state = null, execution = null) {
|
|
32
74
|
if (!result || typeof result !== 'object' || maxChars <= 0) return result;
|
|
33
75
|
|
|
@@ -39,24 +81,22 @@ export function truncateExecutionResult(result, maxChars, state = null, executio
|
|
|
39
81
|
truncatedValue = truncateText(result.value, maxChars);
|
|
40
82
|
didTruncate = true;
|
|
41
83
|
} else if (result.value !== null && typeof result.value === 'object') {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
clonedVal[key] = truncateText(result.value[key], maxChars);
|
|
47
|
-
didTruncate = true;
|
|
48
|
-
}
|
|
84
|
+
const deepValue = truncateDeep(result.value, maxChars);
|
|
85
|
+
if (deepValue !== result.value) {
|
|
86
|
+
truncatedValue = deepValue;
|
|
87
|
+
didTruncate = true;
|
|
49
88
|
}
|
|
50
|
-
if (clonedVal) truncatedValue = clonedVal;
|
|
51
89
|
}
|
|
52
90
|
|
|
53
91
|
if (Array.isArray(result.content)) {
|
|
54
92
|
let clonedContent = null;
|
|
55
93
|
for (let i = 0; i < result.content.length; i++) {
|
|
56
94
|
const block = result.content[i];
|
|
57
|
-
if (block
|
|
58
|
-
|
|
59
|
-
|
|
95
|
+
if (!block || typeof block !== 'object') continue;
|
|
96
|
+
const deepBlock = truncateDeep(block, maxChars);
|
|
97
|
+
if (deepBlock !== block) {
|
|
98
|
+
if (!clonedContent) clonedContent = result.content.slice();
|
|
99
|
+
clonedContent[i] = deepBlock;
|
|
60
100
|
didTruncate = true;
|
|
61
101
|
}
|
|
62
102
|
}
|
|
@@ -37,6 +37,35 @@ export function classifyToolAction(name) {
|
|
|
37
37
|
return 'default';
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
const REALPATH_CACHE_TTL_MS = 30000;
|
|
41
|
+
const REALPATH_CACHE_MAX = 500;
|
|
42
|
+
const realpathCache = new Map();
|
|
43
|
+
|
|
44
|
+
function readRealpathCache(probe) {
|
|
45
|
+
const hit = realpathCache.get(probe);
|
|
46
|
+
if (!hit) return undefined;
|
|
47
|
+
if (Date.now() - hit.at > REALPATH_CACHE_TTL_MS) {
|
|
48
|
+
realpathCache.delete(probe);
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
return hit.real;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function writeRealpathCache(probe, real) {
|
|
55
|
+
if (realpathCache.size >= REALPATH_CACHE_MAX) realpathCache.clear();
|
|
56
|
+
realpathCache.set(probe, { real, at: Date.now() });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** @internal test hook: current realpath cache size (#175). */
|
|
60
|
+
export function getTargetKeyCacheSize() {
|
|
61
|
+
return realpathCache.size;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** @internal test hook: reset the realpath cache (#175). */
|
|
65
|
+
export function clearTargetKeyCache() {
|
|
66
|
+
realpathCache.clear();
|
|
67
|
+
}
|
|
68
|
+
|
|
40
69
|
export function extractTargetKey(name, args) {
|
|
41
70
|
if (!args) return null;
|
|
42
71
|
let raw = null;
|
|
@@ -77,12 +106,17 @@ export function extractTargetKey(name, args) {
|
|
|
77
106
|
|
|
78
107
|
try {
|
|
79
108
|
const probe = path.isAbsolute(normalized) ? normalized : path.resolve(normalized);
|
|
80
|
-
|
|
109
|
+
const cachedReal = readRealpathCache(probe);
|
|
110
|
+
if (cachedReal !== undefined) {
|
|
111
|
+
if (cachedReal) normalized = cachedReal;
|
|
112
|
+
} else if (fs.existsSync(probe)) {
|
|
81
113
|
const stat = fs.lstatSync(probe);
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
114
|
+
let real = null;
|
|
115
|
+
if (stat.isSymbolicLink()) real = fs.realpathSync(probe).replace(/\\/g, '/');
|
|
116
|
+
writeRealpathCache(probe, real);
|
|
117
|
+
if (real) normalized = real;
|
|
118
|
+
} else {
|
|
119
|
+
writeRealpathCache(probe, null);
|
|
86
120
|
}
|
|
87
121
|
} catch {
|
|
88
122
|
// best-effort filesystem check
|