mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
* Improve Catty context budget, step guards, and compaction reliability. Anchors session state across compactions and surfaces compaction to the UI so long tool-heavy turns stay within context limits. Co-authored-by: Cursor <cursoragent@cursor.com> * Fix Codex review: gate reinjection and match sftp_read_file. Only reinject session context after actual compaction adjustments, and recognize the generated sftp_read_file tool name in stale read pruning. Co-authored-by: Cursor <cursoragent@cursor.com> * Scope stale pruning by session for SFTP reads and terminals. Include sessionId in read fingerprints and keep the last two terminal outputs per session instead of globally across the conversation. Co-authored-by: Cursor <cursoragent@cursor.com> * Cap output reserve for small context windows in budget math. Prevent default 4096-token output reserve from collapsing compaction thresholds on 4k/8k models into near-zero values. Co-authored-by: Cursor <cursoragent@cursor.com> * Gate terminal stale pruning on budget pressure and clear old assistant status. Only omit older terminal_execute output when compaction budget is tight, and clear step-compaction statusText from the prior assistant when a new assistant message starts after tool results. Co-authored-by: Cursor <cursoragent@cursor.com> * Gate SFTP read supersession on budget pressure like terminal pruning. Preserve earlier file reads during read-edit-reread compare flows until context compaction is actually needed. Co-authored-by: Cursor <cursoragent@cursor.com> * Use compaction summary budget for pre-summarize pruning. Reserve the 1600-token summary output when fitting messages for LLM compaction, and skip superseding successful reads when a later read failed. Co-authored-by: Cursor <cursoragent@cursor.com> * Prune oldest messages first when fitting compaction summarize input. Keep the portion of pre-tail history closest to the current task instead of dropping recent summarize candidates from the end. Co-authored-by: Cursor <cursoragent@cursor.com> * Detect JSON tool errors before superseding stale reads. Treat structured error payloads as failed reads so a later SFTP failure does not replace the last successful file contents. Co-authored-by: Cursor <cursoragent@cursor.com> * Show compaction as a transient inline shimmer instead of a banner. Emit compaction_start/compaction events and render a single ephemeral status line in the message list that clears when compaction finishes. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Cursor <cursoragent@cursor.com>
99 lines
3.4 KiB
TypeScript
99 lines
3.4 KiB
TypeScript
import type { ModelMessage } from 'ai';
|
|
import {
|
|
estimateModelMessagesTokensWithKind,
|
|
estimateTextTokens,
|
|
estimateUnknownTokens,
|
|
} from './tokenEstimator';
|
|
|
|
export const COMPACTION_PROMPT_RESERVE = 150;
|
|
export const AUTO_COMPACT_BUFFER_CAP = 15_000;
|
|
export const AUTO_COMPACT_BUFFER_RATIO = 0.8;
|
|
export const COMPACTION_SUMMARY_MAX_OUTPUT_TOKENS = 1600;
|
|
export const DEFAULT_MAX_OUTPUT_TOKENS = 4096;
|
|
const MIN_OUTPUT_RESERVE = 256;
|
|
const MAX_OUTPUT_SHARE_OF_WINDOW = 0.25;
|
|
|
|
export function resolveEffectiveMaxOutputTokens(
|
|
contextWindow: number,
|
|
maxOutputTokens: number = DEFAULT_MAX_OUTPUT_TOKENS,
|
|
): number {
|
|
if (contextWindow <= 0) return maxOutputTokens;
|
|
const cappedByWindow = Math.max(
|
|
MIN_OUTPUT_RESERVE,
|
|
Math.floor(contextWindow * MAX_OUTPUT_SHARE_OF_WINDOW),
|
|
);
|
|
return Math.min(maxOutputTokens, cappedByWindow);
|
|
}
|
|
|
|
export interface ComputeCompactionThresholdInput {
|
|
contextWindow: number;
|
|
maxOutputTokens?: number;
|
|
compactionPromptTokens?: number;
|
|
}
|
|
|
|
export function computeCompactionBuffer(contextWindow: number, maxOutputTokens: number): number {
|
|
const remaining = Math.max(0, contextWindow - maxOutputTokens);
|
|
const ratioCompactionBuffer = Math.ceil((1 - AUTO_COMPACT_BUFFER_RATIO) * remaining);
|
|
const safeCompactionBuffer = Math.max(maxOutputTokens, ratioCompactionBuffer);
|
|
return Math.min(safeCompactionBuffer, AUTO_COMPACT_BUFFER_CAP);
|
|
}
|
|
|
|
/** Continue-style threshold: compact before the next turn would exceed the window. */
|
|
export function computeCompactionThreshold(input: ComputeCompactionThresholdInput): number {
|
|
const maxOutputTokens = resolveEffectiveMaxOutputTokens(
|
|
input.contextWindow,
|
|
input.maxOutputTokens ?? DEFAULT_MAX_OUTPUT_TOKENS,
|
|
);
|
|
const compactionPromptTokens = input.compactionPromptTokens ?? COMPACTION_PROMPT_RESERVE;
|
|
const buffer = computeCompactionBuffer(input.contextWindow, maxOutputTokens);
|
|
const threshold = input.contextWindow - maxOutputTokens - buffer - compactionPromptTokens;
|
|
return Math.max(1, threshold);
|
|
}
|
|
|
|
export interface ComputeTotalInputTokensInput {
|
|
messages: ModelMessage[];
|
|
providerId?: string | null;
|
|
systemPrompt?: string;
|
|
toolNames?: string[];
|
|
reservedTokens?: number;
|
|
}
|
|
|
|
export function computeTotalInputTokens(input: ComputeTotalInputTokensInput): number {
|
|
const messageTokens = estimateModelMessagesTokensWithKind({
|
|
messages: input.messages,
|
|
providerId: input.providerId,
|
|
}).tokens;
|
|
const systemTokens = input.systemPrompt
|
|
? estimateTextTokens(input.systemPrompt, input.providerId)
|
|
: 0;
|
|
const toolTokens = input.toolNames?.length
|
|
? estimateUnknownTokens({ tools: input.toolNames }, input.providerId)
|
|
: 0;
|
|
const reserved = Math.max(0, Math.ceil(input.reservedTokens ?? 0));
|
|
return messageTokens + systemTokens + toolTokens + reserved;
|
|
}
|
|
|
|
export function shouldCompactByBudget(input: {
|
|
messages: ModelMessage[];
|
|
contextWindow: number;
|
|
maxOutputTokens?: number;
|
|
providerId?: string | null;
|
|
systemPrompt?: string;
|
|
toolNames?: string[];
|
|
reservedTokens?: number;
|
|
forceThreshold?: number;
|
|
}): boolean {
|
|
const total = computeTotalInputTokens({
|
|
messages: input.messages,
|
|
providerId: input.providerId,
|
|
systemPrompt: input.systemPrompt,
|
|
toolNames: input.toolNames,
|
|
reservedTokens: input.reservedTokens,
|
|
});
|
|
const threshold = input.forceThreshold ?? computeCompactionThreshold({
|
|
contextWindow: input.contextWindow,
|
|
maxOutputTokens: input.maxOutputTokens,
|
|
});
|
|
return total >= threshold;
|
|
}
|