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(codex): ein Lauf teilt sich nicht mehr den Lauf-Zustand einer anderen Unterhaltung

purple154ebe41b4dc
diff --git a/src/hooks/__tests__/useCodex-lauf-gehoert-seiner-unterhaltung.test.ts b/src/hooks/__tests__/useCodex-lauf-gehoert-seiner-unterhaltung.test.ts
new file mode 100644
index 00000000..f668552c
--- /dev/null
+++ b/src/hooks/__tests__/useCodex-lauf-gehoert-seiner-unterhaltung.test.ts
@@ -0,0 +1,68 @@
+/**
+ * B2 Commit 6: mirror the useChat.ts fix into useCodex.ts, the second copy
+ * of the same logic.
+ *
+ * `runningRef` in useCodex.ts is a single boolean per HOOK INSTANCE, not per
+ * run — and CodexView is not remounted on a conversation switch, so one
+ * `useCodex()` instance really does serve every Code conversation for the
+ * whole window (same premise as the useChat.ts bug, see
+ * useChat-zwei-laeufe-vermischen-nicht.test.ts).
+ *
+ * Three places read or write that shared flag as if it belonged to the ONE
+ * run currently in flight:
+ *
+ * - The ReAct loop's own iteration guard (`... && runningRef.current && ...`
+ * and `if (!runningRef.current || abort.signal.aborted) break`): if
+ * conversation A finishes (its `finally` sets `runningRef.current = false`)
+ * while conversation B is still mid-loop on the SAME hook instance, B's
+ * very next iteration check reads `false` and B's loop exits early — not
+ * because B was stopped, but because A happened to finish first.
+ * - The `/loop` pass driver's re-entry gate (`if (runningRef.current) {
+ * clear the loop; return }`): meant to defer a pass while ITS OWN
+ * conversation is still busy, it actually fires whenever ANY conversation
+ * on this hook instance is running — so a manual send in conversation A
+ * silently cancels conversation B's scheduled loop pass.
+ *
+ * Fix: the ReAct loop already carries a per-call `abort` (its own
+ * AbortController) and every conversation already has a canonical,
+ * per-conversation stop flag (`lib/run-stop.ts`'s `isRunStopped`) — both are
+ * genuinely scoped to the right run. The `/loop` gate is rewritten to ask
+ * the STORE whether THIS conversation is generating, not the shared ref.
+ * `runningRef` itself is retired; nothing correctness-critical should still
+ * read it once these three spots stop.
+ *
+ * This reads the source directly (the same shape as
+ * loop-detection.test.ts / useCodex-streaming.test.ts in this same
+ * directory): a genuine behavioral repro needs two full ReAct runs
+ * interleaved through real tool-call rounds, which the existing behavioral
+ * useCodex tests do not attempt either — the wiring pin is what this file's
+ * neighbours already rely on for logic this deep in the hook.
+ *
+ * Run: npx vitest run src/hooks/__tests__/useCodex-lauf-gehoert-seiner-unterhaltung.test.ts
+ */
+import { describe, it, expect } from 'vitest'
+import { readFileSync } from 'fs'
+import { fileURLToPath } from 'url'
+import { dirname, join } from 'path'
+
+const __filename = fileURLToPath(import.meta.url)
+const __dirname = dirname(__filename)
+const src = readFileSync(join(__dirname, '../useCodex.ts'), 'utf8')
+
+describe('a codex run in useCodex.ts is gated by its OWN state, not a shared hook-instance flag', () => {
+ it('runningRef is gone: no other conversation\'s finally can flip this run\'s own loop guard', () => {
+ expect(src).not.toMatch(/runningRef/)
+ })
+
+ it('the ReAct loop bound checks this run\'s own abort signal and its own conversation\'s stop flag', () => {
+ expect(src).toMatch(/for \(let i = 0; i < MAX_CODEX_ITERATIONS && !abort\.signal\.aborted && !isRunStopped\(convId\); i\+\+\)/)
+ })
+
+ it('the mid-loop break also reads only this run\'s own signal', () => {
+ expect(src).toMatch(/if \(abort\.signal\.aborted \|\| isRunStopped\(convId\)\) break/)
+ })
+
+ it('the /loop pass driver defers on ITS OWN conversation generating, not on any conversation running', () => {
+ expect(src).toContain("if (useGenerationStore.getState().generating[convForLoop])")
+ })
+})
diff --git a/src/hooks/useCodex.ts b/src/hooks/useCodex.ts
index 62f5ae56..9117f810 100644
--- a/src/hooks/useCodex.ts
+++ b/src/hooks/useCodex.ts
@@ -280,7 +280,6 @@ export function useCodex() {
* A. Dieselbe Klammer wie in useChat und useAgentChat.
*/
const abortConvRef = useRef<string | null>(null)
- const runningRef = useRef(false)
// "The user pressed stop" lives in lib/run-stop, keyed by conversation, NOT
// in a ref of this hook instance. The Code view unmounts on every tab switch,
// and the /loop driver's finally runs in the closure of the instance that
@@ -861,7 +860,6 @@ export function useCodex() {
// a delegate_task sub-agent runs (audit AGT-1). Assigned here rather than
// in beginAgentRun because the controller does not exist that early.
run.abortSignal = abort.signal
- runningRef.current = true
setIsRunning(true)
codexStore.setThreadStatus(convId, 'running')
// Bind the generating flag to THIS conversation so the typing indicator +
@@ -875,7 +873,6 @@ export function useCodex() {
// same conversation. With the store aborter, stopCodex (any instance) and
// chat deletion both reach the real controller. Cleared in finally.
useGenerationStore.getState().registerAborter(convId, () => {
- runningRef.current = false
abort.abort()
requestGenerationCancel()
})
@@ -1066,7 +1063,7 @@ export function useCodex() {
// is checked at the top of the loop body; this bound is the runaway
// backstop. Floor of 1 so a stray 0 setting can't zero the loop.
const MAX_CODEX_ITERATIONS = Math.max(settings.agentMaxIterations ?? 200, 1)
- for (let i = 0; i < MAX_CODEX_ITERATIONS && runningRef.current && !abort.signal.aborted; i++) {
+ for (let i = 0; i < MAX_CODEX_ITERATIONS && !abort.signal.aborted && !isRunStopped(convId); i++) {
// Fertige Hintergrundagenten melden sich hier, oben in der Iteration:
// vor dem Modellaufruf und nach den Werkzeugantworten der vorigen
// Runde. Als NUTZER-Material — die Begruendung steht in
@@ -1810,7 +1807,7 @@ export function useCodex() {
}
// Phase 5b (v2.4.0), parallel tool execution via tool-executor.
- if (!runningRef.current || abort.signal.aborted) break
+ if (abort.signal.aborted || isRunStopped(convId)) break
// Every call carries an id from here on. Only the NATIVE channel gives
// us one; a call recovered from prose or rebuilt from Hermes XML had
@@ -2414,7 +2411,6 @@ export function useCodex() {
}
useGenerationStore.getState().clearAborter(convId)
- runningRef.current = false
abortRef.current = null
abortConvRef.current = null
// Close THIS run. The process-wide mirror is only cleared when this run
@@ -2508,10 +2504,18 @@ export function useCodex() {
})
const fireLoopPass = () => {
codexLoopTimers.delete(convForLoop)
- // Bail if the user moved on or started something else meanwhile.
- // Clear the loop store too — leaving it standing painted a LoopBar
- // that promised a pass which was never coming (audit A3).
- if (runningRef.current) {
+ // Bail if THIS conversation is already generating something else
+ // meanwhile (a manual send raced the timer). Clear the loop store
+ // too — leaving it standing painted a LoopBar that promised a pass
+ // which was never coming (audit A3).
+ //
+ // B2 Commit 6: was a single running flag shared by the whole HOOK
+ // INSTANCE rather than scoped per run — CodexView is not
+ // remounted on a conversation switch, so ANY conversation running
+ // on this instance used to cancel every OTHER conversation's
+ // pending pass, not just its own. Reading the store here asks
+ // about the one conversation this pass actually belongs to.
+ if (useGenerationStore.getState().generating[convForLoop]) {
useAgentLoopStore.getState().clear(convForLoop)
return
}
@@ -2601,7 +2605,6 @@ export function useCodex() {
// richtigen Griff gezogen, und der Lauf in der anderen Unterhaltung laeuft
// weiter.
if (abortConvRef.current === stoppedConvId) {
- runningRef.current = false
abortRef.current?.abort()
abortRef.current = null
setIsRunning(false)