@goodandready/dsh-agent-loop-guard 0.3.2 → 0.3.3
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 +15 -0
- package/lib/guard-helpers.js +15 -3
- package/lib/guard-state.js +7 -7
- package/lib/index.js +6 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.3.3] - 2026-11-16
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- **Hot Path Performance & Memory Optimizations (#158)**:
|
|
7
|
+
- **Pre-Compiled Pseudo-Tool Regex**: Regex patterns are compiled once in `LoopGuardState` constructor and config reload (`compiledPseudoPatterns`), eliminating re-compilation on every streaming delta chunk.
|
|
8
|
+
- **Sliding Tail Window Scan**: `observeAssistantText` inspects only an overlap tail window (`text.length + 256` bytes) instead of rescanning up to 64 KB on every delta, eliminating $O(N^2)$ streaming overhead.
|
|
9
|
+
- **Large Payload Fast Fingerprinting**: String arguments exceeding 2048 characters are hashed via deterministic prefix + length + SHA-256 fingerprint in `callFingerprint` and `callRepeatFingerprint`, preventing multi-megabyte JSON serialization and heap spikes during massive file writes.
|
|
10
|
+
- **Bounded Call Sequence History**: `state.calls` is bounded to the most recent 64 entries, preventing unbounded array growth in long-running sessions.
|
|
11
|
+
- **Audit-First Dry-Run Mode (#162)**: Extended `dryRunMode` to output repetition guard (`cancelOnOutputHit`) and pseudo-tool loop detector (`cancelOnPseudoHit`). Violations are recorded to telemetry and incident journals for audit without terminating agent execution or cancelling generation.
|
|
12
|
+
- **Automated Preset Parity Gate (#159)**: Added automated test suite `test/presets-parity.test.js` validating 1:1 parameter parity across server `GUARD_PRESETS` and client `CLIENT_PRESETS` for all profiles (`Strict`, `Balanced`, `Autonomous`). Aligned `LoopGuardState` constructor fallback for `maxUnchangedReadsPerTarget` with schema default (3).
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
- **Dead Imports & Redundant Delegate Elimination (#165)**: Removed unused imports `GUARD_PRESETS` and `hasStopRequest` in `lib/index.js`. Removed redundant wrapper `LoopGuardState.decrementReservations` in favor of direct module function invocation.
|
|
16
|
+
- **SMB Mount Filemode Drift Protection (#166)**: Documented and enforced `core.fileMode = false` for SMB/Windows-mounted checkouts, preventing spurious 100755/100644 git status diffs.
|
|
17
|
+
|
|
3
18
|
## [0.3.2] - 2026-11-16
|
|
4
19
|
|
|
5
20
|
### Added
|
package/lib/guard-helpers.js
CHANGED
|
@@ -95,7 +95,8 @@ export function isPseudoToolCallText(text, customPatterns = []) {
|
|
|
95
95
|
for (const regex of DEFAULT_PSEUDO_TOOL_PATTERNS) {
|
|
96
96
|
if (regex.test(text)) return true;
|
|
97
97
|
}
|
|
98
|
-
|
|
98
|
+
if (!Array.isArray(customPatterns) || customPatterns.length === 0) return false;
|
|
99
|
+
const compiled = (customPatterns[0] instanceof RegExp) ? customPatterns : compilePseudoToolPatterns(customPatterns);
|
|
99
100
|
for (const regex of compiled) {
|
|
100
101
|
if (regex.test(text)) return true;
|
|
101
102
|
}
|
|
@@ -119,14 +120,25 @@ export function compileStopPatterns(customPatterns = []) {
|
|
|
119
120
|
const SECRET_KEY_PATTERN = /(?:token|secret|password|authorization|api[-_]?key|cookie)/iu;
|
|
120
121
|
|
|
121
122
|
function stableJson(value) {
|
|
122
|
-
if (value === null || typeof value !== 'object')
|
|
123
|
+
if (value === null || typeof value !== 'object') {
|
|
124
|
+
if (typeof value === 'string' && value.length > 2048) {
|
|
125
|
+
return JSON.stringify(value.slice(0, 512) + ':len:' + value.length + ':hash:' + createHash('sha256').update(value).digest('hex').slice(0, 16));
|
|
126
|
+
}
|
|
127
|
+
return JSON.stringify(value);
|
|
128
|
+
}
|
|
123
129
|
if (Array.isArray(value)) return '[' + value.map(stableJson).join(',') + ']';
|
|
124
130
|
return '{' + Object.keys(value).sort().map((key) => JSON.stringify(key) + ':' + stableJson(value[key])).join(',') + '}';
|
|
125
131
|
}
|
|
126
132
|
|
|
127
133
|
function normalizedJson(value) {
|
|
128
134
|
if (value === null || typeof value !== 'object') {
|
|
129
|
-
if (typeof value === 'string')
|
|
135
|
+
if (typeof value === 'string') {
|
|
136
|
+
const trimmed = value.trim().replace(/\s+/gu, ' ');
|
|
137
|
+
if (trimmed.length > 2048) {
|
|
138
|
+
return JSON.stringify(trimmed.slice(0, 512) + ':len:' + trimmed.length + ':hash:' + createHash('sha256').update(trimmed).digest('hex').slice(0, 16));
|
|
139
|
+
}
|
|
140
|
+
return JSON.stringify(trimmed);
|
|
141
|
+
}
|
|
130
142
|
return JSON.stringify(value);
|
|
131
143
|
}
|
|
132
144
|
if (Array.isArray(value)) return '[' + value.map(normalizedJson).join(',') + ']';
|
package/lib/guard-state.js
CHANGED
|
@@ -61,13 +61,14 @@ export class LoopGuardState {
|
|
|
61
61
|
maxConsecutiveFailures: positiveInteger(config.maxConsecutiveFailures, 4),
|
|
62
62
|
adaptiveSteering: Boolean(config.adaptiveSteering),
|
|
63
63
|
targetQuotaGuard: config.targetQuotaGuard !== undefined ? Boolean(config.targetQuotaGuard) : true,
|
|
64
|
-
maxUnchangedReadsPerTarget: config.maxUnchangedReadsPerTarget !== undefined ? positiveInteger(config.maxUnchangedReadsPerTarget, 3) :
|
|
64
|
+
maxUnchangedReadsPerTarget: config.maxUnchangedReadsPerTarget !== undefined ? positiveInteger(config.maxUnchangedReadsPerTarget, 3) : 3,
|
|
65
65
|
maxReadsPerTarget: positiveInteger(config.maxReadsPerTarget, 6),
|
|
66
66
|
maxEditsPerTarget: positiveInteger(config.maxEditsPerTarget, 8),
|
|
67
67
|
maxTargetAttempts: positiveInteger(config.maxTargetAttempts, 12),
|
|
68
68
|
};
|
|
69
69
|
this.stopPatterns = stopPatterns;
|
|
70
70
|
this.pseudoPatterns = this.config.pseudoToolCallPatterns;
|
|
71
|
+
this.compiledPseudoPatterns = compilePseudoToolPatterns(this.pseudoPatterns);
|
|
71
72
|
this.logger = config.logger;
|
|
72
73
|
this.onViolation = typeof config.onViolation === 'function' ? config.onViolation : undefined;
|
|
73
74
|
this.progressToolNames = new Set(progressToolNames);
|
|
@@ -123,6 +124,7 @@ export class LoopGuardState {
|
|
|
123
124
|
if (Array.isArray(config.pseudoToolCallPatterns)) {
|
|
124
125
|
this.pseudoPatterns = config.pseudoToolCallPatterns;
|
|
125
126
|
this.config.pseudoToolCallPatterns = this.pseudoPatterns;
|
|
127
|
+
this.compiledPseudoPatterns = compilePseudoToolPatterns(this.pseudoPatterns);
|
|
126
128
|
}
|
|
127
129
|
if (config.oscillationDetection !== undefined) {
|
|
128
130
|
this.config.oscillationDetection = Boolean(config.oscillationDetection);
|
|
@@ -264,7 +266,8 @@ export class LoopGuardState {
|
|
|
264
266
|
const state = this.stateFor(agent);
|
|
265
267
|
if (state.turnHadPseudoCall) return;
|
|
266
268
|
state.turnAssistantText = (state.turnAssistantText + text).slice(-65536);
|
|
267
|
-
|
|
269
|
+
const scanWindow = state.turnAssistantText.slice(-(text.length + 256));
|
|
270
|
+
if (isPseudoToolCallText(scanWindow, this.compiledPseudoPatterns || this.pseudoPatterns)) {
|
|
268
271
|
state.turnHadPseudoCall = true;
|
|
269
272
|
}
|
|
270
273
|
}
|
|
@@ -331,10 +334,6 @@ export class LoopGuardState {
|
|
|
331
334
|
return state.calls.findLast((call) => !call.released && !call.resultSeen);
|
|
332
335
|
}
|
|
333
336
|
|
|
334
|
-
decrementReservations(state, call) {
|
|
335
|
-
decrementReservations(state, call);
|
|
336
|
-
}
|
|
337
|
-
|
|
338
337
|
handleViolation(state, agent, name, args, callId, code, detail) {
|
|
339
338
|
if (code === 'LOOP_GUARD_STOP' || state.answerOnly) {
|
|
340
339
|
return this.violation(state, agent, name, args, callId, code, detail);
|
|
@@ -506,6 +505,7 @@ export class LoopGuardState {
|
|
|
506
505
|
resultSeen: false,
|
|
507
506
|
};
|
|
508
507
|
state.calls.push(call);
|
|
508
|
+
if (state.calls.length > 64) state.calls.shift();
|
|
509
509
|
state.attemptsSinceProgress += 1;
|
|
510
510
|
state.recentCallSequence.push(repeatFingerprint);
|
|
511
511
|
if (state.recentCallSequence.length > 32) state.recentCallSequence.shift();
|
|
@@ -528,7 +528,7 @@ export class LoopGuardState {
|
|
|
528
528
|
if (call === undefined) return { known: false, productive: false };
|
|
529
529
|
if (call.pending && !call.committed) {
|
|
530
530
|
call.released = true;
|
|
531
|
-
|
|
531
|
+
decrementReservations(state, call);
|
|
532
532
|
return { known: false, productive: false };
|
|
533
533
|
}
|
|
534
534
|
const outcome = outcomeFromExecution(execution, maybeResult);
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import Schema from '@deepseek-ai/schemastery';
|
|
2
2
|
import {
|
|
3
3
|
LoopGuardState,
|
|
4
|
-
GUARD_PRESETS,
|
|
5
|
-
hasStopRequest,
|
|
6
4
|
nonNegativeInteger,
|
|
7
5
|
positiveInteger,
|
|
8
6
|
} from './guard-state.js';
|
|
@@ -173,6 +171,9 @@ function cancelOnPseudoHit(ctx, session, hit, state = null) {
|
|
|
173
171
|
} catch (error) {
|
|
174
172
|
ctx.logger?.debug?.('dsh-agent-loop-guard: failed to record external violation: ' + (error instanceof Error ? error.message : String(error)));
|
|
175
173
|
}
|
|
174
|
+
if (state?.config?.dryRunMode) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
176
177
|
cancelForOutputLoop(ctx, session, hit.reason);
|
|
177
178
|
}
|
|
178
179
|
|
|
@@ -197,6 +198,9 @@ function cancelOnOutputHit(ctx, session, assistantState, hit, state = null) {
|
|
|
197
198
|
} catch (error) {
|
|
198
199
|
ctx.logger?.debug?.('dsh-agent-loop-guard: failed to record external violation: ' + (error instanceof Error ? error.message : String(error)));
|
|
199
200
|
}
|
|
201
|
+
if (state?.config?.dryRunMode) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
200
204
|
cancelForOutputLoop(ctx, session, hit.reason);
|
|
201
205
|
}
|
|
202
206
|
|