fix(chat): preserve fresh compaction state across reconnect history

This commit is contained in:
2026-10-20 22:24:29 +01:00
parent 4969d667b3
commit ba8a981932
6 changed files with 48 additions and 10 deletions
+8 -3
View File
@@ -16,8 +16,9 @@ ${["design/tokens/tokens","base","shell","lists","run-detail","chat","dialogs"].
import { createChatView } from '/src/views/chat.js';
import { newChatThread, applyChatEvent, resumedThread } from '/src/chat.js';
const id='scratch';
const state={view:'chat',chatActiveId:id,chatSessions:[{id,title:'Context compaction',backend:'opencode'}],chatOverrides:{},chatDrafts:{},chatThreads:new Map()};
const rt={state,repo:{},renderContent:()=>paint(),closeDetail:()=>{}};
const state={view:'chat',connection:{kind:'connected'},chatSubscriptions:new Set(),chatActiveId:id,chatSessions:[{id,title:'Context compaction',backend:'opencode'}],chatOverrides:{},chatDrafts:{},chatThreads:new Map()};
const repo={subscribeChat:async()=>{event('running');},chatHistory:async()=>({messages:state.chatThreads.get(id).messages}),chatShells:async()=>[]};
const rt={state,repo,renderContent:()=>paint(),closeDetail:()=>{}};
const view=createChatView(rt);
window.paint=()=>document.querySelector('#content').replaceChildren(view.renderChatConversation());
window.setup=(history)=>{
@@ -27,6 +28,7 @@ state.chatThreads.set(id,{...newChatThread(id),loaded:true,messages});paint();};
window.event=(status)=>{state.chatThreads.set(id,applyChatEvent(state.chatThreads.get(id),{session_id:id,kind:'compaction',compaction:{id:'native-compaction',status}}));paint();};
window.end=()=>{state.chatThreads.set(id,applyChatEvent(state.chatThreads.get(id),{session_id:id,kind:'step',step:{status:'ended'}}));paint();};
window.drop=()=>{state.chatThreads.set(id,resumedThread(state.chatThreads.get(id)));paint();};
window.reconnect=()=>{state.chatSubscriptions.clear();return view.resyncOpenChat();};
window.messageCount=()=>state.chatThreads.get(id).messages.length;
setup(true);
</script>`;
@@ -64,5 +66,8 @@ try {
await page.evaluate("setup(true);event('running');drop()");
assert.equal(await page.evaluate("document.querySelectorAll('.chat-compaction-status').length"),0);
await page.screenshot(resolve(out,"compaction-clears-on-disconnect.png"));
console.log("PASS: compaction is visible before the first answer and alongside reasoning/tools, clears on completion/disconnect, and never creates a fake message or ends the turn.");
await page.evaluate("reconnect()");
assert.equal(await page.evaluate("document.querySelector('.chat-compaction-status')?.textContent"),"Compacting context…");
await page.screenshot(resolve(out,"compaction-replay-survives-history-refresh.png"));
console.log("PASS: native compaction is visible before/alongside answers, clears on completion/disconnect, survives reconnect history refresh, and creates no fake message or premature turn completion.");
} finally {await browser?.close();await new Promise(r=>server.close(r));}
+14
View File
@@ -68,3 +68,17 @@ test("authoritative history cannot erase fresh compaction replayed after reconne
assert.deepEqual(synced.messages,[{id:"native-history",role:"user",parts:[]}]);
assert.equal(historyResync(reset,{messages:[]},{preserveActivity:true}).compaction,null);
});
test("native turn identity protects compaction and allows the reattached turn to settle", () => {
const original={...newChatThread("session-1"),running:true,nativeTurnId:"new-turn"};
const old={...compact("running"),native_turn_id:"old-turn"};
assert.equal(applyChatEvent(original,old,100),original);
const reset=resumedThread(original);
assert.equal(reset.nativeTurnId,"");
const replay=applyChatEvent(reset,{...compact("running"),native_turn_id:"current-turn"},200);
assert.equal(replay.nativeTurnId,"current-turn");
assert.equal(applyChatEvent(replay,{...compact("completed"),native_turn_id:"old-turn"},300),replay);
const ended=applyChatEvent(replay,{kind:"step",step:{status:"ended",turn_id:"current-turn"}},400);
assert.equal(ended.running,false);
assert.equal(ended.compaction,null);
});