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.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(verdichtung) die zweite Zusammenfassung loescht die erste nicht mehr
Davidf3bf94279452
diff --git a/src/hooks/useAgentChat.ts b/src/hooks/useAgentChat.tsindex 70551cb0..62b54904 100644--- a/src/hooks/useAgentChat.ts+++ b/src/hooks/useAgentChat.ts@@ -73,7 +73,7 @@ import { useAgentLoopStore } from '../stores/agentLoopStore'import { beginRun, isRunStopped, stopRun } from '../lib/run-stop'import { buildLoopRecheck, loopPassSaysDone } from '../lib/agent-commands'import { applyStoredCompaction } from '../lib/compact-summary'-import { newestCompaction, maybeAutoCompact } from '../lib/run-compact-command'+import { maybeAutoCompact } from '../lib/run-compact-command'import { generateEmbeddings } from '../api/rag'import { truncateToolResult } from '../lib/truncate-tool-result'import { toolFailureNote } from '../lib/tool-failure-note'@@ -662,7 +662,7 @@ export function useAgentChat() {// Ergebnis stuende ohne seinen Aufruf da — genau der 422-Fall, den der// Waisenschnitt beim Ablegen schon vermeidet. useCodex filtert seit je so.convForPayload.messages.filter((m) => m.role !== 'system' && (m.content.trim() !== '' || m.hidden)),- newestCompaction(convForPayload.compactions),+ convForPayload.compactions,).messages.map((m) => ({role: m.role as 'user' | 'assistant' | 'tool',diff --git a/src/hooks/useChat.ts b/src/hooks/useChat.tsindex bd3f8abf..7b74b70f 100644--- a/src/hooks/useChat.ts+++ b/src/hooks/useChat.ts@@ -20,7 +20,7 @@ import { requestGenerationCancel } from "../api/vram-handoff"import { effectiveContextWindow } from "../lib/context-window"import { useAgentChat } from "./useAgentChat"import { parseAgentCommand, parseLoopSpec } from '../lib/agent-commands'-import { runCompactForConversation, compactOutcomeMessage, newestCompaction, maybeAutoCompact } from '../lib/run-compact-command'+import { runCompactForConversation, compactOutcomeMessage, maybeAutoCompact } from '../lib/run-compact-command'import { applyStoredCompaction } from '../lib/compact-summary'import { planResend } from '../lib/resend-plan'import { isOrphanRun } from '../lib/orphan-run'@@ -728,7 +728,7 @@ export function useChat() {// history goes out, which is the honest answer to a stale record....applyStoredCompaction(convNow.messages.filter((m) => m.role !== 'system' && m.content.trim() !== ''),- newestCompaction(convNow.compactions),+ convNow.compactions,).messages.map((m) => ({role: m.role as 'user' | 'assistant' | 'tool',diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.tsindex 9277aa28..0667f605 100644--- a/src/hooks/useCodex.ts+++ b/src/hooks/useCodex.ts@@ -3,7 +3,7 @@ import { markCannotThink } from '../lib/model-compatibility'import { v4 as uuid } from 'uuid'import { useCodexStore } from '../stores/codexStore'import { useModelStore } from '../stores/modelStore'-import { runCompactForConversation, compactOutcomeMessage, newestCompaction, maybeAutoCompact } from '../lib/run-compact-command'+import { runCompactForConversation, compactOutcomeMessage, maybeAutoCompact } from '../lib/run-compact-command'import { applyStoredCompaction } from '../lib/compact-summary'import { useSettingsStore } from '../stores/settingsStore'import { useChatStore, flushChatPersist } from '../stores/chatStore'@@ -744,7 +744,7 @@ export function useCodex() {// this call sits inside each builder rather than in a shared wrapper....applyStoredCompaction(convForPayload.messages.filter(m => m.role !== 'system' && (m.content.trim() || m.hidden)),- newestCompaction(convForPayload.compactions),+ convForPayload.compactions,).messages.map(m => {const msg: ChatMessage = {diff --git a/src/lib/__tests__/die-zweite-verdichtung-behaelt-die-erste.test.ts b/src/lib/__tests__/die-zweite-verdichtung-behaelt-die-erste.test.tsnew file mode 100644index 00000000..fbe01b4d--- /dev/null+++ b/src/lib/__tests__/die-zweite-verdichtung-behaelt-die-erste.test.ts@@ -0,0 +1,101 @@+/**+ * Was nach der ZWEITEN Verdichtung noch beim Modell ankommt.+ *+ * Der Fall, den 2.6.8 zu schliessen behauptet: ein Chat wird verdichtet, laeuft+ * weiter, wird ein zweites Mal verdichtet. Der Nutzer sieht zwei+ * Verdichtungslinien und nimmt an, dass beide Zusammenfassungen zaehlen.+ *+ * Sie tun es nicht. Die Kette, jede Stelle nachgelesen:+ *+ * 1. `newestCompaction` gab AUSSCHLIESSLICH den letzten Datensatz zurueck+ * (run-compact-command.ts:153-157, `compactions[compactions.length - 1]`).+ * 2. `useChat.ts:729-731` baute die Nutzlast genau daraus.+ * 3. `applyStoredCompaction` sucht den Anker DIESES Datensatzes und ruft+ * `spliceSummary(messages, cut + 1, record.summary)` (compact-summary.ts:594-598).+ * 4. `spliceSummary` verwirft alles vor dem Anker (`body.slice(dropLeading)`,+ * compact-summary.ts:539) und haengt genau die EINE uebergebene+ * Zusammenfassung an.+ * 5. Der zweite Datensatz deckt aber nur `visible.slice(prevIdx + 1, cutAt)`+ * ab (run-compact-command.ts:105-113). Das ist Absicht und im Code+ * begruendet: „Do not re-summarise material an earlier summary already+ * covers: that is the summary-of-a-summary decay this feature is designed+ * to avoid."+ *+ * Punkt 5 ist fuer sich richtig. Zusammen mit Punkt 1 ergibt er aber, dass+ * das Material des ersten Abschnitts nirgends mehr steht: nicht als Nachricht,+ * weil Punkt 4 es wegschneidet, und nicht als Zusammenfassung, weil Punkt 1 nur+ * die zweite mitnimmt.+ *+ * Die vorhandenen Faelle pruefen den SCHNITT. Keiner prueft das UEBERLEBEN von+ * Material aus dem ersten Abschnitt. Genau deshalb ist der Fehler unbemerkt+ * geblieben.+ *+ * BEHOBEN am 05.09.2026: `applyStoredCompaction` nimmt jetzt die ganze Kette+ * statt eines einzelnen Datensatzes und schickt jede Zusammenfassung bis zum+ * Schnitt mit. Dieser Fall war vor dem Fix rot und ist die Wache dagegen.+ *+ * Run: npx vitest run src/lib/__tests__/die-zweite-verdichtung-behaelt-die-erste.test.ts+ */+import { describe, it, expect } from 'vitest'+import { applyStoredCompaction } from '../compact-summary'+import type { CompactionRecord } from '../../types/chat'++/** Eine Nachricht in der Form, die `applyStoredCompaction` braucht. */+const n = (id: string, role: 'user' | 'assistant', content: string) => ({ id, role, content })++/** Ein Datensatz, wie ihn ein echter Verdichtungslauf hinterlaesst. */+const satz = (id: string, summary: string, upToMessageId: string, replaced: number): CompactionRecord => ({+ id, summary, upToMessageId, replaced,+ atMessageCount: 0, tokensBefore: 0, tokensAfter: 0, trigger: 'manual', at: 0,+})++describe('zwei Verdichtungen hintereinander', () => {+ // Der Fakt steht NUR im ersten Abschnitt. Wenn er die zweite Verdichtung+ // ueberlebt, dann ausschliesslich ueber die erste Zusammenfassung.+ const FAKT = 'the deploy key lives in the vault under lu/PROD'++ const verlauf = [+ n('m1', 'user', `Remember this: ${FAKT}`),+ n('m2', 'assistant', 'Noted.'),+ n('m3', 'user', 'Now something else entirely.'),+ n('m4', 'assistant', 'Sure.'),+ n('m5', 'user', 'And a third topic.'),+ n('m6', 'assistant', 'Understood.'),+ n('m7', 'user', 'What was the deploy key again?'),+ ]++ // Erste Verdichtung deckt m1 bis m2 ab und traegt den Fakt.+ const erste = satz('c1', `Earlier: ${FAKT}`, 'm2', 2)+ // Zweite deckt m3 bis m4 ab. Sie kennt den Fakt nicht, und das ist Absicht:+ // run-compact-command.ts schneidet ab prevIdx + 1, um Zusammenfassungen von+ // Zusammenfassungen zu vermeiden.+ const zweite = satz('c2', 'Earlier: two unrelated topics were discussed.', 'm4', 2)++ it('nach EINER Verdichtung steht der Fakt in der Nutzlast', () => {+ // POSITIVKONTROLLE. Ohne sie wuerde der Fall unten auch auf einer Fassung+ // rot, die gar nichts anwendet.+ const raus = applyStoredCompaction(verlauf, [erste])+ const text = raus.messages.map((m) => m.content).join('\n')+ expect(text, 'schon die erste Verdichtung verliert den Fakt').toContain(FAKT)+ })++ it('nach der ZWEITEN Verdichtung steht er immer noch da', () => {+ // DER FALL. Der Nutzer fragt in m7 nach dem Fakt. Das Modell bekommt die+ // Nutzlast, die hier herauskommt, und nichts sonst.+ const raus = applyStoredCompaction(verlauf, [erste, zweite])+ const text = raus.messages.map((m) => m.content).join('\n')+ expect(+ text,+ 'das Material des ersten Abschnitts ist weg: die zweite Zusammenfassung deckt es '+ + 'nicht ab, und die erste wird nicht mitgeschickt',+ ).toContain(FAKT)+ })++ it('und die zweite Zusammenfassung ist ueberhaupt angekommen', () => {+ // Trennt „nichts angewendet" von „das Falsche angewendet". Wird dieser Fall+ // gruen und der obige rot, ist genau der beschriebene Verlust bewiesen.+ const raus = applyStoredCompaction(verlauf, [erste, zweite])+ const text = raus.messages.map((m) => m.content).join('\n')+ expect(text, 'die zweite Zusammenfassung fehlt auch').toContain('two unrelated topics')+ })+})diff --git a/src/lib/compact-summary.ts b/src/lib/compact-summary.tsindex f2a2ece8..756e103d 100644--- a/src/lib/compact-summary.ts+++ b/src/lib/compact-summary.ts@@ -574,28 +574,55 @@ export interface IdentifiedMessage {}/**- * Apply a compaction that already happened.- *- * Takes the RENDERED summary from the record rather than re-rendering, so what- * the transcript block shows and what the model receives are the same bytes by- * construction rather than by two code paths agreeing.- *- * A record whose anchor message is gone — deleted, or filtered out before this- * call — returns the array untouched. That is deliberate and it is the reason- * the anchor is an id: a stale record is detectable, and the honest answer to- * "I no longer know where this summary ends" is to send the real history.+ * Apply the compactions that already happened.+ *+ * Takes the RENDERED summaries from the records rather than re-rendering, so+ * what the transcript block shows and what the model receives are the same+ * bytes by construction rather than by two code paths agreeing.+ *+ * Takes the WHOLE chain, not just the newest one, and that is the point.+ * Each run only summarises what the previous run did not cover+ * (`run-compact-command.ts`, `visible.slice(prevIdx + 1, cutAt)`), which is+ * right on its own: it avoids the summary-of-a-summary decay. But the cut here+ * drops everything before the last anchor. Passing only the newest record+ * therefore deleted the first section twice over: not as messages, because the+ * cut removes them, and not as a summary, because it was never sent. A user+ * who compacted twice lost the beginning of the conversation without being+ * told. Every summary up to the cut goes out, in order.+ *+ * A record whose anchor message is gone (deleted, or filtered out before this+ * call) cannot place a cut. The last record whose anchor still exists decides+ * where the cut falls; records after it are stale and ignored. A stale record+ * is detectable, and the honest answer to "I no longer know where this summary+ * ends" is to send the real history.*/export function applyStoredCompaction<T extends IdentifiedMessage>(messages: T[],- record: StoredCompaction | null | undefined,+ records: readonly (StoredCompaction | null | undefined)[] | null | undefined,): ApplyCompactResult<T> {- if (!record?.summary || !record.upToMessageId) {- return { messages, replaced: 0, rendered: '' }- }+ const kette = (records ?? []).filter(+ (r): r is StoredCompaction => Boolean(r?.summary && r?.upToMessageId),+ )+ if (kette.length === 0) return { messages, replaced: 0, rendered: '' }+const body = messages[0]?.role === 'system' ? messages.slice(1) : messages- const cut = body.findIndex((m) => m.id === record.upToMessageId)- if (cut < 0) return { messages, replaced: 0, rendered: '' }- return spliceSummary(messages as unknown as Array<T & { role: string }>, cut + 1, record.summary) as ApplyCompactResult<T>++ // Der letzte Datensatz, dessen Anker noch dasteht, bestimmt den Schnitt.+ // Alles danach ist veraltet und wird uebergangen.+ let letzter = -1+ let schnitt = -1+ for (let i = kette.length - 1; i >= 0; i--) {+ const treffer = body.findIndex((m) => m.id === kette[i].upToMessageId)+ if (treffer >= 0) { letzter = i; schnitt = treffer; break }+ }+ if (letzter < 0) return { messages, replaced: 0, rendered: '' }++ const rendered = kette.slice(0, letzter + 1).map((r) => r.summary).join('\n\n')+ return spliceSummary(+ messages as unknown as Array<T & { role: string }>,+ schnitt + 1,+ rendered,+ ) as ApplyCompactResult<T>}// ───────────────────────────────────────────────────────────────────────────