import type { HealthSummary } from "../commands/health.js"; import { sweepStaleRunContexts } from "../infra/agent-events.js"; import { cleanOldMedia } from "../media/store.js"; import { abortChatRunById, type ChatAbortControllerEntry } from "./chat-abort.js"; import { pruneStaleControlPlaneBuckets } from "./control-plane-rate-limit.js"; import type { ChatRunEntry } from "./server-chat.js"; import {
DEDUPE_MAX,
DEDUPE_TTL_MS,
HEALTH_REFRESH_INTERVAL_MS,
TICK_INTERVAL_MS,
} from "./server-constants.js"; import type { DedupeEntry } from "./server-shared.js"; import { formatError } from "./server-utils.js"; import { setBroadcastHealthUpdate } from "./server/health-state.js";
// Prime cache so first client gets a snapshot without waiting. void params
.refreshGatewayHealthSnapshot({ probe: true })
.catch((err) => params.logHealth.error(`initial refresh failed: ${formatError(err)}`));
// dedupe cache cleanup const dedupeCleanup = setInterval(() => { const AGENT_RUN_SEQ_MAX = 10_000; const now = Date.now(); for (const [k, v] of params.dedupe) { if (now - v.ts > DEDUPE_TTL_MS) {
params.dedupe.delete(k);
}
} if (params.dedupe.size > DEDUPE_MAX) { const entries = [...params.dedupe.entries()].toSorted((a, b) => a[1].ts - b[1].ts); for (let i = 0; i < params.dedupe.size - DEDUPE_MAX; i++) {
params.dedupe.delete(entries[i][0]);
}
}
if (params.agentRunSeq.size > AGENT_RUN_SEQ_MAX) { const excess = params.agentRunSeq.size - AGENT_RUN_SEQ_MAX;
let removed = 0; for (const runId of params.agentRunSeq.keys()) {
params.agentRunSeq.delete(runId);
removed += 1; if (removed >= excess) { break;
}
}
}
const ABORTED_RUN_TTL_MS = 60 * 60_000; for (const [runId, abortedAt] of params.chatRunState.abortedRuns) { if (now - abortedAt <= ABORTED_RUN_TTL_MS) { continue;
}
params.chatRunState.abortedRuns.delete(runId);
params.chatRunBuffers.delete(runId);
params.chatDeltaSentAt.delete(runId);
params.chatDeltaLastBroadcastLen.delete(runId);
}
// Prune expired control-plane rate-limit buckets to prevent unbounded // growth when many unique clients connect over time.
pruneStaleControlPlaneBuckets(now);
// Sweep stale buffers for runs that were never explicitly aborted. // Only reap orphaned buffers after the abort controller is gone; active // runs can legitimately sit idle while tools/models work. for (const [runId, lastSentAt] of params.chatDeltaSentAt) { if (params.chatRunState.abortedRuns.has(runId)) { continue; // already handled above
} if (params.chatAbortControllers.has(runId)) { continue;
} if (now - lastSentAt <= ABORTED_RUN_TTL_MS) { continue;
}
params.chatRunBuffers.delete(runId);
params.chatDeltaSentAt.delete(runId);
params.chatDeltaLastBroadcastLen.delete(runId);
} // Sweep stale agent run contexts (orphaned when lifecycle end/error is missed).
sweepStaleRunContexts();
}, 60_000);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.