jeopi-ai 16.7.0 → 16.7.1

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 CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [16.7.1] - 2026-11-20
6
+
7
+ ### Fixed
8
+
9
+ - Enhanced `ToolCallLoopGuard` to normalize Python/JS subprocess array arguments (e.g. `["git", "status"]`), strip bash redirect and pipeline filters (`head`, `tail`, `cat`), and detect repetitive `read` offset patterns across turns to halt unproductive tool loops.
10
+
5
11
  ## [16.6.27] - 2026-11-17
6
12
 
7
13
  ### Added
@@ -27,7 +27,7 @@ export interface CyclicalToolCallDetection {
27
27
  readonly windowSize: number;
28
28
  }
29
29
  export type ToolCallLoopDetection = RepeatedToolCallDetection | CyclicalToolCallDetection;
30
- /** Detects consecutive identical assistant tool calls across model turns. */
30
+ /** Detects consecutive identical or semantically repeated assistant tool calls across model turns. */
31
31
  export declare class ToolCallLoopGuard {
32
32
  #private;
33
33
  constructor(options: ToolCallLoopGuardOptions);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "jeopi-ai",
4
- "version": "16.7.0",
4
+ "version": "16.7.1",
5
5
  "description": "Unified LLM API with automatic model discovery and provider configuration",
6
6
  "homepage": "https://github.com/akillness/jeopi",
7
7
  "author": "Can Boluk",
@@ -38,9 +38,9 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@bufbuild/protobuf": "^2.12.0",
41
- "jeopi-catalog": "16.7.0",
42
- "jeopi-utils": "16.7.0",
43
- "jeopi-wire": "16.7.0",
41
+ "jeopi-catalog": "16.7.1",
42
+ "jeopi-utils": "16.7.1",
43
+ "jeopi-wire": "16.7.1",
44
44
  "arktype": "2.2.3",
45
45
  "zod": "^4"
46
46
  },
@@ -76,14 +76,99 @@ function summarizeToolResult(toolResults: readonly ToolResultMessage[], toolCall
76
76
  return summarizeText(textParts.join("\n"), RESULT_SUMMARY_LIMIT);
77
77
  }
78
78
 
79
- /** Detects consecutive identical assistant tool calls across model turns. */
79
+ const EVAL_MUTATION_RE =
80
+ /\b(?:write_text|write_bytes|os\.remove|os\.unlink|os\.rename|os\.replace|shutil\.\w+|os\.mkdir|os\.makedirs|Bun\.write|fs\.write\w*|fs\.rm\w*|fs\.unlink\w*|git\s+(?:commit|checkout|reset|apply|merge|rebase)|sed\s+-i|perl\s+-pi)\b|\.write\s*\(|open\s*\([^)]*,\s*["'][wa+]/;
81
+ const EVAL_CMD_TOKEN_RE =
82
+ /\b(?:git\s+(?:status|diff|log|show|branch|rev-parse)|bun\s+(?:run\s+)?(?:typecheck|test|check|lint)|pytest|tsc|cargo\s+(?:test|check|clippy)|jev(?:\s+status)?|node\s+.*?jev-harness\.mjs\s+.*status)\b/g;
83
+ const EVAL_PATH_LITERAL_RE =
84
+ /["']((?:\.\/|\.\.\/|\/|~\/|src\/|packages\/|test\/|tests\/|crates\/|python\/)[^"'\n]{1,120})["']/g;
85
+ const EVAL_API_TOKEN_RE = /\b(?:os\.walk|glob\.glob|os\.listdir|subprocess\.(?:run|check_output|Popen)|read_text)\b/g;
86
+ const READ_PATH_RANGE_RE = /^(.*?)(?::(\d+)(?:-\d+)?)?$/;
87
+
88
+ function normalizeEvalCommandToken(token: string): string {
89
+ const collapsed = token.replace(/\s+/g, " ").trim();
90
+ if (collapsed.startsWith("bun run ")) return `bun ${collapsed.slice(8)}`;
91
+ if (/^git\s+status\b/.test(collapsed)) return "git status";
92
+ if (/jev-harness\.mjs.*status/.test(collapsed)) return "jev status";
93
+ return collapsed;
94
+ }
95
+
96
+ function normalizeBashCommand(command: string): string {
97
+ const stripped = command
98
+ .replace(/\b2>&1\b/g, "")
99
+ .replace(/\|\s*(?:head|tail|cat)\b[^|;&]*$/g, "")
100
+ .replace(/\s+/g, " ")
101
+ .trim();
102
+
103
+ if (/^git\s+status\b/.test(stripped)) return "git status";
104
+ if (/^node\s+.*?jev-harness\.mjs\s+.*status\b/.test(stripped)) return "jev status";
105
+ if (/^bun\s+(?:run\s+)?(?:test|typecheck|check|lint)\b/.test(stripped)) {
106
+ return stripped.replace(/^bun\s+run\s+/, "bun ");
107
+ }
108
+ return stripped;
109
+ }
110
+
111
+ function parseReadTarget(args: Record<string, unknown>): { basePath: string; startLine: number } | null {
112
+ const rawPath =
113
+ typeof args.path === "string" ? args.path.trim() : typeof args.filePath === "string" ? args.filePath.trim() : "";
114
+ if (!rawPath) return null;
115
+ const match = READ_PATH_RANGE_RE.exec(rawPath);
116
+ const basePath = (match?.[1] || rawPath).trim();
117
+ const rangeStart = match?.[2] ? Number.parseInt(match[2], 10) : undefined;
118
+ const lineRange = typeof args.lineRange === "string" ? args.lineRange.trim() : "";
119
+ const lineRangeMatch = lineRange ? /^(\d+)/.exec(lineRange) : null;
120
+ const lineRangeStart = lineRangeMatch?.[1] ? Number.parseInt(lineRangeMatch[1], 10) : undefined;
121
+ const offsetStart =
122
+ typeof args.offset === "number" && Number.isFinite(args.offset) ? Math.trunc(args.offset) : undefined;
123
+ const startLine = Math.max(1, rangeStart ?? lineRangeStart ?? offsetStart ?? 1);
124
+ return { basePath, startLine };
125
+ }
126
+
127
+ function buildEvalLookupKey(args: Record<string, unknown>, canonicalArgs: string): string {
128
+ const lang = typeof args.language === "string" ? args.language.trim() : "";
129
+ const code = typeof args.code === "string" ? args.code.trim() : "";
130
+ if (!code || EVAL_MUTATION_RE.test(code)) {
131
+ const stripped: Record<string, unknown> = { ...args };
132
+ delete stripped.title;
133
+ delete stripped.description;
134
+ delete stripped.timeout;
135
+ return `eval:${JSON.stringify(canonicalizeToolCallValue(stripped))}`;
136
+ }
137
+
138
+ const normalizedListCode = code.replace(/["']\s*,\s*["']/g, " ");
139
+ const cmds = new Set<string>();
140
+ for (const match of normalizedListCode.matchAll(EVAL_CMD_TOKEN_RE)) {
141
+ cmds.add(normalizeEvalCommandToken(match[0]));
142
+ }
143
+ if (cmds.size > 0) {
144
+ return `eval:ro:cmds:${lang}:${[...cmds].sort().join(",")}`;
145
+ }
146
+
147
+ const paths = new Set<string>();
148
+ for (const match of code.matchAll(EVAL_PATH_LITERAL_RE)) {
149
+ const cleaned = match[1]!.split(":")[0]!.trim();
150
+ if (cleaned) paths.add(cleaned);
151
+ }
152
+ const apis = new Set<string>();
153
+ for (const match of code.matchAll(EVAL_API_TOKEN_RE)) {
154
+ apis.add(match[0].trim());
155
+ }
156
+ if (paths.size > 0 || apis.size > 0) {
157
+ return `eval:ro:query:${lang}:paths=${[...paths].sort().join(",")}:apis=${[...apis].sort().join(",")}`;
158
+ }
159
+
160
+ const normalizedCode = code.replace(/\d+/g, "#").replace(/\s+/g, " ").trim();
161
+ return normalizedCode ? `eval:ro:norm:${lang}:${normalizedCode}` : `eval:${canonicalArgs}`;
162
+ }
163
+
164
+ /** Detects consecutive identical or semantically repeated assistant tool calls across model turns. */
80
165
  export class ToolCallLoopGuard {
81
166
  #threshold: number;
82
167
  #exemptTools: ReadonlySet<string>;
83
- #lastHash: string | undefined;
84
- #count = 0;
168
+ #callStreaks = new Map<string, number>();
169
+ #lastReadStartByPath = new Map<string, number>();
85
170
  #cycleWindowSize: number;
86
- #recentCalls: Array<{ hash: string; toolName: string }> = [];
171
+ #recentCalls: Array<{ hash: string; toolNames: string[] }> = [];
87
172
  #lastCycleSignature: string | undefined;
88
173
 
89
174
  constructor(options: ToolCallLoopGuardOptions) {
@@ -92,41 +177,124 @@ export class ToolCallLoopGuard {
92
177
  this.#cycleWindowSize = Math.max(3, Math.trunc(options.cycleWindowSize ?? 6));
93
178
  }
94
179
 
180
+ #isExemptToolCall(toolCall: ToolCall): boolean {
181
+ if (!this.#exemptTools.has(toolCall.name)) return false;
182
+ if (
183
+ (toolCall.name === "subagent" || toolCall.name === "job") &&
184
+ toolCall.arguments &&
185
+ typeof toolCall.arguments === "object" &&
186
+ (toolCall.arguments as Record<string, unknown>).action === "list"
187
+ ) {
188
+ return false;
189
+ }
190
+ return true;
191
+ }
192
+
193
+ #reset(): void {
194
+ this.#callStreaks.clear();
195
+ this.#lastReadStartByPath.clear();
196
+ this.#recentCalls = [];
197
+ this.#lastCycleSignature = undefined;
198
+ }
199
+
200
+ #computeLookupKey(toolCall: ToolCall, canonicalArgs: string, nextReadStartByPath: Map<string, number>): string {
201
+ const args =
202
+ toolCall.arguments && typeof toolCall.arguments === "object" && !Array.isArray(toolCall.arguments)
203
+ ? (toolCall.arguments as Record<string, unknown>)
204
+ : undefined;
205
+
206
+ if (toolCall.name === "eval" && args) {
207
+ return buildEvalLookupKey(args, canonicalArgs);
208
+ }
209
+
210
+ if (toolCall.name === "bash" && args && typeof args.command === "string") {
211
+ const normalizedCmd = normalizeBashCommand(args.command);
212
+ if (normalizedCmd) {
213
+ return `bash:cmd:${normalizedCmd}`;
214
+ }
215
+ }
216
+
217
+ if (toolCall.name === "read" && args) {
218
+ const target = parseReadTarget(args);
219
+ if (target) {
220
+ const prevStart = this.#lastReadStartByPath.get(target.basePath);
221
+ nextReadStartByPath.set(target.basePath, target.startLine);
222
+ if (prevStart !== undefined && target.startLine > prevStart) {
223
+ return `read:page:${target.basePath}:${target.startLine}`;
224
+ }
225
+ return `read:file:${target.basePath}`;
226
+ }
227
+ }
228
+
229
+ return `${toolCall.name}:${canonicalArgs}`;
230
+ }
231
+
95
232
  /** Records one completed turn and returns the threshold/cycle hit, if any. */
96
233
  recordTurn(turn: ToolCallLoopTurn): ToolCallLoopDetection | null {
97
- const toolCalls = turn.message.content.filter((part): part is ToolCall => part.type === "toolCall");
98
- if (toolCalls.length !== 1 || this.#exemptTools.has(toolCalls[0]!.name)) {
99
- this.#lastHash = undefined;
100
- this.#count = 0;
101
- this.#recentCalls = [];
102
- this.#lastCycleSignature = undefined;
234
+ const allToolCalls = turn.message.content.filter((part): part is ToolCall => part.type === "toolCall");
235
+ if (allToolCalls.length === 0) {
236
+ this.#reset();
103
237
  return null;
104
238
  }
105
239
 
106
- const toolCall = toolCalls[0]!;
107
- const canonicalArgs = JSON.stringify(canonicalizeToolCallValue(toolCall.arguments));
108
- const hash = `${toolCall.name}:${canonicalArgs}`;
109
- if (hash === this.#lastHash) {
110
- this.#count++;
111
- } else {
112
- this.#lastHash = hash;
113
- this.#count = 1;
240
+ const trackedCalls = allToolCalls.filter(call => !this.#isExemptToolCall(call));
241
+ if (trackedCalls.length === 0) {
242
+ return null;
243
+ }
244
+
245
+ const nextReadStartByPath = new Map<string, number>(this.#lastReadStartByPath);
246
+ const turnEntries: Array<{
247
+ lookupKey: string;
248
+ toolCall: ToolCall;
249
+ canonicalArgs: string;
250
+ }> = [];
251
+ const seenLookupKeys = new Set<string>();
252
+
253
+ for (const toolCall of trackedCalls) {
254
+ const canonicalArgs = JSON.stringify(canonicalizeToolCallValue(toolCall.arguments));
255
+ const lookupKey = this.#computeLookupKey(toolCall, canonicalArgs, nextReadStartByPath);
256
+ if (seenLookupKeys.has(lookupKey)) continue;
257
+ seenLookupKeys.add(lookupKey);
258
+ turnEntries.push({ lookupKey, toolCall, canonicalArgs });
114
259
  }
115
260
 
116
- if (this.#count === this.#threshold) {
261
+ this.#lastReadStartByPath = nextReadStartByPath;
262
+
263
+ const nextStreaks = new Map<string, number>();
264
+ let hitEntry: { toolCall: ToolCall; canonicalArgs: string; count: number } | undefined;
265
+
266
+ for (const entry of turnEntries) {
267
+ const count = (this.#callStreaks.get(entry.lookupKey) ?? 0) + 1;
268
+ nextStreaks.set(entry.lookupKey, count);
269
+ if (count >= this.#threshold && (!hitEntry || count > hitEntry.count)) {
270
+ hitEntry = { toolCall: entry.toolCall, canonicalArgs: entry.canonicalArgs, count };
271
+ }
272
+ }
273
+
274
+ this.#callStreaks = nextStreaks;
275
+
276
+ if (hitEntry) {
117
277
  return {
118
278
  kind: "repeated_tool_call",
119
- toolName: toolCall.name,
120
- count: this.#count,
121
- resultSummary: summarizeToolResult(turn.toolResults, toolCall.id),
122
- argumentsSummary: summarizeText(canonicalArgs, ARGUMENT_SUMMARY_LIMIT),
279
+ toolName: hitEntry.toolCall.name,
280
+ count: hitEntry.count,
281
+ resultSummary: summarizeToolResult(turn.toolResults, hitEntry.toolCall.id),
282
+ argumentsSummary: summarizeText(hitEntry.canonicalArgs, ARGUMENT_SUMMARY_LIMIT),
123
283
  };
124
284
  }
125
285
 
126
286
  // Alternating A,B,A,B,… pattern: exact-repeat detection above never fires
127
287
  // (the hash changes every turn), but the model is still spinning between
128
- // exactly two distinct calls without making progress.
129
- this.#recentCalls.push({ hash, toolName: toolCall.name });
288
+ // two distinct calls/turn-signatures without making progress.
289
+ const turnHash = turnEntries.map(entry => entry.lookupKey).join("\u001f");
290
+ const turnToolNames: string[] = [];
291
+ for (const entry of turnEntries) {
292
+ if (!turnToolNames.includes(entry.toolCall.name)) {
293
+ turnToolNames.push(entry.toolCall.name);
294
+ }
295
+ }
296
+
297
+ this.#recentCalls.push({ hash: turnHash, toolNames: turnToolNames });
130
298
  if (this.#recentCalls.length > this.#cycleWindowSize) this.#recentCalls.shift();
131
299
  if (this.#recentCalls.length < this.#cycleWindowSize) return null;
132
300
 
@@ -141,7 +309,9 @@ export class ToolCallLoopGuard {
141
309
 
142
310
  const toolNames: string[] = [];
143
311
  for (const call of this.#recentCalls) {
144
- if (!toolNames.includes(call.toolName)) toolNames.push(call.toolName);
312
+ for (const name of call.toolNames) {
313
+ if (!toolNames.includes(name)) toolNames.push(name);
314
+ }
145
315
  }
146
316
  return { kind: "cyclical_tool_calls", toolNames, windowSize: this.#cycleWindowSize };
147
317
  }