{"record":{"id":"98153dc5791ca23f","repo":"musistudio/claude-code-router","slug":"thread-not-found-id","errorCode":null,"errorMessage":"thread not found:  + id","messagePattern":"thread not found:  \\+ id","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/agents/codex/cli-middleware-runtime.ts","lineNumber":3598,"sourceCode":"  getOrCreateThread(params) {\n    const requested = params.threadId || params.thread_id;\n    if (requested && this.threads.has(requested)) return this.threads.get(requested);\n    if (requested) {\n      const thread = this.createThread({ ...params, cwd: params.cwd || process.cwd() });\n      thread.id = requested;\n      thread.sessionId = requested;\n      thread.claudeSessionId = requested;\n      this.threads.delete(Array.from(this.threads.keys()).find((key) => this.threads.get(key) === thread));\n      this.threads.set(requested, thread);\n      return thread;\n    }\n    return this.createThread(params);\n  }\n\n  requireThread(threadId) {\n    const id = String(threadId || \"\");\n    const thread = this.threads.get(id);\n    if (!thread) throw new Error(\"thread not found: \" + id);\n    return thread;\n  }\n\n  threadList(params) {\n    let data = Array.from(this.threads.values())\n      .filter((thread) => Boolean(thread.archived) === Boolean(params.archived))\n      .map((thread) => threadJson(thread, false));\n    const search = String(params.search || params.query || \"\").toLowerCase().trim();\n    if (search) {\n      data = data.filter((thread) => JSON.stringify(thread).toLowerCase().includes(search));\n    }\n    data.sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0));\n    if (Number.isFinite(params.limit)) data = data.slice(0, params.limit);\n    return { data, nextCursor: null, backwardsCursor: null };\n  }\n\n  startTurn(params) {\n    const thread = this.requireThread(params.threadId);","sourceCodeStart":3580,"sourceCodeEnd":3616,"githubUrl":"https://github.com/musistudio/claude-code-router/blob/99f24806c6a2c660b16e53e95211c517448a6c90/packages/core/src/agents/codex/cli-middleware-runtime.ts#L3580-L3616","documentation":"requireThread() looks up a thread by stringified id in the runtime's in-memory threads Map and throws if absent. It is the guard used by all thread-scoped operations, so any request referencing an unknown, evicted, or not-yet-created thread id fails here.","triggerScenarios":"Calling thread-scoped methods (messages, send, archive, etc.) with a threadId that was never created via createThread, belongs to a previous runtime process, was archived/removed, or a falsy/empty id stringified to ''.","commonSituations":"Persisted threadId reused after middleware restart (threads are in-memory); typo or truncated id; race where the client sends messages before createThread resolves; thread pruned by list filters.","solutions":["Create the thread first via createThread and use the returned id","If the runtime restarted, re-create threads or rehydrate state instead of reusing old ids","Validate the id is non-empty and matches an entry from threadList before use","Guard calls with a threads.has() check (or catch and recreate)"],"exampleFix":"// before\nconst thread = requireThread(threadId); // throws 'thread not found'\n\n// after\nfunction getOrCreateThread(runtime, threadId) {\n  return runtime.threads?.get(String(threadId || \"\")) || runtime.createThread({ id: threadId });\n}","handlingStrategy":"type-guard","validationCode":"const id = String(threadId || \"\"); if (!runtime.threads.has(id)) { thread = await runtime.createThread(params); }","typeGuard":"function threadExists(runtime, threadId) { return runtime.threads.has(String(threadId || \"\")); }","tryCatchPattern":"try { return requireThread(id); } catch (e) { if (/thread not found/.test(String(e))) return createThread({ id }); throw e; }","preventionTips":["Never reuse thread ids across runtime restarts — threads are in-memory","Always use the id returned from createThread","List threads before operating on one obtained from external state"],"tags":["thread","lookup","in-memory-state","stale-id"],"backgroundTag":"entity-not-found-by-id","analyzedSha":"99f24806c6a2c660b16e53e95211c517448a6c90","analyzedAt":"2026-08-27T04:11:01.184Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}