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