{"record":{"id":"9d130d826c703ac1","repo":"BigPizzaV3/CodexPlusPlus","slug":"id-9d130d","errorCode":null,"errorMessage":"历史消息缺少稳定 ID","messagePattern":"历史消息缺少稳定 ID","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tools/conversation-canvas/native-history.mjs","lineNumber":51,"sourceCode":"      parsed=partial?.messages?.slice()||[];let itemCursor=partial?.cursor??null,limit=32;const cursors=new Set();\n      do{\n        signal?.throwIfAborted();\n        if(cursors.has(itemCursor))throw Error('历史消息分页游标重复');\n        let page;\n        for(;;){\n          try{page=await send('thread/items/list',{threadId,turnId:turn.id,cursor:itemCursor,limit,sortDirection:'asc'});break;}\n          catch(error){\n            if(limit>1&&/decoded message length too large/i.test(error.message)){limit=Math.max(1,Math.floor(limit/2));continue;}\n            throw error;\n          }\n        }\n        if(!Array.isArray(page?.data))throw Error('历史消息接口返回格式无效');\n        cursors.add(itemCursor);\n        for(const row of page.data){\n          if(row.turnId!=null&&row.turnId!==turn.id)throw Error('历史消息所属轮次不匹配');\n          const item=row.item??row;\n          if(!['userMessage','agentMessage'].includes(item.type))continue;\n          if(typeof item.id!=='string')throw Error('历史消息缺少稳定 ID');\n          const role=item.type==='userMessage'?'user':'assistant';\n          const content=role==='user'?(item.content??[]):[{text:item.text??''}];\n          const message=parseMessage({type:'response_item',payload:{type:'message',id:item.id,role,phase:role==='assistant'?(item.phase??'final_answer'):'',content}},item.id);\n          if(message)parsed.push({...message,turnId:turn.id,turnStatus:turn.status});\n        }\n        itemCursor=page.nextCursor??null;\n        onProgress(`正在读取历史 ${index+1}/${turns.length} · 已读 ${parsed.length} 条消息…`);\n        if(turn.status==='completed'&&itemCursor!==null)await cache.set(`${key}:partial`,{messages:parsed,cursor:itemCursor});\n      }while(itemCursor!==null);\n      if(turn.status==='completed'){await cache.set(key,parsed);await cache.delete(`${key}:partial`);}\n    }\n    // Older completed-turn caches contain text and IDs but no turn metadata.\n    // The current directory and cache key provide the authoritative owner.\n    for(const message of parsed)if(!seen.has(message.id)){seen.add(message.id);messages.push({...message,turnId:turn.id,turnStatus:turn.status});}\n  }\n  // Bound cross-task retention, without truncating the returned conversation.\n  while(cache.size>500)cache.delete(cache.keys().next().value);\n  return messages;","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/tools/conversation-canvas/native-history.mjs#L33-L69","documentation":"For each row of type `userMessage` or `agentMessage`, readNativeHistory requires `item.id` to be a string because it is used as the stable message key for deduplication and cache identity. If the native API returns a message item without a string `id` (missing, null, or numeric), the plugin throws rather than generating unstable IDs that would break caching and dedup.","triggerScenarios":"A `thread/items/list` row of type userMessage/agentMessage lacks `item.id` (after unwrapping `row.item ?? row`) — typically because a Codex Desktop build changed the item schema (e.g. renamed `id` to `itemId` or moved the id into a nested object).","commonSituations":"Codex Desktop upgraded and renamed/relocated the item ID field; rows synthesized by a custom relay that omit IDs; locally-pending/unsent messages surfaced by the API without a server-assigned ID.","solutions":["Update the plugin to a build matching the installed Codex Desktop version (the item schema is pinned per build).","Inspect the failing row's JSON to find the new ID field name and map it in native-history.mjs (e.g. `item.id ?? item.itemId`).","As a last resort, skip items without IDs (`continue`) so history still renders, accepting possible dedup loss.","Check for a custom relay or wrapper stripping/renaming fields and fix it server-side."],"exampleFix":"// before\nif(typeof item.id!=='string')throw Error('历史消息缺少稳定 ID');\n// after\nif(typeof item.id!=='string'){console.warn('item without stable id, skipped',item);continue;}","handlingStrategy":"validation","validationCode":"const usable=page.data.every(r=>{const it=r?.item??r;return !['userMessage','agentMessage'].includes(it?.type)||typeof it?.id==='string';});\n","typeGuard":"function hasStableId(item){return item!=null&&typeof item.id==='string'&&item.id.length>0;}\n","tryCatchPattern":"try{history=await readNativeHistory(send,threadId,cache);}catch(e){if(/缺少稳定 ID/.test(e.message))return degradeToUncachedHistory();throw e;}\n","preventionTips":["Schema-check item rows (type + id) before processing; skip unknown/malformed rows.","Update plugin and Codex Desktop in lockstep so item schemas match.","Keep a fallback rendering path that tolerates ID-less items instead of aborting the whole history import."],"tags":["api","schema-validation","identifier"],"backgroundTag":"empty-required-field","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}