neoagent 3.2.1-beta.2 → 3.2.1-beta.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.
Files changed (144) hide show
  1. package/extensions/chrome-browser/background.mjs +318 -88
  2. package/extensions/chrome-browser/http.mjs +136 -0
  3. package/extensions/chrome-browser/protocol.mjs +511 -89
  4. package/flutter_app/lib/main_chat.dart +118 -739
  5. package/flutter_app/lib/main_controller.dart +29 -20
  6. package/flutter_app/lib/main_models.dart +3 -0
  7. package/flutter_app/lib/main_operations.dart +334 -321
  8. package/flutter_app/lib/main_settings.dart +4 -3
  9. package/flutter_app/lib/main_shared.dart +14 -11
  10. package/flutter_app/lib/src/desktop_companion_actions.dart +185 -31
  11. package/flutter_app/lib/src/desktop_companion_io.dart +319 -86
  12. package/flutter_app/windows/runner/flutter_window.cpp +143 -32
  13. package/lib/manager.js +106 -89
  14. package/lib/schema_migrations.js +67 -13
  15. package/package.json +20 -13
  16. package/runtime/paths.js +49 -5
  17. package/server/guest_agent.js +52 -30
  18. package/server/http/middleware.js +24 -0
  19. package/server/http/routes.js +4 -6
  20. package/server/public/.last_build_id +1 -1
  21. package/server/public/assets/fonts/MaterialIcons-Regular.otf +0 -0
  22. package/server/public/flutter_bootstrap.js +1 -1
  23. package/server/public/main.dart.js +30803 -30805
  24. package/server/routes/admin.js +1 -1
  25. package/server/routes/android.js +30 -34
  26. package/server/routes/browser.js +23 -15
  27. package/server/routes/desktop.js +18 -1
  28. package/server/routes/integrations.js +5 -1
  29. package/server/routes/memory.js +1 -0
  30. package/server/routes/settings.js +16 -5
  31. package/server/routes/social_reach.js +12 -3
  32. package/server/routes/social_video.js +4 -0
  33. package/server/services/ai/compaction.js +7 -2
  34. package/server/services/ai/history.js +44 -5
  35. package/server/services/ai/integrated_tools/http_request.js +8 -0
  36. package/server/services/ai/loop/agent_engine_core.js +347 -162
  37. package/server/services/ai/loop/callbacks.js +1 -0
  38. package/server/services/ai/loop/completion_judge.js +12 -0
  39. package/server/services/ai/loop/conversation_loop.js +348 -242
  40. package/server/services/ai/loop/messaging_delivery.js +129 -57
  41. package/server/services/ai/loop/model_call_guard.js +91 -0
  42. package/server/services/ai/loop/model_io.js +8 -41
  43. package/server/services/ai/loop/tool_dispatch.js +19 -8
  44. package/server/services/ai/loopPolicy.js +24 -19
  45. package/server/services/ai/model_discovery.js +227 -0
  46. package/server/services/ai/model_identity.js +71 -0
  47. package/server/services/ai/models.js +67 -162
  48. package/server/services/ai/providerRetry.js +17 -59
  49. package/server/services/ai/provider_selector.js +111 -0
  50. package/server/services/ai/providers/anthropic.js +2 -2
  51. package/server/services/ai/providers/claudeCode.js +15 -28
  52. package/server/services/ai/providers/githubCopilot.js +36 -16
  53. package/server/services/ai/providers/google.js +23 -5
  54. package/server/services/ai/providers/grok.js +4 -3
  55. package/server/services/ai/providers/grokOauth.js +13 -22
  56. package/server/services/ai/providers/nvidia.js +10 -5
  57. package/server/services/ai/providers/ollama.js +102 -82
  58. package/server/services/ai/providers/ollama_stream.js +142 -0
  59. package/server/services/ai/providers/openai.js +39 -5
  60. package/server/services/ai/providers/openaiCodex.js +11 -4
  61. package/server/services/ai/providers/openrouter.js +29 -7
  62. package/server/services/ai/providers/provider_error.js +36 -0
  63. package/server/services/ai/settings.js +26 -2
  64. package/server/services/ai/taskAnalysis.js +5 -1
  65. package/server/services/ai/terminal_reply.js +45 -0
  66. package/server/services/ai/toolEvidence.js +58 -29
  67. package/server/services/ai/tools.js +124 -111
  68. package/server/services/android/controller.js +770 -237
  69. package/server/services/android/process.js +140 -0
  70. package/server/services/android/sdk_download.js +143 -0
  71. package/server/services/android/uia.js +6 -5
  72. package/server/services/artifacts/store.js +24 -0
  73. package/server/services/browser/controller.js +736 -385
  74. package/server/services/browser/extension/gateway.js +40 -16
  75. package/server/services/browser/extension/protocol.js +12 -1
  76. package/server/services/browser/extension/provider.js +59 -47
  77. package/server/services/browser/extension/registry.js +155 -34
  78. package/server/services/cli/executor.js +62 -9
  79. package/server/services/desktop/gateway.js +41 -4
  80. package/server/services/desktop/protocol.js +3 -0
  81. package/server/services/desktop/provider.js +39 -42
  82. package/server/services/desktop/registry.js +137 -52
  83. package/server/services/integrations/figma/provider.js +78 -12
  84. package/server/services/integrations/github/common.js +11 -6
  85. package/server/services/integrations/github/provider.js +52 -53
  86. package/server/services/integrations/google/provider.js +55 -19
  87. package/server/services/integrations/home_assistant/network.js +17 -20
  88. package/server/services/integrations/home_assistant/provider.js +7 -5
  89. package/server/services/integrations/home_assistant/tools.js +17 -5
  90. package/server/services/integrations/http.js +51 -0
  91. package/server/services/integrations/manager.js +158 -53
  92. package/server/services/integrations/microsoft/provider.js +80 -13
  93. package/server/services/integrations/neoarchive/provider.js +55 -29
  94. package/server/services/integrations/neorecall/client.js +17 -10
  95. package/server/services/integrations/neorecall/provider.js +20 -11
  96. package/server/services/integrations/notion/provider.js +16 -13
  97. package/server/services/integrations/oauth_provider.js +115 -51
  98. package/server/services/integrations/slack/provider.js +98 -9
  99. package/server/services/integrations/spotify/provider.js +67 -71
  100. package/server/services/integrations/trello/provider.js +21 -7
  101. package/server/services/integrations/weather/provider.js +18 -12
  102. package/server/services/integrations/whatsapp/provider.js +76 -16
  103. package/server/services/manager.js +87 -1
  104. package/server/services/memory/embedding_index.js +20 -8
  105. package/server/services/memory/embeddings.js +151 -90
  106. package/server/services/memory/ingestion.js +50 -9
  107. package/server/services/memory/ingestion_documents.js +13 -3
  108. package/server/services/memory/manager.js +52 -19
  109. package/server/services/messaging/automation.js +84 -9
  110. package/server/services/messaging/http_platforms.js +33 -13
  111. package/server/services/messaging/inbound_queue.js +78 -24
  112. package/server/services/messaging/inbound_store.js +224 -0
  113. package/server/services/messaging/manager.js +326 -51
  114. package/server/services/messaging/typing_keepalive.js +5 -2
  115. package/server/services/network/http.js +210 -0
  116. package/server/services/network/safe_request.js +307 -0
  117. package/server/services/runtime/backends/local-vm.js +214 -66
  118. package/server/services/runtime/manager.js +17 -12
  119. package/server/services/social_reach/channels/github.js +10 -4
  120. package/server/services/social_reach/channels/reddit.js +4 -4
  121. package/server/services/social_reach/channels/rss.js +2 -2
  122. package/server/services/social_reach/channels/social_video.js +12 -7
  123. package/server/services/social_reach/channels/v2ex.js +21 -8
  124. package/server/services/social_reach/channels/x.js +2 -2
  125. package/server/services/social_reach/channels/xueqiu.js +5 -5
  126. package/server/services/social_reach/service.js +9 -6
  127. package/server/services/social_reach/utils.js +65 -14
  128. package/server/services/social_video/service.js +160 -50
  129. package/server/services/tasks/integration_runtime.js +18 -8
  130. package/server/services/tasks/runtime.js +39 -4
  131. package/server/services/voice/agentBridge.js +17 -4
  132. package/server/services/voice/bufferedLiveRelayAdapter.js +5 -0
  133. package/server/services/voice/liveSession.js +31 -0
  134. package/server/services/voice/openaiSpeech.js +33 -8
  135. package/server/services/voice/providers.js +233 -151
  136. package/server/services/voice/runtimeManager.js +118 -20
  137. package/server/services/voice/turnRunner.js +6 -0
  138. package/server/services/wearable/firmware_manifest.js +51 -13
  139. package/server/services/wearable/service.js +1 -0
  140. package/server/utils/abort.js +96 -0
  141. package/server/utils/cloud-security.js +110 -3
  142. package/server/utils/files.js +31 -0
  143. package/server/utils/image_payload.js +95 -0
  144. package/server/utils/retry.js +107 -0
@@ -6,7 +6,12 @@ const {
6
6
  const {
7
7
  normalizeOutgoingMessage,
8
8
  } = require('../messagingFallback');
9
- const { withProviderRetry, isTransientError } = require('../providerRetry');
9
+ const { withProviderRetry } = require('../providerRetry');
10
+ const {
11
+ abortableDelay,
12
+ getErrorCode,
13
+ getHttpStatus,
14
+ } = require('../../../utils/retry');
10
15
  const { shortenRunId, summarizeForLog } = require('../logFormat');
11
16
  const {
12
17
  TICK_MS,
@@ -16,6 +21,15 @@ const {
16
21
  evaluateProgressLiveness,
17
22
  } = require('./progress_monitor');
18
23
 
24
+ const SAFE_PRE_DELIVERY_NETWORK_CODES = new Set([
25
+ 'EAI_AGAIN',
26
+ 'ENOTFOUND',
27
+ 'ECONNREFUSED',
28
+ 'ENETUNREACH',
29
+ 'EHOSTUNREACH',
30
+ 'UND_ERR_CONNECT_TIMEOUT',
31
+ ]);
32
+
19
33
  function isoNow() {
20
34
  return new Date().toISOString();
21
35
  }
@@ -39,9 +53,26 @@ function requireSuccessfulMessagingDelivery(result, label = 'Messaging delivery'
39
53
  const error = new Error(`${label} failed: ${reason}`);
40
54
  error.code = 'MESSAGING_DELIVERY_FAILED';
41
55
  error.deliveryResult = result || null;
56
+ error.safeToRetry = result?.safeToRetry === true;
57
+ error.deliveryAmbiguous = result?.deliveryAmbiguous === true;
42
58
  throw error;
43
59
  }
44
60
 
61
+ function isSafeMessagingDeliveryRetry(error) {
62
+ if (!error || error.retryable === false || error.deliveryAmbiguous === true) return false;
63
+ if (error.safeToRetry === true) return true;
64
+ const result = error.deliveryResult;
65
+ if (result?.deliveryAmbiguous === true || result?.retryable === false) return false;
66
+ if (result?.success === false) return true;
67
+
68
+ // A rate-limit response explicitly rejected the request. DNS/connect failures
69
+ // happen before a platform can accept it. Timeouts, resets, broken pipes, and
70
+ // generic 5xx responses are intentionally excluded because the message may
71
+ // already have been accepted and retrying would duplicate it.
72
+ if (getHttpStatus(error) === 429) return true;
73
+ return SAFE_PRE_DELIVERY_NETWORK_CODES.has(String(getErrorCode(error) || ''));
74
+ }
75
+
45
76
  // Harness-driven progress: the originating chat must see autonomous updates during
46
77
  // long runs even when a weak model never calls send_interim_update itself. The
47
78
  // supervisor paces this call (FIRST/REPEAT thresholds + a repeat guard). The update
@@ -66,13 +97,30 @@ async function sendRuntimeMessagingHeartbeat(engine, runId, options = {}) {
66
97
  && typeof runMeta.composeProgressUpdate === 'function') {
67
98
  let statusMsg = '';
68
99
  try {
69
- const composed = await runMeta.composeProgressUpdate({ stalled: options.stalled === true });
100
+ const composed = await runMeta.composeProgressUpdate({
101
+ stalled: options.stalled === true,
102
+ signal: runMeta.messagingProgressSupervisor?.abortController?.signal || null,
103
+ });
70
104
  if (normalizeOutgoingMessage(composed, platform)) statusMsg = String(composed).trim();
71
105
  } catch { /* no dynamic update available this tick */ }
106
+ const currentRunMeta = engine.getRunMeta(runId);
107
+ if (
108
+ currentRunMeta !== runMeta
109
+ || runMeta.aborted
110
+ || runMeta.status !== 'running'
111
+ || runMeta.terminalInterim
112
+ || runMeta.finalDeliverySent
113
+ || runMeta.deliveryState?.finalContentDelivered === true
114
+ ) {
115
+ return { sent: false, skipped: true, terminal: true };
116
+ }
72
117
  if (statusMsg) try {
73
118
  const delivery = await engine.messagingManager.sendMessage(runMeta.userId, platform, chatId, statusMsg, {
74
119
  runId,
75
120
  agentId: runMeta.agentId,
121
+ signal: runMeta.messagingProgressSupervisor?.abortController?.signal
122
+ || runMeta.abortController?.signal
123
+ || null,
76
124
  });
77
125
  if (delivery?.success === true && delivery?.suppressed !== true) {
78
126
  const nowIso = isoNow();
@@ -157,11 +205,12 @@ async function deliverMessagingFinalFallback(engine, {
157
205
  console.info(
158
206
  `[Run ${shortenRunId(runId)}] messaging_fallback chunks=${chunks.length} to=${summarizeForLog(chatId, 80)}`
159
207
  );
208
+ const deliveredChunks = [];
160
209
  for (let i = 0; i < chunks.length; i++) {
161
210
  if (i > 0) {
162
211
  const delay = Math.max(1000, Math.min(chunks[i].length * 30, 4000));
163
212
  await engine.messagingManager.sendTyping(userId, platform, chatId, true, { agentId }).catch(() => {});
164
- await new Promise((resolve) => setTimeout(resolve, delay));
213
+ await abortableDelay(delay, runMeta.abortController?.signal || null);
165
214
  }
166
215
  try {
167
216
  await withProviderRetry(async () => {
@@ -170,32 +219,34 @@ async function deliverMessagingFinalFallback(engine, {
170
219
  platform,
171
220
  chatId,
172
221
  chunks[i],
173
- { runId, agentId },
222
+ {
223
+ runId,
224
+ agentId,
225
+ idempotencyKey: `${runId}:final:${i}`,
226
+ signal: runMeta.abortController?.signal || null,
227
+ },
174
228
  );
175
229
  return requireSuccessfulMessagingDelivery(deliveryResult, 'Final messaging delivery');
176
230
  }, {
177
231
  ...engine.messagingDeliveryRetry,
178
232
  label: `MessagingDelivery ${platform}`,
179
- isRetryable: (error) => (
180
- error?.retryable !== false
181
- && (
182
- error?.code === 'MESSAGING_DELIVERY_FAILED'
183
- || isTransientError(error)
184
- )
185
- ),
233
+ signal: runMeta.abortController?.signal || null,
234
+ isRetryable: isSafeMessagingDeliveryRetry,
186
235
  });
187
236
  } catch (error) {
188
237
  error.disableAutonomousRetry = true;
238
+ error.deliveredChunks = deliveredChunks.slice();
189
239
  throw error;
190
240
  }
241
+ deliveredChunks.push(chunks[i]);
242
+ runMeta.lastSentMessage = chunks[i];
243
+ if (!Array.isArray(runMeta.sentMessages)) runMeta.sentMessages = [];
244
+ runMeta.sentMessages.push(chunks[i]);
191
245
  }
192
246
 
193
- runMeta.lastSentMessage = chunks[chunks.length - 1] || cleanedContent;
194
- runMeta.sentMessages = Array.isArray(runMeta.sentMessages)
195
- ? [...runMeta.sentMessages, ...chunks]
196
- : chunks.slice();
247
+ runMeta.lastSentMessage = deliveredChunks[deliveredChunks.length - 1] || cleanedContent;
197
248
  engine.markRunFinalDelivery(runId, runMeta.lastSentMessage);
198
- return { sent: true, chunks };
249
+ return { sent: true, chunks: deliveredChunks };
199
250
  }
200
251
 
201
252
  async function tickMessagingProgressSupervisor(engine, runId) {
@@ -206,54 +257,71 @@ async function tickMessagingProgressSupervisor(engine, runId) {
206
257
  if (runMeta.terminalInterim) {
207
258
  return { sent: false, skipped: true };
208
259
  }
260
+ if (runMeta.progressSupervisorTickInFlight === true) {
261
+ return { sent: false, skipped: true, inFlight: true };
262
+ }
263
+ runMeta.progressSupervisorTickInFlight = true;
264
+ let settleTick;
265
+ const tickPromise = new Promise((resolve) => { settleTick = resolve; });
266
+ runMeta.progressSupervisorTickPromise = tickPromise;
209
267
 
210
- const ledger = runMeta.progressLedger || buildInitialProgressLedger({
211
- startedAt: runMeta.startedAtIso || isoNow(),
212
- });
213
- runMeta.progressLedger = ledger;
214
- const liveness = evaluateProgressLiveness(runMeta);
215
- const stalled = liveness.stalled;
216
-
217
- if (stalled && !ledger.stallNotifiedAt) {
218
- engine.updateRunProgress(runId, {
219
- stallNotifiedAt: isoNow(),
220
- progressState: 'stalled',
268
+ try {
269
+ const ledger = runMeta.progressLedger || buildInitialProgressLedger({
270
+ startedAt: runMeta.startedAtIso || isoNow(),
221
271
  });
222
- engine.recordRunEvent(runMeta.userId, runId, 'progress_stalled', {
223
- currentTool: ledger.currentTool || null,
224
- currentStep: ledger.currentStep || null,
225
- phase: ledger.currentPhase || 'idle',
226
- }, { agentId: runMeta.agentId });
227
- }
272
+ runMeta.progressLedger = ledger;
273
+ const liveness = evaluateProgressLiveness(runMeta);
274
+ const stalled = liveness.stalled;
228
275
 
229
- if (!liveness.shouldNudge) {
230
- return { sent: false, skipped: true };
231
- }
276
+ if (stalled && !ledger.stallNotifiedAt) {
277
+ engine.updateRunProgress(runId, {
278
+ stallNotifiedAt: isoNow(),
279
+ progressState: 'stalled',
280
+ });
281
+ engine.recordRunEvent(runMeta.userId, runId, 'progress_stalled', {
282
+ currentTool: ledger.currentTool || null,
283
+ currentStep: ledger.currentStep || null,
284
+ phase: ledger.currentPhase || 'idle',
285
+ }, { agentId: runMeta.agentId });
286
+ }
232
287
 
233
- const lastSupervisorNudgeAtMs = timestampMs(runMeta.lastSupervisorNudgeAt, 0);
234
- if (lastSupervisorNudgeAtMs > 0 && (Date.now() - lastSupervisorNudgeAtMs) < REPEAT_UPDATE_MS) {
235
- return { sent: false, skipped: true };
236
- }
288
+ if (!liveness.shouldNudge) {
289
+ return { sent: false, skipped: true };
290
+ }
237
291
 
238
- if (ledger.currentPhase === 'tool' || ledger.currentPhase === 'model') {
239
- return sendRuntimeMessagingHeartbeat(engine, runId, { stalled });
240
- }
292
+ const lastSupervisorNudgeAtMs = timestampMs(runMeta.lastSupervisorNudgeAt, 0);
293
+ if (lastSupervisorNudgeAtMs > 0 && (Date.now() - lastSupervisorNudgeAtMs) < REPEAT_UPDATE_MS) {
294
+ return { sent: false, skipped: true };
295
+ }
241
296
 
242
- if (ledger.currentPhase !== 'idle') {
243
- return { sent: false, skipped: true };
244
- }
297
+ if (ledger.currentPhase === 'tool' || ledger.currentPhase === 'model') {
298
+ return await sendRuntimeMessagingHeartbeat(engine, runId, { stalled });
299
+ }
245
300
 
246
- const queued = engine.enqueueSystemSteering(runId, buildProgressNudge({ stalled }), {
247
- reason: stalled ? 'stalled_progress_check' : 'progress_check',
248
- });
249
- if (!queued) {
250
- return { sent: false, skipped: true };
301
+ if (ledger.currentPhase !== 'idle') {
302
+ return { sent: false, skipped: true };
303
+ }
304
+
305
+ const queued = engine.enqueueSystemSteering(runId, buildProgressNudge({ stalled }), {
306
+ reason: stalled ? 'stalled_progress_check' : 'progress_check',
307
+ });
308
+ if (!queued) {
309
+ return { sent: false, skipped: true };
310
+ }
311
+ runMeta.lastSupervisorNudgeAt = isoNow();
312
+ engine.updateRunProgress(runId, {
313
+ lastUserVisibleUpdateAt: ledger.lastUserVisibleUpdateAt || null,
314
+ });
315
+ return { sent: false, queued: true };
316
+ } finally {
317
+ settleTick();
318
+ if (engine.getRunMeta(runId) === runMeta) {
319
+ runMeta.progressSupervisorTickInFlight = false;
320
+ if (runMeta.progressSupervisorTickPromise === tickPromise) {
321
+ runMeta.progressSupervisorTickPromise = null;
322
+ }
323
+ }
251
324
  }
252
- runMeta.lastSupervisorNudgeAt = isoNow();
253
- engine.updateRunProgress(runId, {
254
- lastUserVisibleUpdateAt: ledger.lastUserVisibleUpdateAt || null,
255
- });
256
- return { sent: false, queued: true };
257
325
  }
258
326
 
259
327
  function startMessagingProgressSupervisor(engine, runId) {
@@ -264,25 +332,29 @@ function startMessagingProgressSupervisor(engine, runId) {
264
332
  if (runMeta.messagingProgressSupervisor?.timer) {
265
333
  return true;
266
334
  }
335
+ const abortController = new AbortController();
267
336
  const timer = setInterval(() => {
268
337
  tickMessagingProgressSupervisor(engine, runId).catch((error) => {
269
338
  console.warn('[Engine] Messaging progress supervisor failed:', error?.message || error);
270
339
  });
271
340
  }, TICK_MS);
272
341
  timer.unref?.();
273
- runMeta.messagingProgressSupervisor = { timer };
342
+ runMeta.messagingProgressSupervisor = { timer, abortController };
274
343
  return true;
275
344
  }
276
345
 
277
346
  function stopMessagingProgressSupervisor(engine, runId) {
278
347
  const runMeta = engine.getRunMeta(runId);
348
+ const inFlight = runMeta?.progressSupervisorTickPromise || null;
279
349
  const timer = runMeta?.messagingProgressSupervisor?.timer || null;
280
350
  if (timer) {
281
351
  clearInterval(timer);
282
352
  }
353
+ runMeta?.messagingProgressSupervisor?.abortController?.abort('Progress supervisor stopped.');
283
354
  if (runMeta?.messagingProgressSupervisor) {
284
355
  runMeta.messagingProgressSupervisor = null;
285
356
  }
357
+ return inFlight || Promise.resolve();
286
358
  }
287
359
 
288
360
  module.exports = {
@@ -0,0 +1,91 @@
1
+ 'use strict';
2
+
3
+ const MODEL_CALL_TIMEOUT_MS = 5 * 60 * 1000;
4
+
5
+ function formatElapsedDuration(durationMs) {
6
+ const totalSeconds = Math.max(1, Math.floor(Number(durationMs || 0) / 1000));
7
+ if (totalSeconds < 60) return `${totalSeconds}s`;
8
+ const minutes = Math.floor(totalSeconds / 60);
9
+ const seconds = totalSeconds % 60;
10
+ if (seconds === 0) return `${minutes}m`;
11
+ return `${minutes}m ${seconds}s`;
12
+ }
13
+
14
+ function resolveModelCallTimeoutMs(options = {}) {
15
+ const requested = Number(options?.modelCallTimeoutMs);
16
+ if (Number.isFinite(requested) && requested > 0) {
17
+ return Math.max(10, requested);
18
+ }
19
+ return MODEL_CALL_TIMEOUT_MS;
20
+ }
21
+
22
+ function abortError(reason = null) {
23
+ if (reason instanceof Error) return reason;
24
+ const error = new Error(String(reason || 'Model call aborted.'));
25
+ error.name = 'AbortError';
26
+ error.code = 'ABORT_ERR';
27
+ return error;
28
+ }
29
+
30
+ async function withModelCallTimeout(promise, options = {}, label = 'Model call') {
31
+ const timeoutMs = resolveModelCallTimeoutMs(options);
32
+ const signal = options?.modelAbortController?.signal || options?.signal || null;
33
+ let timer = null;
34
+ let abortListener = null;
35
+ const timeout = new Promise((_, reject) => {
36
+ timer = setTimeout(() => {
37
+ const error = new Error(`${label} timed out after ${formatElapsedDuration(timeoutMs)}.`);
38
+ error.code = 'MODEL_CALL_TIMEOUT';
39
+ options?.modelAbortController?.abort(error);
40
+ reject(error);
41
+ }, timeoutMs);
42
+ });
43
+ const aborted = new Promise((_, reject) => {
44
+ if (!signal) return;
45
+ abortListener = () => reject(abortError(signal.reason));
46
+ if (signal.aborted) abortListener();
47
+ else signal.addEventListener('abort', abortListener, { once: true });
48
+ });
49
+
50
+ try {
51
+ return await Promise.race([Promise.resolve(promise), timeout, aborted]);
52
+ } finally {
53
+ if (timer) clearTimeout(timer);
54
+ if (abortListener) signal?.removeEventListener('abort', abortListener);
55
+ }
56
+ }
57
+
58
+ async function runAbortableModelCall(factory, options = {}, label = 'Model call') {
59
+ const modelAbortController = new AbortController();
60
+ const parentSignals = [...new Set([
61
+ options?.signal,
62
+ ...(Array.isArray(options?.signals) ? options.signals : []),
63
+ ].filter((signal) => signal && typeof signal.addEventListener === 'function'))];
64
+ const parentListeners = new Map();
65
+ for (const parentSignal of parentSignals) {
66
+ const abortFromParent = () => modelAbortController.abort(parentSignal.reason);
67
+ parentListeners.set(parentSignal, abortFromParent);
68
+ if (parentSignal.aborted) abortFromParent();
69
+ else parentSignal.addEventListener('abort', abortFromParent, { once: true });
70
+ }
71
+
72
+ try {
73
+ const promise = Promise.resolve().then(() => factory(modelAbortController.signal));
74
+ return await withModelCallTimeout(
75
+ promise,
76
+ { ...options, modelAbortController },
77
+ label,
78
+ );
79
+ } finally {
80
+ for (const [parentSignal, abortFromParent] of parentListeners) {
81
+ parentSignal.removeEventListener('abort', abortFromParent);
82
+ }
83
+ }
84
+ }
85
+
86
+ module.exports = {
87
+ abortError,
88
+ resolveModelCallTimeoutMs,
89
+ runAbortableModelCall,
90
+ withModelCallTimeout,
91
+ };
@@ -5,48 +5,16 @@ const { sanitizeModelOutput } = require('../outputSanitizer');
5
5
  const { parseJsonObject } = require('../taskAnalysis');
6
6
  const { withProviderRetry, isTransientError } = require('../providerRetry');
7
7
  const { normalizeUsage, recordModelUsage } = require('../usage');
8
-
9
- const MODEL_CALL_TIMEOUT_MS = 5 * 60 * 1000;
8
+ const {
9
+ resolveModelCallTimeoutMs,
10
+ runAbortableModelCall,
11
+ withModelCallTimeout,
12
+ } = require('./model_call_guard');
10
13
 
11
14
  function isoNow() {
12
15
  return new Date().toISOString();
13
16
  }
14
17
 
15
- function formatElapsedDuration(durationMs) {
16
- const totalSeconds = Math.max(1, Math.floor(Number(durationMs || 0) / 1000));
17
- if (totalSeconds < 60) return `${totalSeconds}s`;
18
- const minutes = Math.floor(totalSeconds / 60);
19
- const seconds = totalSeconds % 60;
20
- if (seconds === 0) return `${minutes}m`;
21
- return `${minutes}m ${seconds}s`;
22
- }
23
-
24
- function resolveModelCallTimeoutMs(options = {}) {
25
- const requested = Number(options?.modelCallTimeoutMs);
26
- if (Number.isFinite(requested) && requested > 0) {
27
- return Math.max(10, requested);
28
- }
29
- return MODEL_CALL_TIMEOUT_MS;
30
- }
31
-
32
- async function withModelCallTimeout(promise, options = {}, label = 'Model call') {
33
- const timeoutMs = resolveModelCallTimeoutMs(options);
34
- let timer = null;
35
- const timeout = new Promise((_, reject) => {
36
- timer = setTimeout(() => {
37
- options?.modelAbortController?.abort(`${label} timed out.`);
38
- const error = new Error(`${label} timed out after ${formatElapsedDuration(timeoutMs)}.`);
39
- error.code = 'MODEL_CALL_TIMEOUT';
40
- reject(error);
41
- }, timeoutMs);
42
- });
43
- try {
44
- return await Promise.race([Promise.resolve(promise), timeout]);
45
- } finally {
46
- if (timer) clearTimeout(timer);
47
- }
48
- }
49
-
50
18
  async function requestStructuredJson(engine, {
51
19
  provider,
52
20
  providerName,
@@ -76,7 +44,6 @@ async function requestStructuredJson(engine, {
76
44
  });
77
45
  }
78
46
 
79
- let completed = false;
80
47
  try {
81
48
  const response = await withProviderRetry(
82
49
  () => withModelCallTimeout(
@@ -99,9 +66,9 @@ async function requestStructuredJson(engine, {
99
66
  {
100
67
  label: `Engine ${model} (structured)`,
101
68
  isRetryable: (err) => !modelAbortController.signal.aborted && isTransientError(err),
69
+ signal: modelAbortController.signal,
102
70
  }
103
71
  );
104
- completed = true;
105
72
  if (telemetry?.runId && telemetry?.userId) {
106
73
  recordModelUsage({
107
74
  runId: telemetry.runId,
@@ -132,8 +99,6 @@ async function requestStructuredJson(engine, {
132
99
  currentStep: null,
133
100
  currentTool: null,
134
101
  currentStepStartedAt: null,
135
- }, {
136
- verified: completed,
137
102
  });
138
103
  }
139
104
  }
@@ -235,6 +200,7 @@ async function requestModelResponse(engine, {
235
200
  phase: 'recovering',
236
201
  });
237
202
  },
203
+ signal: modelAbortController.signal,
238
204
  }));
239
205
  } finally {
240
206
  parentSignal?.removeEventListener('abort', abortFromParent);
@@ -279,5 +245,6 @@ module.exports = {
279
245
  requestModelResponse,
280
246
  requestStructuredJson,
281
247
  resolveModelCallTimeoutMs,
248
+ runAbortableModelCall,
282
249
  withModelCallTimeout,
283
250
  };
@@ -4,7 +4,10 @@ const { v4: uuidv4 } = require('uuid');
4
4
  const db = require('../../../db/database');
5
5
  const { globalHooks } = require('../hooks');
6
6
  const { summarizeForLog } = require('../logFormat');
7
- const { inferToolFailureMessage } = require('../toolEvidence');
7
+ const {
8
+ inferToolFailureMessage,
9
+ resolveDeclaredToolAccess,
10
+ } = require('../toolEvidence');
8
11
 
9
12
  function getAvailableTools(_engine, app, options = {}) {
10
13
  const { getAvailableTools: loadAvailableTools } = require('../tools');
@@ -16,8 +19,21 @@ async function executeTool(engine, toolName, args, context) {
16
19
  return runTool(toolName, args, context, engine);
17
20
  }
18
21
 
19
- function isReadOnlyToolCall(toolCall) {
22
+ function isReadOnlyToolCall(toolCall, toolDefinition = null) {
20
23
  const name = String(toolCall?.function?.name || '');
24
+ let toolArgs = {};
25
+ try {
26
+ toolArgs = typeof toolCall?.function?.arguments === 'string'
27
+ ? JSON.parse(toolCall.function.arguments || '{}')
28
+ : (toolCall?.function?.arguments || {});
29
+ } catch {
30
+ return false;
31
+ }
32
+
33
+ const declaredAccess = resolveDeclaredToolAccess(toolDefinition, toolArgs);
34
+ if (declaredAccess === 'read') return true;
35
+ if (declaredAccess === 'write') return false;
36
+
21
37
  const readOnly = new Set([
22
38
  'read_file',
23
39
  'read_files',
@@ -35,12 +51,7 @@ function isReadOnlyToolCall(toolCall) {
35
51
  'read_health_data',
36
52
  ]);
37
53
  if (name === 'http_request') {
38
- try {
39
- const args = JSON.parse(toolCall.function.arguments || '{}');
40
- return String(args.method || 'GET').toUpperCase() === 'GET';
41
- } catch {
42
- return false;
43
- }
54
+ return String(toolArgs.method || 'GET').toUpperCase() === 'GET';
44
55
  }
45
56
  return readOnly.has(name);
46
57
  }
@@ -1,9 +1,9 @@
1
- // Limits resolve in priority order: per-run options → agent AI settings → hardcoded defaults.
1
+ // Limits resolve in priority order: per-run options → agent AI settings → conservative defaults.
2
2
  // They are safety nets only; task_complete / progress guards are the real exit signals.
3
3
 
4
4
  // The iteration ceiling is a pure runaway safety net, NOT the primary stop signal.
5
5
  // A run stops when it makes no real progress (consecutiveReadOnlyIterations cap,
6
- // which resets the moment the agent does anything state-changing), or when the
6
+ // which resets on a state change or genuinely new evidence), or when the
7
7
  // repetition / tool-failure / model-recovery guards fire, or when the model signals
8
8
  // task_complete. These ceilings are set high so they only ever catch a genuine
9
9
  // runaway and never guillotine a long, legitimately-progressing complex task.
@@ -13,8 +13,8 @@ const DEFAULT_PLAN_EXECUTE_MAX_ITERATIONS = 250;
13
13
  // Less aggressive than 0.60 so the model retains file contents it already read for
14
14
  // longer, instead of losing them to compaction and re-reading the same files.
15
15
  const DEFAULT_COMPACTION_THRESHOLD = 0.80;
16
- // The real "stop when stuck" guard. Counts consecutive turns of pure reading/
17
- // searching/inspecting with zero state change; resets to 0 on any concrete progress.
16
+ // The real "stop when stuck" guard. Counts consecutive turns with no state
17
+ // change and no new evidence; resets to 0 on any concrete progress.
18
18
  const DEFAULT_MAX_CONSECUTIVE_READ_ONLY_ITERATIONS = 8;
19
19
  const DEFAULT_MAX_CONSECUTIVE_TOOL_FAILURES = 5;
20
20
  const DEFAULT_MAX_MODEL_FAILURE_RECOVERIES = 3;
@@ -25,8 +25,9 @@ const MAX_ALLOWED_TOOL_FAILURES = 50;
25
25
  const MAX_ALLOWED_MODEL_RECOVERIES = 10;
26
26
  const MAX_ALLOWED_BUDGET_CHARS = 500_000;
27
27
 
28
- function finitePositive(n, fallback) {
29
- return Number.isFinite(n) && n > 0 ? n : fallback;
28
+ function optionalNumber(value) {
29
+ if (value == null || value === '') return Number.NaN;
30
+ return Number(value);
30
31
  }
31
32
 
32
33
  function clampFinite(n, lo, hi, fallback) {
@@ -70,23 +71,27 @@ function buildLoopPolicy(aiSettings = {}, triggerType = 'chat', analysisMode = '
70
71
 
71
72
  // ── Tool result size budget ───────────────────────────────────────────────
72
73
  // Must be a finite positive integer; bad values fall back to 2400.
73
- const defaultBudget = clampFinite(
74
- Math.floor(Number(aiSettings.tool_replay_budget_chars) || 0),
75
- 500,
76
- MAX_ALLOWED_BUDGET_CHARS,
77
- 2400,
78
- );
74
+ const requestedDefaultBudget = optionalNumber(aiSettings.tool_replay_budget_chars);
75
+ const defaultBudget = Number.isFinite(requestedDefaultBudget) && requestedDefaultBudget > 0
76
+ ? clampFinite(Math.floor(requestedDefaultBudget), 500, MAX_ALLOWED_BUDGET_CHARS, 2400)
77
+ : 2400;
79
78
 
80
79
  // ── Scalar policy fields ─────────────────────────────────────────────────
81
80
  const maxConsecutiveToolFailures = clampFinite(
82
- Math.floor(Number(aiSettings.max_consecutive_tool_failures)),
81
+ Math.floor(optionalNumber(
82
+ options.maxConsecutiveToolFailures
83
+ ?? aiSettings.max_consecutive_tool_failures,
84
+ )),
83
85
  1,
84
86
  MAX_ALLOWED_TOOL_FAILURES,
85
87
  DEFAULT_MAX_CONSECUTIVE_TOOL_FAILURES,
86
88
  );
87
89
 
88
90
  const maxModelFailureRecoveries = clampFinite(
89
- Math.floor(Number(aiSettings.max_model_failure_recoveries)),
91
+ Math.floor(optionalNumber(
92
+ options.maxModelFailureRecoveries
93
+ ?? aiSettings.max_model_failure_recoveries,
94
+ )),
90
95
  0,
91
96
  MAX_ALLOWED_MODEL_RECOVERIES,
92
97
  DEFAULT_MAX_MODEL_FAILURE_RECOVERIES,
@@ -94,7 +99,7 @@ function buildLoopPolicy(aiSettings = {}, triggerType = 'chat', analysisMode = '
94
99
 
95
100
  const rawReadOnlyIterations = options.maxConsecutiveReadOnlyIterations != null
96
101
  ? Number(options.maxConsecutiveReadOnlyIterations)
97
- : Number(aiSettings.max_consecutive_read_only_iterations);
102
+ : optionalNumber(aiSettings.max_consecutive_read_only_iterations);
98
103
  const maxConsecutiveReadOnlyIterations = clampFinite(
99
104
  Math.floor(rawReadOnlyIterations),
100
105
  3,
@@ -104,7 +109,7 @@ function buildLoopPolicy(aiSettings = {}, triggerType = 'chat', analysisMode = '
104
109
 
105
110
  // compactionThreshold must be in (0, 1]; clamp to [0.1, 1].
106
111
  const compactionThreshold = clampFinite(
107
- Number(aiSettings.compaction_threshold),
112
+ optionalNumber(options.compactionThreshold ?? aiSettings.compaction_threshold),
108
113
  0.1,
109
114
  1,
110
115
  DEFAULT_COMPACTION_THRESHOLD,
@@ -122,9 +127,9 @@ function buildLoopPolicy(aiSettings = {}, triggerType = 'chat', analysisMode = '
122
127
  // Per-category tool result size budgets (chars)
123
128
  toolResultBudget: {
124
129
  default: defaultBudget,
125
- file: clampFinite(Math.floor(Number(aiSettings.tool_replay_budget_file_chars)), 500, MAX_ALLOWED_BUDGET_CHARS, Math.max(defaultBudget, 6000)),
126
- browser: clampFinite(Math.floor(Number(aiSettings.tool_replay_budget_browser_chars)), 500, MAX_ALLOWED_BUDGET_CHARS, Math.max(defaultBudget, 4000)),
127
- command: clampFinite(Math.floor(Number(aiSettings.tool_replay_budget_command_chars)), 500, MAX_ALLOWED_BUDGET_CHARS, Math.max(defaultBudget, 4000)),
130
+ file: clampFinite(Math.floor(optionalNumber(aiSettings.tool_replay_budget_file_chars)), 500, MAX_ALLOWED_BUDGET_CHARS, Math.max(defaultBudget, 6000)),
131
+ browser: clampFinite(Math.floor(optionalNumber(aiSettings.tool_replay_budget_browser_chars)), 500, MAX_ALLOWED_BUDGET_CHARS, Math.max(defaultBudget, 4000)),
132
+ command: clampFinite(Math.floor(optionalNumber(aiSettings.tool_replay_budget_command_chars)), 500, MAX_ALLOWED_BUDGET_CHARS, Math.max(defaultBudget, 4000)),
128
133
  },
129
134
 
130
135
  // Hard ceiling is always 2× soft, capped at a reasonable absolute max