// The create form's and the inference dialog's context limits, kept out of the // components so `npm test` can run them: the `soft_context_percent` field (the // agent compacts above it) and the `fresh_session_percent` field (above it, a // turn that finds the prompt cache cold starts a fresh session), and the slider // that moves both. Each is the text of its box, where empty keeps the agent's // own value. /** The lowest `soft_context_percent` the swarm accepts. */ export const MIN_SOFT_CONTEXT_PERCENT = 5; /** The highest: the whole context window. */ export const MAX_SOFT_CONTEXT_PERCENT = 100; /** * `soft_context_percent` as typed: `undefined` for empty (the agent's own * default), the number for a whole number from [`MIN_SOFT_CONTEXT_PERCENT`] * to [`MAX_SOFT_CONTEXT_PERCENT`], else `null`. */ export function softContextValue(text: string): number | null | undefined { const t = text.trim(); if (t === "") return undefined; if (!/^\d+$/.test(t)) return null; const n = Number(t); return n >= MIN_SOFT_CONTEXT_PERCENT && n <= MAX_SOFT_CONTEXT_PERCENT ? n : null; } /** How far along the slider's range a percentage sits, 0 to 100: the thumb's * place, which the zones behind the slider are drawn to meet. */ export function along(percent: number): number { return ( ((percent - MIN_SOFT_CONTEXT_PERCENT) / (MAX_SOFT_CONTEXT_PERCENT - MIN_SOFT_CONTEXT_PERCENT)) * 100 ); } /** What the harness uses for the fresh-session threshold when none is set. */ export const DEFAULT_FRESH_SESSION_PERCENT = 50; /** What the harness uses for the soft limit when none is set. */ export const DEFAULT_SOFT_CONTEXT_PERCENT = 75; /** The two limits as the slider draws them. */ export interface Limits { /** Where the fresh-session handle sits. */ fresh: number; /** Where the soft-limit handle sits. */ soft: number; /** Whether the swarm sets the fresh-session threshold, or the agent's own applies. */ freshSet: boolean; /** Whether the swarm sets the soft limit. */ softSet: boolean; } function percentOf(text: string): number | undefined { const v = softContextValue(text); return typeof v === "number" ? v : undefined; } /** * Where the handles sit: a set value where there is one, else the harness's own * default. An unset fresh-session threshold never sits above the soft limit * (the agent compacts there, so the context would not get past it), and a * typed value that is not a valid percent counts as unset. */ export function limitsOf(freshText: string, softText: string): Limits { const freshValue = percentOf(freshText); const softValue = percentOf(softText); const soft = softValue ?? DEFAULT_SOFT_CONTEXT_PERCENT; const fresh = freshValue ?? Math.min(DEFAULT_FRESH_SESSION_PERCENT, soft); return { fresh, soft, freshSet: freshValue !== undefined, softSet: softValue !== undefined, }; } /** Where the fresh-session handle may go: not past the soft handle. */ export function clampFresh(to: number, limits: Limits): number { return Math.min( Math.max(Math.round(to), MIN_SOFT_CONTEXT_PERCENT), limits.soft, ); } /** * Where the soft handle may go: not below the fresh-session handle when the * swarm sets one. An unset fresh-session handle only follows the soft one down * (`limitsOf`), so it does not hold it up. */ export function clampSoft(to: number, limits: Limits): number { const floor = limits.freshSet ? limits.fresh : MIN_SOFT_CONTEXT_PERCENT; return Math.min( Math.max(Math.round(to), floor, MIN_SOFT_CONTEXT_PERCENT), MAX_SOFT_CONTEXT_PERCENT, ); } /** * Why the two boxes are refused together: a fresh-session threshold above the * soft limit, both set. Each box's own range is `draftProblem`'s. */ export function crossProblem( freshText: string, softText: string, ): string | null { const fresh = percentOf(freshText); const soft = percentOf(softText); return fresh !== undefined && soft !== undefined && fresh > soft ? `the fresh-session threshold (${fresh}%) is above the soft limit (${soft}%): the agent compacts at the soft limit, so a fresh start above it would never happen` : null; }