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
+11 -4
View File
@@ -1219,7 +1219,7 @@ export const EVENT_RESYNC = "resync";
/** ChatStep.Status values. `ended` is the one status that settles a turn. */
export const STEP_ENDED = "ended";
/** The three things an in-flight turn can be doing (ChatFormat.kt TurnPhase). */
/** Native activity phases; compaction extends the ordinary turn phases. */
export const TURN_PHASE_THINKING = "thinking";
export const TURN_PHASE_TOOL = "tool";
export const TURN_PHASE_WAITING = "waiting";
@@ -1397,6 +1397,7 @@ export function newChatThread(sessionId = "") {
loading: false,
error: "",
running: false,
nativeTurnId: "",
runningSince: 0,
compaction: null,
toolStartedAt: {},
@@ -1432,6 +1433,7 @@ export function settledThread(thread) {
return {
...(thread ?? {}),
running: false,
nativeTurnId: "",
runningSince: 0,
compaction: null,
toolStartedAt: {},
@@ -1607,7 +1609,8 @@ export function applyChatForm(thread, form) {
/**
* Applies one pushed `chat_event` frame to a conversation and returns the next
* thread. Any frame clears `queued` (the turn moved on); `user` echoes another
* thread. Turn frames clear `queued` (compaction does not admit a queued prompt);
* `user` echoes another
* client's prompt once (never this client's own), `text`/`reasoning` append to the
* part their kind started, `tool` merges field by field, `step` drives the running
* flag, and `shell` folds one background process into the retained set without
@@ -1654,8 +1657,11 @@ function applyChatCompaction(thread, frame, now) {
if (current.sessionId && frame.session_id !== current.sessionId) return current;
const incoming = frame.compaction;
if (!incoming || !["running", "completed", "failed"].includes(incoming.status)) return current;
const nativeTurnId = str(frame.native_turn_id);
if (nativeTurnId && current.nativeTurnId && nativeTurnId !== current.nativeTurnId) return current;
if (incoming.status === "running") {
return setThreadRunning({ ...current, compaction: { id: str(incoming.id), status: "running" } }, true, now);
return setThreadRunning({ ...current, nativeTurnId: nativeTurnId || current.nativeTurnId || "",
compaction: { id: str(incoming.id), status: "running" } }, true, now);
}
if (!current.compaction || (incoming.id && current.compaction.id && incoming.id !== current.compaction.id)) return current;
// A compacted context can immediately continue the same model turn. Only a
@@ -1972,7 +1978,8 @@ export function composerSubmit(thread, selected = 0) {
/**
* The queued flag after one pushed frame for the session: any frame means the
* turn the prompt was waiting behind has moved on, so the queued notice clears
* (mirrors ChatStore.event). A null frame leaves the flag untouched.
* (mirrors ChatStore.event). Null/compaction frames leave it untouched: context
* maintenance alone does not admit the queued prompt.
*/
export function queuedAfterFrame(queued, frame) {
if (frame == null || frame.kind === EVENT_COMPACTION) return queued === true;
+6 -2
View File
@@ -1025,7 +1025,7 @@ async function resyncOpenChat() {
const page = await repo.chatHistory(id);
state.chatThreads.set(id, historyResync(state.chatThreads.get(id), page, { preserveActivity: true }));
} catch {
// The history refresh failed; the settled thread stands and the badge is honest.
// Keep fresh replayed activity if any; otherwise the settled reset stands.
}
await loadChatShells(id);
renderContent();
@@ -1382,6 +1382,9 @@ function refreshChatComposerState(sessionId) {
function onChatFrame(message) {
const frame = message?.chat_event;
if (!frame || !frame.session_id) return;
// onChatEvent already folded this session activity with native identity
// checks and refreshed the composer. Do not reapply an ignored stale start.
if (frame.kind === "compaction") return;
rt.touchView?.("chat");
const sessionId = frame.session_id;
const thread = state.chatThreads.get(sessionId);
@@ -4330,7 +4333,8 @@ function updateChatIndicator(now) {
}
const phase = [...document.querySelectorAll("#chatTranscript .chat-activity-phase")].at(-1);
if (phase) {
phase.textContent = turnPhaseLabel(threadPhase(thread, now) ?? TURN_PHASE_WAITING);
const label = turnPhaseLabel(threadPhase(thread, now) ?? TURN_PHASE_WAITING);
if (phase.textContent !== label) phase.textContent = label;
}
}