+82
-63
@@ -24,7 +24,7 @@ const VOICE_COMPACT_PROMPT =
|
||||
'1. Latest user request\n' +
|
||||
'2. Facts from tools\n' +
|
||||
'3. What was already answered\n' +
|
||||
'4. Open follow-ups\n' +
|
||||
'4. Open follow-ups, original goal, user preferences and corrections\n' +
|
||||
'Be specific. Do not greet. Do not say the conversation was compacted.';
|
||||
|
||||
function contentChars(content) {
|
||||
@@ -75,16 +75,19 @@ function estimateTokens(messages, tools) {
|
||||
return conversationTokens(messages) + toolTokens(tools);
|
||||
}
|
||||
|
||||
function historyBudget(ctxSize, tools, attempt) {
|
||||
function historyBudget(ctxSize, tools, attempt, extraTokens = 0) {
|
||||
const cap = ctxSize > 0 ? Number(ctxSize) : 8192;
|
||||
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);
|
||||
return Math.max(240, Math.floor(cap * 0.72) - toolTok - reserve - extraTokens);
|
||||
}
|
||||
|
||||
function shouldCompact(messages, tools, ctxSize) {
|
||||
if (nonSystemCount(messages) < MIN_COMPACT_MESSAGES) return false;
|
||||
return conversationTokens(messages) > historyBudget(ctxSize, tools, 0);
|
||||
function shouldCompact(messages, tools, ctxSize, extraTokens = 0) {
|
||||
const budget = historyBudget(ctxSize, tools, 0, extraTokens);
|
||||
const tokens = conversationTokens(messages);
|
||||
// Large first requests or tool results can overflow before four messages.
|
||||
// A small greeting must not be discarded merely because schemas are large.
|
||||
return tokens > budget && (nonSystemCount(messages) >= MIN_COMPACT_MESSAGES || conversationTokens((messages || []).filter((m) => m.role !== 'system')) > 1024);
|
||||
}
|
||||
|
||||
function isOverflowError(err) {
|
||||
@@ -129,29 +132,31 @@ function isRealUser(m) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Keep native tool exchanges atomic: results must never outlive their calls.
|
||||
function messageGroups(messages) {
|
||||
const groups = [];
|
||||
for (const message of messages) {
|
||||
const previous = groups[groups.length - 1];
|
||||
if (message.role === 'tool') {
|
||||
const calls = previous && previous[0].tool_calls;
|
||||
if (calls && calls.some((call) => call.id === message.tool_call_id)) previous.push(message);
|
||||
continue;
|
||||
}
|
||||
groups.push([message]);
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
function rebuildHistory(messages, summary) {
|
||||
const list = Array.isArray(messages) ? messages : [];
|
||||
const sys =
|
||||
list[0] && list[0].role === 'system'
|
||||
? { role: 'system', content: list[0].content }
|
||||
: { role: 'system', content: '' };
|
||||
let lastUser = null;
|
||||
for (let i = list.length - 1; i >= 0; i--) {
|
||||
if (isRealUser(list[i])) {
|
||||
lastUser = { role: 'user', content: list[i].content };
|
||||
break;
|
||||
}
|
||||
}
|
||||
const tail = [];
|
||||
for (let i = list.length - 1; i >= 1 && tail.length < 6; i--) {
|
||||
const m = list[i];
|
||||
if (!m || m.role === 'system') continue;
|
||||
if (lastUser && m.role === 'user' && m.content === lastUser.content) continue;
|
||||
tail.unshift({ role: m.role, content: m.content, name: m.name, tool_call_id: m.tool_call_id });
|
||||
}
|
||||
const out = [sys, { role: 'user', content: '[conversation summary]\n' + String(summary || '').trim() }];
|
||||
if (lastUser) out.push(lastUser);
|
||||
return out.concat(tail);
|
||||
const systems = list.filter((m) => m.role === 'system');
|
||||
const lastUser = lastRealUserIndex(list);
|
||||
// Older turns belong in the summary, never after the latest request.
|
||||
const tail = lastUser >= 0 ? list.slice(lastUser) : list.filter((m) => m.role !== 'system');
|
||||
return systems.concat(
|
||||
{ role: 'user', content: '[conversation summary]\n' + String(summary || '').trim() },
|
||||
messageGroups(tail.filter((m) => m.role !== 'system')).flat()
|
||||
);
|
||||
}
|
||||
|
||||
function truncateMsg(m, maxChars) {
|
||||
@@ -186,44 +191,55 @@ function overHistoryBudget(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();
|
||||
const list = messages || [];
|
||||
const tokens = conversationTokens(list);
|
||||
if (tokens <= budget || (!opts.aggressive && nonSystemCount(list) < MIN_COMPACT_MESSAGES && conversationTokens(list.filter((m) => m.role !== 'system')) <= 1024))
|
||||
return list.slice();
|
||||
|
||||
const systems = list.filter((m) => m.role === 'system');
|
||||
const lastUser = lastRealUserIndex(list);
|
||||
const request = lastUser >= 0 ? list[lastUser] : null;
|
||||
const older = list.slice(0, lastUser >= 0 ? lastUser : 0).filter((m) => m.role !== 'system');
|
||||
const tail = list.slice(lastUser >= 0 ? lastUser + 1 : 0).filter((m) => m.role !== 'system');
|
||||
let groups = messageGroups(tail);
|
||||
// Preserve an existing summary across repeated compactions. The fallback
|
||||
// carries real excerpts rather than replacing memory with a generic notice.
|
||||
const memory = transcript(older.concat(tail));
|
||||
const fixed = systems.concat(request ? [request] : []);
|
||||
const spareChars = Math.max(0, (budget - conversationTokens(fixed) - 12) * CHAR_PER_TOKEN);
|
||||
const summaryCap = Math.min(2400, Math.floor(spareChars * (groups.length ? 0.4 : 1)));
|
||||
let summary = memory && summaryCap >= 64
|
||||
? { role: 'user', content: '[conversation summary]\n' + boundedExcerpt(memory, summaryCap) }
|
||||
: null;
|
||||
const assemble = () => systems.concat(summary ? [summary] : [], request ? [request] : [], groups.flat());
|
||||
let keep = assemble();
|
||||
// Shrink bulky results first; preserve call ids, arguments and ordering.
|
||||
for (const cap of [1600, 600, 180]) {
|
||||
if (!overHistoryBudget(keep, budget)) break;
|
||||
groups = groups.map((group) => group.map((m) => truncateMsg(m, cap)));
|
||||
keep = assemble();
|
||||
}
|
||||
let keep = messages.slice();
|
||||
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);
|
||||
while (overHistoryBudget(keep, budget) && groups.length) {
|
||||
groups.shift();
|
||||
keep = assemble();
|
||||
}
|
||||
const maxMsg = aggressive ? 1200 : 3200;
|
||||
if (overHistoryBudget(keep, budget)) {
|
||||
const lastUser = lastRealUserIndex(keep);
|
||||
keep = keep.map((m, i) => (i === 0 || i === lastUser ? m : truncateMsg(m, maxMsg)));
|
||||
}
|
||||
if (overHistoryBudget(keep, budget) && keep.length > 3) {
|
||||
const head = keep[0];
|
||||
const lastUserIdx = lastRealUserIndex(keep);
|
||||
const lastUser = lastUserIdx >= 0 ? keep[lastUserIdx] : null;
|
||||
const tail = [];
|
||||
for (let i = keep.length - 1; i >= 1 && tail.length < (aggressive ? 2 : 4); i--) {
|
||||
if (i === lastUserIdx) continue;
|
||||
tail.unshift(keep[i]);
|
||||
}
|
||||
keep = [head, { role: 'user', content: '[conversation summary]\nEarlier turns were compacted to fit the model window.' }];
|
||||
if (lastUser) keep.push(lastUser);
|
||||
keep = keep.concat(tail);
|
||||
keep = keep.map((m, i) => (i === 0 ? m : truncateMsg(m, aggressive ? 700 : 1800)));
|
||||
}
|
||||
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);
|
||||
if (overHistoryBudget(keep, budget) && summary) {
|
||||
summary = null;
|
||||
keep = assemble();
|
||||
}
|
||||
// The system instructions and latest request are never silently truncated.
|
||||
// If these alone exceed the window, the caller reports the overflow.
|
||||
return keep;
|
||||
}
|
||||
|
||||
function boundedExcerpt(text, maxChars) {
|
||||
if (text.length <= maxChars) return text;
|
||||
const marker = '\n[earlier details omitted]\n';
|
||||
const remaining = Math.max(0, maxChars - marker.length);
|
||||
const head = Math.floor(remaining * 0.6);
|
||||
return text.slice(0, head) + marker + text.slice(-(remaining - head));
|
||||
}
|
||||
|
||||
function compact(messages, opts) {
|
||||
return heuristicCompact(messages, opts);
|
||||
}
|
||||
@@ -233,7 +249,10 @@ function transcript(messages) {
|
||||
.map((m) => {
|
||||
const role = m && m.role ? m.role : 'unknown';
|
||||
const name = m && m.name ? ' ' + m.name : '';
|
||||
return role + name + ':\n' + String(m && m.content != null ? m.content : '');
|
||||
const content = m && m.content != null ? m.content : '';
|
||||
const body = typeof content === 'string' ? content : JSON.stringify(content);
|
||||
if (body.startsWith('[conversation summary]')) return body.slice('[conversation summary]'.length).trim();
|
||||
return role + name + ':\n' + body + (m && m.tool_calls ? '\nTool calls: ' + JSON.stringify(m.tool_calls) : '');
|
||||
})
|
||||
.join('\n\n');
|
||||
}
|
||||
@@ -275,7 +294,7 @@ function compactReminder(opts) {
|
||||
async function compactWithLlm(messages, opts) {
|
||||
opts = opts || {};
|
||||
const fallback = () => heuristicCompact(messages, opts);
|
||||
if (!opts.aggressive && nonSystemCount(messages) < MIN_COMPACT_MESSAGES) {
|
||||
if (!opts.aggressive && nonSystemCount(messages) < MIN_COMPACT_MESSAGES && conversationTokens((messages || []).filter((m) => m.role !== 'system')) <= 1024) {
|
||||
return (messages || []).slice();
|
||||
}
|
||||
const complete = opts.complete;
|
||||
@@ -284,7 +303,7 @@ async function compactWithLlm(messages, 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 body = truncate.truncateWithMarker(transcript((messages || []).filter((m) => m.role !== 'system')), cap);
|
||||
const prompt = voice ? VOICE_COMPACT_PROMPT : COMPACT_PROMPT;
|
||||
const sys = voice
|
||||
? 'Reply with the four summary sections only. No tools. Do not greet.'
|
||||
@@ -299,7 +318,7 @@ async function compactWithLlm(messages, opts) {
|
||||
});
|
||||
const summary = result && result.text;
|
||||
if (isDegenerate(summary)) return fallback();
|
||||
return rebuildHistory(messages, summary);
|
||||
return heuristicCompact(rebuildHistory(messages, summary), Object.assign({}, opts, { aggressive: true }));
|
||||
} catch (_) {
|
||||
return fallback();
|
||||
}
|
||||
|
||||
Vendored
+39
-45
@@ -279,18 +279,24 @@ function applyPlanWrite(session, name, args) {
|
||||
return { ok: true, path: file, bytes: text.length };
|
||||
}
|
||||
|
||||
function appendCompactReminders(extra, session, tracker, budget) {
|
||||
if (!tracker || !tracker.pendingCompactReminder) return;
|
||||
const voice = !!(budget && budget.voice);
|
||||
const reminders = [{ role: 'user', content: compaction.compactReminder({ voice }) }];
|
||||
const continuation = compaction.autoContinue(session.history, { voice });
|
||||
if (continuation) reminders.push(continuation);
|
||||
for (const reminder of reminders) {
|
||||
if (!extra.some((m) => m.content === reminder.content)) extra.push(reminder);
|
||||
}
|
||||
tracker.pendingCompactReminder = false;
|
||||
}
|
||||
|
||||
function sidecarMessages(session, tracker, budget) {
|
||||
const extra = [];
|
||||
if (budget && budget.answerOnly) {
|
||||
extra.push({ role: 'user', content: toolBudget.answerNowMessage() });
|
||||
}
|
||||
if (tracker && tracker.pendingCompactReminder) {
|
||||
const voice = !!(budget && budget.voice);
|
||||
extra.push({ role: 'user', content: compaction.compactReminder({ voice }) });
|
||||
const cont = compaction.autoContinue(session.history, { voice });
|
||||
if (cont) extra.push(cont);
|
||||
tracker.pendingCompactReminder = false;
|
||||
}
|
||||
appendCompactReminders(extra, session, tracker, budget);
|
||||
if (planMode.isActive(tracker)) {
|
||||
extra.push({
|
||||
role: 'user',
|
||||
@@ -596,33 +602,34 @@ async function runTurn(ctx) {
|
||||
const toolDefs = budget.answerOnly ? [] : buildToolDefs(session, payload, tracker);
|
||||
ctx.planMode = planMode.isActive(tracker);
|
||||
const ctxSize = loadedCtxSize();
|
||||
const beforeLen = session.history.length;
|
||||
// Generate one-shot reminders once; usage estimation must not consume
|
||||
// them before inference. Reserve their space during compaction as well.
|
||||
const turnSidecars = sidecarMessages(session, tracker, budget);
|
||||
const sidecarTokens = compaction.conversationTokens(turnSidecars) + 256;
|
||||
const beforeUsage = compaction.usage(session.history, toolDefs, ctxSize);
|
||||
emitLive(emit, session.id, jobId, Object.assign({ type: 'context' }, beforeUsage));
|
||||
if (compaction.shouldCompact(session.history, toolDefs, ctxSize)) {
|
||||
const useLlm = !budget.voice || compaction.nonSystemCount(session.history) >= 8;
|
||||
if (compaction.shouldCompact(session.history, toolDefs, ctxSize, sidecarTokens)) {
|
||||
emitUpdate(emit, session.id, jobId, {
|
||||
type: 'compaction',
|
||||
status: 'start',
|
||||
method: useLlm ? 'llm' : 'heuristic',
|
||||
method: 'llm',
|
||||
used: beforeUsage.used,
|
||||
limit: beforeUsage.limit,
|
||||
pct: beforeUsage.pct,
|
||||
threshold: beforeUsage.threshold,
|
||||
});
|
||||
const compactOpts = {
|
||||
budgetTokens: compaction.historyBudget(ctxSize, toolDefs, 0),
|
||||
budgetTokens: compaction.historyBudget(ctxSize, toolDefs, 0, sidecarTokens),
|
||||
tools: toolDefs,
|
||||
voice: !!budget.voice,
|
||||
};
|
||||
session.history = useLlm
|
||||
? await compaction.compactWithLlm(
|
||||
session.history,
|
||||
Object.assign({}, compactOpts, {
|
||||
complete: (opts) => engine.complete(Object.assign({}, opts, { desktopVision: false })),
|
||||
})
|
||||
)
|
||||
: compaction.compact(session.history, compactOpts);
|
||||
session.history = await compaction.compactWithLlm(session.history, Object.assign({}, compactOpts, {
|
||||
complete: (opts) => engine.complete(Object.assign({}, opts, {
|
||||
desktopVision: false,
|
||||
timeoutMs: budget.completeTimeoutMs,
|
||||
idleMs: budget.completeIdleMs,
|
||||
})),
|
||||
}));
|
||||
sessions.replaceHistory(session.id, session.history);
|
||||
tracker.pendingCompactReminder = true;
|
||||
if (hostWorkspace) {
|
||||
@@ -636,7 +643,7 @@ async function runTurn(ctx) {
|
||||
emitUpdate(emit, session.id, jobId, {
|
||||
type: 'compaction',
|
||||
status: 'done',
|
||||
method: useLlm ? 'llm' : 'heuristic',
|
||||
method: 'llm',
|
||||
used: afterUsage.used,
|
||||
limit: afterUsage.limit,
|
||||
pct: afterUsage.pct,
|
||||
@@ -644,31 +651,12 @@ async function runTurn(ctx) {
|
||||
threshold: afterUsage.threshold,
|
||||
});
|
||||
emitLive(emit, session.id, jobId, Object.assign({ type: 'context' }, afterUsage));
|
||||
} else {
|
||||
session.history = compaction.compact(session.history, {
|
||||
budgetTokens: compaction.historyBudget(ctxSize, toolDefs, 0),
|
||||
tools: toolDefs,
|
||||
voice: !!budget.voice,
|
||||
});
|
||||
if (session.history.length !== beforeLen) {
|
||||
sessions.replaceHistory(session.id, session.history);
|
||||
const afterUsage = compaction.usage(session.history, toolDefs, ctxSize);
|
||||
emitUpdate(emit, session.id, jobId, {
|
||||
type: 'compaction',
|
||||
status: 'done',
|
||||
method: 'heuristic',
|
||||
used: afterUsage.used,
|
||||
limit: afterUsage.limit,
|
||||
pct: afterUsage.pct,
|
||||
before: beforeUsage.used,
|
||||
threshold: afterUsage.threshold,
|
||||
});
|
||||
emitLive(emit, session.id, jobId, Object.assign({ type: 'context' }, afterUsage));
|
||||
}
|
||||
}
|
||||
if (cancelled()) return endTurn(emit, session, jobId, tracker, { reason: 'cancelled', turns: turn });
|
||||
appendCompactReminders(turnSidecars, session, tracker, budget);
|
||||
|
||||
emitUpdate(emit, session.id, jobId, { type: 'turn', turn });
|
||||
let streamBase = compaction.usage(session.history.concat(sidecarMessages(session, tracker, budget)), toolDefs, ctxSize);
|
||||
let streamBase = compaction.usage(session.history.concat(turnSidecars), toolDefs, ctxSize);
|
||||
emitLive(emit, session.id, jobId, Object.assign({ type: 'context' }, streamBase));
|
||||
let streamChars = 0;
|
||||
function liveUsed() {
|
||||
@@ -680,7 +668,7 @@ async function runTurn(ctx) {
|
||||
}
|
||||
let result;
|
||||
for (let overflowTry = 0; overflowTry < 4; overflowTry++) {
|
||||
const history = session.history.concat(sidecarMessages(session, tracker, budget));
|
||||
const history = session.history.concat(turnSidecars);
|
||||
streamBase = compaction.usage(history, toolDefs, ctxSize);
|
||||
streamChars = 0;
|
||||
try {
|
||||
@@ -724,13 +712,19 @@ async function runTurn(ctx) {
|
||||
threshold: streamBase.threshold,
|
||||
});
|
||||
session.history = compaction.compact(session.history, {
|
||||
budgetTokens: compaction.historyBudget(ctxSize, toolDefs, overflowTry + 1),
|
||||
// The estimate can be lower than the model's tokenizer count.
|
||||
// Every overflow retry must shrink even an apparently small history.
|
||||
budgetTokens: Math.min(
|
||||
compaction.historyBudget(ctxSize, toolDefs, overflowTry + 1, sidecarTokens),
|
||||
Math.max(1, Math.floor(compaction.conversationTokens(session.history) * 0.85))
|
||||
),
|
||||
tools: toolDefs,
|
||||
aggressive: true,
|
||||
voice: !!budget.voice,
|
||||
});
|
||||
sessions.replaceHistory(session.id, session.history);
|
||||
tracker.pendingCompactReminder = true;
|
||||
appendCompactReminders(turnSidecars, session, tracker, budget);
|
||||
emitCompactDone(emit, session, jobId, toolDefs, ctxSize, streamBase, 'overflow');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user