Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(compact): the line after a trimmed turn no longer reads "→ 0"

purple18b6daac0aa7
diff --git a/src/lib/__tests__/auto-compact-wiring.test.ts b/src/lib/__tests__/auto-compact-wiring.test.ts
index c0b73232..ec77d6f9 100644
--- a/src/lib/__tests__/auto-compact-wiring.test.ts
+++ b/src/lib/__tests__/auto-compact-wiring.test.ts
@@ -57,6 +57,7 @@ vi.mock('../compact-run', async (importOriginal) => {
import { maybeAutoCompact, runCompactForConversation } from '../run-compact-command'
import { compactCuts, layerItems } from '../compact-layers'
import { useRunActivityStore } from '../../stores/runActivityStore'
+import { useSendSizeStore } from '../../stores/sendSizeStore'
import { deriveRunStatus } from '../../components/status/run-status'
import { runCompactSummary } from '../compact-run'
import { resolveAgentNumCtxWithConfidence } from '../agent-num-ctx'
@@ -662,6 +663,33 @@ describe('Auto-Kompaktierung, die klappt, sagt es auch', () => {
expect(ids(layerItems(conv.messages, cuts, null))).not.toContain(rec!.upToMessageId)
})
+ it('ein schon gekuerzter Zug macht "nachher" nicht zu 0', async () => {
+ // T1 29.10.: eine Antwort mit 82 500 Zeichen, gesendet nur gekuerzt. Der
+ // Zaehler las 737, die ersetzten Zuege zaehlten roh ueber 20 000, und die
+ // Linie sagte "Compacted, 737 → 0". Nachher stehen mindestens die
+ // Zusammenfassung und die behaltenen Nachrichten.
+ setzeSchwelle(0.5)
+ const id = seedGespraech(MIN_MESSAGES_TO_COMPACT + 4)
+ useChatStore.getState().addMessage(id, { id: 'riesig', role: 'assistant', content: 'y'.repeat(82_500), timestamp: 999 })
+ for (let i = 0; i < 12; i++) {
+ useChatStore.getState().addMessage(id, {
+ id: `kurz${i}`, role: i % 2 === 0 ? 'user' : 'assistant', content: 'z'.repeat(200), timestamp: 1000 + i,
+ })
+ }
+ const n = useChatStore.getState().conversations.find((c) => c.id === id)!.messages.length
+ useSendSizeStore.getState().report(id, { tokens: 737, window: 8192, atMessageCount: n, trimmedResults: 0, savedChars: 0 })
+ zusammenfassen.mockResolvedValue({ ok: true, summary: ZUSAMMENFASSUNG } as never)
+
+ const out = await runCompactForConversation({ conversationId: id, activeModel: MODELL, trigger: 'manual', window: 8192 })
+ expect(out.ok).toBe(true)
+ const rec = out.ok ? out.record : null
+ expect(rec!.tokensBefore).toBeGreaterThan(rec!.contextBefore!)
+ expect(rec!.contextAfter).toBeGreaterThan(0)
+ expect(rec!.contextAfter).toBeGreaterThanOrEqual(rec!.tokensAfter)
+ expect(rec!.contextAfter).toBeLessThanOrEqual(rec!.contextBefore!)
+ useSendSizeStore.getState().clear(id)
+ })
+
it('der manuelle Weg schreibt auch keine Notiz', async () => {
// /compact zeigt sein Ergebnis als dieselbe Linie (der Datensatz).
setzeSchwelle(0.5)
diff --git a/src/lib/run-compact-command.ts b/src/lib/run-compact-command.ts
index f0ffeb49..9222acfa 100644
--- a/src/lib/run-compact-command.ts
+++ b/src/lib/run-compact-command.ts
@@ -267,10 +267,18 @@ export async function runCompactForConversation(opts: {
// The whole payload, for the line in the transcript: what the counter read
// before, less what the summary saves. An estimate both ways, like the
// counter's own number between two real usage reports.
+ //
+ // tokensBefore is the raw size of the replaced turns, and a turn the
+ // mechanical trim already shortened never went out whole, so the saving
+ // can exceed what the counter read. The payload after still carries the
+ // summary and the kept messages: that is its floor, capped at the number
+ // before (T1 29.10.: an 82K-character answer gave "Compacted, 737 → 0").
const contextBefore = contextFillOf(conv).used
if (contextBefore > 0) {
+ const kept = visible.slice(cutAt).reduce((n, m) => n + estimateTokens(m.content) + MESSAGE_OVERHEAD_TOKENS, 0)
+ const floor = Math.min(contextBefore, record.tokensAfter + kept)
record.contextBefore = contextBefore
- record.contextAfter = Math.max(0, contextBefore - Math.max(0, record.tokensBefore - record.tokensAfter))
+ record.contextAfter = Math.max(floor, contextBefore - Math.max(0, record.tokensBefore - record.tokensAfter))
}
// The answer the agent loops have already opened (empty, no step yet) is
// not part of the stand before: what it will say comes after. Mac run