Files
gnome-jarvis/vendor/agent-harness/agent/compaction.js
T
2026-09-11 13:41:22 -04:00

274 lines
8.6 KiB
JavaScript

/**
* Context compaction: heuristic fallback + optional one-shot LLM summary.
*/
const truncate = require('./truncate.js');
const CHAR_PER_TOKEN = 3;
const THRESHOLD = 0.68;
const MIN_SUMMARY = 80;
const COMPACT_PROMPT =
'Summarize this coding-agent conversation. Use exactly these sections:\n' +
'1. Goal\n' +
'2. Done\n' +
'3. Files / decisions\n' +
'4. Open work\n' +
'5. Next action\n' +
'Be specific (paths, names, errors). Do not say the conversation was compacted.';
function contentChars(content) {
if (content == null) return 0;
if (typeof content === 'string') return content.length;
if (Array.isArray(content)) {
let n = 0;
for (let i = 0; i < content.length; i++) {
const p = content[i];
if (p == null) continue;
if (typeof p === 'string') n += p.length;
else if (p.text) n += String(p.text).length;
else if (p.type === 'image_url' || p.image_url) n += 1600;
else n += JSON.stringify(p).length;
}
return n;
}
return JSON.stringify(content).length;
}
function messageChars(m) {
if (!m) return 0;
let n = contentChars(m.content);
if (m.tool_calls) n += JSON.stringify(m.tool_calls).length;
if (m.name) n += String(m.name).length;
return n + 8;
}
function estimateTokens(messages, tools) {
let n = 0;
for (const m of messages || []) n += messageChars(m);
n += JSON.stringify(tools || []).length;
return Math.ceil(n / CHAR_PER_TOKEN);
}
function historyBudget(ctxSize, tools, attempt) {
const cap = ctxSize > 0 ? Number(ctxSize) : 8192;
const toolTok = Math.ceil(JSON.stringify(tools || []).length / CHAR_PER_TOKEN);
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);
}
function isOverflowError(err) {
const s = String((err && err.message) || err || '');
return /context window|context.?length|too many tokens|maximum context|prompt too (?:long|large)|prompt exceeds|ContextOverflow|reduce the prompt size/i.test(
s
);
}
function usage(messages, tools, ctxSize) {
return snapshot(estimateTokens(messages, tools), ctxSize);
}
function snapshot(used, ctxSize) {
const limit = ctxSize > 0 ? Number(ctxSize) : 8192;
const n = Math.max(0, Math.round(Number(used) || 0));
const pct = limit ? Math.min(100, Math.round((n / limit) * 1000) / 10) : 0;
return {
used: n,
limit,
pct,
threshold: Math.round(THRESHOLD * 100),
over: n > Math.floor(limit * THRESHOLD),
};
}
function isDegenerate(summary) {
const s = String(summary || '').trim();
if (s.length < MIN_SUMMARY) return true;
if (/conversation compacted/i.test(s)) return true;
if (/^\s*\[earlier conversation compacted\]\s*$/i.test(s)) return true;
return false;
}
function isRealUser(m) {
if (!m || m.role !== 'user') return false;
const c = String(m.content || '');
if (c.indexOf('<system-reminder>') >= 0) return false;
if (c.indexOf('[conversation summary]') >= 0) return false;
if (c.indexOf('[memory]') === 0) return false;
if (c.indexOf('[git status]') === 0) return false;
return true;
}
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);
}
function truncateMsg(m, maxChars) {
if (!m) return m;
const copy = Object.assign({}, m);
const c = copy.content;
if (typeof c === 'string' && c.length > maxChars) {
copy.content = truncate.truncateWithMarker(c, maxChars);
} else if (Array.isArray(c)) {
copy.content = c.map((part) => {
if (!part || typeof part !== 'object') return part;
if (typeof part.text === 'string' && part.text.length > maxChars) {
return Object.assign({}, part, { text: truncate.truncateWithMarker(part.text, maxChars) });
}
return part;
});
}
return copy;
}
function lastRealUserIndex(list) {
for (let i = list.length - 1; i >= 0; i--) {
if (isRealUser(list[i])) return i;
}
return -1;
}
function heuristicCompact(messages, opts) {
opts = opts || {};
const budget = opts.budgetTokens || 6000;
const aggressive = !!opts.aggressive;
let keep = messages.slice();
while (estimateTokens(keep, opts.tools) > 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) {
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) {
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 (estimateTokens(keep, opts.tools) > budget && keep.length > 3) {
const dropAt = keep.findIndex((m, i) => i > 1 && !isRealUser(m));
if (dropAt < 0) break;
keep.splice(dropAt, 1);
}
return keep;
}
function compact(messages, opts) {
return heuristicCompact(messages, opts);
}
function transcript(messages) {
return (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 : '');
})
.join('\n\n');
}
function autoContinue(messages) {
const list = messages || [];
const last = list[list.length - 1];
if (!last) return null;
if (last.role === 'tool' || last.role === 'assistant') {
return {
role: 'user',
content:
'<system-reminder>\nContinue the work from the summary. Do not wait for a new user request.\n</system-reminder>',
};
}
return null;
}
function compactReminder() {
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' +
'</system-reminder>'
);
}
async function compactWithLlm(messages, opts) {
opts = opts || {};
const fallback = () => heuristicCompact(messages, opts);
const complete = opts.complete;
if (typeof complete !== 'function') return fallback();
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);
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 },
],
tools: [],
});
const summary = result && result.text;
if (isDegenerate(summary)) return fallback();
return rebuildHistory(messages, summary);
} catch (_) {
return fallback();
}
}
module.exports = {
CHAR_PER_TOKEN,
THRESHOLD,
MIN_SUMMARY,
COMPACT_PROMPT,
estimateTokens,
historyBudget,
shouldCompact,
isOverflowError,
usage,
snapshot,
isDegenerate,
rebuildHistory,
heuristicCompact,
compact,
autoContinue,
compactReminder,
compactWithLlm,
};