kovence-cli 1.0.3 → 1.3.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.
@@ -1,100 +1,350 @@
1
1
  "use strict";
2
2
 
3
+ function isPlainObject(val) {
4
+ return Boolean(val) && typeof val === "object" && !Array.isArray(val);
5
+ }
6
+
3
7
  /**
4
- * Repair common malformed JSON emitted by smaller models in tool-call arguments.
8
+ * Scan JSON outside string literals to perform ONLY safe repairs:
9
+ * - remove trailing commas before `}` or `]`
10
+ * - quote unquoted bare object keys in key position (`{` or `,` followed by identifier and `:`)
11
+ * Never modifies text inside string literals and never balances unclosed braces/brackets/quotes.
5
12
  */
6
- function repairToolArguments(rawArgs) {
7
- if (rawArgs && typeof rawArgs === "object") return rawArgs;
8
- let src = String(rawArgs ?? "").trim();
13
+ function scanSafeJsonRepairs(src) {
14
+ let out = "";
15
+ let inStr = false;
16
+ let esc = false;
17
+ let expectKey = false;
18
+ const len = src.length;
19
+
20
+ for (let i = 0; i < len; i++) {
21
+ const ch = src[i];
22
+
23
+ if (inStr) {
24
+ out += ch;
25
+ if (esc) {
26
+ esc = false;
27
+ } else if (ch === "\\") {
28
+ esc = true;
29
+ } else if (ch === '"') {
30
+ inStr = false;
31
+ }
32
+ continue;
33
+ }
34
+
35
+ if (ch === '"') {
36
+ inStr = true;
37
+ expectKey = false;
38
+ out += ch;
39
+ continue;
40
+ }
41
+
42
+ if (ch === "{") {
43
+ expectKey = true;
44
+ out += ch;
45
+ continue;
46
+ }
47
+
48
+ if (ch === "[") {
49
+ expectKey = false;
50
+ out += ch;
51
+ continue;
52
+ }
53
+
54
+ if (ch === ",") {
55
+ let j = i + 1;
56
+ while (j < len && /\s/.test(src[j])) j++;
57
+ if (j < len && (src[j] === "}" || src[j] === "]")) {
58
+ // Drop trailing comma outside string literals
59
+ expectKey = false;
60
+ continue;
61
+ }
62
+ expectKey = true;
63
+ out += ch;
64
+ continue;
65
+ }
66
+
67
+ if (ch === "}" || ch === "]") {
68
+ expectKey = false;
69
+ out += ch;
70
+ continue;
71
+ }
72
+
73
+ if (/\s/.test(ch)) {
74
+ out += ch;
75
+ continue;
76
+ }
77
+
78
+ if (expectKey && /[A-Za-z_]/.test(ch)) {
79
+ let end = i + 1;
80
+ while (end < len && /[A-Za-z0-9_]/.test(src[end])) end++;
81
+ let colonIdx = end;
82
+ while (colonIdx < len && /\s/.test(src[colonIdx])) colonIdx++;
83
+ if (colonIdx < len && src[colonIdx] === ":") {
84
+ out += `"${src.slice(i, end)}"`;
85
+ i = end - 1;
86
+ expectKey = false;
87
+ continue;
88
+ }
89
+ }
90
+
91
+ expectKey = false;
92
+ out += ch;
93
+ }
94
+
95
+ return out;
96
+ }
97
+
98
+ /**
99
+ * Parse tool-call JSON arguments with strict safety:
100
+ * - JSON.parse first
101
+ * - When strict=false, allow only safe repairs (BOM, markdown fences, trailing commas, bare keys outside strings)
102
+ * - NEVER balance unclosed braces, brackets, or quotes
103
+ */
104
+ function repairToolArguments(rawArgs, { strict = false } = {}) {
105
+ if (isPlainObject(rawArgs)) return rawArgs;
106
+ if (Array.isArray(rawArgs)) {
107
+ throw new Error("Invalid tool JSON arguments: expected a JSON object, got array");
108
+ }
109
+
110
+ let src = String(rawArgs ?? "")
111
+ .replace(/^\uFEFF/, "")
112
+ .trim();
9
113
  if (!src) return {};
10
114
 
11
- // Strip markdown ```json fences if wrapped
115
+ try {
116
+ const parsed = JSON.parse(src);
117
+ if (!isPlainObject(parsed)) {
118
+ throw new Error("Tool arguments must be a JSON object");
119
+ }
120
+ return parsed;
121
+ } catch (firstErr) {
122
+ if (strict) {
123
+ throw new Error(`Invalid tool JSON arguments: ${firstErr.message}`);
124
+ }
125
+ }
126
+
127
+ // Safe repair: strip markdown ```json fences and BOM
12
128
  src = src
13
129
  .replace(/^```(?:json)?\s*/i, "")
14
130
  .replace(/\s*```$/i, "")
131
+ .replace(/^\uFEFF/, "")
15
132
  .trim();
16
133
 
134
+ if (!src) return {};
135
+
17
136
  try {
18
- return JSON.parse(src);
137
+ const parsed = JSON.parse(src);
138
+ if (!isPlainObject(parsed)) {
139
+ throw new Error("Tool arguments must be a JSON object");
140
+ }
141
+ return parsed;
19
142
  } catch {}
20
143
 
21
- // Remove trailing commas before } or ]
22
- let fixed = src.replace(/,\s*([}\]])/g, "$1");
23
- // Quote bare object keys: { path: "a" } -> { "path": "a" }
24
- fixed = fixed.replace(/([{,]\s*)([A-Za-z_][A-Za-z0-9_]*)\s*:/g, '$1"$2":');
25
- // Balance missing closing braces/brackets if truncated
26
- let opensCurly = 0;
27
- let opensSquare = 0;
28
- let inStr = false;
29
- let esc = false;
30
- for (let i = 0; i < fixed.length; i++) {
31
- const ch = fixed[i];
32
- if (esc) {
33
- esc = false;
34
- continue;
144
+ const fixed = scanSafeJsonRepairs(src);
145
+ try {
146
+ const parsed = JSON.parse(fixed);
147
+ if (!isPlainObject(parsed)) {
148
+ throw new Error("Tool arguments must be a JSON object");
35
149
  }
36
- if (ch === "\\") {
37
- esc = true;
38
- continue;
150
+ return parsed;
151
+ } catch (err) {
152
+ throw new Error(`Invalid tool JSON arguments: ${err.message}`);
153
+ }
154
+ }
155
+
156
+ function validateSchemaValue(val, schema, pathName) {
157
+ if (!schema || typeof schema !== "object") return;
158
+
159
+ if (schema.type === "string") {
160
+ if (typeof val !== "string") {
161
+ throw new Error(`Parameter '${pathName}' must be a string, got ${typeof val}`);
39
162
  }
40
- if (ch === '"') {
41
- inStr = !inStr;
42
- continue;
163
+ } else if (schema.type === "integer") {
164
+ if (typeof val !== "number" || !Number.isInteger(val)) {
165
+ throw new Error(`Parameter '${pathName}' must be an integer`);
166
+ }
167
+ } else if (schema.type === "number") {
168
+ if (typeof val !== "number" || !Number.isFinite(val)) {
169
+ throw new Error(`Parameter '${pathName}' must be a number`);
170
+ }
171
+ } else if (schema.type === "boolean") {
172
+ if (typeof val !== "boolean") {
173
+ throw new Error(`Parameter '${pathName}' must be a boolean, got ${typeof val}`);
174
+ }
175
+ } else if (schema.type === "array") {
176
+ if (!Array.isArray(val)) {
177
+ throw new Error(`Parameter '${pathName}' must be an array`);
178
+ }
179
+ if (schema.items) {
180
+ for (let i = 0; i < val.length; i++) {
181
+ validateSchemaValue(val[i], schema.items, `${pathName}[${i}]`);
182
+ }
183
+ }
184
+ } else if (schema.type === "object") {
185
+ if (!isPlainObject(val)) {
186
+ throw new Error(`Parameter '${pathName}' must be an object`);
43
187
  }
44
- if (inStr) continue;
45
- if (ch === "{") opensCurly += 1;
46
- else if (ch === "}") opensCurly -= 1;
47
- else if (ch === "[") opensSquare += 1;
48
- else if (ch === "]") opensSquare -= 1;
188
+ validateObjectAgainstSchema(val, schema, pathName);
49
189
  }
50
- if (inStr) fixed += '"';
51
- while (opensSquare > 0) {
52
- fixed += "]";
53
- opensSquare -= 1;
190
+
191
+ if (Array.isArray(schema.enum) && !schema.enum.includes(val)) {
192
+ throw new Error(`Parameter '${pathName}' must be one of: ${schema.enum.join(", ")}`);
54
193
  }
55
- while (opensCurly > 0) {
56
- fixed += "}";
57
- opensCurly -= 1;
194
+ }
195
+
196
+ function validateObjectAgainstSchema(obj, schema, prefix = "") {
197
+ if (!schema || typeof schema !== "object") return;
198
+ if (!isPlainObject(obj)) {
199
+ throw new Error(prefix ? `Parameter '${prefix}' must be an object` : "Tool arguments must be an object");
58
200
  }
59
201
 
60
- try {
61
- return JSON.parse(fixed);
62
- } catch (err) {
63
- throw new Error(`Invalid tool JSON arguments: ${err.message}`);
202
+ if (Array.isArray(schema.required)) {
203
+ for (const reqKey of schema.required) {
204
+ if (obj[reqKey] === undefined || obj[reqKey] === null) {
205
+ const full = prefix ? `${prefix}.${reqKey}` : reqKey;
206
+ throw new Error(`Missing required parameter '${full}'`);
207
+ }
208
+ }
209
+ }
210
+
211
+ if (schema.properties && typeof schema.properties === "object") {
212
+ for (const [key, val] of Object.entries(obj)) {
213
+ if (val === undefined || val === null) continue;
214
+ const propSchema = schema.properties[key];
215
+ if (!propSchema) continue;
216
+ const full = prefix ? `${prefix}.${key}` : key;
217
+ validateSchemaValue(val, propSchema, full);
218
+ }
64
219
  }
65
220
  }
66
221
 
67
222
  /**
68
- * Detect repetitive identical tool calls across consecutive steps so agents don't spin forever.
223
+ * Validate parsed tool arguments against a JSON Schema parameter definition.
224
+ */
225
+ function validateToolArgs(args, schema) {
226
+ validateObjectAgainstSchema(args, schema, "");
227
+ return args;
228
+ }
229
+
230
+ /**
231
+ * Determine whether a tool result represents a failure.
232
+ * Uses structured flags when present; otherwise falls back to standard error prefixes.
233
+ */
234
+ function isToolFailure(result) {
235
+ if (result && typeof result === "object") {
236
+ if (typeof result.isError === "boolean") return result.isError;
237
+ if (typeof result.is_error === "boolean") return result.is_error;
238
+ return isToolFailure(String(result.content ?? ""));
239
+ }
240
+ const text = String(result ?? "").trimStart();
241
+ if (!text) return false;
242
+ return /^(?:Error:|Denied(?:\b|:)|Exit\s+(?!0(?:\s|:|$)))/i.test(text);
243
+ }
244
+
245
+ const LOOP_HINT_TEXT =
246
+ "You already made this call (or an equivalent one) with the same result. Change your approach: re-read the relevant file, use a different tool, or explain what blocks you.";
247
+
248
+ /**
249
+ * Detect repetitive identical tool calls, A-B-A-B alternation, and repeated failing edits
250
+ * across steps so agents don't spin forever.
69
251
  */
70
252
  class LoopDetector {
71
253
  constructor(maxRepeats = 3) {
72
254
  this.maxRepeats = maxRepeats;
73
255
  this.history = [];
256
+ this.failingEditCounts = new Map();
257
+ this.detections = 0;
74
258
  }
75
259
 
76
- record(toolCalls = []) {
260
+ record(toolCalls = [], toolResults = null) {
77
261
  const sig = toolCalls
78
262
  .map((tc) => `${tc.function?.name || ""}:${String(tc.function?.arguments || "").replace(/\s+/g, "")}`)
79
263
  .sort()
80
264
  .join("|");
81
- if (!sig) return { repeated: false, count: 0 };
265
+ if (!sig) {
266
+ return {
267
+ repeated: false,
268
+ count: 0,
269
+ pattern: null,
270
+ detections: this.detections,
271
+ shouldStop: false,
272
+ hint: "",
273
+ signature: "",
274
+ };
275
+ }
82
276
 
83
277
  this.history.push(sig);
278
+
279
+ // 1. Consecutive identical calls (A, A, A)
84
280
  let consecutive = 0;
85
281
  for (let i = this.history.length - 1; i >= 0; i--) {
86
282
  if (this.history[i] === sig) consecutive += 1;
87
283
  else break;
88
284
  }
285
+ let pattern = consecutive >= this.maxRepeats ? "exact" : null;
286
+
287
+ // 2. A-B-A-B alternation (4 consecutive steps: h[n-4]==h[n-2], h[n-3]==h[n-1], A!==B)
288
+ const n = this.history.length;
289
+ if (
290
+ !pattern &&
291
+ n >= 4 &&
292
+ this.history[n - 1] === this.history[n - 3] &&
293
+ this.history[n - 2] === this.history[n - 4] &&
294
+ this.history[n - 1] !== this.history[n - 2]
295
+ ) {
296
+ pattern = "alternation";
297
+ }
298
+
299
+ // 3. Repeated failing edits to the same file + target
300
+ if (Array.isArray(toolResults)) {
301
+ for (let i = 0; i < toolCalls.length; i++) {
302
+ const tc = toolCalls[i];
303
+ const res = toolResults[i];
304
+ const name = tc?.function?.name || "";
305
+ if (name !== "edit_file" && name !== "multi_edit") continue;
306
+ if (!isToolFailure(res)) continue;
307
+ let argsObj = {};
308
+ try {
309
+ argsObj =
310
+ typeof tc.function?.arguments === "string"
311
+ ? JSON.parse(tc.function.arguments || "{}")
312
+ : tc.function?.arguments || {};
313
+ } catch {}
314
+ const targetKey =
315
+ name === "edit_file"
316
+ ? String(argsObj.target || "")
317
+ : JSON.stringify((argsObj.edits || []).map((e) => e?.target || ""));
318
+ const editKey = `${name}::${String(argsObj.path || "")}::${targetKey}`;
319
+ const nextCount = (this.failingEditCounts.get(editKey) || 0) + 1;
320
+ this.failingEditCounts.set(editKey, nextCount);
321
+ if (nextCount >= 2 && !pattern) {
322
+ pattern = "failing_edit";
323
+ }
324
+ }
325
+ }
326
+
327
+ const repeated = Boolean(pattern);
328
+ if (repeated) {
329
+ this.detections += 1;
330
+ }
331
+
89
332
  return {
90
- repeated: consecutive >= this.maxRepeats,
333
+ repeated,
91
334
  count: consecutive,
335
+ pattern,
336
+ detections: this.detections,
337
+ shouldStop: this.detections >= 3,
338
+ hint: repeated ? LOOP_HINT_TEXT : "",
92
339
  signature: sig,
93
340
  };
94
341
  }
95
342
  }
96
343
 
97
344
  module.exports = {
345
+ LOOP_HINT_TEXT,
98
346
  repairToolArguments,
347
+ validateToolArgs,
348
+ isToolFailure,
99
349
  LoopDetector,
100
350
  };
@@ -0,0 +1,167 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Incremental streaming JSON parser designed for tool call argument deltas.
5
+ * Robustly extracts `path` and unescapes `content`/`replacement` chunks on the fly,
6
+ * even when escape sequences or Unicode sequences are split across chunk boundaries,
7
+ * or when `content` arrives before `path`.
8
+ */
9
+ class StreamingToolArgsParser {
10
+ constructor({ onPath, onContentChunk, onContentEnd }) {
11
+ this.onPath = onPath;
12
+ this.onContentChunk = onContentChunk;
13
+ this.onContentEnd = onContentEnd;
14
+
15
+ this.mode = "SEEK_KEY"; // SEEK_KEY | IN_KEY | AFTER_KEY | IN_VALUE_STRING | IN_VALUE_OTHER
16
+ this.keyBuf = "";
17
+ this.activeKey = "";
18
+ this.escape = false;
19
+ this.inUnicode = false;
20
+ this.unicodeBuf = "";
21
+ this.path = "";
22
+ this.pathEmitted = false;
23
+ this.contentEnded = false;
24
+ this.bufferedChunks = [];
25
+ }
26
+
27
+ push(chunk) {
28
+ if (!chunk) return;
29
+ let pending = "";
30
+ let pendingKey = "";
31
+
32
+ for (let i = 0; i < chunk.length; i++) {
33
+ const ch = chunk[i];
34
+
35
+ if (this.mode === "SEEK_KEY") {
36
+ if (ch === '"') {
37
+ this.mode = "IN_KEY";
38
+ this.keyBuf = "";
39
+ }
40
+ } else if (this.mode === "IN_KEY") {
41
+ if (ch === '"') {
42
+ this.activeKey = this.keyBuf;
43
+ this.mode = "AFTER_KEY";
44
+ } else {
45
+ this.keyBuf += ch;
46
+ }
47
+ } else if (this.mode === "AFTER_KEY") {
48
+ if (ch === ":") {
49
+ // separator; wait for quote or non-whitespace
50
+ } else if (ch === '"') {
51
+ this.mode = "IN_VALUE_STRING";
52
+ this.escape = false;
53
+ this.inUnicode = false;
54
+ } else if (!/\s/.test(ch)) {
55
+ this.mode = "IN_VALUE_OTHER";
56
+ }
57
+ } else if (this.mode === "IN_VALUE_OTHER") {
58
+ if (ch === "," || ch === "}") {
59
+ this.mode = "SEEK_KEY";
60
+ this.activeKey = "";
61
+ }
62
+ } else if (this.mode === "IN_VALUE_STRING") {
63
+ if (this.escape) {
64
+ if (this.inUnicode) {
65
+ this.unicodeBuf += ch;
66
+ if (this.unicodeBuf.length === 4) {
67
+ const code = parseInt(this.unicodeBuf, 16);
68
+ const unich = Number.isNaN(code) ? "" : String.fromCharCode(code);
69
+ if (this.activeKey === "path") {
70
+ this.path += unich;
71
+ } else if (
72
+ this.activeKey === "content" ||
73
+ this.activeKey === "replacement" ||
74
+ this.activeKey === "patch"
75
+ ) {
76
+ pending += unich;
77
+ pendingKey = this.activeKey;
78
+ }
79
+ this.escape = false;
80
+ this.inUnicode = false;
81
+ }
82
+ } else if (ch === "u") {
83
+ this.inUnicode = true;
84
+ this.unicodeBuf = "";
85
+ } else {
86
+ let decoded = ch;
87
+ if (ch === "n") decoded = "\n";
88
+ else if (ch === "r") decoded = "\r";
89
+ else if (ch === "t") decoded = "\t";
90
+ else if (ch === "b") decoded = "\b";
91
+ else if (ch === "f") decoded = "\f";
92
+ else if (ch === '"') decoded = '"';
93
+ else if (ch === "\\") decoded = "\\";
94
+ else if (ch === "/") decoded = "/";
95
+
96
+ if (this.activeKey === "path") {
97
+ this.path += decoded;
98
+ } else if (
99
+ this.activeKey === "content" ||
100
+ this.activeKey === "replacement" ||
101
+ this.activeKey === "patch"
102
+ ) {
103
+ pending += decoded;
104
+ pendingKey = this.activeKey;
105
+ }
106
+ this.escape = false;
107
+ }
108
+ } else if (ch === "\\") {
109
+ this.escape = true;
110
+ } else if (ch === '"') {
111
+ // Current string value ended
112
+ if (this.activeKey === "path") {
113
+ this._flushPath();
114
+ } else if (
115
+ this.activeKey === "content" ||
116
+ this.activeKey === "replacement" ||
117
+ this.activeKey === "patch"
118
+ ) {
119
+ if (pending.length > 0) {
120
+ this._emitContent(pending, pendingKey);
121
+ pending = "";
122
+ }
123
+ this.contentEnded = true;
124
+ this.onContentEnd?.(this.path, this.activeKey);
125
+ }
126
+ this.mode = "SEEK_KEY";
127
+ this.activeKey = "";
128
+ } else {
129
+ if (this.activeKey === "path") {
130
+ this.path += ch;
131
+ } else if (
132
+ this.activeKey === "content" ||
133
+ this.activeKey === "replacement" ||
134
+ this.activeKey === "patch"
135
+ ) {
136
+ pending += ch;
137
+ pendingKey = this.activeKey;
138
+ }
139
+ }
140
+ }
141
+ }
142
+
143
+ if (pending.length > 0) {
144
+ this._emitContent(pending, pendingKey);
145
+ }
146
+ }
147
+
148
+ _flushPath() {
149
+ if (!this.pathEmitted && this.path) {
150
+ this.pathEmitted = true;
151
+ this.onPath?.(this.path);
152
+ this.bufferedChunks = [];
153
+ if (this.contentEnded) {
154
+ this.onContentEnd?.(this.path, "content");
155
+ }
156
+ }
157
+ }
158
+
159
+ _emitContent(str, key) {
160
+ // Stream live chunks immediately even when content arrives before `path`, so UI line/token counters update in real time.
161
+ this.onContentChunk?.(str, this.path || "", key);
162
+ }
163
+ }
164
+
165
+ module.exports = {
166
+ StreamingToolArgsParser,
167
+ };