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(code): Tests zur A8-Nachbesserung, je mit Gegenprobe
purpleb096eb119f35
diff --git a/src/components/chat/__tests__/the-header-can-let-the-folder-go.test.ts b/src/components/chat/__tests__/the-header-can-let-the-folder-go.test.tsindex f7405435..ce8bdbdd 100644--- a/src/components/chat/__tests__/the-header-can-let-the-folder-go.test.ts+++ b/src/components/chat/__tests__/the-header-can-let-the-folder-go.test.ts@@ -43,59 +43,87 @@ vi.mock('../CodexConfirmDialog', () => ({ CodexConfirmDialog: () => null }))const { CodexView } = await import('../CodexView')const { useCodexStore } = await import('../../../stores/codexStore')const { useGenerationStore } = await import('../../../stores/generationStore')+const { useAgentLoopStore } = await import('../../../stores/agentLoopStore')+const { useAgentModeStore } = await import('../../../stores/agentModeStore')+const { useSettingsStore } = await import('../../../stores/settingsStore')const { useChatStore } = await import('../../../stores/chatStore')+const { DEFAULT_SETTINGS } = await import('../../../lib/constants')const WINDOWS_PATH = 'C:\\Users\\dielitakira\\Documents\\huge-tree'-const show = () => render(createElement(CodexView))+// The git probe on mount resolves after the first paint, so every render is+// awaited: without it React logs an act() warning for a state update this file+// does not care about.+const show = async () => {+ let out!: ReturnType<typeof render>+ await act(async () => { out = render(createElement(CodexView)) })+ return out+}const removeButton = () => screen.queryByTestId('codex-remove-folder')beforeEach(() => {- useCodexStore.setState({ workingDirectory: '', threads: {} })+ useCodexStore.setState({ workingDirectory: '', threads: {}, sendsInFlight: 0 })useGenerationStore.setState({ generating: {} })+ useAgentLoopStore.setState({ loop: null })+ useAgentModeStore.setState({ workspaces: {} })+ useSettingsStore.setState({ settings: { ...DEFAULT_SETTINGS } })useChatStore.setState({ conversations: [], activeConversationId: null })})afterEach(() => cleanup())describe('the header shows the folder, so it also gives it back', () => {- it('carries a Remove control while a folder is set', () => {+ it('carries a Remove control while a folder is set', async () => {act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))- show()+ await show()expect(removeButton()).not.toBeNull()})- it('carries none while the agent is in its sandbox', () => {- show()+ it('carries none while the agent is in its sandbox', async () => {+ await show()// Negative control: bound to the folder, not always painted.expect(removeButton()).toBeNull()})- it('a click empties the store and the header falls back to the sandbox label', () => {+ it('a click empties the store and the header names the fallback instead', async () => {act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))- const { container } = show()+ const { container } = await show()expect(container.textContent).toContain(WINDOWS_PATH)fireEvent.click(removeButton()!)expect(useCodexStore.getState().workingDirectory).toBe('')expect(container.textContent).not.toContain(WINDOWS_PATH)- expect(container.textContent).toContain('sandbox')+ expect(container.textContent).toContain('~/agent-workspace')})- it('the empty transcript then says how to pick a new folder', () => {+ it('and names the workspace that really wins, not a sandbox it will never use', async () => {+ // review S4, the header half.+ useSettingsStore.setState({+ settings: { ...DEFAULT_SETTINGS, defaultWorkspace: { kind: 'folder', path: '/home/dave/default' } },+ })act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))- show()+ const { container } = await show()+ fireEvent.click(removeButton()!)+ expect(container.textContent).toContain('/home/dave/default')+ expect(container.textContent).not.toContain('~/agent-workspace')+ })++ it('the empty transcript then says how to pick a new folder', async () => {+ act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))+ await show()expect(screen.queryByTestId('codex-no-folder-hint')).toBeNull()fireEvent.click(removeButton()!)const hint = screen.getByTestId('codex-no-folder-hint')expect(hint.textContent).toContain('Select folder...')- expect(hint.textContent).toContain('agent-workspace')+ expect(hint.textContent).toContain('~/agent-workspace')})- it('is locked with a reason while a coding turn is in flight', () => {- act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))- useGenerationStore.setState({ generating: { 'conv-1': true } })- show()+ it('is locked with a reason from the first synchronous moment of a send', async () => {+ act(() => {+ useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH)+ useCodexStore.getState().beginSend()+ })+ await show()const button = removeButton() as HTMLButtonElementexpect(button.disabled).toBe(true)expect(button.getAttribute('title')).toContain('Wait for the current run to finish')@@ -104,9 +132,25 @@ describe('the header shows the folder, so it also gives it back', () => {expect(useCodexStore.getState().workingDirectory).toBe(WINDOWS_PATH)})- it('and unlocked again once nothing is running', () => {+ it('is locked between two loop passes as well', async () => {+ act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))+ useAgentLoopStore.setState({+ loop: { conversationId: 'conv-1', pass: 2, cap: 0, task: 'go', intervalMs: 30000, nextAt: 0 },+ })+ await show()+ expect((removeButton() as HTMLButtonElement).getAttribute('title')).toContain('loop')+ })++ it('is NOT locked by a Chat tab streaming somewhere else', async () => {+ act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))+ useGenerationStore.setState({ generating: { 'some-other-chat': true } })+ await show()+ expect((removeButton() as HTMLButtonElement).disabled).toBe(false)+ })++ it('and unlocked again once nothing is running', async () => {act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))- show()+ await show()expect((removeButton() as HTMLButtonElement).disabled).toBe(false)})})diff --git a/src/components/chat/__tests__/the-working-directory-can-be-removed.test.ts b/src/components/chat/__tests__/the-working-directory-can-be-removed.test.tsindex d78fcea8..7cd62128 100644--- a/src/components/chat/__tests__/the-working-directory-can-be-removed.test.ts+++ b/src/components/chat/__tests__/the-working-directory-can-be-removed.test.ts@@ -16,8 +16,13 @@* - clicking it empties the store, so the picker and the run agree,* - what is left behind says how to pick a new folder instead of going quiet.*- * Plus the one thing a Remove button must not do: fire in the middle of a run.- * The folder is global, so it would move the ground under every open chat.+ * Plus the one thing neither folder button may do: fire in the middle of a run.+ * The folder is global, so it would send the next turn somewhere the user is+ * not looking. The lock covers a send from its first synchronous moment, a+ * running thread, and the pause between two /loop passes, and it covers the+ * PICKER as well as Remove: a picker that stays free while Remove is held is+ * the same hole (review S1, S2, S8). What it must NOT do is care about a Chat+ * tab streaming in another conversation (review S3).** Run: npx vitest run src/components/chat/__tests__/the-working-directory-can-be-removed.test.ts*/@@ -41,17 +46,29 @@ vi.mock('../FilePreview', () => ({ FilePreview: () => null }))const { ExplorerPanel } = await import('../ExplorerPanel')const { useCodexStore } = await import('../../../stores/codexStore')const { useGenerationStore } = await import('../../../stores/generationStore')+const { useAgentLoopStore } = await import('../../../stores/agentLoopStore')+const { useAgentModeStore } = await import('../../../stores/agentModeStore')+const { useSettingsStore } = await import('../../../stores/settingsStore')const { useUIStore } = await import('../../../stores/uiStore')const { useModelStore } = await import('../../../stores/modelStore')+const { DEFAULT_SETTINGS } = await import('../../../lib/constants')const WINDOWS_PATH = 'C:\\Users\\helpslowlydying\\Documents\\My Projects'const show = () => render(createElement(ExplorerPanel, { onApprovePlan: () => {} }))const removeButton = () => screen.queryByTestId('explorer-remove-folder')+const pickButton = () => screen.queryByTestId('explorer-pick-folder') as HTMLButtonElement++const A_STANDING_LOOP = {+ conversationId: 'conv-1', pass: 2, cap: 0, task: 'keep going', intervalMs: 30000, nextAt: 0,+}beforeEach(() => {- useCodexStore.setState({ workingDirectory: '', threads: {} })+ useCodexStore.setState({ workingDirectory: '', threads: {}, sendsInFlight: 0 })useGenerationStore.setState({ generating: {} })+ useAgentLoopStore.setState({ loop: null })+ useAgentModeStore.setState({ workspaces: {} })+ useSettingsStore.setState({ settings: { ...DEFAULT_SETTINGS } })useUIStore.setState({ explorerCollapsed: false })useModelStore.setState({ activeModel: null })})@@ -93,13 +110,16 @@ describe('clicking it actually lets go', () => {expect(useCodexStore.getState().workingDirectory).toBe('')})- it('unpins the open thread, so the run stops walking the old tree', () => {+ it('unpins the open thread at its next send, so the run stops walking the old tree', () => {act(() => {useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH)useCodexStore.getState().initThread('conv-1', WINDOWS_PATH)})show()fireEvent.click(removeButton()!)+ // The click owns the store; the thread follows at that chat's next send,+ // which is the only moment it may move without stepping on another chat.+ expect(useCodexStore.getState().syncThreadWorkingDirectory('conv-1')).toBe('')expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe('')})@@ -112,7 +132,21 @@ describe('clicking it actually lets go', () => {fireEvent.click(removeButton()!)const empty = screen.getByTestId('explorer-no-folder')expect(empty.textContent).toContain('Select folder...')- expect(empty.textContent).toContain('agent-workspace')+ expect(empty.textContent).toContain('~/agent-workspace')+ })++ it('and names the workspace that really wins, not a sandbox it will never use', () => {+ // review S4: settings.defaultWorkspace beats an empty picker in the run+ // resolver, so promising ~/agent-workspace here was simply untrue.+ useSettingsStore.setState({+ settings: { ...DEFAULT_SETTINGS, defaultWorkspace: { kind: 'folder', path: '/home/dave/default' } },+ })+ act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))+ show()+ fireEvent.click(removeButton()!)+ const empty = screen.getByTestId('explorer-no-folder')+ expect(empty.textContent).toContain('/home/dave/default')+ expect(empty.textContent).not.toContain('~/agent-workspace')})it('the path itself is gone from the column, not just from the store', () => {@@ -127,8 +161,10 @@ describe('clicking it actually lets go', () => {describe('a run in flight holds it', () => {it('locks the control and says why, instead of hiding it', () => {- act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))- useGenerationStore.setState({ generating: { 'conv-1': true } })+ act(() => {+ useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH)+ useCodexStore.getState().beginSend()+ })show()const button = removeButton()!expect(button).not.toBeNull()@@ -136,9 +172,23 @@ describe('a run in flight holds it', () => {expect(button.getAttribute('title')).toContain('Wait for the current run to finish')})+ it('holds it from the first synchronous moment of the send, before any await', () => {+ // review S1: the thread only turns 'running' five awaits into the send.+ // The counter is taken before the first one, and this is that window.+ act(() => {+ useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH)+ useCodexStore.getState().beginSend()+ })+ show()+ expect(useCodexStore.getState().threads).toEqual({})+ expect((removeButton() as HTMLButtonElement).disabled).toBe(true)+ })+it('a locked click changes nothing', () => {- act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))- useGenerationStore.setState({ generating: { 'conv-1': true } })+ act(() => {+ useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH)+ useCodexStore.getState().beginSend()+ })show()fireEvent.click(removeButton()!)expect(useCodexStore.getState().workingDirectory).toBe(WINDOWS_PATH)@@ -162,4 +212,66 @@ describe('a run in flight holds it', () => {show()expect((removeButton() as HTMLButtonElement).disabled).toBe(false)})++ it('the pause between two loop passes holds it too, and says so', () => {+ // review S2: the thread is idle here and the next pass is on a timer, so+ // the old lock was wide open and the pass would have walked into a folder+ // nobody chose.+ act(() => {+ useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH)+ useCodexStore.getState().initThread('conv-1', WINDOWS_PATH)+ })+ useAgentLoopStore.setState({ loop: A_STANDING_LOOP })+ show()+ const button = removeButton() as HTMLButtonElement+ expect(button.disabled).toBe(true)+ expect(button.getAttribute('title')).toContain('loop')+ })++ it('a Chat tab streaming in another conversation does NOT hold it', () => {+ // review S3: the first cut read every conversation's generating flag, so+ // any chat anywhere locked this column for no reason.+ act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))+ useGenerationStore.setState({ generating: { 'some-other-chat': true } })+ show()+ expect((removeButton() as HTMLButtonElement).disabled).toBe(false)+ fireEvent.click(removeButton()!)+ expect(useCodexStore.getState().workingDirectory).toBe('')+ })+})++describe('the picker is held by exactly the same lock', () => {+ it('is free while nothing runs', () => {+ act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))+ show()+ expect(pickButton().disabled).toBe(false)+ })++ it('is locked during a send, with the same sentence as Remove', () => {+ // review S8: leaving the picker open while Remove was held meant the+ // folder could still be moved under a run, which is the same hole.+ act(() => {+ useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH)+ useCodexStore.getState().beginSend()+ })+ show()+ expect(pickButton().disabled).toBe(true)+ expect(pickButton().getAttribute('title')).toBe(removeButton()!.getAttribute('title'))+ })+})++describe('a collapsed column is not a dead end', () => {+ it('hides its own Remove, because the whole column is gone', () => {+ // review S7. The way out then lives in the header, which is always shown+ // and is tested in the-header-can-let-the-folder-go.test.ts.+ act(() => useCodexStore.getState().setWorkingDirectory(WINDOWS_PATH))+ useUIStore.setState({ explorerCollapsed: true })+ show()+ expect(removeButton()).toBeNull()+ // Negative control: expanded, the same state shows it.+ cleanup()+ useUIStore.setState({ explorerCollapsed: false })+ show()+ expect(removeButton()).not.toBeNull()+ })})diff --git a/src/hooks/__tests__/the-send-holds-the-working-directory.test.ts b/src/hooks/__tests__/the-send-holds-the-working-directory.test.tsnew file mode 100644index 00000000..da7f90f7--- /dev/null+++ b/src/hooks/__tests__/the-send-holds-the-working-directory.test.ts@@ -0,0 +1,76 @@+/**+ * @vitest-environment jsdom+ *+ * A8 review S1 and B1, at the wiring rather than at the store.+ *+ * The folder buttons are locked by `codexStore.sendsInFlight`. The store side+ * of that counter is proven in codexStore-working-directory.test.ts, and the+ * two views' reaction to it in the component tests. What neither of those can+ * see is the part that actually matters: that a send TAKES the counter before+ * its first await, and gives it back however it ends.+ *+ * It has to be synchronous. `setThreadStatus('running')`, the old signal, is+ * five awaits into the send (workspace slug over IPC, server tool support,+ * token budget, memory search, .lurules), and both buttons were free for that+ * whole stretch. A Remove pressed in the gap changed a store the send had+ * already read past.+ *+ * Run: npx vitest run src/hooks/__tests__/the-send-holds-the-working-directory.test.ts+ */+import { describe, it, expect, beforeEach, vi } from 'vitest'+import { renderHook } from '@testing-library/react'++// The one await at the top of the send, and the reason the gap exists. Made to+// throw in the last test so the release path is proven for a rejection too,+// which is what a counter that only decremented in the body would leak on.+const slugThrows = { value: false }+vi.mock('../../api/workspace-slug', () => ({+ resolveChatWorkspaceSlug: async () => {+ if (slugThrows.value) throw new Error('bridge is gone')+ return 'slug'+ },+}))++const { useCodex } = await import('../useCodex')+const { useCodexStore } = await import('../../stores/codexStore')+const { useModelStore } = await import('../../stores/modelStore')+const { useChatStore } = await import('../../stores/chatStore')++const sendsInFlight = () => useCodexStore.getState().sendsInFlight++beforeEach(() => {+ slugThrows.value = false+ useCodexStore.setState({ sendsInFlight: 0, threads: {}, workingDirectory: '' })+ useChatStore.setState({ conversations: [], activeConversationId: null })+ useModelStore.setState({ activeModel: null })+})++describe('a send holds the working directory for its whole length', () => {+ it('takes the lock synchronously, before the first await', () => {+ const { result } = renderHook(() => useCodex())+ // Negative control: nothing is held before the call.+ expect(sendsInFlight()).toBe(0)++ const pending = result.current.sendInstruction('do the thing')+ // Not awaited on purpose. This is the exact window the old lock missed.+ expect(sendsInFlight()).toBe(1)+ return pending+ })++ it('gives it back when the send returns', async () => {+ const { result } = renderHook(() => useCodex())+ await result.current.sendInstruction('do the thing')+ expect(sendsInFlight()).toBe(0)+ })++ it('gives it back when the send throws, so the folder is never locked for good', async () => {+ useModelStore.setState({ activeModel: 'ollama::qwen3:14b' })+ slugThrows.value = true+ const { result } = renderHook(() => useCodex())++ await expect(result.current.sendInstruction('do the thing')).rejects.toThrow('bridge is gone')+ // A leak here would hold both folder buttons for the rest of the session,+ // which is the same dead end A8 is about.+ expect(sendsInFlight()).toBe(0)+ })+})diff --git a/src/lib/__tests__/codex-workdir.test.ts b/src/lib/__tests__/codex-workdir.test.tsnew file mode 100644index 00000000..8466f8bf--- /dev/null+++ b/src/lib/__tests__/codex-workdir.test.ts@@ -0,0 +1,121 @@+/**+ * The two verdicts A8 turned out to need, pulled out of the hook and the two+ * views so they can be tested at all (review S6, S3).+ *+ * The precedence used to be a three-line boolean chain inside a 2000-line+ * send, and "is a run in flight" was written out by hand on each surface, once+ * per surface, differently. That is how the first cut of the fix ended up+ * overwriting a deliberate per-chat workspace and locking the folder picker+ * whenever an unrelated Chat tab was streaming.+ *+ * Run: npx vitest run src/lib/__tests__/codex-workdir.test.ts+ */+import { describe, it, expect } from 'vitest'+import {+ CODEX_SANDBOX,+ CODEX_WORKDIR_LOCK_TITLE,+ codexBusyReason,+ codexFallbackLabel,+ resolveCodexWorkDir,+} from '../codex-workdir'++const WINDOWS_PATH = 'C:\\Users\\helpslowlydying\\Documents\\My Projects'++describe('where the Coding Agent works', () => {+ it('the thread wins, it carries what the picker last said', () => {+ expect(resolveCodexWorkDir({+ threadDir: '/thread', workspacePath: '/workspace', storeDir: '/store',+ })).toBe('/thread')+ })++ it("a thread holding the bare '.' is not a pick, so the workspace wins", () => {+ expect(resolveCodexWorkDir({+ threadDir: CODEX_SANDBOX, workspacePath: '/workspace', storeDir: '',+ })).toBe('/workspace')+ // Negative control: a real path in the same slot does win.+ expect(resolveCodexWorkDir({+ threadDir: '/thread', workspacePath: '/workspace', storeDir: '',+ })).toBe('/thread')+ })++ it('an empty thread hands it to the per-chat workspace or the default one', () => {+ expect(resolveCodexWorkDir({+ threadDir: '', workspacePath: '/workspace', storeDir: '',+ })).toBe('/workspace')+ })++ it('nothing anywhere means the per-chat sandbox', () => {+ expect(resolveCodexWorkDir({+ threadDir: '', workspacePath: null, storeDir: '',+ })).toBe(CODEX_SANDBOX)+ // Negative control against null and undefined leaking through as a path.+ expect(resolveCodexWorkDir({+ threadDir: undefined, workspacePath: undefined, storeDir: undefined,+ })).toBe(CODEX_SANDBOX)+ })++ it('the picker still catches a thread that has not been synced yet', () => {+ expect(resolveCodexWorkDir({+ threadDir: '', workspacePath: null, storeDir: WINDOWS_PATH,+ })).toBe(WINDOWS_PATH)+ })++ it('a Windows path passes through untouched', () => {+ const out = resolveCodexWorkDir({+ threadDir: WINDOWS_PATH, workspacePath: null, storeDir: '',+ })+ expect(out).toBe(WINDOWS_PATH)+ expect(out).not.toContain('/')+ })+})++describe('what the empty state is allowed to promise', () => {+ it('names the workspace that actually wins over an empty picker', () => {+ expect(codexFallbackLabel('/home/dave/default-repo')).toBe('/home/dave/default-repo')+ })++ it('and only says the sandbox when nothing else is pinned', () => {+ expect(codexFallbackLabel(null)).toBe('~/agent-workspace')+ expect(codexFallbackLabel('')).toBe('~/agent-workspace')+ })+})++describe('when the working directory is held', () => {+ const free = { sendsInFlight: 0, threads: {}, loop: null }++ it('is free when nothing at all is going on', () => {+ expect(codexBusyReason(free)).toBeNull()+ })++ it('is held from the first synchronous moment of a send', () => {+ expect(codexBusyReason({ ...free, sendsInFlight: 1 })).toBe('run')+ })++ it('is held by a thread the store calls running', () => {+ expect(codexBusyReason({ ...free, threads: { a: { status: 'running' } } })).toBe('run')+ })++ it('is held between two loop passes, where the thread says idle', () => {+ // The dangerous gap: status idle, next pass on a setTimeout. Moving the+ // folder here used to send that pass somewhere else with nobody watching.+ expect(codexBusyReason({+ ...free,+ threads: { a: { status: 'idle' } },+ loop: { conversationId: 'a', pass: 2, cap: 0, task: 't', intervalMs: 30000, nextAt: 0 },+ })).toBe('loop')+ })++ it('an idle thread on its own holds nothing', () => {+ expect(codexBusyReason({ ...free, threads: { a: { status: 'idle' }, b: { status: 'error' } } })).toBeNull()+ })++ it('a finished send releases it, and a double release cannot go negative', () => {+ expect(codexBusyReason({ ...free, sendsInFlight: 0 })).toBeNull()+ expect(codexBusyReason({ ...free, sendsInFlight: -1 })).toBeNull()+ })++ it('says a different sentence for a loop than for a run', () => {+ expect(CODEX_WORKDIR_LOCK_TITLE.run).not.toBe(CODEX_WORKDIR_LOCK_TITLE.loop)+ expect(CODEX_WORKDIR_LOCK_TITLE.loop).toContain('loop')+ })+})diff --git a/src/stores/__tests__/codexStore-working-directory.test.ts b/src/stores/__tests__/codexStore-working-directory.test.tsindex 2b2efbc5..7195de29 100644--- a/src/stores/__tests__/codexStore-working-directory.test.ts+++ b/src/stores/__tests__/codexStore-working-directory.test.ts@@ -17,6 +17,11 @@* running conversation attached to the old tree. The same drift made a* mis-click permanent: picking a second folder did not move the agent.*+ * The first cut fixed the second half by rewriting EVERY thread on every set,+ * which quietly overwrote a deliberate per-chat workspace in another chat+ * (review S5). The store is the single truth now, and a conversation's thread+ * is pulled onto it one at a time, at that conversation's next send.+ ** Windows paths are checked verbatim on purpose. Both reporters are on Windows,* and this store must never touch a backslash or a drive letter.*@@ -47,7 +52,7 @@ const WINDOWS_PATH = 'C:\\Users\\helpslowlydying\\Documents\\My Projects'beforeEach(() => {localStorage.clear()- useCodexStore.setState({ workingDirectory: '', threads: {} })+ useCodexStore.setState({ workingDirectory: '', threads: {}, sendsInFlight: 0 })})describe('the folder can be given back', () => {@@ -72,24 +77,35 @@ describe('the folder can be given back', () => {expect(Object.keys(persisted().state)).toContain('workingDirectory')})- it('unpins the open thread, which is where the folder actually reaches the run', () => {+ it('unpins the open thread at its next send, which is where the folder reaches the run', () => {useCodexStore.getState().setWorkingDirectory('/home/dave/repo')useCodexStore.getState().initThread('conv-1', '/home/dave/repo')// Negative control: the thread really did carry the folder before.expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe('/home/dave/repo')useCodexStore.getState().clearWorkingDirectory()+ useCodexStore.getState().syncThreadWorkingDirectory('conv-1')expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe('')})- it('unpins every open thread, not just the one in front', () => {+ it('touches ONLY the conversation being sent, a per-chat workspace elsewhere survives', () => {useCodexStore.getState().setWorkingDirectory('/repo')useCodexStore.getState().initThread('conv-1', '/repo')- useCodexStore.getState().initThread('conv-2', '/repo')+ // conv-2 sits on a folder of its own, the way a per-chat agent workspace+ // reaches the thread. The first cut of this fix wiped it (review S5).+ useCodexStore.getState().initThread('conv-2', '/other/deliberate/workspace')++ useCodexStore.getState().clearWorkingDirectory()+ useCodexStore.getState().syncThreadWorkingDirectory('conv-1')+ expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe('')+ expect(useCodexStore.getState().getThread('conv-2')!.workingDirectory).toBe('/other/deliberate/workspace')+ })+ it('a clear on its own moves no thread at all, the send is what pulls them', () => {+ useCodexStore.getState().setWorkingDirectory('/repo')+ useCodexStore.getState().initThread('conv-1', '/repo')useCodexStore.getState().clearWorkingDirectory()- const threads = useCodexStore.getState().threads- expect(Object.values(threads).map((t) => t.workingDirectory)).toEqual(['', ''])+ expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe('/repo')})it('leaves the rest of the thread alone: events, status and id survive', () => {@@ -100,6 +116,7 @@ describe('the folder can be given back', () => {useCodexStore.getState().setThreadStatus('conv-1', 'error')useCodexStore.getState().clearWorkingDirectory()+ useCodexStore.getState().syncThreadWorkingDirectory('conv-1')const thread = useCodexStore.getState().getThread('conv-1')!expect(thread.id).toBe(id)expect(thread.events).toHaveLength(1)@@ -108,22 +125,72 @@ describe('the folder can be given back', () => {})describe('a mis-click is not permanent either', () => {- it('picking a second folder moves the open thread with it', () => {+ it('picking a second folder moves the open thread at the next send', () => {useCodexStore.getState().setWorkingDirectory('/wrong/huge/tree')useCodexStore.getState().initThread('conv-1', '/wrong/huge/tree')useCodexStore.getState().setWorkingDirectory('/right/repo')+ expect(useCodexStore.getState().syncThreadWorkingDirectory('conv-1')).toBe('/right/repo')expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe('/right/repo')})- it('a set that changes nothing keeps the same threads object, so nobody re-renders', () => {+ it('the sync reads the store LIVE, so a Remove during the send still wins', () => {+ // B1: the send takes its store snapshot, then awaits the workspace slug+ // over IPC. A Remove pressed in that gap used to be undone by the stale+ // snapshot. This action has no snapshot to go stale.+ useCodexStore.getState().setWorkingDirectory('/wrong/huge/tree')+ useCodexStore.getState().initThread('conv-1', '/wrong/huge/tree')+ const staleSnapshot = useCodexStore.getState()++ useCodexStore.getState().clearWorkingDirectory()+ expect(staleSnapshot.workingDirectory).toBe('/wrong/huge/tree')+ expect(staleSnapshot.syncThreadWorkingDirectory('conv-1')).toBe('')+ expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe('')+ })++ it('a sync that changes nothing keeps the same threads object, so nobody re-renders', () => {useCodexStore.getState().setWorkingDirectory('/repo')useCodexStore.getState().initThread('conv-1', '/repo')const before = useCodexStore.getState().threads- useCodexStore.getState().setWorkingDirectory('/repo')+ useCodexStore.getState().syncThreadWorkingDirectory('conv-1')expect(useCodexStore.getState().threads).toBe(before)})++ it('a sync for a conversation with no thread yet is a no-op, not a crash', () => {+ useCodexStore.getState().setWorkingDirectory('/repo')+ expect(useCodexStore.getState().syncThreadWorkingDirectory('never-sent')).toBe('/repo')+ expect(useCodexStore.getState().getThread('never-sent')).toBeUndefined()+ })+})++describe('the send counter that holds the folder from the first synchronous moment', () => {+ it('counts up and back down', () => {+ useCodexStore.getState().beginSend()+ expect(useCodexStore.getState().sendsInFlight).toBe(1)+ useCodexStore.getState().endSend()+ expect(useCodexStore.getState().sendsInFlight).toBe(0)+ })++ it('carries two overlapping sends without losing one', () => {+ useCodexStore.getState().beginSend()+ useCodexStore.getState().beginSend()+ useCodexStore.getState().endSend()+ // Negative control: one release must not free a send that is still going.+ expect(useCodexStore.getState().sendsInFlight).toBe(1)+ useCodexStore.getState().endSend()+ expect(useCodexStore.getState().sendsInFlight).toBe(0)+ })++ it('never goes negative, a stray release cannot unlock a running send', () => {+ useCodexStore.getState().endSend()+ expect(useCodexStore.getState().sendsInFlight).toBe(0)+ })++ it('stays off the disk, a crash must not leave a ghost send holding the folder', () => {+ useCodexStore.getState().beginSend()+ expect(persisted().state?.sendsInFlight).toBeUndefined()+ })})describe('Windows paths are carried through byte for byte', () => {@@ -148,8 +215,15 @@ describe('Windows paths are carried through byte for byte', () => {expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe(WINDOWS_PATH)useCodexStore.getState().clearWorkingDirectory()+ useCodexStore.getState().syncThreadWorkingDirectory('conv-1')expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe('')})++ it('setThreadWorkingDirectory keeps the backslashes on the thread too', () => {+ useCodexStore.getState().initThread('conv-1', '')+ useCodexStore.getState().setThreadWorkingDirectory('conv-1', WINDOWS_PATH)+ expect(useCodexStore.getState().getThread('conv-1')!.workingDirectory).toBe(WINDOWS_PATH)+ })})describe('R1 DOWNGRADE-KONTRAKT still holds', () => {@@ -158,6 +232,7 @@ describe('R1 DOWNGRADE-KONTRAKT still holds', () => {useCodexStore.getState().chooseCodexMode('x', 'plan', false)useCodexStore.getState().initThread('conv-1', WINDOWS_PATH)useCodexStore.getState().clearWorkingDirectory()+ useCodexStore.getState().beginSend()const raw = persisted()expect(Object.keys(raw.state).sort()).toEqual(['modeByConversation', 'workingDirectory'])