From f36a77fcfee9f4a24a89c7ddf194f534ba7d7275 Mon Sep 17 00:00:00 2001 From: Mayaskara Date: Sat, 28 Nov 2026 02:05:21 +0530 Subject: [PATCH] fix(codex): resume unloaded threads before starting a turn Existing Codex chats failed with "thread not found" after AndCode restarted. Co-Authored-By: Claude Opus 5.5 --- .../andcode/runtime/local/CodexRuntime.kt | 47 ++++++++++++++++++- .../runtime/local/CodexThreadResumeTest.kt | 43 +++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 app/src/test/java/com/yugahashimoto/andcode/runtime/local/CodexThreadResumeTest.kt diff --git a/app/src/main/java/com/yugahashimoto/andcode/runtime/local/CodexRuntime.kt b/app/src/main/java/com/yugahashimoto/andcode/runtime/local/CodexRuntime.kt index cd5713205..68e2cd3f4 100644 --- a/app/src/main/java/com/yugahashimoto/andcode/runtime/local/CodexRuntime.kt +++ b/app/src/main/java/com/yugahashimoto/andcode/runtime/local/CodexRuntime.kt @@ -82,7 +82,9 @@ class CodexRuntime( val process: Process, val client: CodexJsonRpcClient, val readerJob: Job, - ) + ) { + val loadedThreads = CodexLoadedThreads() + } @Volatile private var server: ServerProcess? = null @@ -350,6 +352,7 @@ class CodexRuntime( val result = call("thread/start", params) val thread = result["thread"]?.jsonObject ?: error("Codex did not return a thread") val session = threadToSession(thread) + server?.loadedThreads?.markLoaded(session.id) val requestedTitle = title?.takeIf(String::isNotBlank) if (requestedTitle == null) return session // Applied to the returned object directly rather than re-fetching the thread: the rename @@ -407,7 +410,10 @@ class CodexRuntime( attachments: List = emptyList(), ) { val params = codexTurnStartParams(sessionId, prompt, model, effort, serviceTier, attachments) - runCatching { call("turn/start", params) } + runCatching { + ensureThreadLoaded(sessionId) + call("turn/start", params) + } .onSuccess { result -> // Recorded by turn id, not just session id, so onServerEnded can evict exactly this // turn's in-progress assistant message from CodexItemParser if the process dies @@ -473,6 +479,19 @@ class CodexRuntime( server = null } + /** + * A new app-server process (after the app restarts, or after the previous one died) only knows + * a thread from its rollout on disk and answers `turn/start` on it with "thread not found" + * until the thread is loaded with `thread/resume`. + */ + private suspend fun ensureThreadLoaded(threadId: String) { + ensureServer() + val current = server ?: return + if (!current.loadedThreads.needsResume(threadId)) return + current.client.call("thread/resume", codexThreadResumeParams(threadId)) + current.loadedThreads.markLoaded(threadId) + } + private suspend fun call( method: String, params: JsonElement? = null, @@ -742,6 +761,30 @@ internal fun resolveCodexTurnSettings( /** The app-server's explicit "standard speed" tier, which turns off a fast tier left on earlier. */ internal const val DEFAULT_SERVICE_TIER = "default" +/** + * The threads one app-server process has loaded, by `thread/start` or `thread/resume`. Each process + * gets its own instance, so a restarted app-server starts with none. + */ +internal class CodexLoadedThreads { + private val ids: MutableSet = ConcurrentHashMap.newKeySet() + + fun needsResume(threadId: String): Boolean = threadId !in ids + + fun markLoaded(threadId: String) { + ids += threadId + } +} + +/** + * The `thread/resume` params that load [threadId] into the running app-server. Its past turns are + * left out of the response: the chat already renders them from its own message store. + */ +internal fun codexThreadResumeParams(threadId: String): JsonObject = + buildJsonObject { + put("threadId", JsonPrimitive(threadId)) + put("excludeTurns", JsonPrimitive(true)) + } + /** * The `turn/start` params for one prompt. [effort] and [serviceTier] are sent only when non-blank: * callers that want the explicit-every-turn behaviour resolve them with [resolveCodexTurnSettings] diff --git a/app/src/test/java/com/yugahashimoto/andcode/runtime/local/CodexThreadResumeTest.kt b/app/src/test/java/com/yugahashimoto/andcode/runtime/local/CodexThreadResumeTest.kt new file mode 100644 index 000000000..379f0b778 --- /dev/null +++ b/app/src/test/java/com/yugahashimoto/andcode/runtime/local/CodexThreadResumeTest.kt @@ -0,0 +1,43 @@ +package com.yugahashimoto.andcode.runtime.local + +import kotlinx.serialization.json.jsonPrimitive +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertTrue +import org.junit.Test + +/** + * A thread created by an earlier app-server process must be loaded with `thread/resume` before + * `turn/start`, or Codex answers "thread not found" (seen with codex-cli 0.172.0 after restarting + * the app). [CodexLoadedThreads] tracks which threads the current process has loaded. + */ +class CodexThreadResumeTest { + @Test + fun `a thread the process has not loaded needs a resume`() { + assertTrue(CodexLoadedThreads().needsResume("thr_1")) + } + + @Test + fun `a loaded thread is not resumed again`() { + val loaded = CodexLoadedThreads() + loaded.markLoaded("thr_1") + + assertFalse(loaded.needsResume("thr_1")) + assertTrue(loaded.needsResume("thr_2")) + } + + @Test + fun `a new process starts with no loaded threads`() { + CodexLoadedThreads().markLoaded("thr_1") + + assertTrue(CodexLoadedThreads().needsResume("thr_1")) + } + + @Test + fun `resume params name the thread and leave out its past turns`() { + val params = codexThreadResumeParams("thr_1") + + assertEquals("thr_1", params.getValue("threadId").jsonPrimitive.content) + assertEquals("true", params.getValue("excludeTurns").jsonPrimitive.content) + } +}