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(chat): /compact bucht jetzt die lokale Spur, Stop wirkt
purple17495638ab30
diff --git a/src/hooks/__tests__/compact-bucht-lokale-spur.test.ts b/src/hooks/__tests__/compact-bucht-lokale-spur.test.tsnew file mode 100644index 00000000..2e3d62f8--- /dev/null+++ b/src/hooks/__tests__/compact-bucht-lokale-spur.test.ts@@ -0,0 +1,205 @@+/**+ * @vitest-environment jsdom+ *+ * Runde 5 Nachtrag (review-lanes.md Runde 2, Grep-Audit dieser Runde,+ * David's Nachtrag danach): useChat.ts's `/compact` branch fired a genuine+ * inference call (`runCompactForConversation` -> `compact-run.ts`'s+ * `provider.chatStream`) entirely BEFORE `sendMessage`'s own `runInLane`+ * call for the normal turn. A local `/compact` could stream from the+ * built-in engine at the same time as an unrelated local conversation, the+ * same VRAM-swap Blocker A and B this round were about, just triggered from+ * a slash command instead of a send. `useCodex.ts`'s equivalent `/compact`+ * branch sits INSIDE its own `runInLane` body and was never affected.+ *+ * `runCompactForConversation` itself is mocked here (its own tests live in+ * `run-compact-command.test.ts`); this file only proves the LANE discipline+ * around it: a local `/compact` books the local lane, a second local+ * conversation queues behind it, and Stop reaches it in both states it can+ * be in (still queued, or the summary call actually in flight).+ *+ * Run: npx vitest run src/hooks/__tests__/compact-bucht-lokale-spur.test.ts+ */+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'+import { renderHook, act } from '@testing-library/react'++vi.mock('../../api/cloud/supabase', () => ({+ getAccessToken: async () => 'session-token-abc',+}))+vi.mock('../../lib/ttsBridge', () => ({ autoSpeak: () => {} }))+vi.mock('../../api/vram-handoff', () => ({ requestGenerationCancel: () => {} }))+vi.mock('../useMemory', () => ({+ useMemory: () => ({ extractAndSave: async () => {} }),+ extractMemoriesFromPair: async () => {},+}))+vi.mock('../../lib/run-lane-of-model', () => ({+ laneOf: (model: string) => (model.startsWith('lu-cloud::') ? 'cloud' : 'local'),+ currentLaneFacts: () => ({ openaiSlotIsLocal: true, ollamaBaseIsLocal: true }),+}))++let compactResolvers: Array<(outcome: unknown) => void> = []+let lastCompactSignal: AbortSignal | undefined+const runCompactForConversationMock = vi.fn((opts: { signal?: AbortSignal }) => {+ lastCompactSignal = opts.signal+ return new Promise((resolve) => { compactResolvers.push(resolve as (o: unknown) => void) })+})+vi.mock('../../lib/run-compact-command', () => ({+ runCompactForConversation: (opts: { signal?: AbortSignal }) => runCompactForConversationMock(opts),+ compactOutcomeMessage: (outcome: { ok: boolean; reason?: string }) =>+ outcome.ok ? 'summary-ok' : `not-ok:${outcome.reason}`,+}))++import { useChat } from '../useChat'+import { useChatStore } from '../../stores/chatStore'+import { useModelStore } from '../../stores/modelStore'+import { useSettingsStore } from '../../stores/settingsStore'+import { useProviderStore } from '../../stores/providerStore'+import { useGenerationStore } from '../../stores/generationStore'+import { __resetRunLanesForTests, admit, localLaneHolder, queuedRunIds } from '../../lib/run-lanes'+import { DEFAULT_SETTINGS } from '../../lib/constants'++const LOCAL_MODEL = 'openai::local-compact-model'+const CLOUD_MODEL = 'lu-cloud::zai-org/GLM-5.3'++function seed(model: string): string {+ const convId = useChatStore.getState().createConversation(model, '')+ useChatStore.getState().setActiveConversation(convId)+ return convId+}++beforeEach(() => {+ __resetRunLanesForTests()+ compactResolvers = []+ lastCompactSignal = undefined+ runCompactForConversationMock.mockClear()+ useChatStore.setState({ conversations: [], activeConversationId: null })+ useGenerationStore.setState({ generating: {}, aborters: {}, runs: {} })+ useModelStore.setState({ models: [], activeModel: LOCAL_MODEL })+ useSettingsStore.setState({+ settings: { ...DEFAULT_SETTINGS, appMode: 'local', cavemanMode: 'off', chatToolsEnabled: false },+ })+ useProviderStore.setState((s) => ({+ providers: {+ ...s.providers,+ openai: { ...s.providers.openai, enabled: true },+ 'lu-cloud': { ...s.providers['lu-cloud'], enabled: true },+ },+ }))+})+afterEach(() => vi.restoreAllMocks())++describe('/compact bucht die lokale Spur', () => {+ it('eine zweite lokale Unterhaltung wartet, waehrend /compact laeuft, und wird nach dessen Ende befoerdert', async () => {+ const convA = seed(LOCAL_MODEL)+ const { result } = renderHook(() => useChat())++ let compactCall: Promise<void> = Promise.resolve()+ await act(async () => {+ compactCall = result.current.sendMessage('/compact')+ for (let i = 0; i < 10; i++) await Promise.resolve()+ })++ expect(runCompactForConversationMock).toHaveBeenCalledTimes(1)+ expect(localLaneHolder()).toBe(convA)++ // A second, genuinely different local conversation must queue while+ // /compact still holds the lane.+ expect(admit('local', 'conv-b', () => {})).toBe('queued')+ expect(queuedRunIds()).toEqual(['conv-b'])++ await act(async () => {+ compactResolvers[0]({ ok: false, reason: 'nothing-to-compact' })+ await compactCall+ })++ // /compact released the lane, the waiting conversation was promoted.+ expect(localLaneHolder()).toBe('conv-b')+ expect(queuedRunIds()).toEqual([])+ })++ it('Stop waehrend /compact NOCH WARTET: sagt "Stopped", ruft runCompactForConversation nie auf', async () => {+ // Occupy the local lane with an unrelated run first.+ admit('local', 'conv-holder', () => {})+ expect(localLaneHolder()).toBe('conv-holder')++ const convA = seed(LOCAL_MODEL)+ const { result } = renderHook(() => useChat())++ let compactCall: Promise<void> = Promise.resolve()+ await act(async () => {+ compactCall = result.current.sendMessage('/compact')+ await Promise.resolve()+ })++ expect(queuedRunIds()).toEqual([convA])+ expect(runCompactForConversationMock).not.toHaveBeenCalled()++ await act(async () => {+ useGenerationStore.getState().abortConversation(convA)+ await compactCall+ })++ expect(runCompactForConversationMock).not.toHaveBeenCalled()+ // The holder was never touched; a Stop on the WAITING conversation must+ // not promote anyone (nothing to promote to besides itself).+ expect(localLaneHolder()).toBe('conv-holder')+ const conv = useChatStore.getState().conversations.find((c) => c.id === convA)!+ const notice = conv.messages.find((m) => m.content === 'not-ok:aborted')+ expect(notice).toBeTruthy()+ })++ it('Stop waehrend /compact WIRKLICH LAEUFT: der AbortSignal der Zusammenfassung wird ausgeloest', async () => {+ const convA = seed(LOCAL_MODEL)+ const { result } = renderHook(() => useChat())++ let compactCall: Promise<void> = Promise.resolve()+ await act(async () => {+ compactCall = result.current.sendMessage('/compact')+ for (let i = 0; i < 10; i++) await Promise.resolve()+ })++ expect(runCompactForConversationMock).toHaveBeenCalledTimes(1)+ expect(lastCompactSignal?.aborted).toBe(false)++ await act(async () => {+ useGenerationStore.getState().abortConversation(convA)+ })++ expect(lastCompactSignal?.aborted).toBe(true)++ await act(async () => {+ compactResolvers[0]({ ok: false, reason: 'aborted' })+ await compactCall+ })+ expect(localLaneHolder()).toBeNull()+ })++ it('GEGENPROBE: /compact auf einem Cloud-Modell wartet nicht auf eine laufende lokale Unterhaltung', async () => {+ admit('local', 'conv-local-holder', () => {})+ expect(localLaneHolder()).toBe('conv-local-holder')++ useModelStore.setState({ models: [], activeModel: CLOUD_MODEL })+ useSettingsStore.setState({+ settings: { ...DEFAULT_SETTINGS, appMode: 'cloud', cavemanMode: 'off', chatToolsEnabled: false },+ })+ seed(CLOUD_MODEL)+ const { result } = renderHook(() => useChat())++ let compactCall: Promise<void> = Promise.resolve()+ await act(async () => {+ compactCall = result.current.sendMessage('/compact')+ for (let i = 0; i < 10; i++) await Promise.resolve()+ })++ // Not queued: a cloud /compact never touches the local engine, so it must+ // not wait on the local holder.+ expect(runCompactForConversationMock).toHaveBeenCalledTimes(1)+ expect(queuedRunIds()).toEqual([])++ await act(async () => {+ compactResolvers[0]({ ok: false, reason: 'nothing-to-compact' })+ await compactCall+ })+ // The local holder is untouched by the cloud compaction's own lifecycle.+ expect(localLaneHolder()).toBe('conv-local-holder')+ })+})diff --git a/src/hooks/useChat.ts b/src/hooks/useChat.tsindex e0b0e165..40118ac2 100644--- a/src/hooks/useChat.ts+++ b/src/hooks/useChat.ts@@ -500,13 +500,55 @@ export function useChat() {id: noticeId, role: 'system', notice: 'info',content: 'Summarising the earlier turns…', timestamp: Date.now(),})- const outcome = await runCompactForConversation({- conversationId: convId,- activeModel,- trigger: 'manual',- focus: cmd.args || undefined,- })- useChatStore.getState().updateMessageContent(convId, noticeId, compactOutcomeMessage(outcome))+ // Runde 5 Nachtrag (review-lanes.md Runde 2, Grep-Audit dieser Runde):+ // this used to call runCompactForConversation straight away, a real+ // inference call that never touched run-lanes.ts. A local /compact+ // could stream from the built-in engine at the same time as an+ // unrelated local conversation, the same VRAM-swap Blocker A and B+ // this round were about, just triggered from a slash command instead+ // of a send. It now books the same lane a normal turn would, with a+ // real AbortController so Stop reaches it, whether Stop lands while+ // it is still queued (nothing was ever asked of the model, the+ // outcome is reported the same way an aborted summary call itself+ // would report it) or while it is actually running.+ const compactAbort = new AbortController()+ setIsGenerating(true)+ useGenerationStore.getState().setGenerating(convId, true)+ try {+ // No model selected: `runCompactForConversation` reports `no-model`+ // on its own without touching any provider, so there is nothing+ // local to guard against; `'cloud'` starts immediately and never+ // queues, same as any other lane-less no-op would.+ const lane = activeModel ? laneOf(activeModel, currentLaneFacts()) : 'cloud'+ const laneOutcome = await runInLane(+ { conversationId: convId, lane, abort: () => compactAbort.abort() },+ async () => {+ const outcome = await runCompactForConversation({+ conversationId: convId,+ activeModel,+ trigger: 'manual',+ focus: cmd.args || undefined,+ signal: compactAbort.signal,+ })+ useChatStore.getState().updateMessageContent(convId, noticeId, compactOutcomeMessage(outcome))+ },+ )+ if (laneOutcome === 'cancelled-while-queued') {+ useChatStore.getState().updateMessageContent(+ convId, noticeId, compactOutcomeMessage({ ok: false, reason: 'aborted' }),+ )+ }+ } finally {+ // The summary (or the "Stopped" notice) is already in the store by+ // this point; only the announcement that flips Stop back to Send+ // waits for the write, same contract as the send path below and+ // runGroupRound above (stores/durability.ts has the measurement+ // that made the order matter).+ await endTurnDurably(() => {+ setIsGenerating(false)+ useGenerationStore.getState().setGenerating(convId, false)+ })+ }return}}diff --git a/src/stores/__tests__/a-turn-is-finished-when-it-is-stored.test.ts b/src/stores/__tests__/a-turn-is-finished-when-it-is-stored.test.tsindex 043a36ae..47c118e3 100644--- a/src/stores/__tests__/a-turn-is-finished-when-it-is-stored.test.ts+++ b/src/stores/__tests__/a-turn-is-finished-when-it-is-stored.test.ts@@ -340,16 +340,24 @@ describe('the turn end starts the write before it starts anything else', () => {// and useAgentChat after it, and neither costs the write anything — pinning// that difference would be pinning noise.- it('all four turn ends were found and parsed', () => {+ it('all five turn ends were found and parsed', () => {// `useCodex/runInstruction` und nicht mehr `sendInstruction`: 2.6.8 hat den// grossen useCallback in `runInstruction` umbenannt und `sendInstruction`// zur duennen Huelle mit beginSend/endSend gemacht. Das Rundenende sitzt// seither in `runInstruction`. Die Liste steht hier woertlich, damit ein// solcher Umbau auffaellt, statt den Vertrag still auf drei Bloecke zu// verkuerzen.+ //+ // Zwei Eintraege lauten `useChat/sendMessage`, absichtlich: `/compact`+ // (Runde 5 Nachtrag, review-lanes.md Runde 2) bekam SEIN EIGENES+ // `runInLane`/`endTurnDurably`-Paar, textlich getrennt vom normalen Zug+ // weiter unten in derselben `useCallback`, aber demselben Vertrag+ // unterworfen: die Zusammenfassung steht schon im Speicher, bevor die+ // App "nicht mehr am Generieren" meldet.expect(TURN_ENDS.map((b) => b.name)).toEqual(['useChat/runGroupRound','useChat/sendMessage',+ 'useChat/sendMessage','useCodex/runInstruction','useAgentChat/sendAgentMessage',])