openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
36 lines (35 loc) • 1.9 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
//#region src/agents/context-pressure.ts
function decideContextPressure(e) {
if (exceedsContextWindow(e.estimatedPromptTokens, e.windowTokens)) return "overflow";
if (!e.maintenanceDue || !e.boundarySafe) return;
return e.runId && isBoundaryMaintenanceDeclined(e.runId) ? void 0 : "maintenance";
}
/** The only failure predicate: the priced request is larger than the window. */
function exceedsContextWindow(e, t) {
return typeof e === "number" && typeof t === "number" && Number.isFinite(e) && Number.isFinite(t) && t > 0 && e > t;
}
/**
* A request boundary is safe for maintenance when stopping here and continuing
* from the transcript is indistinguishable from sending the request: every
* tool call has a recorded result, no tool item is still running, no tool
* started background work that reports into this attempt, no Code Mode run is
* parked (its `wait` is bound to this session, which compaction may rotate),
* and no approval prompt is waiting for the user.
*/
function isToolBatchBoundarySafe(e) {
return e.pendingToolCalls === 0 && e.itemLifecycle.activeCount === 0 && e.itemLifecycle.completedCount === e.itemLifecycle.startedCount && !e.asyncActivity && !e.parkedCodeModeRun && !e.approvalPromptSent;
}
const declinedRuns = resolveGlobalSingleton(Symbol.for("openclaw.boundaryMaintenanceDeclined"), () => /* @__PURE__ */ new Set());
/** Maintenance that could not reduce context is not retried for the rest of the run. */
function declineBoundaryMaintenance(e) {
declinedRuns.add(e);
}
function isBoundaryMaintenanceDeclined(e) {
return declinedRuns.has(e);
}
function clearBoundaryMaintenance(e) {
declinedRuns.delete(e);
}
//#endregion
export { isToolBatchBoundarySafe as a, exceedsContextWindow as i, decideContextPressure as n, declineBoundaryMaintenance as r, clearBoundaryMaintenance as t };