@probelabs/probe 0.6.0-rc220 → 0.6.0-rc222

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.
@@ -3748,7 +3748,7 @@ var init_grep = __esm({
3748
3748
  import { randomUUID } from "crypto";
3749
3749
  async function delegate({
3750
3750
  task,
3751
- timeout = 300,
3751
+ timeout = DEFAULT_DELEGATE_TIMEOUT,
3752
3752
  debug = false,
3753
3753
  currentIteration = 0,
3754
3754
  maxIterations = 30,
@@ -3926,7 +3926,7 @@ async function delegate({
3926
3926
  throw new Error(`Delegation failed: ${error.message}`);
3927
3927
  }
3928
3928
  }
3929
- var DelegationManager, defaultDelegationManager;
3929
+ var DelegationManager, defaultDelegationManager, DEFAULT_DELEGATE_TIMEOUT;
3930
3930
  var init_delegate = __esm({
3931
3931
  "src/delegate.js"() {
3932
3932
  "use strict";
@@ -4063,9 +4063,12 @@ var init_delegate = __esm({
4063
4063
  }
4064
4064
  /**
4065
4065
  * Process the wait queue - grant slot to next waiting delegation
4066
+ * Uses setImmediate to avoid blocking the event loop when resolving promises.
4066
4067
  * @private
4067
4068
  */
4068
4069
  _processQueue(debug = false) {
4070
+ const toResolve = [];
4071
+ const toReject = [];
4069
4072
  while (this.waitQueue.length > 0 && this.globalActive < this.maxConcurrent) {
4070
4073
  const next = this.waitQueue.shift();
4071
4074
  if (!next) break;
@@ -4077,7 +4080,7 @@ var init_delegate = __esm({
4077
4080
  if (debug) {
4078
4081
  console.error(`[DelegationManager] Session limit (${this.maxPerSession}) reached for queued item, rejecting`);
4079
4082
  }
4080
- reject2(new Error(`Maximum delegations per session (${this.maxPerSession}) reached for session ${parentSessionId}`));
4083
+ toReject.push({ reject: reject2, error: new Error(`Maximum delegations per session (${this.maxPerSession}) reached for session ${parentSessionId}`) });
4081
4084
  continue;
4082
4085
  }
4083
4086
  }
@@ -4086,7 +4089,17 @@ var init_delegate = __esm({
4086
4089
  const waitTime = Date.now() - queuedAt;
4087
4090
  console.error(`[DelegationManager] Granted slot from queue (waited ${waitTime}ms). Active: ${this.globalActive}/${this.maxConcurrent}`);
4088
4091
  }
4089
- resolve8(true);
4092
+ toResolve.push(resolve8);
4093
+ }
4094
+ if (toResolve.length > 0 || toReject.length > 0) {
4095
+ setImmediate(() => {
4096
+ for (const resolve8 of toResolve) {
4097
+ resolve8(true);
4098
+ }
4099
+ for (const { reject: reject2, error } of toReject) {
4100
+ reject2(error);
4101
+ }
4102
+ });
4090
4103
  }
4091
4104
  }
4092
4105
  /**
@@ -4135,6 +4148,7 @@ var init_delegate = __esm({
4135
4148
  }
4136
4149
  };
4137
4150
  defaultDelegationManager = new DelegationManager();
4151
+ DEFAULT_DELEGATE_TIMEOUT = parseInt(process.env.DELEGATE_TIMEOUT, 10) || 300;
4138
4152
  }
4139
4153
  });
4140
4154
 
package/build/delegate.js CHANGED
@@ -194,9 +194,14 @@ class DelegationManager {
194
194
 
195
195
  /**
196
196
  * Process the wait queue - grant slot to next waiting delegation
197
+ * Uses setImmediate to avoid blocking the event loop when resolving promises.
197
198
  * @private
198
199
  */
199
200
  _processQueue(debug = false) {
201
+ // Collect callbacks to invoke after the loop to avoid blocking the event loop
202
+ const toResolve = [];
203
+ const toReject = [];
204
+
200
205
  // Process queue items one at a time when slots are available
201
206
  // Items are only removed when they can be granted or must be rejected
202
207
  while (this.waitQueue.length > 0 && this.globalActive < this.maxConcurrent) {
@@ -216,7 +221,7 @@ class DelegationManager {
216
221
  if (debug) {
217
222
  console.error(`[DelegationManager] Session limit (${this.maxPerSession}) reached for queued item, rejecting`);
218
223
  }
219
- reject(new Error(`Maximum delegations per session (${this.maxPerSession}) reached for session ${parentSessionId}`));
224
+ toReject.push({ reject, error: new Error(`Maximum delegations per session (${this.maxPerSession}) reached for session ${parentSessionId}`) });
220
225
  // Continue to process next item in queue
221
226
  continue;
222
227
  }
@@ -230,7 +235,21 @@ class DelegationManager {
230
235
  console.error(`[DelegationManager] Granted slot from queue (waited ${waitTime}ms). Active: ${this.globalActive}/${this.maxConcurrent}`);
231
236
  }
232
237
 
233
- resolve(true);
238
+ toResolve.push(resolve);
239
+ }
240
+
241
+ // Defer promise resolutions/rejections to next tick to avoid blocking the event loop.
242
+ // This is critical: synchronous resolve()/reject() calls in a tight loop can saturate
243
+ // the microtask queue and prevent other async operations from proceeding.
244
+ if (toResolve.length > 0 || toReject.length > 0) {
245
+ setImmediate(() => {
246
+ for (const resolve of toResolve) {
247
+ resolve(true);
248
+ }
249
+ for (const { reject, error } of toReject) {
250
+ reject(error);
251
+ }
252
+ });
234
253
  }
235
254
  }
236
255
 
@@ -293,6 +312,9 @@ const defaultDelegationManager = new DelegationManager();
293
312
  // Export the class for per-instance usage
294
313
  export { DelegationManager };
295
314
 
315
+ // Default delegation timeout from environment variable or 300 seconds (5 minutes)
316
+ const DEFAULT_DELEGATE_TIMEOUT = parseInt(process.env.DELEGATE_TIMEOUT, 10) || 300;
317
+
296
318
  /**
297
319
  * Delegate a big distinct task to a probe subagent (used automatically by AI agents)
298
320
  *
@@ -309,7 +331,7 @@ export { DelegationManager };
309
331
  *
310
332
  * @param {Object} options - Delegate options
311
333
  * @param {string} options.task - A complete, self-contained task for the subagent. Should be specific and focused on one area of expertise.
312
- * @param {number} [options.timeout=300] - Timeout in seconds (default: 5 minutes)
334
+ * @param {number} [options.timeout] - Timeout in seconds (default: DELEGATE_TIMEOUT env var or 300)
313
335
  * @param {boolean} [options.debug=false] - Enable debug logging
314
336
  * @param {number} [options.currentIteration=0] - Current tool iteration count from parent agent
315
337
  * @param {number} [options.maxIterations=30] - Maximum tool iterations allowed
@@ -335,7 +357,7 @@ export { DelegationManager };
335
357
  */
336
358
  export async function delegate({
337
359
  task,
338
- timeout = 300,
360
+ timeout = DEFAULT_DELEGATE_TIMEOUT,
339
361
  debug = false,
340
362
  currentIteration = 0,
341
363
  maxIterations = 30,