erosolar-cli 2.1.172 → 2.1.174

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.
Files changed (172) hide show
  1. package/README.md +1 -1
  2. package/agents/erosolar-code.rules.json +2 -2
  3. package/agents/general.rules.json +21 -3
  4. package/dist/capabilities/statusCapability.js +2 -2
  5. package/dist/capabilities/statusCapability.js.map +1 -1
  6. package/dist/contracts/agent-schemas.json +5 -5
  7. package/dist/core/agent.d.ts +83 -24
  8. package/dist/core/agent.d.ts.map +1 -1
  9. package/dist/core/agent.js +499 -248
  10. package/dist/core/agent.js.map +1 -1
  11. package/dist/core/preferences.d.ts +1 -0
  12. package/dist/core/preferences.d.ts.map +1 -1
  13. package/dist/core/preferences.js +8 -1
  14. package/dist/core/preferences.js.map +1 -1
  15. package/dist/core/reliabilityPrompt.d.ts +9 -0
  16. package/dist/core/reliabilityPrompt.d.ts.map +1 -0
  17. package/dist/core/reliabilityPrompt.js +31 -0
  18. package/dist/core/reliabilityPrompt.js.map +1 -0
  19. package/dist/core/schemaValidator.js +3 -3
  20. package/dist/core/schemaValidator.js.map +1 -1
  21. package/dist/core/toolPreconditions.d.ts +0 -11
  22. package/dist/core/toolPreconditions.d.ts.map +1 -1
  23. package/dist/core/toolPreconditions.js +33 -164
  24. package/dist/core/toolPreconditions.js.map +1 -1
  25. package/dist/core/toolRuntime.d.ts.map +1 -1
  26. package/dist/core/toolRuntime.js +9 -114
  27. package/dist/core/toolRuntime.js.map +1 -1
  28. package/dist/core/updateChecker.d.ts +61 -1
  29. package/dist/core/updateChecker.d.ts.map +1 -1
  30. package/dist/core/updateChecker.js +147 -3
  31. package/dist/core/updateChecker.js.map +1 -1
  32. package/dist/headless/headlessApp.d.ts.map +1 -1
  33. package/dist/headless/headlessApp.js +0 -39
  34. package/dist/headless/headlessApp.js.map +1 -1
  35. package/dist/plugins/tools/nodeDefaults.d.ts.map +1 -1
  36. package/dist/plugins/tools/nodeDefaults.js +0 -2
  37. package/dist/plugins/tools/nodeDefaults.js.map +1 -1
  38. package/dist/providers/openaiResponsesProvider.d.ts.map +1 -1
  39. package/dist/providers/openaiResponsesProvider.js +79 -74
  40. package/dist/providers/openaiResponsesProvider.js.map +1 -1
  41. package/dist/runtime/agentController.d.ts.map +1 -1
  42. package/dist/runtime/agentController.js +6 -3
  43. package/dist/runtime/agentController.js.map +1 -1
  44. package/dist/runtime/agentSession.d.ts +0 -2
  45. package/dist/runtime/agentSession.d.ts.map +1 -1
  46. package/dist/runtime/agentSession.js +2 -2
  47. package/dist/runtime/agentSession.js.map +1 -1
  48. package/dist/shell/interactiveShell.d.ts +11 -18
  49. package/dist/shell/interactiveShell.d.ts.map +1 -1
  50. package/dist/shell/interactiveShell.js +273 -291
  51. package/dist/shell/interactiveShell.js.map +1 -1
  52. package/dist/shell/shellApp.d.ts.map +1 -1
  53. package/dist/shell/shellApp.js +7 -1
  54. package/dist/shell/shellApp.js.map +1 -1
  55. package/dist/shell/systemPrompt.d.ts.map +1 -1
  56. package/dist/shell/systemPrompt.js +4 -15
  57. package/dist/shell/systemPrompt.js.map +1 -1
  58. package/dist/subagents/taskRunner.js +2 -1
  59. package/dist/subagents/taskRunner.js.map +1 -1
  60. package/dist/tools/bashTools.d.ts.map +1 -1
  61. package/dist/tools/bashTools.js +101 -8
  62. package/dist/tools/bashTools.js.map +1 -1
  63. package/dist/tools/diffUtils.d.ts +8 -2
  64. package/dist/tools/diffUtils.d.ts.map +1 -1
  65. package/dist/tools/diffUtils.js +72 -13
  66. package/dist/tools/diffUtils.js.map +1 -1
  67. package/dist/tools/grepTools.d.ts.map +1 -1
  68. package/dist/tools/grepTools.js +10 -2
  69. package/dist/tools/grepTools.js.map +1 -1
  70. package/dist/tools/planningTools.d.ts +0 -10
  71. package/dist/tools/planningTools.d.ts.map +1 -1
  72. package/dist/tools/planningTools.js +0 -16
  73. package/dist/tools/planningTools.js.map +1 -1
  74. package/dist/tools/searchTools.d.ts.map +1 -1
  75. package/dist/tools/searchTools.js +4 -2
  76. package/dist/tools/searchTools.js.map +1 -1
  77. package/dist/ui/PromptController.d.ts +1 -4
  78. package/dist/ui/PromptController.d.ts.map +1 -1
  79. package/dist/ui/PromptController.js +1 -7
  80. package/dist/ui/PromptController.js.map +1 -1
  81. package/dist/ui/ShellUIAdapter.d.ts +292 -28
  82. package/dist/ui/ShellUIAdapter.d.ts.map +1 -1
  83. package/dist/ui/ShellUIAdapter.js +1513 -121
  84. package/dist/ui/ShellUIAdapter.js.map +1 -1
  85. package/dist/ui/UnifiedUIController.d.ts +81 -0
  86. package/dist/ui/UnifiedUIController.d.ts.map +1 -0
  87. package/dist/ui/UnifiedUIController.js +212 -0
  88. package/dist/ui/UnifiedUIController.js.map +1 -0
  89. package/dist/ui/UnifiedUIRenderer.d.ts +133 -30
  90. package/dist/ui/UnifiedUIRenderer.d.ts.map +1 -1
  91. package/dist/ui/UnifiedUIRenderer.js +939 -370
  92. package/dist/ui/UnifiedUIRenderer.js.map +1 -1
  93. package/dist/ui/animatedStatus.d.ts +128 -6
  94. package/dist/ui/animatedStatus.d.ts.map +1 -1
  95. package/dist/ui/animatedStatus.js +383 -50
  96. package/dist/ui/animatedStatus.js.map +1 -1
  97. package/dist/ui/animation/AnimationScheduler.d.ts +192 -0
  98. package/dist/ui/animation/AnimationScheduler.d.ts.map +1 -0
  99. package/dist/ui/animation/AnimationScheduler.js +432 -0
  100. package/dist/ui/animation/AnimationScheduler.js.map +1 -0
  101. package/dist/ui/display.d.ts +182 -26
  102. package/dist/ui/display.d.ts.map +1 -1
  103. package/dist/ui/display.js +678 -97
  104. package/dist/ui/display.js.map +1 -1
  105. package/dist/ui/inPlaceUpdater.d.ts +181 -0
  106. package/dist/ui/inPlaceUpdater.d.ts.map +1 -0
  107. package/dist/ui/inPlaceUpdater.js +515 -0
  108. package/dist/ui/inPlaceUpdater.js.map +1 -0
  109. package/dist/ui/interrupts/InterruptManager.d.ts +142 -0
  110. package/dist/ui/interrupts/InterruptManager.d.ts.map +1 -0
  111. package/dist/ui/interrupts/InterruptManager.js +439 -0
  112. package/dist/ui/interrupts/InterruptManager.js.map +1 -0
  113. package/dist/ui/layout.d.ts +0 -1
  114. package/dist/ui/layout.d.ts.map +1 -1
  115. package/dist/ui/layout.js +0 -12
  116. package/dist/ui/layout.js.map +1 -1
  117. package/dist/ui/orchestration/UIUpdateCoordinator.d.ts +61 -7
  118. package/dist/ui/orchestration/UIUpdateCoordinator.d.ts.map +1 -1
  119. package/dist/ui/orchestration/UIUpdateCoordinator.js +232 -20
  120. package/dist/ui/orchestration/UIUpdateCoordinator.js.map +1 -1
  121. package/dist/ui/shortcutsHelp.d.ts.map +1 -1
  122. package/dist/ui/shortcutsHelp.js +0 -1
  123. package/dist/ui/shortcutsHelp.js.map +1 -1
  124. package/dist/ui/telemetry/ResponseTracker.d.ts +22 -0
  125. package/dist/ui/telemetry/ResponseTracker.d.ts.map +1 -0
  126. package/dist/ui/telemetry/ResponseTracker.js +60 -0
  127. package/dist/ui/telemetry/ResponseTracker.js.map +1 -0
  128. package/dist/ui/telemetry/UITelemetry.d.ts +181 -0
  129. package/dist/ui/telemetry/UITelemetry.d.ts.map +1 -0
  130. package/dist/ui/telemetry/UITelemetry.js +446 -0
  131. package/dist/ui/telemetry/UITelemetry.js.map +1 -0
  132. package/dist/ui/unified/index.d.ts +30 -1
  133. package/dist/ui/unified/index.d.ts.map +1 -1
  134. package/dist/ui/unified/index.js +45 -2
  135. package/dist/ui/unified/index.js.map +1 -1
  136. package/dist/ui/unified/layout.d.ts +12 -0
  137. package/dist/ui/unified/layout.d.ts.map +1 -0
  138. package/dist/ui/unified/layout.js +96 -0
  139. package/dist/ui/unified/layout.js.map +1 -0
  140. package/package.json +2 -3
  141. package/dist/StringUtils.d.ts +0 -8
  142. package/dist/StringUtils.d.ts.map +0 -1
  143. package/dist/StringUtils.js +0 -11
  144. package/dist/StringUtils.js.map +0 -1
  145. package/dist/core/aiFlowSupervisor.d.ts +0 -44
  146. package/dist/core/aiFlowSupervisor.d.ts.map +0 -1
  147. package/dist/core/aiFlowSupervisor.js +0 -299
  148. package/dist/core/aiFlowSupervisor.js.map +0 -1
  149. package/dist/core/cliTestHarness.d.ts +0 -200
  150. package/dist/core/cliTestHarness.d.ts.map +0 -1
  151. package/dist/core/cliTestHarness.js +0 -549
  152. package/dist/core/cliTestHarness.js.map +0 -1
  153. package/dist/core/testUtils.d.ts +0 -121
  154. package/dist/core/testUtils.d.ts.map +0 -1
  155. package/dist/core/testUtils.js +0 -235
  156. package/dist/core/testUtils.js.map +0 -1
  157. package/dist/core/toolValidation.d.ts +0 -116
  158. package/dist/core/toolValidation.d.ts.map +0 -1
  159. package/dist/core/toolValidation.js +0 -282
  160. package/dist/core/toolValidation.js.map +0 -1
  161. package/dist/ui/planOverlay.d.ts +0 -28
  162. package/dist/ui/planOverlay.d.ts.map +0 -1
  163. package/dist/ui/planOverlay.js +0 -156
  164. package/dist/ui/planOverlay.js.map +0 -1
  165. package/dist/ui/streamingFormatter.d.ts +0 -30
  166. package/dist/ui/streamingFormatter.d.ts.map +0 -1
  167. package/dist/ui/streamingFormatter.js +0 -91
  168. package/dist/ui/streamingFormatter.js.map +0 -1
  169. package/dist/utils/errorUtils.d.ts +0 -16
  170. package/dist/utils/errorUtils.d.ts.map +0 -1
  171. package/dist/utils/errorUtils.js +0 -66
  172. package/dist/utils/errorUtils.js.map +0 -1
@@ -4,65 +4,7 @@ import { safeErrorMessage } from './secretStore.js';
4
4
  * Maximum number of context overflow recovery attempts
5
5
  */
6
6
  const MAX_CONTEXT_RECOVERY_ATTEMPTS = 3;
7
- /**
8
- * Maximum number of auto-continuation attempts when model expresses intent but doesn't act
9
- */
10
- const MAX_AUTO_CONTINUE_ATTEMPTS = 3;
11
- /**
12
- * Streaming safety timeouts (ms)
13
- * - First chunk timeout: fail fast if the stream never starts
14
- * - Inactivity timeout: abort if no chunks arrive for an extended period
15
- */
16
- // Allow more headroom before declaring a streaming stall to avoid premature fallbacks.
17
- const STREAM_FIRST_CHUNK_TIMEOUT_MS = 25000;
18
- const STREAM_INACTIVITY_TIMEOUT_MS = 60000;
19
- /**
20
- * Patterns that indicate the model intends to take action but hasn't yet
21
- * These suggest the model should be prompted to continue
22
- */
23
- const INTENT_WITHOUT_ACTION_PATTERNS = [
24
- // "Let me X" patterns - model is stating what it will do
25
- /\blet me\s+(create|write|implement|add|update|edit|modify|fix|build|make|refactor|read|check|look|search|find|analyze|examine|review)/i,
26
- // "I'll X" / "I will X" patterns
27
- /\bi['']ll\s+(create|write|implement|add|update|edit|modify|fix|build|make|refactor|read|check|look|search|find|analyze|start|begin|now)/i,
28
- /\bi will\s+(create|write|implement|add|update|edit|modify|fix|build|make|refactor|read|check|look|search|find|analyze|start|begin|now)/i,
29
- // "I'm going to X" patterns
30
- /\bi['']m going to\s+(create|write|implement|add|update|edit|modify|fix|build|make|refactor|read|check|look|search|find|analyze)/i,
31
- // "Now I'll X" / "First, I'll X" patterns
32
- /\b(now|first|next)\s*(,)?\s*i['']ll\s+/i,
33
- // Explicit continuation signals
34
- /\bhere['']s (the|my) (plan|approach|solution|implementation)/i,
35
- // Numbered steps suggesting action to come
36
- /^\s*\d+\.\s+(create|write|implement|add|update|edit|modify|fix|show|read|check)/im,
37
- // Bullet points suggesting planned actions
38
- /^[\s•\-\*]+\s*(create|write|implement|add|update|edit|modify|fix|shows?|reads?|checks?)\s/im,
39
- ];
40
- const AUTO_CONTINUE_PROMPTS = [
41
- 'Continue. Use tools now: start with Read/read_file to inspect the target file, then call Edit (or Write if available) with file_path/old_string/new_string to apply changes. Keep using tools until the task is done.',
42
- 'You MUST call tools immediately. Issue Read -> Edit/Write tool calls with explicit parameters; no more explaining or planning.',
43
- 'CRITICAL: Call a tool right now. Use Edit with file_path, old_string, new_string (or Write with file_path and content). Respond with tool calls only.',
44
- ];
45
- /**
46
- * Check if response indicates intent to act without actually acting
47
- * This detects when the model says "let me do X" but doesn't call any tools
48
- */
49
- function shouldAutoContinue(content, hasToolCalls) {
50
- // If there are tool calls, no need to auto-continue
51
- if (hasToolCalls) {
52
- return false;
53
- }
54
- // If content is very short, likely not an incomplete intent
55
- if (content.length < 50) {
56
- return false;
57
- }
58
- // Check for intent patterns
59
- for (const pattern of INTENT_WITHOUT_ACTION_PATTERNS) {
60
- if (pattern.test(content)) {
61
- return true;
62
- }
63
- }
64
- return false;
65
- }
7
+ // Streaming runs without timeouts - we let the model take as long as it needs
66
8
  /**
67
9
  * Check if an error is a context overflow error
68
10
  */
@@ -77,15 +19,53 @@ function isContextOverflowError(error) {
77
19
  message.includes('max_tokens') ||
78
20
  message.includes('context window'));
79
21
  }
80
- class StreamInterruptionError extends Error {
81
- reason;
82
- partialResponse;
83
- constructor(reason, message, partialResponse) {
84
- super(message);
85
- this.name = 'StreamInterruptionError';
86
- this.reason = reason;
87
- this.partialResponse = partialResponse;
22
+ /**
23
+ * Check if an error is a transient/retryable error (network issues, rate limits, server errors)
24
+ */
25
+ function isTransientError(error) {
26
+ if (!(error instanceof Error))
27
+ return false;
28
+ const message = error.message.toLowerCase();
29
+ // Network errors
30
+ const networkPatterns = [
31
+ 'econnrefused', 'econnreset', 'enotfound', 'etimedout', 'epipe',
32
+ 'network error', 'connection error', 'fetch failed', 'socket hang up',
33
+ 'network is unreachable', 'connection refused', 'connection reset',
34
+ ];
35
+ if (networkPatterns.some(p => message.includes(p))) {
36
+ return true;
88
37
  }
38
+ // Rate limit errors
39
+ if (message.includes('rate limit') || message.includes('429') || message.includes('too many requests')) {
40
+ return true;
41
+ }
42
+ // Server errors (5xx)
43
+ if (message.includes('500') || message.includes('502') || message.includes('503') || message.includes('504')) {
44
+ return true;
45
+ }
46
+ // Temporary service errors
47
+ if (message.includes('service unavailable') || message.includes('temporarily unavailable') ||
48
+ message.includes('overloaded') || message.includes('server error')) {
49
+ return true;
50
+ }
51
+ return false;
52
+ }
53
+ /**
54
+ * Maximum number of transient error retries
55
+ */
56
+ const MAX_TRANSIENT_RETRIES = 3;
57
+ /**
58
+ * Delay before retry (in ms), with exponential backoff
59
+ */
60
+ function getRetryDelay(attempt) {
61
+ // Base delay of 1 second, doubles each attempt: 1s, 2s, 4s
62
+ return Math.min(1000 * Math.pow(2, attempt - 1), 10000);
63
+ }
64
+ /**
65
+ * Sleep for the specified milliseconds
66
+ */
67
+ function sleep(ms) {
68
+ return new Promise(resolve => setTimeout(resolve, ms));
89
69
  }
90
70
  export class AgentRuntime {
91
71
  messages = [];
@@ -99,7 +79,53 @@ export class AgentRuntime {
99
79
  modelId;
100
80
  workingDirectory;
101
81
  cancellationRequested = false;
102
- _autoContinueEnabled = false;
82
+ // Loop detection: track last tool calls to detect stuck loops
83
+ lastToolCallSignature = null;
84
+ repeatedToolCallCount = 0;
85
+ static MAX_REPEATED_TOOL_CALLS = 5; // Allow up to 4 identical calls before stopping
86
+ // Behavioral loop detection: track recent tool calls to catch repetitive patterns
87
+ // e.g., calling "execute_bash" with "git status" 5 times even if output differs slightly
88
+ recentToolCalls = [];
89
+ static TOOL_HISTORY_SIZE = 12;
90
+ static BEHAVIORAL_LOOP_THRESHOLD = 3; // Same tool+cmd 3+ times in last 12 = stuck
91
+ // Never cache stateful tools - they must always execute to reflect current system state
92
+ static NON_CACHEABLE_TOOL_NAMES = new Set([
93
+ 'bash',
94
+ 'execute_bash',
95
+ 'execute_command',
96
+ 'run_command',
97
+ 'edit',
98
+ 'edit_file',
99
+ 'notebookedit',
100
+ 'read',
101
+ 'read_file',
102
+ 'read_files',
103
+ 'list_files',
104
+ 'list_dir',
105
+ 'glob',
106
+ 'grep',
107
+ 'search',
108
+ 'search_text',
109
+ 'git_status',
110
+ 'git_diff',
111
+ 'git_log',
112
+ 'git_commit',
113
+ 'git_push',
114
+ ]);
115
+ // Skip loop short-circuiting for direct execution tools to avoid blocking user commands
116
+ static LOOP_EXEMPT_TOOL_NAMES = new Set([
117
+ 'bash',
118
+ 'execute_bash',
119
+ 'execute_command',
120
+ 'run_command',
121
+ 'edit',
122
+ 'edit_file',
123
+ 'notebookedit',
124
+ ]);
125
+ // Tool result cache: prevent duplicate identical tool calls by returning cached results
126
+ // Key: tool signature (name + JSON args), Value: result string
127
+ toolResultCache = new Map();
128
+ static TOOL_CACHE_MAX_SIZE = 50; // Keep last 50 tool results
103
129
  constructor(options) {
104
130
  this.provider = options.provider;
105
131
  this.toolRuntime = options.toolRuntime;
@@ -108,7 +134,6 @@ export class AgentRuntime {
108
134
  this.providerId = options.providerId ?? 'unknown';
109
135
  this.modelId = options.modelId ?? 'unknown';
110
136
  this.workingDirectory = options.workingDirectory ?? process.cwd();
111
- this._autoContinueEnabled = options.autoContinue ?? false;
112
137
  const trimmedPrompt = options.systemPrompt.trim();
113
138
  this.baseSystemPrompt = trimmedPrompt || null;
114
139
  if (trimmedPrompt) {
@@ -134,25 +159,14 @@ export class AgentRuntime {
134
159
  isRunning() {
135
160
  return this.activeRun !== null;
136
161
  }
137
- /**
138
- * Check if auto-continuation is enabled.
139
- */
140
- isAutoContinueEnabled() {
141
- return this._autoContinueEnabled;
142
- }
143
- /**
144
- * Enable or disable auto-continuation.
145
- */
146
- setAutoContinue(enabled) {
147
- this._autoContinueEnabled = enabled;
148
- }
149
162
  async send(text, useStreaming = false) {
150
163
  const prompt = text.trim();
151
164
  if (!prompt) {
152
165
  return '';
153
166
  }
154
- // Reset cancellation flag at start of new request
167
+ // Reset cancellation flag and loop tracking at start of new request
155
168
  this.cancellationRequested = false;
169
+ this.resetBehavioralLoopTracking();
156
170
  // Handle multi-line paste: show summary to user, send full content to AI
157
171
  if (isMultilinePaste(prompt)) {
158
172
  const processed = processPaste(prompt);
@@ -168,28 +182,9 @@ export class AgentRuntime {
168
182
  const run = { startedAt: Date.now() };
169
183
  this.activeRun = run;
170
184
  try {
185
+ // Always use streaming when available - no fallback
171
186
  if (useStreaming && this.provider.generateStream) {
172
- try {
173
- return await this.processConversationStreaming();
174
- }
175
- catch (error) {
176
- const message = safeErrorMessage(error);
177
- const reason = error instanceof StreamInterruptionError ? error.reason : undefined;
178
- const partialResponse = error instanceof StreamInterruptionError ? error.partialResponse : undefined;
179
- console.warn(`[agent] Streaming failed, falling back to non-streaming: ${message}`);
180
- // If we captured part of the response, seed it into history and ask the model to continue
181
- // so we don't restart the answer from scratch during fallback.
182
- if (partialResponse && partialResponse.trim()) {
183
- const partial = partialResponse.trim();
184
- this.messages.push({ role: 'assistant', content: partial });
185
- this.messages.push({
186
- role: 'user',
187
- content: 'Continue your previous response from where it stopped. Do not repeat text you already provided.',
188
- });
189
- }
190
- this.callbacks.onStreamFallback?.({ message, error, reason, partialResponse });
191
- return await this.processConversation();
192
- }
187
+ return await this.processConversationStreaming();
193
188
  }
194
189
  return await this.processConversation();
195
190
  }
@@ -203,7 +198,7 @@ export class AgentRuntime {
203
198
  }
204
199
  async processConversation() {
205
200
  let contextRecoveryAttempts = 0;
206
- let autoContinueAttempts = 0;
201
+ let transientRetryAttempts = 0;
207
202
  while (true) {
208
203
  // Check for cancellation at start of each iteration
209
204
  if (this.cancellationRequested) {
@@ -219,44 +214,66 @@ export class AgentRuntime {
219
214
  // Reset recovery attempts on successful generation
220
215
  contextRecoveryAttempts = 0;
221
216
  if (response.type === 'tool_calls') {
222
- const suppressNarration = this.shouldSuppressToolNarration();
223
- const narration = suppressNarration ? '' : response.content?.trim();
217
+ // BEHAVIORAL LOOP DETECTION: Check if model is stuck calling same tool repeatedly
218
+ const behavioralLoopResult = this.checkBehavioralLoop(response.toolCalls);
219
+ if (behavioralLoopResult) {
220
+ this.emitAssistantMessage(behavioralLoopResult, { isFinal: true, usage, contextStats });
221
+ this.messages.push({ role: 'assistant', content: behavioralLoopResult });
222
+ return behavioralLoopResult;
223
+ }
224
+ // Loop detection: check if same tool calls are being repeated (exact signature match)
225
+ const signatureCalls = response.toolCalls.filter(call => !this.shouldSkipLoopDetection(call));
226
+ const toolSignature = signatureCalls.length
227
+ ? signatureCalls
228
+ .map((t) => `${t.name}:${JSON.stringify(t.arguments)}`)
229
+ .sort()
230
+ .join('|')
231
+ : null;
232
+ if (toolSignature && toolSignature === this.lastToolCallSignature) {
233
+ this.repeatedToolCallCount++;
234
+ if (this.repeatedToolCallCount >= AgentRuntime.MAX_REPEATED_TOOL_CALLS) {
235
+ // Break out of loop - model is stuck
236
+ const loopMsg = `Tool loop detected: same tools called ${this.repeatedToolCallCount} times. Please try a different approach or provide more specific instructions.`;
237
+ this.emitAssistantMessage(loopMsg, { isFinal: true, usage, contextStats });
238
+ this.messages.push({ role: 'assistant', content: loopMsg });
239
+ this.lastToolCallSignature = null;
240
+ this.repeatedToolCallCount = 0;
241
+ return loopMsg;
242
+ }
243
+ }
244
+ else if (toolSignature) {
245
+ this.lastToolCallSignature = toolSignature;
246
+ this.repeatedToolCallCount = 1;
247
+ }
248
+ else {
249
+ this.lastToolCallSignature = null;
250
+ this.repeatedToolCallCount = 0;
251
+ }
252
+ // Emit narration if present - it shows the AI's thought process before tools
253
+ const narration = response.content?.trim();
224
254
  if (narration) {
225
- this.emitAssistantMessage(narration, { isFinal: false, usage, contextStats });
255
+ this.emitAssistantMessage(narration, {
256
+ isFinal: false,
257
+ usage,
258
+ contextStats,
259
+ });
226
260
  }
227
261
  const assistantMessage = {
228
262
  role: 'assistant',
229
- content: suppressNarration ? '' : (response.content ?? ''),
263
+ content: response.content ?? '',
230
264
  };
231
265
  if (response.toolCalls?.length) {
232
266
  assistantMessage.toolCalls = response.toolCalls;
233
267
  }
234
268
  this.messages.push(assistantMessage);
235
269
  await this.resolveToolCalls(response.toolCalls);
236
- // Reset auto-continue counter since model is actively working
237
- autoContinueAttempts = 0;
238
270
  continue;
239
271
  }
240
272
  const reply = response.content?.trim() ?? '';
241
- // Check if model expressed intent to act but didn't call tools
242
- // This catches "Let me create..." without actual tool calls
243
- // Only auto-continue if the feature is enabled
244
- if (this._autoContinueEnabled && shouldAutoContinue(reply, false) && autoContinueAttempts < MAX_AUTO_CONTINUE_ATTEMPTS) {
245
- autoContinueAttempts++;
246
- // Emit the planning content but mark as non-final
247
- if (reply) {
248
- this.emitAssistantMessage(reply, { isFinal: false, usage, contextStats });
249
- }
250
- this.messages.push({ role: 'assistant', content: reply });
251
- // Auto-prompt with increasingly direct instructions
252
- const promptIndex = Math.min(autoContinueAttempts - 1, AUTO_CONTINUE_PROMPTS.length - 1);
253
- this.messages.push({
254
- role: 'user',
255
- content: AUTO_CONTINUE_PROMPTS[promptIndex],
256
- });
257
- const autoContinueMessage = `Model expressed intent but didn't use tools. Auto-prompting to continue...`;
258
- this.callbacks.onAutoContinue?.(autoContinueAttempts, MAX_AUTO_CONTINUE_ATTEMPTS, autoContinueMessage);
259
- continue;
273
+ // Reset loop detection when we get a text response (not just tool calls)
274
+ if (reply.length >= 10) {
275
+ this.lastToolCallSignature = null;
276
+ this.repeatedToolCallCount = 0;
260
277
  }
261
278
  if (reply) {
262
279
  this.emitAssistantMessage(reply, { isFinal: true, usage, contextStats });
@@ -278,6 +295,14 @@ export class AgentRuntime {
278
295
  continue;
279
296
  }
280
297
  }
298
+ // Auto-retry transient errors (network issues, rate limits, server errors)
299
+ if (isTransientError(error) && transientRetryAttempts < MAX_TRANSIENT_RETRIES) {
300
+ transientRetryAttempts++;
301
+ const delayMs = getRetryDelay(transientRetryAttempts);
302
+ this.callbacks.onRetrying?.(transientRetryAttempts, MAX_TRANSIENT_RETRIES, error);
303
+ await sleep(delayMs);
304
+ continue;
305
+ }
281
306
  // Re-throw if not recoverable or recovery failed
282
307
  throw error;
283
308
  }
@@ -288,7 +313,7 @@ export class AgentRuntime {
288
313
  return this.processConversation();
289
314
  }
290
315
  let contextRecoveryAttempts = 0;
291
- let autoContinueAttempts = 0;
316
+ let transientRetryAttempts = 0;
292
317
  while (true) {
293
318
  // Check for cancellation at start of each iteration
294
319
  if (this.cancellationRequested) {
@@ -321,45 +346,10 @@ export class AgentRuntime {
321
346
  }
322
347
  }
323
348
  };
324
- const buildTimeoutError = (reason) => {
325
- const base = reason === 'startup-timeout'
326
- ? 'Streaming stalled before any content arrived.'
327
- : 'Streaming stalled due to inactivity.';
328
- return new StreamInterruptionError(reason, `${base} Falling back to non-streaming.`, fullContent || reasoningContent);
329
- };
330
- // Timer for first token arrival
331
- let startupTimer = null;
332
- const startupTimeoutPromise = new Promise((_, reject) => {
333
- startupTimer = setTimeout(() => reject(buildTimeoutError('startup-timeout')), STREAM_FIRST_CHUNK_TIMEOUT_MS);
334
- });
335
- const createIdleTimeout = () => {
336
- let idleTimer = null;
337
- const promise = new Promise((_, reject) => {
338
- idleTimer = setTimeout(() => reject(buildTimeoutError('idle-timeout')), STREAM_INACTIVITY_TIMEOUT_MS);
339
- });
340
- const cancel = () => {
341
- if (idleTimer) {
342
- clearTimeout(idleTimer);
343
- idleTimer = null;
344
- }
345
- };
346
- return { promise, cancel };
347
- };
348
- let idleTimeout = createIdleTimeout();
349
- let firstChunkSeen = false;
349
+ // Simple streaming loop - no timeouts, let the stream run until done
350
350
  try {
351
351
  while (true) {
352
- const races = [
353
- iterator.next(),
354
- idleTimeout.promise,
355
- ];
356
- if (!firstChunkSeen) {
357
- races.push(startupTimeoutPromise);
358
- }
359
- const result = (await Promise.race(races));
360
- // Reset idle timer for the next iteration
361
- idleTimeout.cancel();
362
- idleTimeout = createIdleTimeout();
352
+ const result = await iterator.next();
363
353
  // Check for cancellation during streaming
364
354
  if (this.cancellationRequested) {
365
355
  await closeStream();
@@ -374,17 +364,10 @@ export class AgentRuntime {
374
364
  break;
375
365
  }
376
366
  const chunk = result.value;
377
- if (!firstChunkSeen) {
378
- firstChunkSeen = true;
379
- if (startupTimer) {
380
- clearTimeout(startupTimer);
381
- startupTimer = null;
382
- }
383
- }
384
367
  if (chunk.type === 'reasoning' && chunk.content) {
368
+ // Buffer reasoning content - don't stream token-by-token
369
+ // It will be emitted as a complete block when ready
385
370
  reasoningContent += chunk.content;
386
- // Surface reasoning tokens to the UI so thought process is visible
387
- this.callbacks.onStreamChunk?.(chunk.content, 'reasoning');
388
371
  continue;
389
372
  }
390
373
  if (chunk.type === 'content' && chunk.content) {
@@ -397,11 +380,19 @@ export class AgentRuntime {
397
380
  }
398
381
  }
399
382
  else if (chunk.type === 'tool_call' && chunk.toolCall) {
400
- toolCalls.push(chunk.toolCall);
401
- // Drop any speculative narration once we know the model is actually calling tools
402
- if (suppressStreamNarration) {
403
- bufferedContent = '';
383
+ // On first tool call, flush any buffered content
384
+ if (toolCalls.length === 0) {
385
+ // Emit complete reasoning block first
386
+ if (reasoningContent.trim()) {
387
+ this.callbacks.onStreamChunk?.(reasoningContent, 'reasoning');
388
+ }
389
+ // Then emit buffered narration content
390
+ if (suppressStreamNarration && bufferedContent) {
391
+ this.callbacks.onStreamChunk?.(bufferedContent, 'content');
392
+ bufferedContent = '';
393
+ }
404
394
  }
395
+ toolCalls.push(chunk.toolCall);
405
396
  }
406
397
  else if (chunk.type === 'usage' && chunk.usage) {
407
398
  usage = chunk.usage;
@@ -409,61 +400,88 @@ export class AgentRuntime {
409
400
  }
410
401
  }
411
402
  finally {
412
- idleTimeout.cancel();
413
- if (startupTimer) {
414
- clearTimeout(startupTimer);
415
- }
416
403
  await closeStream();
417
404
  }
418
405
  // Reset recovery attempts on successful generation
419
406
  contextRecoveryAttempts = 0;
420
407
  const contextStats = this.getContextStats();
421
408
  const combinedContent = fullContent || reasoningContent;
422
- // If no tool calls were issued, flush any buffered narration now
423
- if (suppressStreamNarration && toolCalls.length === 0 && bufferedContent) {
424
- this.callbacks.onStreamChunk?.(bufferedContent, 'content');
425
- bufferedContent = '';
409
+ // If no tool calls were issued, emit reasoning and buffered content as complete blocks
410
+ if (toolCalls.length === 0) {
411
+ // Emit complete reasoning block if we have one
412
+ if (reasoningContent.trim()) {
413
+ this.callbacks.onStreamChunk?.(reasoningContent, 'reasoning');
414
+ }
415
+ // Emit buffered narration content
416
+ if (suppressStreamNarration && bufferedContent) {
417
+ this.callbacks.onStreamChunk?.(bufferedContent, 'content');
418
+ bufferedContent = '';
419
+ }
426
420
  }
427
421
  // Check if we got tool calls
428
422
  if (toolCalls.length > 0) {
429
- const suppressNarration = this.shouldSuppressToolNarration();
430
- const narration = suppressNarration ? '' : combinedContent.trim();
423
+ // BEHAVIORAL LOOP DETECTION: Check if model is stuck calling same tool repeatedly
424
+ // This catches patterns like "git status" called 5 times even with slightly different outputs
425
+ const behavioralLoopResult = this.checkBehavioralLoop(toolCalls);
426
+ if (behavioralLoopResult) {
427
+ this.emitAssistantMessage(behavioralLoopResult, { isFinal: true, usage, contextStats, wasStreamed: true });
428
+ this.messages.push({ role: 'assistant', content: behavioralLoopResult });
429
+ return behavioralLoopResult;
430
+ }
431
+ // Loop detection: check if same tool calls are being repeated (exact signature match)
432
+ const signatureCalls = toolCalls.filter(call => !this.shouldSkipLoopDetection(call));
433
+ const toolSignature = signatureCalls.length
434
+ ? signatureCalls
435
+ .map((t) => `${t.name}:${JSON.stringify(t.arguments)}`)
436
+ .sort()
437
+ .join('|')
438
+ : null;
439
+ if (toolSignature && toolSignature === this.lastToolCallSignature) {
440
+ this.repeatedToolCallCount++;
441
+ if (this.repeatedToolCallCount >= AgentRuntime.MAX_REPEATED_TOOL_CALLS) {
442
+ // Break out of loop - model is stuck
443
+ const loopMsg = `Tool loop detected: same tools called ${this.repeatedToolCallCount} times. Please try a different approach or provide more specific instructions.`;
444
+ this.emitAssistantMessage(loopMsg, { isFinal: true, usage, contextStats, wasStreamed: true });
445
+ this.messages.push({ role: 'assistant', content: loopMsg });
446
+ this.lastToolCallSignature = null;
447
+ this.repeatedToolCallCount = 0;
448
+ return loopMsg;
449
+ }
450
+ }
451
+ else if (toolSignature) {
452
+ this.lastToolCallSignature = toolSignature;
453
+ this.repeatedToolCallCount = 1;
454
+ }
455
+ else {
456
+ this.lastToolCallSignature = null;
457
+ this.repeatedToolCallCount = 0;
458
+ }
459
+ // Content was already streamed via onStreamChunk, just record it for context
460
+ // (wasStreamed=true prevents duplicate display)
461
+ // Note: Acknowledgement injection happens during streaming (when first tool_call chunk arrives)
462
+ const narration = combinedContent.trim();
431
463
  if (narration) {
432
- // Mark as wasStreamed since content was already output via onStreamChunk
433
- this.emitAssistantMessage(narration, { isFinal: false, usage, contextStats, wasStreamed: true });
464
+ this.emitAssistantMessage(narration, {
465
+ isFinal: false,
466
+ usage,
467
+ contextStats,
468
+ wasStreamed: true,
469
+ });
434
470
  }
435
471
  const assistantMessage = {
436
472
  role: 'assistant',
437
- content: suppressNarration ? '' : combinedContent,
473
+ content: combinedContent,
438
474
  toolCalls,
439
475
  };
440
476
  this.messages.push(assistantMessage);
441
477
  await this.resolveToolCalls(toolCalls);
442
- // Reset auto-continue counter since model is actively working
443
- autoContinueAttempts = 0;
444
478
  continue;
445
479
  }
446
- // Check if model expressed intent to act but didn't call tools
447
- // This catches "Let me create..." without actual tool calls
448
- // Only auto-continue if the feature is enabled
449
480
  const reply = combinedContent.trim();
450
- if (this._autoContinueEnabled && shouldAutoContinue(reply, false) && autoContinueAttempts < MAX_AUTO_CONTINUE_ATTEMPTS) {
451
- autoContinueAttempts++;
452
- // Emit the planning content but mark as non-final
453
- // Mark as wasStreamed since content was already output via onStreamChunk
454
- if (reply) {
455
- this.emitAssistantMessage(reply, { isFinal: false, usage, contextStats, wasStreamed: true });
456
- }
457
- this.messages.push({ role: 'assistant', content: reply });
458
- // Auto-prompt with increasingly direct instructions
459
- const promptIndex = Math.min(autoContinueAttempts - 1, AUTO_CONTINUE_PROMPTS.length - 1);
460
- this.messages.push({
461
- role: 'user',
462
- content: AUTO_CONTINUE_PROMPTS[promptIndex],
463
- });
464
- const autoContinueMessage = `Model expressed intent but didn't use tools. Auto-prompting to continue...`;
465
- this.callbacks.onAutoContinue?.(autoContinueAttempts, MAX_AUTO_CONTINUE_ATTEMPTS, autoContinueMessage);
466
- continue;
481
+ // Reset loop detection when we get a text response (not just tool calls)
482
+ if (reply.length >= 10) {
483
+ this.lastToolCallSignature = null;
484
+ this.repeatedToolCallCount = 0;
467
485
  }
468
486
  // Final message - mark as streamed to avoid double-display in UI
469
487
  if (reply) {
@@ -486,6 +504,14 @@ export class AgentRuntime {
486
504
  continue;
487
505
  }
488
506
  }
507
+ // Auto-retry transient errors (network issues, rate limits, server errors)
508
+ if (isTransientError(error) && transientRetryAttempts < MAX_TRANSIENT_RETRIES) {
509
+ transientRetryAttempts++;
510
+ const delayMs = getRetryDelay(transientRetryAttempts);
511
+ this.callbacks.onRetrying?.(transientRetryAttempts, MAX_TRANSIENT_RETRIES, error);
512
+ await sleep(delayMs);
513
+ continue;
514
+ }
489
515
  // Re-throw if not recoverable or recovery failed
490
516
  throw error;
491
517
  }
@@ -517,7 +543,23 @@ export class AgentRuntime {
517
543
  // Fast path: single tool call
518
544
  if (numCalls === 1) {
519
545
  const call = toolCalls[0];
546
+ // Check cache first - prevent duplicate identical tool calls
547
+ const cached = this.getCachedToolResult(call);
548
+ if (cached !== null) {
549
+ // Return cached result with indicator that it was from cache
550
+ this.messages.push({
551
+ role: 'tool',
552
+ name: call.name,
553
+ toolCallId: call.id,
554
+ content: `[Cached result - identical call already executed]\n\n${cached}`,
555
+ });
556
+ return;
557
+ }
558
+ this.callbacks.onToolExecution?.(call.name, true);
520
559
  const output = await this.toolRuntime.execute(call);
560
+ this.callbacks.onToolExecution?.(call.name, false);
561
+ // Cache the result for future identical calls
562
+ this.cacheToolResult(call, output);
521
563
  this.messages.push({
522
564
  role: 'tool',
523
565
  name: call.name,
@@ -527,56 +569,106 @@ export class AgentRuntime {
527
569
  return;
528
570
  }
529
571
  // PERF: For reasonable batch sizes, execute all in parallel
572
+ // Check cache for each call and only execute non-cached ones
530
573
  if (numCalls <= 10) {
531
- const results = await Promise.all(toolCalls.map(async (call) => ({
532
- call,
533
- output: await this.toolRuntime.execute(call),
534
- })));
535
- // Add results to messages in the same order as tool calls
536
- for (const { call, output } of results) {
537
- this.messages.push({
538
- role: 'tool',
539
- name: call.name,
540
- toolCallId: call.id,
541
- content: output,
542
- });
574
+ const cachedResults = [];
575
+ const toExecute = [];
576
+ // Separate cached from non-cached calls
577
+ for (const call of toolCalls) {
578
+ const cached = this.getCachedToolResult(call);
579
+ if (cached !== null) {
580
+ cachedResults.push({ call, output: cached, fromCache: true });
581
+ }
582
+ else {
583
+ toExecute.push(call);
584
+ }
585
+ }
586
+ // Execute non-cached calls in parallel
587
+ if (toExecute.length > 0) {
588
+ const toolNames = toExecute.map(c => c.name).join(', ');
589
+ this.callbacks.onToolExecution?.(toolNames, true);
590
+ const executed = await Promise.all(toExecute.map(async (call) => {
591
+ const output = await this.toolRuntime.execute(call);
592
+ this.cacheToolResult(call, output);
593
+ return { call, output, fromCache: false };
594
+ }));
595
+ this.callbacks.onToolExecution?.(toolNames, false);
596
+ cachedResults.push(...executed);
597
+ }
598
+ // Add all results to messages in the original order
599
+ for (const originalCall of toolCalls) {
600
+ const result = cachedResults.find(r => r.call.id === originalCall.id);
601
+ if (result) {
602
+ const content = result.fromCache
603
+ ? `[Cached result - identical call already executed]\n\n${result.output}`
604
+ : result.output;
605
+ this.messages.push({
606
+ role: 'tool',
607
+ name: result.call.name,
608
+ toolCallId: result.call.id,
609
+ content,
610
+ });
611
+ }
543
612
  }
544
613
  return;
545
614
  }
546
- // PERF: For large batches, use chunked parallel execution
547
- // This prevents memory pressure from too many concurrent operations
615
+ // PERF: For large batches, use chunked parallel execution with caching
548
616
  const CHUNK_SIZE = 8;
549
- const results = [];
617
+ const allResults = [];
550
618
  for (let i = 0; i < numCalls; i += CHUNK_SIZE) {
551
619
  const chunk = toolCalls.slice(i, i + CHUNK_SIZE);
552
- const chunkResults = await Promise.all(chunk.map(async (call) => ({
553
- call,
554
- output: await this.toolRuntime.execute(call),
555
- })));
556
- results.push(...chunkResults);
557
- }
558
- // Add results to messages in order
559
- for (const { call, output } of results) {
560
- this.messages.push({
561
- role: 'tool',
562
- name: call.name,
563
- toolCallId: call.id,
564
- content: output,
565
- });
620
+ const cachedInChunk = [];
621
+ const toExecuteInChunk = [];
622
+ for (const call of chunk) {
623
+ const cached = this.getCachedToolResult(call);
624
+ if (cached !== null) {
625
+ cachedInChunk.push({ call, output: cached, fromCache: true });
626
+ }
627
+ else {
628
+ toExecuteInChunk.push(call);
629
+ }
630
+ }
631
+ if (toExecuteInChunk.length > 0) {
632
+ const chunkNames = toExecuteInChunk.map(c => c.name).join(', ');
633
+ this.callbacks.onToolExecution?.(chunkNames, true);
634
+ const executed = await Promise.all(toExecuteInChunk.map(async (call) => {
635
+ const output = await this.toolRuntime.execute(call);
636
+ this.cacheToolResult(call, output);
637
+ return { call, output, fromCache: false };
638
+ }));
639
+ this.callbacks.onToolExecution?.(chunkNames, false);
640
+ cachedInChunk.push(...executed);
641
+ }
642
+ allResults.push(...cachedInChunk);
643
+ }
644
+ // Add results to messages in original order
645
+ for (const originalCall of toolCalls) {
646
+ const result = allResults.find(r => r.call.id === originalCall.id);
647
+ if (result) {
648
+ const content = result.fromCache
649
+ ? `[Cached result - identical call already executed]\n\n${result.output}`
650
+ : result.output;
651
+ this.messages.push({
652
+ role: 'tool',
653
+ name: result.call.name,
654
+ toolCallId: result.call.id,
655
+ content,
656
+ });
657
+ }
566
658
  }
567
659
  }
568
660
  get providerTools() {
569
661
  return this.toolRuntime.listProviderTools();
570
662
  }
571
663
  /**
572
- * OpenAI models frequently add speculative tool narration in the content field.
573
- * Suppress that text to avoid surfacing hallucinated tool usage in the UI.
664
+ * Whether to suppress tool narration in the content field.
665
+ * Previously suppressed for OpenAI but now we show all thinking/narration.
574
666
  */
575
667
  shouldSuppressToolNarration() {
576
- return this.providerId.toLowerCase().includes('openai');
668
+ return false; // Always show thinking/narration
577
669
  }
578
670
  emitAssistantMessage(content, metadata) {
579
- if (!content) {
671
+ if (!content || !content.trim()) {
580
672
  return;
581
673
  }
582
674
  const elapsedMs = this.activeRun ? Date.now() - this.activeRun.startedAt : undefined;
@@ -626,6 +718,165 @@ export class AgentRuntime {
626
718
  model: this.modelId,
627
719
  });
628
720
  }
721
+ /**
722
+ * Extract a "command hash" from tool arguments for behavioral loop detection.
723
+ * For execute_bash, this is the actual command. For other tools, key identifying args.
724
+ */
725
+ extractCmdHash(name, args) {
726
+ // For bash/execute commands, extract the command itself
727
+ if (name === 'execute_bash' || name === 'Bash') {
728
+ const cmd = args['command'];
729
+ if (cmd) {
730
+ // Normalize: trim, take first 100 chars, remove variable parts like timestamps
731
+ return cmd.trim().slice(0, 100).replace(/\d{10,}/g, 'N');
732
+ }
733
+ }
734
+ // For file operations, use the path
735
+ if (name === 'read_file' || name === 'Read' || name === 'read_files') {
736
+ const path = args['path'] || args['file_path'] || args['paths'];
737
+ if (path)
738
+ return `path:${JSON.stringify(path).slice(0, 100)}`;
739
+ }
740
+ if (name === 'list_files' || name === 'Glob') {
741
+ const path = args['path'] || args['pattern'];
742
+ if (path)
743
+ return `path:${JSON.stringify(path).slice(0, 100)}`;
744
+ }
745
+ // For search, use the query/pattern
746
+ if (name === 'Grep' || name === 'grep' || name === 'search') {
747
+ const pattern = args['pattern'] || args['query'];
748
+ if (pattern)
749
+ return `search:${String(pattern).slice(0, 100)}`;
750
+ }
751
+ // Default: use first significant arg value
752
+ const firstArg = Object.values(args)[0];
753
+ if (firstArg) {
754
+ return String(firstArg).slice(0, 100);
755
+ }
756
+ return 'no-args';
757
+ }
758
+ /**
759
+ * Check for behavioral loops - model calling the same tool with similar args repeatedly.
760
+ * Returns an error message if a loop is detected, null otherwise.
761
+ *
762
+ * FUNDAMENTAL PREVENTION: Cached calls are excluded from loop detection since they
763
+ * don't actually execute (the cache provides the result). This means:
764
+ * - First call: executes and caches result
765
+ * - Second identical call: returns cached result, NOT counted toward loop
766
+ * - Only genuinely NEW (non-cached) repetitive calls trigger loop detection
767
+ *
768
+ * Direct execution tools (bash/edit) are also exempt to avoid short-circuiting
769
+ * legitimate repeated user commands.
770
+ *
771
+ * This catches patterns like:
772
+ * - "git status -sb" called 3 times with DIFFERENT outputs (cache miss each time)
773
+ * - Repeated file reads where file content changed
774
+ * - Repeated searches with same pattern but new results
775
+ */
776
+ checkBehavioralLoop(toolCalls) {
777
+ // Skip loop detection for direct execution tools (bash/edit) to avoid false positives
778
+ const loopEligibleCalls = toolCalls.filter(call => !this.shouldSkipLoopDetection(call));
779
+ if (loopEligibleCalls.length === 0) {
780
+ return null;
781
+ }
782
+ // Filter out calls that will be served from cache - these don't count toward loops
783
+ // since they're handled fundamentally by the caching mechanism
784
+ const nonCachedCalls = loopEligibleCalls.filter(call => this.getCachedToolResult(call) === null);
785
+ // If all calls are cached, no loop detection needed
786
+ if (nonCachedCalls.length === 0) {
787
+ return null;
788
+ }
789
+ // Count existing occurrences in recent history
790
+ const existingCounts = new Map();
791
+ for (const { name, cmdHash } of this.recentToolCalls) {
792
+ const key = `${name}:${cmdHash}`;
793
+ existingCounts.set(key, (existingCounts.get(key) ?? 0) + 1);
794
+ }
795
+ // Check if ANY incoming NON-CACHED call would exceed threshold
796
+ for (const call of nonCachedCalls) {
797
+ const cmdHash = this.extractCmdHash(call.name, call.arguments ?? {});
798
+ const key = `${call.name}:${cmdHash}`;
799
+ const currentCount = existingCounts.get(key) ?? 0;
800
+ // If adding this call would reach or exceed threshold, block immediately
801
+ if (currentCount + 1 >= AgentRuntime.BEHAVIORAL_LOOP_THRESHOLD) {
802
+ // Reset history to prevent immediate re-trigger
803
+ this.recentToolCalls = [];
804
+ return `Behavioral loop detected: "${call.name}" called ${currentCount + 1} times with similar arguments. The task appears stuck. Please try a different approach or provide more specific instructions.`;
805
+ }
806
+ }
807
+ // Track only non-cached tool calls (cached ones are handled by caching)
808
+ for (const call of nonCachedCalls) {
809
+ const cmdHash = this.extractCmdHash(call.name, call.arguments ?? {});
810
+ this.recentToolCalls.push({ name: call.name, cmdHash });
811
+ }
812
+ // Keep only recent history
813
+ while (this.recentToolCalls.length > AgentRuntime.TOOL_HISTORY_SIZE) {
814
+ this.recentToolCalls.shift();
815
+ }
816
+ return null;
817
+ }
818
+ /**
819
+ * Reset behavioral loop tracking (called when user provides new input or task completes)
820
+ */
821
+ resetBehavioralLoopTracking() {
822
+ this.recentToolCalls = [];
823
+ this.lastToolCallSignature = null;
824
+ this.repeatedToolCallCount = 0;
825
+ // Note: we DON'T clear toolResultCache here for cacheable tools; stateful tools bypass caching
826
+ }
827
+ /**
828
+ * Create a stable cache key for a tool call based on name and arguments
829
+ */
830
+ getToolCacheKey(call) {
831
+ const args = call.arguments ?? {};
832
+ // Sort keys for consistent ordering
833
+ const sortedArgs = Object.keys(args).sort().reduce((acc, key) => {
834
+ acc[key] = args[key];
835
+ return acc;
836
+ }, {});
837
+ return `${call.name}:${JSON.stringify(sortedArgs)}`;
838
+ }
839
+ /**
840
+ * Only cache tools that are safe to reuse; stateful commands must always execute.
841
+ */
842
+ isCacheableTool(call) {
843
+ const nameLower = call.name.toLowerCase();
844
+ return !AgentRuntime.NON_CACHEABLE_TOOL_NAMES.has(nameLower);
845
+ }
846
+ /**
847
+ * Direct execution tools should not trigger behavioral loop short-circuiting.
848
+ */
849
+ shouldSkipLoopDetection(call) {
850
+ const nameLower = call.name.toLowerCase();
851
+ return AgentRuntime.LOOP_EXEMPT_TOOL_NAMES.has(nameLower);
852
+ }
853
+ /**
854
+ * Get cached result for a tool call, or null if not cached
855
+ */
856
+ getCachedToolResult(call) {
857
+ if (!this.isCacheableTool(call)) {
858
+ return null;
859
+ }
860
+ const key = this.getToolCacheKey(call);
861
+ return this.toolResultCache.get(key) ?? null;
862
+ }
863
+ /**
864
+ * Cache a tool result for future identical calls
865
+ */
866
+ cacheToolResult(call, result) {
867
+ if (!this.isCacheableTool(call)) {
868
+ return;
869
+ }
870
+ const key = this.getToolCacheKey(call);
871
+ // Evict oldest entries if cache is full
872
+ if (this.toolResultCache.size >= AgentRuntime.TOOL_CACHE_MAX_SIZE) {
873
+ const firstKey = this.toolResultCache.keys().next().value;
874
+ if (firstKey) {
875
+ this.toolResultCache.delete(firstKey);
876
+ }
877
+ }
878
+ this.toolResultCache.set(key, result);
879
+ }
629
880
  getHistory() {
630
881
  return this.messages.map(cloneMessage);
631
882
  }