+71
-13
@@ -1,5 +1,8 @@
|
||||
/**
|
||||
* Context compaction: heuristic fallback + optional one-shot LLM summary.
|
||||
*
|
||||
* Tool schemas are reserved out of historyBudget. Compact against conversation
|
||||
* tokens only — never treat a large tool list as "history is full".
|
||||
*/
|
||||
|
||||
const truncate = require('./truncate.js');
|
||||
@@ -7,6 +10,7 @@ const truncate = require('./truncate.js');
|
||||
const CHAR_PER_TOKEN = 3;
|
||||
const THRESHOLD = 0.68;
|
||||
const MIN_SUMMARY = 80;
|
||||
const MIN_COMPACT_MESSAGES = 4;
|
||||
const COMPACT_PROMPT =
|
||||
'Summarize this coding-agent conversation. Use exactly these sections:\n' +
|
||||
'1. Goal\n' +
|
||||
@@ -15,6 +19,13 @@ const COMPACT_PROMPT =
|
||||
'4. Open work\n' +
|
||||
'5. Next action\n' +
|
||||
'Be specific (paths, names, errors). Do not say the conversation was compacted.';
|
||||
const VOICE_COMPACT_PROMPT =
|
||||
'Summarize this spoken assistant conversation for the next turn. Use exactly these sections:\n' +
|
||||
'1. Latest user request\n' +
|
||||
'2. Facts from tools\n' +
|
||||
'3. What was already answered\n' +
|
||||
'4. Open follow-ups\n' +
|
||||
'Be specific. Do not greet. Do not say the conversation was compacted.';
|
||||
|
||||
function contentChars(content) {
|
||||
if (content == null) return 0;
|
||||
@@ -42,23 +53,38 @@ function messageChars(m) {
|
||||
return n + 8;
|
||||
}
|
||||
|
||||
function estimateTokens(messages, tools) {
|
||||
function nonSystemCount(messages) {
|
||||
let n = 0;
|
||||
for (const m of messages || []) {
|
||||
if (m && m.role !== 'system') n += 1;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function conversationTokens(messages) {
|
||||
let n = 0;
|
||||
for (const m of messages || []) n += messageChars(m);
|
||||
n += JSON.stringify(tools || []).length;
|
||||
return Math.ceil(n / CHAR_PER_TOKEN);
|
||||
}
|
||||
|
||||
function toolTokens(tools) {
|
||||
return Math.ceil(JSON.stringify(tools || []).length / CHAR_PER_TOKEN);
|
||||
}
|
||||
|
||||
function estimateTokens(messages, tools) {
|
||||
return conversationTokens(messages) + toolTokens(tools);
|
||||
}
|
||||
|
||||
function historyBudget(ctxSize, tools, attempt) {
|
||||
const cap = ctxSize > 0 ? Number(ctxSize) : 8192;
|
||||
const toolTok = Math.ceil(JSON.stringify(tools || []).length / CHAR_PER_TOKEN);
|
||||
const toolTok = toolTokens(tools);
|
||||
const reserve = Math.max(384, Math.floor(cap * (0.18 + (Number(attempt) || 0) * 0.08)));
|
||||
return Math.max(240, Math.floor(cap * 0.72) - toolTok - reserve);
|
||||
}
|
||||
|
||||
function shouldCompact(messages, tools, ctxSize) {
|
||||
const cap = ctxSize > 0 ? ctxSize : 8192;
|
||||
return estimateTokens(messages, tools) > Math.floor(cap * THRESHOLD);
|
||||
if (nonSystemCount(messages) < MIN_COMPACT_MESSAGES) return false;
|
||||
return conversationTokens(messages) > historyBudget(ctxSize, tools, 0);
|
||||
}
|
||||
|
||||
function isOverflowError(err) {
|
||||
@@ -153,23 +179,30 @@ function lastRealUserIndex(list) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
function overHistoryBudget(keep, budget) {
|
||||
return conversationTokens(keep) > budget;
|
||||
}
|
||||
|
||||
function heuristicCompact(messages, opts) {
|
||||
opts = opts || {};
|
||||
const budget = opts.budgetTokens || 6000;
|
||||
const aggressive = !!opts.aggressive;
|
||||
if (!aggressive && nonSystemCount(messages) < MIN_COMPACT_MESSAGES) {
|
||||
return (messages || []).slice();
|
||||
}
|
||||
let keep = messages.slice();
|
||||
while (estimateTokens(keep, opts.tools) > budget && keep.length > 4) {
|
||||
while (overHistoryBudget(keep, budget) && keep.length > 4) {
|
||||
let idx = keep.findIndex((m, i) => i > 0 && m.role === 'tool');
|
||||
if (idx < 0) idx = keep.findIndex((m, i) => i > 1 && m.role === 'assistant');
|
||||
if (idx < 0) break;
|
||||
keep.splice(idx, 1);
|
||||
}
|
||||
const maxMsg = aggressive ? 1200 : 3200;
|
||||
if (estimateTokens(keep, opts.tools) > budget) {
|
||||
if (overHistoryBudget(keep, budget)) {
|
||||
const lastUser = lastRealUserIndex(keep);
|
||||
keep = keep.map((m, i) => (i === 0 || i === lastUser ? m : truncateMsg(m, maxMsg)));
|
||||
}
|
||||
if (estimateTokens(keep, opts.tools) > budget && keep.length > 3) {
|
||||
if (overHistoryBudget(keep, budget) && keep.length > 3) {
|
||||
const head = keep[0];
|
||||
const lastUserIdx = lastRealUserIndex(keep);
|
||||
const lastUser = lastUserIdx >= 0 ? keep[lastUserIdx] : null;
|
||||
@@ -183,7 +216,7 @@ function heuristicCompact(messages, opts) {
|
||||
keep = keep.concat(tail);
|
||||
keep = keep.map((m, i) => (i === 0 ? m : truncateMsg(m, aggressive ? 700 : 1800)));
|
||||
}
|
||||
while (estimateTokens(keep, opts.tools) > budget && keep.length > 3) {
|
||||
while (overHistoryBudget(keep, budget) && keep.length > 3) {
|
||||
const dropAt = keep.findIndex((m, i) => i > 1 && !isRealUser(m));
|
||||
if (dropAt < 0) break;
|
||||
keep.splice(dropAt, 1);
|
||||
@@ -205,7 +238,12 @@ function transcript(messages) {
|
||||
.join('\n\n');
|
||||
}
|
||||
|
||||
function autoContinue(messages) {
|
||||
function isVoice(opts) {
|
||||
return !!(opts && opts.voice);
|
||||
}
|
||||
|
||||
function autoContinue(messages, opts) {
|
||||
if (isVoice(opts)) return null;
|
||||
const list = messages || [];
|
||||
const last = list[list.length - 1];
|
||||
if (!last) return null;
|
||||
@@ -219,7 +257,14 @@ function autoContinue(messages) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function compactReminder() {
|
||||
function compactReminder(opts) {
|
||||
if (isVoice(opts)) {
|
||||
return (
|
||||
'<system-reminder>\n' +
|
||||
'Context was compacted. Trust the summary and the last user request. Answer that request. Do not greet again.\n' +
|
||||
'</system-reminder>'
|
||||
);
|
||||
}
|
||||
return (
|
||||
'<system-reminder>\n' +
|
||||
'Context was compacted. Trust the summary and the last user request. Re-read files before further edits. Follow AGENTS.md if present.\n' +
|
||||
@@ -230,17 +275,25 @@ function compactReminder() {
|
||||
async function compactWithLlm(messages, opts) {
|
||||
opts = opts || {};
|
||||
const fallback = () => heuristicCompact(messages, opts);
|
||||
if (!opts.aggressive && nonSystemCount(messages) < MIN_COMPACT_MESSAGES) {
|
||||
return (messages || []).slice();
|
||||
}
|
||||
const complete = opts.complete;
|
||||
if (typeof complete !== 'function') return fallback();
|
||||
const voice = isVoice(opts);
|
||||
const cap =
|
||||
opts.maxTranscriptChars ||
|
||||
Math.max(1500, Math.min(24000, Math.floor((opts.budgetTokens || 4000) * CHAR_PER_TOKEN * 0.45)));
|
||||
const body = truncate.truncateWithMarker(transcript(messages), cap);
|
||||
const prompt = voice ? VOICE_COMPACT_PROMPT : COMPACT_PROMPT;
|
||||
const sys = voice
|
||||
? 'Reply with the four summary sections only. No tools. Do not greet.'
|
||||
: 'Reply with the five summary sections only. No tools.';
|
||||
try {
|
||||
const result = await complete({
|
||||
history: [
|
||||
{ role: 'system', content: 'Reply with the five summary sections only. No tools.' },
|
||||
{ role: 'user', content: COMPACT_PROMPT + '\n\n---\n\n' + body },
|
||||
{ role: 'system', content: sys },
|
||||
{ role: 'user', content: prompt + '\n\n---\n\n' + body },
|
||||
],
|
||||
tools: [],
|
||||
});
|
||||
@@ -256,10 +309,15 @@ module.exports = {
|
||||
CHAR_PER_TOKEN,
|
||||
THRESHOLD,
|
||||
MIN_SUMMARY,
|
||||
MIN_COMPACT_MESSAGES,
|
||||
COMPACT_PROMPT,
|
||||
VOICE_COMPACT_PROMPT,
|
||||
conversationTokens,
|
||||
toolTokens,
|
||||
estimateTokens,
|
||||
historyBudget,
|
||||
shouldCompact,
|
||||
nonSystemCount,
|
||||
isOverflowError,
|
||||
usage,
|
||||
snapshot,
|
||||
|
||||
Reference in New Issue
Block a user