@@ -100,8 +100,8 @@ export class ConversationView {
|
||||
this.thinkTab = new St.Button({ label: 'Thinking', style_class: 'jarvis-tab', reactive: true, can_focus: true });
|
||||
this.chatTab.accessible_name = 'Chat tab';
|
||||
this.thinkTab.accessible_name = 'Thinking tab';
|
||||
this._bindChip(this.chatTab, () => this.showTab('chat', { user: true }));
|
||||
this._bindChip(this.thinkTab, () => this.showTab('thinking', { user: true }));
|
||||
this._bindChip(this.chatTab, () => this.showTab('chat'));
|
||||
this._bindChip(this.thinkTab, () => this.showTab('thinking'));
|
||||
this.tabs.add_child(this.chatTab);
|
||||
this.tabs.add_child(this.thinkTab);
|
||||
this.notice = new St.Label({ text: '', style_class: 'jarvis-notice', visible: false, x_expand: true });
|
||||
@@ -174,7 +174,6 @@ export class ConversationView {
|
||||
this.replyFinalized = false;
|
||||
this._state = 'ARMED';
|
||||
this._tab = 'chat';
|
||||
this._userPickedTab = false;
|
||||
this._activity = '';
|
||||
this.reducedMotion = false;
|
||||
this._chatLayoutTimers = [];
|
||||
@@ -204,28 +203,17 @@ export class ConversationView {
|
||||
this.streamingReply = false;
|
||||
this.replyFinalized = false;
|
||||
this._activity = '';
|
||||
this._userPickedTab = false;
|
||||
this.showTab('chat');
|
||||
}
|
||||
setNotice(text) { const body = shortError(text); this.notice.text = body; this.notice.visible = Boolean(body); }
|
||||
_bindFollow(scroll) {
|
||||
const adjustment = scroll?.get_vadjustment?.() || scroll?.vadjustment;
|
||||
if (!adjustment || typeof adjustment.connect !== 'function') return;
|
||||
scroll._jarvisFollow = true;
|
||||
adjustment.connect('notify::value', () => {
|
||||
if (this._pinning) return;
|
||||
scroll._jarvisFollow = this._nearBottom(adjustment);
|
||||
// Layout can change the range after the token handler has returned.
|
||||
for (const signal of ['notify::upper', 'notify::page-size'])
|
||||
adjustment?.connect?.(signal, () => this._pinScroll(scroll));
|
||||
scroll.connect('notify::mapped', () => {
|
||||
if (scroll.mapped) this._followAfterLayout(scroll);
|
||||
});
|
||||
adjustment.connect('notify::upper', () => {
|
||||
if (scroll.visible === false) return;
|
||||
if (scroll._jarvisFollow !== false) this._pinScroll(scroll, { force: true });
|
||||
});
|
||||
}
|
||||
_nearBottom(adjustment) {
|
||||
const upper = Number(adjustment?.upper) || 0;
|
||||
const page = Number(adjustment?.page_size) || 0;
|
||||
const value = Number(adjustment?.value) || 0;
|
||||
return value >= Math.max(0, upper - page) - 32;
|
||||
}
|
||||
_clearLayoutTimers(slot) {
|
||||
for (const id of this[slot] || []) {
|
||||
@@ -239,42 +227,32 @@ export class ConversationView {
|
||||
try { child?.queue_relayout?.(); } catch {}
|
||||
try { child?.get_first_child?.()?.queue_relayout?.(); } catch {}
|
||||
}
|
||||
_pinScroll(scroll, { force = false } = {}) {
|
||||
_pinScroll(scroll) {
|
||||
if (this._destroyed) return;
|
||||
const adjustment = scroll?.get_vadjustment?.() || scroll?.vadjustment;
|
||||
if (!adjustment) return;
|
||||
if (force) scroll._jarvisFollow = true;
|
||||
if (!force && scroll._jarvisFollow === false) return;
|
||||
const upper = Number(adjustment.upper) || 0;
|
||||
const page = Number(adjustment.page_size) || 0;
|
||||
this._pinning = true;
|
||||
try { adjustment.value = Math.max(0, upper - page); } catch {}
|
||||
this._pinning = false;
|
||||
}
|
||||
_followScroll(scroll, { force = false } = {}) {
|
||||
this._pinScroll(scroll, { force });
|
||||
GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE || GLib.PRIORITY_DEFAULT, () => {
|
||||
this._pinScroll(scroll, { force });
|
||||
return GLib.SOURCE_REMOVE;
|
||||
});
|
||||
}
|
||||
_followAfterLayout(scroll, { force = true } = {}) {
|
||||
_followAfterLayout(scroll) {
|
||||
if (this._destroyed) return;
|
||||
const slot = scroll === this.thinkingScroll ? '_thinkLayoutTimers' : '_chatLayoutTimers';
|
||||
this._relayoutPane(scroll);
|
||||
this._followScroll(scroll, { force });
|
||||
this._clearLayoutTimers(slot);
|
||||
this[slot] = [0, 50].map((delay) => GLib.timeout_add(GLib.PRIORITY_DEFAULT, delay, () => {
|
||||
this._pinScroll(scroll, { force });
|
||||
this._pinScroll(scroll);
|
||||
// Coalesce bursts without postponing the follow on every token. Track the
|
||||
// source until it runs so teardown never leaves callbacks on dead actors.
|
||||
if (this[slot].length) return;
|
||||
this[slot] = [GLib.timeout_add(GLib.PRIORITY_DEFAULT, 0, () => {
|
||||
this[slot] = [];
|
||||
this._pinScroll(scroll);
|
||||
return GLib.SOURCE_REMOVE;
|
||||
}));
|
||||
})];
|
||||
}
|
||||
_followConversation() { this._followScroll(this.scroll); }
|
||||
_followThinking() { this._followScroll(this.thinkingScroll); }
|
||||
followActive() {
|
||||
const scroll = this._tab === 'thinking' ? this.thinkingScroll : this.scroll;
|
||||
this._followAfterLayout(scroll, { force: true });
|
||||
this._followAfterLayout(this._tab === 'thinking' ? this.thinkingScroll : this.scroll);
|
||||
}
|
||||
showTab(name, { user = false } = {}) {
|
||||
if (user) this._userPickedTab = true;
|
||||
showTab(name) {
|
||||
this._tab = name === 'thinking' ? 'thinking' : 'chat';
|
||||
this._applyTab();
|
||||
}
|
||||
@@ -300,8 +278,7 @@ export class ConversationView {
|
||||
if (typeof this.transcript.remove_child === 'function') this.transcript.remove_child(first);
|
||||
else first.destroy();
|
||||
}
|
||||
if (this._tab === 'chat') this._followAfterLayout(this.scroll, { force: false });
|
||||
else this._followConversation();
|
||||
this.showTab('chat');
|
||||
return row;
|
||||
}
|
||||
token(text) {
|
||||
@@ -316,19 +293,16 @@ export class ConversationView {
|
||||
}
|
||||
row.text = `${row.text}${chunk}`;
|
||||
row.accessible_name = `Jarvis: ${row.text}`;
|
||||
if (this._tab === 'chat') this._followAfterLayout(this.scroll, { force: false });
|
||||
else this._followConversation();
|
||||
this.showTab('chat');
|
||||
}
|
||||
updateThinking(text) {
|
||||
const chunk = safeText(text);
|
||||
if (!chunk) return;
|
||||
this.thinking.text = `${this.thinking.text || ''}${chunk}`;
|
||||
if (this._state === 'THINKING' && !this._userPickedTab) this.showTab('thinking');
|
||||
if (this._tab === 'thinking') this._followAfterLayout(this.thinkingScroll, { force: false });
|
||||
else this._followThinking();
|
||||
this.showTab('thinking');
|
||||
}
|
||||
toggleThinking() { this.showTab(this._tab === 'thinking' ? 'chat' : 'thinking', { user: true }); }
|
||||
finishThinking() { if (this._state !== 'THINKING') this.showTab('chat'); }
|
||||
toggleThinking() { this.showTab(this._tab === 'thinking' ? 'chat' : 'thinking'); }
|
||||
finishThinking() { this.showTab('chat'); }
|
||||
addToolCall(json) {
|
||||
this.streamingReply = false;
|
||||
this.replyFinalized = false;
|
||||
@@ -381,8 +355,7 @@ export class ConversationView {
|
||||
row.text = body;
|
||||
}
|
||||
row.accessible_name = `Activity: ${body}`;
|
||||
if (this._tab === 'chat') this._followAfterLayout(this.scroll, { force: false });
|
||||
else this._followConversation();
|
||||
this.showTab('chat');
|
||||
return row;
|
||||
}
|
||||
_addToolRow(text) { return this._setToolActivity(text); }
|
||||
@@ -441,17 +414,20 @@ export class ConversationView {
|
||||
}
|
||||
setState(state) {
|
||||
const value = STATES.has(state) ? state : 'ARMED';
|
||||
const changed = this._state !== value;
|
||||
this._state = value;
|
||||
// Polling repeats the coarse daemon state while thinking and answer tokens
|
||||
// alternate. Only a transition may select a pane; content selects it next.
|
||||
if (changed) {
|
||||
if (value === 'LISTENING') {
|
||||
this._userPickedTab = false;
|
||||
this._activity = '';
|
||||
this.showTab('chat');
|
||||
} else if (value === 'THINKING') {
|
||||
if (!this._userPickedTab) this.showTab('thinking');
|
||||
this.showTab('thinking');
|
||||
} else if (value === 'SPEAKING' || value === 'ARMED') {
|
||||
this._userPickedTab = false;
|
||||
this.showTab('chat');
|
||||
}
|
||||
}
|
||||
this._refreshStatusLine();
|
||||
const name = this._assistantName || 'Jarvis';
|
||||
this.title.text = value === 'SLEEPING' ? `${name} · privacy` : name;
|
||||
@@ -489,6 +465,7 @@ export class ConversationView {
|
||||
}
|
||||
endTalk() { this.onTalk?.(false); }
|
||||
destroy() {
|
||||
this._destroyed = true;
|
||||
this._clearLayoutTimers('_chatLayoutTimers');
|
||||
this._clearLayoutTimers('_thinkLayoutTimers');
|
||||
this.root.destroy();
|
||||
|
||||
@@ -122,3 +122,189 @@ test('LLM compact skips a two-turn voice chat', async () => {
|
||||
assert.equal(out.length, hist.length);
|
||||
assert.equal(out[2].content, hist[2].content);
|
||||
});
|
||||
|
||||
function exchange(id, size = 5000) {
|
||||
return [
|
||||
{ role: 'assistant', content: 'Checking the device.', tool_calls: [{ id, type: 'function', function: { name: 'lookup', arguments: '{"device":"nest"}' } }] },
|
||||
{ role: 'tool', tool_call_id: id, name: 'lookup', content: 'Hostname: nest. ' + 'details '.repeat(size) },
|
||||
];
|
||||
}
|
||||
|
||||
function assertValidTools(history) {
|
||||
let calls = new Set();
|
||||
for (const message of history) {
|
||||
if (message.role === 'tool') assert.ok(calls.has(message.tool_call_id), 'tool result must follow its original call');
|
||||
else calls = new Set((message.tool_calls || []).map((call) => call.id));
|
||||
}
|
||||
}
|
||||
|
||||
test('summary rebuild keeps the latest request in order and preserves native tool metadata', () => {
|
||||
const latest = { role: 'user', content: 'What about its memory?' };
|
||||
const tool = exchange('memory', 1);
|
||||
const rebuilt = compaction.rebuildHistory([
|
||||
{ role: 'system', content: 'You are Jarvis.' },
|
||||
{ role: 'user', content: 'Check my computer.' },
|
||||
{ role: 'assistant', content: 'It is named nest.' },
|
||||
latest, ...tool,
|
||||
], 'The user is asking about their computer named nest.');
|
||||
assert.deepEqual(rebuilt.slice(2), [latest, ...tool]);
|
||||
assertValidTools(rebuilt);
|
||||
});
|
||||
|
||||
test('large tool results trigger compaction even in a short conversation', () => {
|
||||
const history = [{ role: 'system', content: 'Jarvis' }, { role: 'user', content: 'Check nest.' }, ...exchange('large')];
|
||||
assert.equal(compaction.shouldCompact(history, [], 8192), true);
|
||||
const compacted = compaction.compact(history, { budgetTokens: 800 });
|
||||
assert.ok(compaction.conversationTokens(compacted) <= 800);
|
||||
assert.ok(compacted.some((m) => m.content === 'Check nest.'));
|
||||
assert.match(JSON.stringify(compacted), /Hostname: nest/);
|
||||
assertValidTools(compacted);
|
||||
});
|
||||
|
||||
test('heuristic fallback retains useful conversational facts and fits its budget', () => {
|
||||
const request = { role: 'user', content: 'And what should I do next?' };
|
||||
const history = [
|
||||
{ role: 'system', content: 'Jarvis' },
|
||||
{ role: 'user', content: 'My computer is nest. Keep answers brief.' },
|
||||
{ role: 'assistant', content: 'We are investigating memory usage. ' + 'details '.repeat(3000) },
|
||||
request,
|
||||
];
|
||||
const compacted = compaction.compact(history, { budgetTokens: 500, aggressive: true });
|
||||
assert.ok(compaction.conversationTokens(compacted) <= 500);
|
||||
assert.match(compacted[1].content, /nest.*Keep answers brief/);
|
||||
assert.equal(compacted.at(-1), request);
|
||||
assert.doesNotMatch(compacted[1].content, /Earlier turns were compacted to fit/);
|
||||
});
|
||||
|
||||
test('LLM summaries are bounded and repeated compaction retains preferences and the latest request', async () => {
|
||||
const request = { role: 'user', content: 'What is the next step?' };
|
||||
let history = [
|
||||
{ role: 'system', content: 'Jarvis' },
|
||||
{ role: 'user', content: 'My computer is nest. Keep answers brief.' },
|
||||
{ role: 'assistant', content: 'We are checking memory.' },
|
||||
request, ...exchange('memory'),
|
||||
];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
history = await compaction.compactWithLlm(history, {
|
||||
voice: true, aggressive: true, budgetTokens: 700,
|
||||
complete: async ({ history: prompt }) => {
|
||||
assert.match(prompt[1].content, /nest/);
|
||||
assert.match(prompt[1].content, /Tool calls:/);
|
||||
return { text: 'The user owns nest and wants brief answers. We are investigating memory usage. ' + 'Additional findings. '.repeat(1000) };
|
||||
},
|
||||
});
|
||||
assert.ok(compaction.conversationTokens(history) <= 700);
|
||||
assert.match(history[1].content, /nest.*brief answers/);
|
||||
assert.ok(history.includes(request));
|
||||
assertValidTools(history);
|
||||
history.push(...exchange('next-' + i));
|
||||
}
|
||||
});
|
||||
|
||||
test('failed summarization falls back without erasing the latest request or tool identities', async () => {
|
||||
const request = { role: 'user', content: 'Please check the computer named nest.' };
|
||||
const history = [{ role: 'system', content: 'Jarvis' }, request, ...exchange('failure')];
|
||||
const compacted = await compaction.compactWithLlm(history, {
|
||||
budgetTokens: 600, complete: async () => { throw new Error('summary failed'); },
|
||||
});
|
||||
assert.ok(compaction.conversationTokens(compacted) <= 600);
|
||||
assert.ok(compacted.includes(request));
|
||||
assertValidTools(compacted);
|
||||
});
|
||||
|
||||
test('compaction never silently clips an oversized latest user request', () => {
|
||||
const request = { role: 'user', content: 'Keep this exact constraint. '.repeat(1000) };
|
||||
const compacted = compaction.compact([{ role: 'system', content: 'Jarvis' }, request], { budgetTokens: 300, aggressive: true });
|
||||
assert.equal(compacted.at(-1), request);
|
||||
});
|
||||
|
||||
test('sidecar context is reserved in the history budget', () => {
|
||||
assert.equal(compaction.historyBudget(8192, [], 0) - compaction.historyBudget(8192, [], 0, 512), 512);
|
||||
});
|
||||
|
||||
for (const overflow of [false, true]) {
|
||||
test(`turn loop persists compaction and sends one-shot reminders through retries (overflow=${overflow})`, async (t) => {
|
||||
const { mkdtempSync, rmSync } = await import('node:fs');
|
||||
const { tmpdir } = await import('node:os');
|
||||
const path = await import('node:path');
|
||||
const engine = require('../vendor/agent-harness/lib/qvac.js');
|
||||
const sessions = require('../vendor/agent-harness/agent/sessions.js');
|
||||
const mcp = require('../vendor/agent-harness/agent/mcp.js');
|
||||
const loop = require('../vendor/agent-harness/agent/loop.js');
|
||||
const root = mkdtempSync(path.join(tmpdir(), 'jarvis-compaction-'));
|
||||
const previousRoot = process.env.AGENT_HARNESS_HOME;
|
||||
process.env.AGENT_HARNESS_HOME = root;
|
||||
const oldComplete = engine.complete;
|
||||
const oldLoaded = engine.getLoaded;
|
||||
const oldNotes = mcp.handshakeReminders;
|
||||
t.after(() => {
|
||||
engine.complete = oldComplete;
|
||||
engine.getLoaded = oldLoaded;
|
||||
mcp.handshakeReminders = oldNotes;
|
||||
if (previousRoot === undefined) delete process.env.AGENT_HARNESS_HOME;
|
||||
else process.env.AGENT_HARNESS_HOME = previousRoot;
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
const model = 'test-model';
|
||||
engine.getLoaded = () => ({ modelId: model, friendlyId: model, ctxSize: 8192 });
|
||||
let noteSent = false;
|
||||
mcp.handshakeReminders = () => {
|
||||
if (noteSent) return [];
|
||||
noteSent = true;
|
||||
return ['MCP test handshake failed: unavailable'];
|
||||
};
|
||||
const meta = sessions.create({ origin: 'jarvis-qvac', cwd: root, hostWorkspace: false, builtinTools: false, model });
|
||||
sessions.replaceHistory(meta.id, [
|
||||
{ role: 'user', content: 'My computer is nest. Please keep answers brief.' },
|
||||
{ role: 'assistant', content: 'Checking its memory. ' + 'history '.repeat(5000) },
|
||||
{ role: 'user', content: 'Remember the hostname.' },
|
||||
{ role: 'assistant', content: 'I will remember nest.' },
|
||||
]);
|
||||
let summaries = 0;
|
||||
let completions = 0;
|
||||
const promptSizes = [];
|
||||
engine.complete = async ({ history }) => {
|
||||
if (history[0].content.startsWith('Reply with the four summary sections')) {
|
||||
summaries++;
|
||||
return { text: 'The computer is named nest. The user wants brief answers. We have checked memory and should continue answering follow-up questions about this computer.' };
|
||||
}
|
||||
completions++;
|
||||
promptSizes.push(compaction.conversationTokens(history));
|
||||
assert.equal(history.filter((m) => /Context was compacted/.test(m.content)).length, 1);
|
||||
assert.equal(history.filter((m) => /MCP test handshake/.test(m.content)).length, 1);
|
||||
assert.ok(history.some((m) => m.content === 'What is its hostname?'));
|
||||
assert.match(JSON.stringify(history), /nest/);
|
||||
assert.ok(compaction.estimateTokens(history, []) < 8192);
|
||||
if (overflow && completions === 1) throw new Error('prompt too long for context window');
|
||||
return { text: 'Its hostname is nest.', toolCalls: [] };
|
||||
};
|
||||
const updates = [];
|
||||
const result = await loop.runTurn({
|
||||
session: sessions.load(meta.id), userText: 'What is its hostname?', jobId: 'context-test',
|
||||
payload: { voice: true, system: 'Jarvis', maxTurns: 1 },
|
||||
emit: (_kind, event) => updates.push(event),
|
||||
});
|
||||
assert.equal(result.text, 'Its hostname is nest.');
|
||||
assert.equal(summaries, 1);
|
||||
assert.equal(completions, overflow ? 2 : 1);
|
||||
if (overflow) assert.ok(promptSizes[1] < promptSizes[0], 'overflow recovery must actually shrink the prompt');
|
||||
assert.ok(updates.some((ev) => ev.type === 'compaction' && ev.status === 'done'));
|
||||
const saved = sessions.load(meta.id).history;
|
||||
assert.match(saved[1].content, /nest/);
|
||||
assert.equal(saved.at(-1).content, 'Its hostname is nest.');
|
||||
assert.ok(compaction.conversationTokens(saved) < 2000);
|
||||
|
||||
// Follow-up requests reload the persisted summary, not the original large history.
|
||||
engine.complete = async ({ history }) => {
|
||||
assert.match(history[1].content, /nest/);
|
||||
assert.equal(history.at(-1).role, 'user');
|
||||
assert.ok(history.some((m) => m.content === 'And what were we checking?'));
|
||||
return { text: 'We were checking memory.', toolCalls: [] };
|
||||
};
|
||||
const followup = await loop.runTurn({
|
||||
session: sessions.load(meta.id), userText: 'And what were we checking?', jobId: 'followup-test',
|
||||
payload: { voice: true, system: 'Jarvis', maxTurns: 1 }, emit() {},
|
||||
});
|
||||
assert.equal(followup.text, 'We were checking memory.');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ function stripModules(source) {
|
||||
function harness() {
|
||||
const timers = new Map();
|
||||
const chrome = [];
|
||||
let nextTimer = 1;
|
||||
class Actor {
|
||||
constructor(props = {}) {
|
||||
if ('hexpand' in props) throw new Error('No property hexpand on StWidget');
|
||||
@@ -27,7 +28,11 @@ function harness() {
|
||||
this.visible = props.visible !== false;
|
||||
this.text = props.text || props.hint_text || '';
|
||||
this.clutter_text = { connect() {}, ellipsize: null };
|
||||
this.vadjustment = { value: 0, upper: 240, page_size: 80 };
|
||||
this.vadjustment = {
|
||||
value: 0, upper: 240, page_size: 80, handlers: {},
|
||||
connect(name, handler) { this.handlers[name] = handler; },
|
||||
emit(name) { this.handlers[name]?.(); },
|
||||
};
|
||||
}
|
||||
get_vadjustment() { return this.vadjustment; }
|
||||
add_child(child) { this.children.push(child); child.get_parent = () => this; }
|
||||
@@ -105,7 +110,7 @@ function harness() {
|
||||
GLib: {
|
||||
PRIORITY_DEFAULT: 0, PRIORITY_DEFAULT_IDLE: 200, SOURCE_REMOVE: false,
|
||||
idle_add(_priority, callback) { callback(); return 1; },
|
||||
timeout_add(_priority, _delay, callback) { const id = timers.size + 1; timers.set(id, callback); return id; },
|
||||
timeout_add(_priority, _delay, callback) { const id = nextTimer++; timers.set(id, callback); return id; },
|
||||
Source: { remove(id) { timers.delete(id); } },
|
||||
},
|
||||
log() {},
|
||||
@@ -492,8 +497,8 @@ test('Chat and Thinking tabs exist and thinking auto-follows', () => {
|
||||
assert.equal(view.scroll.vadjustment.value, 160);
|
||||
view.showTab('chat', { user: true });
|
||||
view.updateThinking('still thinking');
|
||||
assert.equal(view._tab, 'chat');
|
||||
assert.equal(view.thinkingScroll.visible, false);
|
||||
assert.equal(view._tab, 'thinking');
|
||||
assert.equal(view.thinkingScroll.visible, true);
|
||||
assert.match(view.thinking.text, /still thinking/);
|
||||
view.thinkingScroll.vadjustment.value = 0;
|
||||
view.showTab('thinking', { user: true });
|
||||
@@ -541,3 +546,61 @@ test('the Shell helper can capture a screenshot from inside GNOME', () => {
|
||||
assert.match(source, /new Shell\.Screenshot/);
|
||||
assert.match(source, /screenshot\(false, stream\)/);
|
||||
});
|
||||
|
||||
for (const compact of [true, false]) {
|
||||
test(`inference follows alternating content despite polling and manual navigation (${compact})`, () => {
|
||||
const { ConversationView } = harness();
|
||||
const view = new ConversationView({ compact });
|
||||
view.setState('THINKING');
|
||||
for (let i = 0; i < 12; i++) {
|
||||
view.updateThinking(`reason ${i}. `);
|
||||
assert.equal(view._tab, 'thinking');
|
||||
view.token(`answer ${i}. `);
|
||||
assert.equal(view._tab, 'chat');
|
||||
view.setState('THINKING');
|
||||
assert.equal(view._tab, 'chat');
|
||||
view.showTab('thinking', { user: true });
|
||||
view.addToolCall('{"name":"lookup"}');
|
||||
assert.equal(view._tab, 'chat');
|
||||
view.updateThinking('checking. ');
|
||||
view.addToolResult('{"name":"lookup","result":"done"}');
|
||||
assert.equal(view._tab, 'chat');
|
||||
}
|
||||
assert.match(view.thinking.text, /reason 0.*reason 11/);
|
||||
view.updateThinking('finished');
|
||||
view.finalizeReply('Final answer');
|
||||
assert.equal(view._tab, 'chat');
|
||||
assert.match(view.transcript.get_last_child().text, /Final answer/);
|
||||
});
|
||||
|
||||
test(`follow survives delayed layout, scrolling, remapping and teardown (${compact})`, () => {
|
||||
const { ConversationView, timers } = harness();
|
||||
const view = new ConversationView({ compact });
|
||||
view.setState('THINKING');
|
||||
for (const pane of [view.scroll, view.thinkingScroll]) {
|
||||
const adjustment = pane.vadjustment;
|
||||
adjustment.value = 0;
|
||||
adjustment.emit('notify::value');
|
||||
view.token('reply');
|
||||
view.updateThinking('reason');
|
||||
adjustment.upper = 1000;
|
||||
adjustment.emit('notify::upper');
|
||||
assert.equal(adjustment.value, 920);
|
||||
adjustment.page_size = 200;
|
||||
adjustment.emit('notify::page-size');
|
||||
assert.equal(adjustment.value, 800);
|
||||
adjustment.value = 0;
|
||||
pane.mapped = true;
|
||||
pane.handlers['notify::mapped']();
|
||||
assert.equal(adjustment.value, 800);
|
||||
}
|
||||
for (let i = 0; i < 100; i++) view.token(' more');
|
||||
assert.equal(timers.size, 2);
|
||||
for (const [id, callback] of [...timers]) { timers.delete(id); callback(); }
|
||||
assert.equal(timers.size, 0);
|
||||
view.updateThinking('again');
|
||||
view.token('done');
|
||||
view.destroy();
|
||||
assert.equal(timers.size, 0);
|
||||
});
|
||||
}
|
||||
|
||||
+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,27 +643,7 @@ async function runTurn(ctx) {
|
||||
emitUpdate(emit, session.id, jobId, {
|
||||
type: 'compaction',
|
||||
status: 'done',
|
||||
method: useLlm ? 'llm' : '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));
|
||||
} 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',
|
||||
method: 'llm',
|
||||
used: afterUsage.used,
|
||||
limit: afterUsage.limit,
|
||||
pct: afterUsage.pct,
|
||||
@@ -665,10 +652,11 @@ async function runTurn(ctx) {
|
||||
});
|
||||
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