UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

177 lines (176 loc) • 6.55 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { v as normalizeUniqueTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs"; import { a as resolveBootstrapTotalMaxChars, i as resolveBootstrapMaxChars, t as USER_BOOTSTRAP_MAX_CHARS } from "./bootstrap-BoYamcpL.mjs"; import path from "node:path"; //#region src/agents/bootstrap-budget.ts const DEFAULT_BOOTSTRAP_PROMPT_WARNING_SIGNATURE_HISTORY_MAX = 32; const BOOTSTRAP_TRUNCATION_NOTICE = [ "[Bootstrap truncation warning]", "Some workspace bootstrap files were truncated before Project Context injection.", "Treat Project Context as partial and read the relevant files directly if details seem missing." ].join("\n"); /** Builds the compact truncation notice mirrored into run metadata. */ function buildBootstrapPromptWarningNotice(e) { return e?.some((e) => e.trim().length > 0) ? BOOTSTRAP_TRUNCATION_NOTICE : void 0; } function appendSeenSignature(e, t) { if (!t.trim() || e.includes(t)) return e; const n = [...e, t]; return n.length <= DEFAULT_BOOTSTRAP_PROMPT_WARNING_SIGNATURE_HISTORY_MAX ? n : n.slice(-32); } function buildBootstrapTruncationSignature(e) { if (!e.hasTruncation) return; const t = e.truncatedFiles.map((e) => ({ path: e.path || e.name, rawChars: e.rawChars, injectedChars: e.injectedChars, causes: e.causes.toSorted() })).toSorted((e, t) => e.path.localeCompare(t.path) || e.rawChars - t.rawChars || e.injectedChars - t.injectedChars || e.causes.join("+").localeCompare(t.causes.join("+"))); return JSON.stringify({ bootstrapMaxChars: e.totals.bootstrapMaxChars, bootstrapTotalMaxChars: e.totals.bootstrapTotalMaxChars, files: t }); } const DEFAULT_BOOTSTRAP_NEAR_LIMIT_RATIO = .85; function normalizePositiveLimit(e) { if (!Number.isFinite(e) || e <= 0) return 1; return Math.floor(e); } function effectiveBootstrapFileLimit(e, t) { return e.toLowerCase() === "user.md" ? Math.min(t, USER_BOOTSTRAP_MAX_CHARS) : t; } /** * USER.md carries a deliberate fixed cap: tuning bootstrapMaxChars can only * lower it, so per-file remediation must never suggest raising that setting * for it. The effective limit equals the fixed cap exactly when the cap (not * the configured limit) is the binding constraint. */ function isFixedUserCapFile(e) { return e.name.toLowerCase() === "user.md" && e.effectiveFileLimit === 4e3; } /** Restores prompt-warning dedupe state from a previous bootstrap report. */ function resolveBootstrapWarningSignaturesSeen(e) { const t = e?.bootstrapTruncation; const n = normalizeUniqueTrimmedStringList(t?.warningSignaturesSeen); if (n.length > 0) return n; if (t?.warningMode === "off") return []; const r = normalizeOptionalString(t?.promptWarningSignature); return r ? [r] : []; } /** * Matches injected content by source path, because basenames can repeat even * when the total budget drops one of those files. Account before remapping * source paths into the prompt workspace. */ function buildBootstrapInjectionStats(e) { const t = /* @__PURE__ */ new Map(); for (const n of e.injectedFiles) { const e = normalizeOptionalString(n.path) ?? ""; if (e && !t.has(e)) t.set(e, n.content); } return e.bootstrapFiles.map((e) => { const n = normalizeOptionalString(e.path) ?? ""; const r = n.replace(/\\/g, "/"); const i = normalizeOptionalString(e.name) ?? (r ? path.posix.basename(r) : "bootstrap"); const a = e.missing ? 0 : (e.content ?? "").trimEnd().length; const o = (n ? t.get(n) : void 0)?.length ?? 0; const s = !e.missing && o < a; return { name: i, path: n || i, origin: e.origin, missing: e.missing, rawChars: a, injectedChars: o, truncated: s }; }); } /** Classifies bootstrap truncation and near-limit pressure for prompt/report output. */ function analyzeBootstrapBudget(e) { const t = normalizePositiveLimit(e.bootstrapMaxChars); const n = normalizePositiveLimit(e.bootstrapTotalMaxChars); const r = DEFAULT_BOOTSTRAP_NEAR_LIMIT_RATIO; let i = 0; let a = 0; let o = n; const s = e.files.map((e) => { const n = effectiveBootstrapFileLimit(e.name, t); const s = o; o = Math.max(0, o - e.injectedChars); if (e.missing) return { ...e, effectiveFileLimit: n, nearLimit: false, causes: [] }; i += e.rawChars; a += e.injectedChars; const c = e.rawChars > n; const l = e.rawChars >= Math.ceil(n * r); const u = []; if (e.truncated) { if (c) u.push("per-file-limit"); if (s < n && e.rawChars > s) u.push("total-limit"); } return { ...e, effectiveFileLimit: n, nearLimit: l, causes: u }; }); const c = s.filter((e) => e.truncated); return { truncatedFiles: c, nearLimitFiles: s.filter((e) => e.nearLimit), totalNearLimit: a >= Math.ceil(n * r), hasTruncation: c.length > 0, totals: { rawChars: i, injectedChars: a, bootstrapMaxChars: t, bootstrapTotalMaxChars: n } }; } /** Builds the canonical bootstrap budget diagnosis after caller-owned routing. */ function buildBootstrapBudgetState(e) { const t = resolveBootstrapMaxChars(e.config, e.agentId); const n = resolveBootstrapTotalMaxChars(e.config, e.agentId); const r = analyzeBootstrapBudget({ files: e.files, bootstrapMaxChars: t, bootstrapTotalMaxChars: n }); const i = buildBootstrapTruncationSignature(r); let a = normalizeUniqueTrimmedStringList(e.seenSignatures); if (e.previousSignature && !a.includes(e.previousSignature)) a = appendSeenSignature(a, e.previousSignature); const o = Boolean(i); return { bootstrapAnalysis: r, bootstrapMaxChars: t, bootstrapPromptWarning: { signature: i, warningShown: o, lines: o ? [BOOTSTRAP_TRUNCATION_NOTICE] : [], warningSignaturesSeen: i ? appendSeenSignature(a, i) : a }, bootstrapTotalMaxChars: n }; } /** Serializes truncation warning state for run reports and future dedupe. */ function buildBootstrapTruncationReportMeta(e) { return { warningMode: "always", warningShown: e.warning.warningShown, promptWarningSignature: e.warning.signature, ...e.warning.warningSignaturesSeen.length > 0 ? { warningSignaturesSeen: e.warning.warningSignaturesSeen } : {}, truncatedFiles: e.analysis.truncatedFiles.length, nearLimitFiles: e.analysis.nearLimitFiles.length, totalNearLimit: e.analysis.totalNearLimit }; } //#endregion export { buildBootstrapTruncationReportMeta as a, buildBootstrapPromptWarningNotice as i, buildBootstrapBudgetState as n, isFixedUserCapFile as o, buildBootstrapInjectionStats as r, resolveBootstrapWarningSignaturesSeen as s, analyzeBootstrapBudget as t };