




















@@ -20,6 +20,7 @@ const agentRunCache = new Map<string, AgentRunSnapshot>();
2020const agentRunStarts = new Map<string, number>();
2121const pendingAgentRunErrors = new Map<string, PendingAgentRunError>();
2222const pendingAgentRunTimeouts = new Map<string, PendingAgentRunTerminal>();
23+const agentRunWaiterCounts = new Map<string, number>();
2324let agentRunListenerStarted = false;
24252526type AgentRunSnapshot = {
@@ -195,6 +196,23 @@ function getCachedAgentRun(runId: string) {
195196return agentRunCache.get(runId);
196197}
197198199+function addAgentRunWaiter(runId: string): () => void {
200+agentRunWaiterCounts.set(runId, (agentRunWaiterCounts.get(runId) ?? 0) + 1);
201+let removed = false;
202+return () => {
203+if (removed) {
204+return;
205+}
206+removed = true;
207+const nextCount = (agentRunWaiterCounts.get(runId) ?? 1) - 1;
208+if (nextCount <= 0) {
209+agentRunWaiterCounts.delete(runId);
210+return;
211+}
212+agentRunWaiterCounts.set(runId, nextCount);
213+};
214+}
215+198216export async function waitForAgentJob(params: {
199217runId: string;
200218timeoutMs: number;
@@ -216,6 +234,7 @@ export async function waitForAgentJob(params: {
216234let pendingErrorTimer: NodeJS.Timeout | undefined;
217235let pendingTimeoutTimer: NodeJS.Timeout | undefined;
218236let onAbort: (() => void) | undefined;
237+let removeWaiter = () => {};
219238220239const clearPendingErrorTimer = () => {
221240if (!pendingErrorTimer) {
@@ -242,6 +261,7 @@ export async function waitForAgentJob(params: {
242261clearPendingErrorTimer();
243262clearPendingTimeoutTimer();
244263unsubscribe();
264+removeWaiter();
245265if (onAbort) {
246266signal?.removeEventListener("abort", onAbort);
247267}
@@ -334,6 +354,7 @@ export async function waitForAgentJob(params: {
334354recordAgentRunSnapshot(snapshot);
335355finish(snapshot);
336356});
357+removeWaiter = addAgentRunWaiter(runId);
337358338359const timer = setSafeTimeout(() => finish(null), timeoutMs);
339360onAbort = () => finish(null);
@@ -342,3 +363,19 @@ export async function waitForAgentJob(params: {
342363}
343364344365ensureAgentRunListener();
366+367+export const __testing = {
368+getWaiterCount(runId?: string): number {
369+if (runId) {
370+return agentRunWaiterCounts.get(runId) ?? 0;
371+}
372+let total = 0;
373+for (const count of agentRunWaiterCounts.values()) {
374+total += count;
375+}
376+return total;
377+},
378+resetWaiters(): void {
379+agentRunWaiterCounts.clear();
380+},
381+};
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。