Skip to content

The agent that grows with you

fix(desktop): announce a deferred /compress completion

denis.dresvyanskiy5f50e5fbaa5f
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/compaction-event.test.tsx b/apps/desktop/src/app/session/hooks/use-message-stream/compaction-event.test.tsx
index 3a548e200a8..d0498fd596c 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/compaction-event.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/compaction-event.test.tsx
@@ -3,7 +3,8 @@ import { act, cleanup } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createClientSessionState } from '@/lib/chat-runtime'
-import { $compactingSessions, setSessionCompacting } from '@/store/compaction'
+import { $compactingSessions, markCompressDeferred, setSessionCompacting, takeCompressDeferred } from '@/store/compaction'
+import { $notifications, clearNotifications } from '@/store/notifications'
import { type MessageStreamHarness, renderMessageStream } from './test-harness'
@@ -22,6 +23,8 @@ function emit(type: GatewayEvent['type'], payload: GatewayEvent['payload'] = {})
describe('useMessageStream compaction lifecycle', () => {
beforeEach(() => {
$compactingSessions.set({})
+ clearNotifications()
+ takeCompressDeferred(SID)
})
afterEach(() => {
@@ -109,4 +112,83 @@ describe('useMessageStream compaction lifecycle', () => {
emit('session.info', { running: false })
expect($compactingSessions.get()).toEqual({})
})
+
+ // A deferred /compress (#97948) answers `pending` and returns, so the
+ // handler's summary + success toast never run. The terminal edge is the only
+ // completion the client sees — before this it merely cleared a spinner and
+ // the compression finished in total silence.
+ it('announces a deferred /compress completion on the terminal edge', async () => {
+ const hydrateFromStoredSession = vi.fn(async () => undefined)
+ const states = new Map([[SID, { ...createClientSessionState(), storedSessionId: 'stored-1' }]])
+
+ stream = renderMessageStream(SID, { hydrateFromStoredSession, states })
+ markCompressDeferred(SID)
+
+ await act(async () => {
+ stream.handleEvent({
+ payload: { kind: 'compacted', text: '\u2713 Context compaction complete' },
+ session_id: SID,
+ type: 'status.update'
+ })
+ })
+
+ // Same notice id the compress handler used, so the pending toast is
+ // replaced in place rather than stacked.
+ expect($notifications.get().find(entry => entry.id === `session-compress:${SID}`)).toMatchObject({
+ kind: 'success',
+ message: '\u2713 Context compaction complete'
+ })
+
+ // Appended after the hydrate resolves, or the refresh would drop it.
+ expect(hydrateFromStoredSession).toHaveBeenCalledWith(3, 'stored-1', SID)
+ expect(stream.state(SID).messages.at(-1)).toMatchObject({ role: 'system' })
+ expect(stream.text(SID)).toBe('\u2713 Context compaction complete')
+ })
+
+ it('stays silent on an auto-compaction the user never asked for', async () => {
+ const states = new Map([[SID, { ...createClientSessionState(), storedSessionId: 'stored-1' }]])
+
+ stream = renderMessageStream(SID, { states })
+
+ await act(async () => {
+ stream.handleEvent({ payload: { kind: 'compacting' }, session_id: SID, type: 'status.update' })
+ stream.handleEvent({ payload: { kind: 'compacted' }, session_id: SID, type: 'status.update' })
+ })
+
+ expect($notifications.get()).toEqual([])
+ expect(stream.state(SID).messages).toEqual([])
+ })
+
+ it('announces a deferred compress once, not on every later compaction', async () => {
+ const states = new Map([[SID, { ...createClientSessionState(), storedSessionId: 'stored-1' }]])
+
+ stream = renderMessageStream(SID, { states })
+ markCompressDeferred(SID)
+
+ await act(async () => {
+ stream.handleEvent({ payload: { kind: 'ready' }, session_id: SID, type: 'status.update' })
+ })
+ expect(stream.state(SID).messages).toHaveLength(1)
+
+ clearNotifications()
+ await act(async () => {
+ stream.handleEvent({ payload: { kind: 'compacted' }, session_id: SID, type: 'status.update' })
+ })
+
+ expect($notifications.get()).toEqual([])
+ expect(stream.state(SID).messages).toHaveLength(1)
+ })
+
+ it('falls back to translated copy when the edge carries no text', async () => {
+ const states = new Map([[SID, { ...createClientSessionState(), storedSessionId: 'stored-1' }]])
+
+ stream = renderMessageStream(SID, { states })
+ markCompressDeferred(SID)
+
+ await act(async () => {
+ stream.handleEvent({ payload: { kind: 'ready' }, session_id: SID, type: 'status.update' })
+ })
+
+ expect(stream.text(SID)).toBe('Context compression finished')
+ })
})
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/status.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/status.ts
index 27ca3e5b9ed..19411666b3f 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/status.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/status.ts
@@ -8,7 +8,7 @@ import { errorCardText } from '@/lib/error-surface-copy'
import { isProviderSetupErrorMessage } from '@/lib/provider-setup-errors'
import { type AgentNoticePayload, clearAgentNotice, nativeNoticeInput, showAgentNotice } from '@/store/agent-notices'
import { clearClarifyRequest } from '@/store/clarify'
-import { reconcileSessionCompacting, setSessionCompacting } from '@/store/compaction'
+import { reconcileSessionCompacting, setSessionCompacting, takeCompressDeferred } from '@/store/compaction'
import { refreshBackgroundProcesses } from '@/store/composer-status'
import { applyGoalStatusText } from '@/store/goals'
import { dispatchNativeNotification } from '@/store/native-notifications'
@@ -49,15 +49,61 @@ export function handleStatusEvent(ctx: GatewayEventContext): boolean {
reconcileSessionCompacting(sessionId, 'terminal')
compactedTurnRef.current.delete(sessionId)
+ // That same `pending` reply returned before the compress handler could
+ // render the summary or flip its "still running in the background"
+ // toast, so this edge is the only completion the client ever sees.
+ // Without announcing it a deferred /compress finishes in silence: the
+ // corner toast merely expires and the transcript changes underneath the
+ // user with no acknowledgement it ever ran.
+ const deferredCompress = takeCompressDeferred(sessionId)
+
+ const completionText =
+ coerceGatewayText(payload?.text).trim() || translateNow('notifications.compressDeferredDone')
+
+ if (deferredCompress) {
+ // Reuse the id the compress handler notified under, so this replaces
+ // the pending toast in place instead of stacking a second one.
+ notify({
+ durationMs: 5_000,
+ id: `session-compress:${sessionId}`,
+ kind: 'success',
+ message: completionText
+ })
+ }
+
+ const announceDeferredCompress = () => {
+ if (!deferredCompress) {
+ return
+ }
+
+ flushQueuedDeltas(sessionId)
+ updateSessionState(sessionId, state => ({
+ ...state,
+ messages: [
+ ...state.messages,
+ {
+ id: `compress-complete-${occurredAt ?? Date.now()}`,
+ role: 'system',
+ parts: [textPart(completionText, occurredAt)],
+ timestamp: occurredAt
+ }
+ ]
+ }))
+ }
+
// A compress that finished with no live turn (manual /compress whose
// RPC answered `pending` because the compute host outlived the wait,
// #97948) has no turn-end hydrate to refresh the transcript — the
// summarized bubbles would stay on screen forever. Mid-turn compaction
- // still defers to the turn's own settle path.
+ // still defers to the turn's own settle path. The notice is appended
+ // only after that hydrate resolves: it replaces the transcript wholesale
+ // and would otherwise drop the line we just added.
const state = sessionStateByRuntimeIdRef.current.get(sessionId)
if (isActiveEvent && state && !state.busy && !state.awaitingResponse && !state.streamId) {
- void hydrateFromStoredSession(3, state.storedSessionId, sessionId)
+ void hydrateFromStoredSession(3, state.storedSessionId, sessionId).then(announceDeferredCompress)
+ } else {
+ announceDeferredCompress()
}
} else if (sessionId && payload?.kind === 'process') {
// The gateway's notification poller announces background process
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
index baee82f366e..6cb86bd3a02 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts
@@ -20,6 +20,7 @@ import { isMissingRpcMethod } from '@/lib/gateway-rpc'
import { applyReasoningSlashResult, reasoningSlashParams } from '@/lib/reasoning-slash'
import { setSessionYolo } from '@/lib/yolo-session'
import { openCommandPalettePage } from '@/store/command-palette'
+import { markCompressDeferred } from '@/store/compaction'
import { setComposerDraft } from '@/store/composer'
import { applyGoalStatusText } from '@/store/goals'
import { dismissNotification, notify, notifyError } from '@/store/notifications'
@@ -676,6 +677,11 @@ export function useSlashCommand(deps: SlashCommandDeps) {
// running there; it pushes session.info + a `compacted` status edge
// when the host finishes. Not an error (#97948).
if (result?.status === 'pending') {
+ // Hand the completion off to the status edge: this reply carries
+ // no summary and the host is still working, so nothing below
+ // runs for a deferred compress.
+ markCompressDeferred(sessionId)
+
const pendingMessage = result.message || 'compression still running in the background'
notify({ durationMs: 8_000, id: noticeId, kind: 'info', message: pendingMessage })
diff --git a/apps/desktop/src/i18n/en.ts b/apps/desktop/src/i18n/en.ts
index f7d6d0786be..0277862e8b3 100644
--- a/apps/desktop/src/i18n/en.ts
+++ b/apps/desktop/src/i18n/en.ts
@@ -467,6 +467,7 @@ export const en: Translations = {
details: 'Details',
copyDetail: 'Copy detail',
copyDetailFailed: 'Could not copy notification detail',
+ compressDeferredDone: 'Context compression finished',
backendOutOfDateTitle: 'Backend out of date',
backendOutOfDateMessage:
'Your Hermes backend is older than this desktop build and may not work correctly. Update to align them.',
diff --git a/apps/desktop/src/i18n/types.ts b/apps/desktop/src/i18n/types.ts
index 640a7d80fde..05ad3117d76 100644
--- a/apps/desktop/src/i18n/types.ts
+++ b/apps/desktop/src/i18n/types.ts
@@ -491,6 +491,7 @@ export interface Translations {
dismiss: string
details: string
copyDetail: string
+ compressDeferredDone: string
copyDetailFailed: string
backendOutOfDateTitle: string
backendOutOfDateMessage: string
diff --git a/apps/desktop/src/store/compaction.ts b/apps/desktop/src/store/compaction.ts
index 21da350aeae..d3100958f65 100644
--- a/apps/desktop/src/store/compaction.ts
+++ b/apps/desktop/src/store/compaction.ts
@@ -36,6 +36,22 @@ export function setSessionCompacting(sessionId: string | null | undefined, activ
$compactingSessions.set(next)
}
+// A manual /compress whose RPC answers `pending` (#97948) returns before the
+// compute host is done, so the handler never reaches the branch that renders
+// the summary. Claim the session here and let the `compacted`/`ready` edge
+// announce the completion instead — that edge is the only one the client gets.
+const deferredCompressSessions = new Set<string>()
+
+export function markCompressDeferred(sessionId: string | null | undefined): void {
+ deferredCompressSessions.add(keyFor(sessionId))
+}
+
+/** Consume the claim. True only for the first terminal edge, so a later
+ * auto-compaction on the same session cannot replay the notice. */
+export function takeCompressDeferred(sessionId: string | null | undefined): boolean {
+ return deferredCompressSessions.delete(keyFor(sessionId))
+}
+
/** Clear compaction only when the gateway proves the turn resumed or ended. */
export function reconcileSessionCompacting(
sessionId: string | null | undefined,