From 2772e0723cfc7898443320515e165a625de1db46 Mon Sep 17 00:00:00 2001 From: Adam Malczewski Date: Mon, 22 Jun 2026 01:31:29 +0900 Subject: feat(compaction): conversation compacting + auto-compact threshold MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Consume the compaction handoff (wire@0.11.0, transport-contract@0.15.0). Re-pinned file: deps + re-mirrored .dispatch/*.reference.md. - New 'Compaction' sidebar view (CompactionView.svelte): - 'Compact now' button → POST /conversations/:id/compact (loading indicator + result: 'N messages summarized, M kept') - Auto-compact threshold number input → GET/PUT /conversations/:id/compact-threshold (0 = disabled, default 350000) - Re-mounts per conversation via {#key} - App store: compactNow() + compactThreshold reactive state + setCompactThreshold(), seeded on focus change (like reasoning-effort + cwd) - conversation.compacted WS handler: reloads the SAME conversation's history (ID unchanged — old history forked to an archive, not a tab switch) - WS adapter parses newConversationId field on ConversationCompactedMessage - conformance guards + tests cover the new type 686 tests green. --- src/adapters/ws/logic.ts | 2 + src/app/App.svelte | 49 ++++++++- src/app/store.svelte.ts | 125 ++++++++++++++++++++++- src/core/wire/conformance.test.ts | 1 + src/features/chat/index.ts | 2 + src/features/chat/ui/CompactionView.svelte | 153 +++++++++++++++++++++++++++++ 6 files changed, 328 insertions(+), 4 deletions(-) create mode 100644 src/features/chat/ui/CompactionView.svelte (limited to 'src') diff --git a/src/adapters/ws/logic.ts b/src/adapters/ws/logic.ts index 53955f8..b11c5c4 100644 --- a/src/adapters/ws/logic.ts +++ b/src/adapters/ws/logic.ts @@ -136,11 +136,13 @@ export function parseServerMessage(data: string): WsServerMessage | null { } case "conversation.compacted": { if (typeof parsed.conversationId !== "string") return null; + if (typeof parsed.newConversationId !== "string") return null; if (typeof parsed.messagesSummarized !== "number") return null; if (typeof parsed.messagesKept !== "number") return null; const msg: ConversationCompactedMessage = { type: "conversation.compacted", conversationId: parsed.conversationId, + newConversationId: parsed.newConversationId, messagesSummarized: parsed.messagesSummarized, messagesKept: parsed.messagesKept, }; diff --git a/src/app/App.svelte b/src/app/App.svelte index e065759..350db49 100644 --- a/src/app/App.svelte +++ b/src/app/App.svelte @@ -10,11 +10,14 @@ } from "../features/cache-warming"; import { ChatView, + CompactionView, Composer, manifest as chatManifest, ModelSelector, ReasoningEffortSelector, + type CompactNowResult, type ReasoningEffortSaveResult, + type SaveCompactThresholdResult, } from "../features/chat"; import { manifest as conversationCacheManifest } from "../features/conversation-cache"; import { manifest as markdownManifest } from "../features/markdown"; @@ -65,11 +68,20 @@ { id: "extensions", label: "Extensions" }, { id: "cache-warming", label: "Cache Warming" }, { id: "tasks", label: "Tasks" }, + { id: "compaction", label: "Compaction" }, { id: "settings", label: "Settings" }, ] as const; - // Default sidebar layout: Model, Language Servers, Extensions, Cache Warming, Tasks, Settings. - const initialViews = ["model", "lsp", "extensions", "cache-warming", "tasks", "settings"] as const; + // Default sidebar layout: Model, Language Servers, Extensions, Cache Warming, Tasks, Compaction, Settings. + const initialViews = [ + "model", + "lsp", + "extensions", + "cache-warming", + "tasks", + "compaction", + "settings", + ] as const; // Frontend module list for the "Loaded Modules" view, AGGREGATED from each // feature's public `manifest` export so it can't drift from what's actually @@ -211,6 +223,29 @@ : { ok: false, error: result.error }; } + // Adapt the store's compact result to the compaction view's port. + async function compactNow(): Promise { + const result = await store.compactNow(); + if (result === null) return null; + return result.ok + ? { + ok: true, + messagesSummarized: result.response.messagesSummarized, + messagesKept: result.response.messagesKept, + } + : { ok: false, error: result.error }; + } + + async function saveCompactThreshold( + threshold: number, + ): Promise { + const result = await store.setCompactThreshold(threshold); + if (result === null) return null; + return result.ok + ? { ok: true, threshold: result.threshold } + : { ok: false, error: result.error }; + } + // Adapt the store's chat-limit result to the settings feature's port. On a // raise the active chat refills (prepends older history); preserve the // reader's viewport over the prepend (the manual analogue of CSS scroll @@ -426,6 +461,16 @@

No tasks yet.

{/if} {/key} + {:else if kind === "compaction"} + + {#key store.currentConversationId} + + {/key} {:else if kind === "settings"} diff --git a/src/app/store.svelte.ts b/src/app/store.svelte.ts index 6fd8e5e..1359fcd 100644 --- a/src/app/store.svelte.ts +++ b/src/app/store.svelte.ts @@ -1,6 +1,8 @@ import type { ChatDeltaMessage, ChatErrorMessage, + CompactResponse, + CompactThresholdResponse, ConversationCompactedMessage, ConversationHistoryResponse, ConversationListResponse, @@ -12,6 +14,7 @@ import type { ModelsResponse, ReasoningEffort, ReasoningEffortResponse, + SetCompactThresholdRequest, SetCwdRequest, SetReasoningEffortRequest, WarmRequest, @@ -65,6 +68,16 @@ export type ReasoningEffortResult = | { readonly ok: true; readonly reasoningEffort: ReasoningEffort } | { readonly ok: false; readonly error: string }; +/** Outcome of `POST /conversations/:id/compact` (manual compaction). */ +export type CompactResult = + | { readonly ok: true; readonly response: CompactResponse } + | { readonly ok: false; readonly error: string }; + +/** Outcome of `PUT /conversations/:id/compact-threshold`. */ +export type CompactThresholdResult = + | { readonly ok: true; readonly threshold: number } + | { readonly ok: false; readonly error: string }; + /** Outcome of persisting a chat-limit setting (localStorage; FE-local). */ export type ChatLimitResult = | { readonly ok: true; readonly chatLimit: number } @@ -122,6 +135,24 @@ export interface AppStore { * Takes effect from the NEXT turn; resolution stays server-owned. */ setReasoningEffort(level: ReasoningEffort): Promise; + /** + * Manually trigger conversation compaction (`POST /conversations/:id/compact`). + * Summarizes old messages + retains the most recent N. Returns null when no + * conversation is focused (a draft has nothing to compact). + */ + compactNow(keepLastN?: number): Promise; + /** + * The workspace conversation's auto-compact threshold (tokens). `0` = disabled + * (manual only); a positive number = auto-compact triggers when the last + * turn's input tokens exceed it. Seeded from the backend on focus change. + */ + readonly compactThreshold: number | null; + /** + * Persist the workspace conversation's auto-compact threshold + * (`PUT /conversations/:id/compact-threshold`). `0` disables; any positive + * number enables. Works for a draft too (its id survives promotion). + */ + setCompactThreshold(threshold: number): Promise; /** * Fetch the workspace conversation's language-server status (`GET /conversations/:id/lsp`). * The backend lazily spawns servers, so this may take a moment on the first call for a cwd. @@ -317,6 +348,26 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { } } + // The workspace conversation's auto-compact threshold. Seeded from the + // backend on focus change; null = not yet fetched. 0 = disabled. + let compactThreshold = $state(null); + + /** Refetch the workspace conversation's compact threshold (works for a draft too). */ + async function refreshCompactThreshold(): Promise { + const id = workspaceConversationId(); + compactThreshold = null; + try { + const res = await fetchImpl( + `${httpBase}/conversations/${encodeURIComponent(id)}/compact-threshold`, + ); + if (!res.ok) return; + const data = (await res.json()) as CompactThresholdResponse; + if (workspaceConversationId() === id) compactThreshold = data.threshold; + } catch { + // Non-fatal: a threshold fetch failure just leaves null. + } + } + function getActiveChat(): ChatStore { const activeId = tabsStore.activeConversationId; if (activeId === null) { @@ -481,6 +532,7 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { syncSubscriptions(); void refreshCwd(); void refreshReasoningEffort(); + void refreshCompactThreshold(); } // Conversation lifecycle status (backend-owned, pushed via WS + @@ -562,8 +614,9 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { } }, onConversationCompacted(msg: ConversationCompactedMessage): void { - // The conversation's history was summarized — reload it from the server. - // Dispose the old store (stale cache) + create a fresh one + load. + // Compaction keeps the conversation ID — the old full history is forked + // to an archive (newConversationId). Just reload the same conversation's + // history (dispose stale store + cache + re-fetch). const cid = msg.conversationId; const wasActive = tabsStore.activeConversationId === cid; const store = chatStores.get(cid); @@ -650,6 +703,7 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { refreshActiveChat(); void refreshCwd(); void refreshReasoningEffort(); + void refreshCompactThreshold(); // Fetch the authoritative open-conversation list from the backend (cross- // device tab sync). Merges with the localStorage-restored tabs: opens new @@ -692,6 +746,9 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { get reasoningEffort(): ReasoningEffort | null { return reasoningEffort; }, + get compactThreshold(): number | null { + return compactThreshold; + }, get chatLimit(): number { return chatLimit; }, @@ -730,6 +787,7 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { syncSubscriptions(); void refreshCwd(); void refreshReasoningEffort(); + void refreshCompactThreshold(); // Now send on the promoted store chatStores.get(conversationId)?.send(text); } else { @@ -766,6 +824,7 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { syncSubscriptions(); void refreshCwd(); void refreshReasoningEffort(); + void refreshCompactThreshold(); }, selectTab(conversationId: string): void { @@ -778,6 +837,7 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { syncSubscriptions(); void refreshCwd(); void refreshReasoningEffort(); + void refreshCompactThreshold(); }, closeTab(conversationId: string): void { @@ -874,6 +934,67 @@ export function createAppStore(opts?: CreateAppStoreOptions): AppStore { } }, + async compactNow(keepLastN?: number): Promise { + const conversationId = tabsStore.activeConversationId; + if (conversationId === null) return null; + const body: Record = {}; + if (keepLastN !== undefined) body.keepLastN = keepLastN; + try { + const res = await fetchImpl( + `${httpBase}/conversations/${encodeURIComponent(conversationId)}/compact`, + { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }, + ); + if (!res.ok) { + const errBody = (await res.json().catch(() => null)) as { error?: string } | null; + return { + ok: false, + error: errBody?.error ?? `Compact failed (HTTP ${res.status})`, + }; + } + const data = (await res.json()) as CompactResponse; + return { ok: true, response: data }; + } catch (err) { + return { + ok: false, + error: err instanceof Error ? err.message : "Compact request failed", + }; + } + }, + + async setCompactThreshold(threshold: number): Promise { + const id = workspaceConversationId(); + const body: SetCompactThresholdRequest = { threshold }; + try { + const res = await fetchImpl( + `${httpBase}/conversations/${encodeURIComponent(id)}/compact-threshold`, + { + method: "PUT", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }, + ); + if (!res.ok) { + const errBody = (await res.json().catch(() => null)) as { error?: string } | null; + return { + ok: false, + error: errBody?.error ?? `Set compact threshold failed (HTTP ${res.status})`, + }; + } + const data = (await res.json()) as CompactThresholdResponse; + if (workspaceConversationId() === id) compactThreshold = data.threshold; + return { ok: true, threshold: data.threshold }; + } catch (err) { + return { + ok: false, + error: err instanceof Error ? err.message : "Set compact threshold request failed", + }; + } + }, + async setChatLimit(limit: number): Promise { const next = normalizeChatLimit(limit); chatLimitStore.save(next); diff --git a/src/core/wire/conformance.test.ts b/src/core/wire/conformance.test.ts index 58cba3a..c50cbf4 100644 --- a/src/core/wire/conformance.test.ts +++ b/src/core/wire/conformance.test.ts @@ -148,6 +148,7 @@ describe("classifies every WsServerMessage type", () => { { type: "conversation.compacted" as const, conversationId: "c1", + newConversationId: "c2", messagesSummarized: 10, messagesKept: 5, }, diff --git a/src/features/chat/index.ts b/src/features/chat/index.ts index 9b94392..9c65cd4 100644 --- a/src/features/chat/index.ts +++ b/src/features/chat/index.ts @@ -17,6 +17,8 @@ export { export type { ChatStore, ChatStoreDependencies } from "./store.svelte"; export { createChatStore } from "./store.svelte"; export { default as ChatView } from "./ui/ChatView.svelte"; +export type { CompactNowResult, SaveCompactThresholdResult } from "./ui/CompactionView.svelte"; +export { default as CompactionView } from "./ui/CompactionView.svelte"; export { default as Composer } from "./ui/Composer.svelte"; export { default as ModelSelector } from "./ui/ModelSelector.svelte"; export { default as ReasoningEffortSelector } from "./ui/ReasoningEffortSelector.svelte"; diff --git a/src/features/chat/ui/CompactionView.svelte b/src/features/chat/ui/CompactionView.svelte new file mode 100644 index 0000000..ce2a0a0 --- /dev/null +++ b/src/features/chat/ui/CompactionView.svelte @@ -0,0 +1,153 @@ + + +
+ +
+ Manual compaction + + {#if !canCompact} +

Open or start a conversation to compact its history.

+ {:else if compactError} +

{compactError}

+ {:else if compactResult} +

+ Compacted — {compactResult.summarized} messages summarized, {compactResult.kept} kept. +

+ {:else} +

+ Summarizes old messages into a system summary + retains the most recent messages. +

+ {/if} +
+ + +
+ Auto-compact threshold +
+ + tokens + {#if savingThreshold} + + {/if} +
+

+ Current: {thresholdLabel} +
+ 0 disables auto-compact. Default is {DEFAULT_THRESHOLD.toLocaleString("en-US")}. +

+ {#if thresholdError} +

{thresholdError}

+ {:else if thresholdSaved} +

Saved.

+ {/if} +
+
-- cgit v1.2.3